Integrating ESP32 with ROS via WiFi
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 | $ sudo apt-get install ros-${ROS_DISTRO}-rosserial-arduino |
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 | /* |
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 |
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 | void setupWiFi() |
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 | void setup(){ |
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 | void loop(){ |
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.