Combining ROS2 and MQTT on ESP32 to send Twist messages

RoboFoundry
5 min readSep 16, 2022

I have been tinkering quite a bit recently with micro-ros as you may have seen the article on getting it working on RPi Pico. One of the things I wanted to accomplish with micro-ros was to completely replace Raspberry Pi 4 with either one or some combination of Pico and ESP32 micro-controllers. I thought ESP32 was a good candidate for this purpose because it already has support for micro-ros and it has built in support for wifi [Pico W is not yet supported on micro-ros with wifi transport as of writing of this article]. So I went to work and got the basic publisher and subscriber code working on ESP32. However, I have to admit writing micro-ros C language code is not at all fun or easy, the code is very cryptic and it is hard to figure out what all the libraries and include files and series of compiler flags you need to enable in order to do things like serial communication, GPIO pin control and so on.

After going through the experience of getting micro-ros to work on ESP32, I was experiencing a lot of fatigue and honestly the speed of prototyping was not as fast I was hoping for. While I was doing the tinkering with micro-ros I ran into a video presentation of a small but very cute robot named Ro-Bud from Scott Monaghan [@scottmonaghan] where he talks about MQTT and Mosuitto [with two Ts] broker for doing pub/sub messaging between various components of robot. I figured what’s the harm in exploring that and I might learn something new, so I jumped into researching the MQTT and how to get it working with ESP32. In this article, I’ll explain my experience and a unique approach I wanted to try out to subscribe from ESP32 to listen for Twist messages from my laptop over MQTT using Mosquitto broker. The idea is to then have ESP32 control a motor controller or some other sensors on a robot.

Here is what the basic architecture of the app/prototype looks like:

As you can see from diagram, there are some pieces that need to be installed/configured on computer/laptop and some on the ESP32.

On Laptop

  1. Install Mosquitto broker using Ubuntu snap [this was really snap of an install pun intended!!!]
  2. Configure a launch file to run ROS2 node for Teleop twist [I plugged in the USB key for the joystick controller into my laptop and running Teleop Twist will listen for joy messages and publishes Twist messages to any subscriber that wants to listen]
  3. Create and Run another ROS2 node that listens for Twist messages published by teleop_twist_joy node on /cmd_vel topic and turns around and publishes the data from that same Twist message as JSON string containing the linear and angular velocity values to MQTT topic.

On ESP32

Running micropython with standard installation [this already includes Mqtt libraries out of the box]. If you don’t know know how to do it follow this article.

After getting the basic micropython installed on the ESP32, we need to write two python code modules:

  • boot.py — file to establish wifi connection with my router
  • main.py — file to subscribe to esp32/cmd_vel topic and print the values of linear [x] and angular [z] to the console.

The code is available at the github repository here — https://github.com/robofoundry/ros2_mqtt.git

In order to run the example code follow the instructions below or in README.md file on the github repo.

Setting up things on computer

Install snap for mosquitto broker as follows:

### install broker on the laptopsnap install mosquitto### create and edit config file for mosquitto broker like this
sudo cp /var/snap/mosquitto/common/mosquitto_example.conf /var/snap/mosquitto/common/mosquitto.conf
### stop and start broker as needed
sudo systemctl stop snap.mosquitto.mosquitto.service
sudo systemctl start snap.mosquitto.mosquitto.service

First on computer update configurations in following places

1. in ros2_mqtt/config/param.yaml file — replace 192.168.1.123 with ip address of where you are running mqtt broker [your computer]

2. in esp32/boot.py — update SSID and PWD for your wifi

3. in esp32/main.py — replace IP_ADDR_OF_MQTT_BROKER with your mqtt broker ip address [same as ####1]

Here are some additional useful commands you may want to run:

### build the project from root folder of workspacecolcon build### source ros2 foxy
source /opt/ros/foxy/setup.bash
### launch the ros2mqtt node along with teleop twist nodesros2 launch ros2_mqtt ros2_mqtt.launch.py### show twist messages by echoing the messagesros2 topic echo /cmd_vel### show mqtt messagesmosquitto_sub -v -t ‘esp32/cmd_vel’

Setting up things on ESP32

  1. Install Thonny and set interpreter to ESP32

2. Connect esp32 via usb to your computer

3. Install esptool and flash micropython on esp32 [follow instructions here if you haven’t done it before]

4. Upload boot.py and main.py files from the esp32 directory of the git repo on esp32 and connect with esp32 again

Make sure in boot.py you have updated wifi credentials and in main.py you have updated the IP address of your mqtt broker.

After these steps are done, you can unplug the USB cable from ESP32 as we are going to be using its WiFi connection so we don’t need the dongle.

At this point, you should be able to send twist messages using joystick and should be able to confirm [assuming you know how to configure joystick to work with Ubuntu and you have plugged in the USB key of joystick into one of USB ports of your computer]

The Twist messages are traveling to ESP32 as shown in the diagram above and looking at the console log on Thonny, ESP32 should be logging same messages. See the video example below:

As you can imagine, this is just a proof of concept to make sure the concept works. The next step would be to send data from ESP32 over uart/serial port to something like a motor controller to move the robot based on the Twist messages received or even have IMU data read and publish it back to MQTT broker. Ultimately, being able to run most of robot hardware via ESP32 and run rest of ROS2 processes on the computer to do the robot localization or navigation. But I’m still working on those bigger goals, this is just a small step in that direction.

Thanks to Scott for inspiration and ideas.

Hope you can try it out. Enjoy!!!

References

Github Repo — https://github.com/robofoundry/ros2_mqtt

Joystick on Amazon[not affiliate link] — https://a.co/d/2jVDnk0

--

--