Arduino UNO + ESP8266 + ROS

Good afternoon everyone! I am doing an experiment with Arduino UNO, ESP8266 and ROS. Basically, 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.

  • I installed Ubuntu 20 on virtual machine with ROS noetic.

  • 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.

  • 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.

  • 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:

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

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

  • 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);
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.