ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Arduino UNO and ESP8266

asked 2021-03-06 10:55:03 -0500

VisoVa gravatar image

updated 2021-03-14 13:19:17 -0500

Hello everyone! I would like to control a motor and receive sensor data through Arduino UNO and ESP8266 via WIFI using ROS. I would like to make this a thread where I make my system working step by step, so that it might be helpful for other working on similar projects. Here follows a recap of the step done so far.

  1. I installed Ubuntu 20 on virtual machine with ROS noetic.
  2. As a first start with rosserial, I created a ROS node on Arduino that it’s working fine. Now I would like to get rid of the cables, therefore I want to communicate data via WIFI.
  3. I got an ESP8266. First of all, I flashed it with the latest firmware version. Furthermore, I have successfully created some projects using the “Blynk” library, to check ESP8266 was working fine with Arduino UNO.
  4. I adapted the code of the thread https://answers.ros.org/question/2327..., to write a simple publisher via WIFI, the code follows. Now, I am a bit stuck here and my questions are:

a) Is this code compatible with ESP8266 and Arduino UNO? Do I have just to load this code on the Arduino UNO, right?

b) How do I connect the ESP8266 and the Arduino board to make this code working? (I mean: pin_arduino --> pin_ESP)

c) To run the node, is it correct to type rosrun rosserial_python serial_node.py _port:=tcp _baud:=9600 from command line after a launching a roscore?

I guess more questions will come once solved this issues

code:

#include <ros.h>
#include <std_msgs/Float32.h>
#include <WiFi.h>

IPAddress server(192, 168, 0, 10);
WiFiClient client;

// -- WIFI CLASS--

class WiFiHardware {

  public:
  WiFiHardware() {};

  void init() {
    // do your initialization here. this probably includes TCP server/client setup
    client.connect(server, 11411);
  }

  // read a byte from the serial port. -1 = failure
  int read() {
    // implement this method so that it reads a byte from the TCP connection and returns it
    //  you may return -1 is there is an error; for example if the TCP connection is not open
    return client.read();         //will return -1 when it will works
  }

  // write data to the connection to ROS
  void write(uint8_t* data, int length) {
    // implement this so that it takes the arguments and writes or prints them to the TCP connection
    for(int i=0; i<length; i++)
      client.write(data[i]);
  }

  // returns milliseconds since start of program
  unsigned long time() {
     return millis(); // easy; did this one for you
  }
};


// -- WIFI data --

char ssid[] = "ssid";
char pass[] = "password";

// -- ROS publisher --

std_msgs::Float32 str_msg;
ros::NodeHandle_<WiFiHardware> nh;
ros::Publisher chatter("chatter", &str_msg);
int temp;

//-- SETUP--

void setup() {
  WiFi.begin(ssid, pass);       // wifi start
  nh.initNode();                // starting ros node
  nh.advertise(chatter);        // starting the publisher
}

//-- LOOP --

    void loop() {
      temp = analogRead(A4);       // getting data from pin A4
      str_msg.data = temp;         // ros data type assignment
      chatter.publish( &str_msg ); // here I punlish the data
      nh.spinOnce();

      delay(500);
    }
edit retag flag offensive close merge delete

Comments

Can you say me how you connected wifi and arduino together . Can you share me the steps to be followed for that. or any source regarding it. I also want such function in my next project. So it will be useful if you help. Thank you

Rakee003 gravatar image Rakee003  ( 2021-10-08 11:38:38 -0500 )edit

2 Answers

Sort by » oldest newest most voted
1

answered 2021-03-13 08:28:57 -0500

VisoVa gravatar image

updated 2021-03-14 13:32:00 -0500

Hei! I decided to continue my project with an Arduino UNO wifi rev2, basically an arduino uno + esp32. I successffully created a ros wifi node that reads accelerations of the embedded IMU and publish them. I put here the code, maybe it can be helpful.

from the command line run:

roscore

and then, in another terminal.

rosrun rosserial_python serial_node.py tcp

If you are experiencing troubles of connection, try using 'ping' on linux. To see your IP adress, 'Hostname -I', take the 192. ... address.

Ah, I am using ros noetic, ubuntu 20.04 and version 0.7.8 of rosserial library. In general, try to run this code with different versions of the rosserial library, since with some I encountered problems.

I really hope I am helping somebody here!! let me know if it works!!

code:

#include <ros.h>
//#include ROSSERIAL_ARDUINO_TCP
#include <std_msgs/Float32.h>
#include <Arduino.h>
#include <SPI.h>
#include <WiFiNINA.h>
#include <Arduino_LSM6DS3.h> // questa è per leggere le accelerazioni


// ====================== MASTER DATA =========================

// metti ip del tuo pc
// roscore
// fai "rosrun rosserial_python serial_node.py tcp" e aspetta tanto
// usa ping + ip adress dell'arduino per vedere se funziona
//IPAddress server(192,178,82,123);
IPAddress server(192,178,82,126);
WiFiClient client;


//======================= WIFI CLASS DEFINITION ========================

class WiFiHardware {

  public:
  WiFiHardware() {};

  void init() {
    // do your initialization here. this probably includes TCP server/client setup
     client.connect(server, 11411);//11411
  }

  // read a byte from the serial port. -1 = failure
  int read() {
    // implement this method so that it reads a byte from the TCP connection and returns it
    //  you may return -1 is there is an error; for example if the TCP connection is not open
    return client.read();         //will return -1 when it will works
  }

  // write data to the connection to ROS
  void write(uint8_t* data, int length) {
    // implement this so that it takes the arguments and writes or prints them to the TCP connection
    for(int i=0; i<length; i++)
      client.write(data[i]);
  }

  // returns milliseconds since start of program
  unsigned long time() {
     return millis(); // easy; did this one for you
  }
};

//======================= VARIABLE DEFINITION ========================

char ssid[] = "network_name";
char pass[] = "network_password";
ros::NodeHandle_<WiFiHardware> nh; // here I say to ROS that I am using the WiFiHardware to manage ros

std_msgs::Float32 acc_x_msg;                     // acceleration on x-body axis
ros::Publisher pub_acc_x("acc_x", &acc_x_msg);
std_msgs::Float32 acc_y_msg;                     // acceleration on y-body axis
ros::Publisher pub_acc_y("acc_y", &acc_y_msg);
std_msgs::Float32 acc_z_msg;                     // acceleration on z-body axis
ros::Publisher pub_acc_z("acc_z", &acc_z_msg);
std_msgs::Float32 om_x_msg;                      // angular velocity on x-body axis
ros::Publisher pub_om_x("om_x", &om_x_msg);
std_msgs::Float32 om_y_msg;                      // angular velocity on y-body axis
ros::Publisher pub_om_y("om_y", &om_y_msg);
std_msgs::Float32 om_z_msg;                      // angular velocity on z-body axis
ros::Publisher pub_om_z("om_z", &om_z_msg);

//============================ SETUP =====================================

void setup() {
  Serial.begin(9600);

  // wifi setup:
  Serial.println("\nStarting connection to server...");
  WiFi.begin(ssid, pass);
  Serial.println("\nconnected to server");
  delay(10000);

  // accellerometer setup:
  IMU.begin();
  delay(10000);

  // ros setup:
  nh.initNode();
  nh.advertise(pub_acc_x);
  nh.advertise(pub_acc_y);
  nh.advertise(pub_acc_z);
  nh.advertise(pub_om_x);
  nh.advertise(pub_om_y);
  nh.advertise(pub_om_z);

  delay(10000);

}

//============================ LOOP =====================================

void loop() {

   // accellerometer ...
(more)
edit flag offensive delete link more
0

answered 2022-09-29 12:23:49 -0500

Hey! I know I'm asking a year later but I am hoping to do a very similar project. I have a working ESP8266-01S WiFi module but I need to erase the current firmware, and flash some usable firmware. In your first post you said you flashed your ESP8266 with the latest firmware... could you point me towards where I could find this? Thanks :)

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2021-03-06 10:38:42 -0500

Seen: 1,598 times

Last updated: Mar 14 '21