Integrating ESP32 with ROS via WiFi

xiangyu fu Lv3

This blog post outlines how to connect an ESP32 development board to the Robot Operating System (ROS) using WiFi.

Project GitHub:

1
https://github.com/Xiangyu-Fu/ESP32_ROS_wifi

Prerequisites

Before we begin, ensure you have the following development environment set up:

  • Ubuntu 20.04
  • ROS Noetic
  • PlatformIO espressif32
  • ESP32 Arduino Framework
  • frankjoshua/Rosserial Arduino Library@^0.9.1

You can also use a Raspberry Pi instead of a PC.

Environment Setup

On PC or Raspberry Pi

Install the necessary ROS packages. Use the following commands in a terminal:

1
2
$ sudo apt-get install ros-${ROS_DISTRO}-rosserial-arduino
$ sudo apt-get install ros-${ROS_DISTRO}-rosserial

After installing the required packages, start the ROS master node:

1
$ roscore

Then, in a new terminal window, run the rosserial node:

1
$ rosrun rosserial_python serial_node.py tcp

This will start a ROS node that will communicate with our ESP32 over TCP.

On Embedded Device (ESP32)

Ensure that appropriate example code has been flashed onto the development board. This can be done using PlatformIO environment or Arduino IDE.

Running the Example

Here is the complete code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
* Code initializes and connects to a WiFi network using given SSID and password,
* then publishes a "Hello World!" message to a ROS topic "chatter" at regular intervals.
* Make sure to update the SSID, password, IP and server details as per your network.
*
* Create by Stan Fu on 2023/08/07
*/
#include <arduino.h>
#include "WiFi.h"
#include <ros.h>
#include <std_msgs/String.h>

IPAddress server(192, 168, 178, 48);
uint16_t serverPort = 11411;
const char* ssid = "your wifi name";
const char* password = "your wifi password";

// Be polite and say hello
char hello[13] = "hello world!";
uint16_t period = 1000;
uint32_t last_time = 0;

ros::NodeHandle nh;
// Make a chatter publisher
std_msgs::String str_msg;
ros::Publisher chatter("chatter", &str_msg);

void setupWiFi();

void setup(){
Serial.begin(115200);
setupWiFi();

nh.getHardware()->setConnection(server, serverPort);
nh.initNode();

// Another way to get IP
Serial.print("ROS IP = ");
Serial.println(nh.getHardware()->getLocalIP());

// Start to be polite
nh.advertise(chatter);

}

void loop(){
if(millis() - last_time >= period)
{
last_time = millis();
if (nh.connected())
{
Serial.println("Connected");
// Say hello
str_msg.data = hello;
chatter.publish( &str_msg );
} else {
Serial.println("Not Connected");
}
}
nh.spinOnce();
delay(1);
}


void setupWiFi()
{
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) { delay(500);Serial.print("."); }
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
Serial.print("IP: ");
Serial.println(WiFi.localIP());
}

Let's delve into each part in detail.

Dependencies

The first step is to include necessary libraries. These include Arduino core libraries, WiFi library for network connection, and ROS libraries for interaction with the ROS system.

1
2
3
4
#include <arduino.h>
#include "WiFi.h"
#include <ros.h>
#include <std_msgs/String.h>

We define several global variables:

  • IP address of the Arduino board and ROS server.
  • Server port of the ROS server.
  • SSID and password of the WiFi network.
  • Message ("hello world!") to be sent to the ROS server and the frequency of sending it.
  • ROS node handle and publisher for sending messages to the ROS system.

Connecting to WiFi

Our setupWiFi() function connects the Arduino board to the WiFi network. It repeatedly checks the connection status, printing a dot for each attempt until the connection is established. Once connected, it prints the SSID of the WiFi connection and the local IP address.

1
2
3
4
5
6
7
8
9
void setupWiFi()
{
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) { delay(500);Serial.print("."); }
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
Serial.print("IP: ");
Serial.println(WiFi.localIP());
}

Main Setup

In the setup() function, we initialize serial communication for debugging, set up the WiFi connection, establish the connection to the ROS server, initialize the ROS node, and advertise the publisher.

1
2
3
4
5
6
7
8
9
10
11
12
void setup(){
Serial.begin(115200);
setupWiFi();

nh.getHardware()->setConnection(server, serverPort);
nh.initNode();

Serial.print("ROS IP = ");
Serial.println(nh.getHardware()->getLocalIP());

nh.advertise(chatter);
}

Main Loop

In the main loop(), we check the time, and if enough time has passed since the last message, we send a new message. We also check the connection status and print a message to the serial monitor accordingly. After these checks, we call nh.spinOnce() to process any incoming messages and delay for one millisecond.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void loop(){
if(millis() - last_time >= period)
{
last_time = millis();
if (nh.connected())
{
Serial.println("Connected");
str_msg.data = hello;
chatter.publish( &str_msg );
} else {
Serial.println("Not Connected");
}
}
nh.spinOnce();
delay(1);
}

Now you're ready to interact with ROS using Arduino over WiFi.

Once all setups are completed, the ESP32 connects to the same network as your personal computer or Raspberry Pi, and it should start sending "hello world!" messages to the ROS system. You can visualize this output using tools like rqt_console or simply monitor it in the terminal.

If everything is set up correctly, you should see a screen similar to the following:

The ESP32 is now connected to ROS via WiFi. This basic framework can serve as the foundation for more complex projects where ESP32 interacts and controls robots or other devices via ROS.

  • Title: Integrating ESP32 with ROS via WiFi
  • Author: xiangyu fu
  • Created at : 2023-08-07 17:51:57
  • Updated at : 2024-06-23 00:59:53
  • Link: https://redefine.ohevan.com/2023/08/07/misc/esp32_ros_wifi/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments