MQTT, or Message Queuing Telemetry Protocol, is an open standard for allowing devices or applications to publish and subscribe to messages using named topics as routes (i.e. /sensors/temperature). It has built-in features like QoS (Quality of Service) that let you specify how you want to prioritize or guarantee message delivery. Its a favorite of IoT hobbyists and industrial automation alike.
EMQX is an open-source MQTT broker (the server that devices publish and subscribe through) with well-rounded security, efficiency, and scale. It can be run locally, in the cloud, and even on a Raspberry Pi. Its also offered as a cloud service, with a serverless free-tier version available.
This guide will demonstrate how to use MQTT with the Robot Operating System, a framework for easily creating powerful robotics applications using independent nodes that communicate using ROS topics. You will be able to bidirectionally bridge ROS topics to MQTT topics. This can allow remote monitoring or more advanced real-time communication and logging.
There are many versions of ROS2. For testing I used a prebuilt image for Raspberry Pi that used ROS Humble, the latest is Iron.
Follow whatever instructions best suit the device you are installing ROS on.
Iron Install: https://docs.ros.org/en/iron/Installation.html
Raspberry Pi: https://github.com/ros-realtime/ros-realtime-rpi4-image
Running EMQX Locally and Creating a Client UserEMQX can be installed on a local machine or cloud instance in a number of ways. The easiest is to follow the instructions for your operating system here https://www.emqx.io/downloads
For instructions for a Raspberry Pi, go here https://www.hackster.io/virgilvox/running-emqx-on-raspberry-pi-82ddba
All these instructions are for running in the terminal of your operating system. You can also build the project from source via GitHub. https://github.com/emqx/emqx
Once installed and running, the EMQX admin dashboard can be found at http://127.0.0.1:18083/
You'll need to create user credentials for a client, in this case the ROS2 MQTT client, to connect as. Use the following instructions.
1 / 4
- Open the EMQX Dashboard and login with the default username "admin" and password "public".
- From the dashboard navigate to the Authentication page.
- Click on + Create.
- Select Password-Based and click Next.
- Select Built-in Database and click Next.
- Leave all the default settings and click Create.
- From the Authentication page, under the Actions column for the Password-based authenticator, click Users.
- Click the plus icon to create a new user with a username/password.
A free serverless instance of EMQX can be deployed by navigating to https://www.emqx.io/ and clicking on "Try EMQX Cloud". While limited in some ways, it still provides a powerful MQTT broker with 1 million session/minutes per month, forever.
You'll need to create user credentials for a client, in this case Node-Red, to connect as. Use the following instructions.
1 / 2
- Navigate to "Authentication & ACL" from the project page for your serverless instance of EMQX.
- Click on the plus sign to You'll need to create user credentials for a client, in this case ROS 2, to connect as.
- Create an arbitrary and secure username and password.
- Click Save.
The ROS MQTT Client allows you to bridge ROS topics to MQTT. We'll need to install it on the machine where ROS is installed and then configure it to connect to the EMQX broker and set which topics forward where.
Navigate to http://wiki.ros.org/mqtt_client if you need to dive deeper but the following instructions should suffice.
Open a terminal session on the machine where ROS2 is installed and run the following commands to install the MQTT client.
sudo apt update
sudo apt install ros-$ROS_DISTRO-mqtt-client
Once installed successfully create a new directory and create a file params.yaml with the following, replacing broker and username/pass for the ones you created. TLS fields are optional and can be left out.
mkdir mqtt-test
cd mqtt-test
nano params.yaml
params.yaml:
mqtt_client:
ros__parameters:
broker:
host: localhost
port: 1883
user: ros2
pass: test
tls:
enabled: # [false] whether to connect via SSL/TLS
ca_certificate:
bridge:
ros2mqtt:
ros_topics:
- /ping/primitive
/ping/primitive:
mqtt_topic: pingpong/primitive
primitive: true
mqtt2ros:
mqtt_topics:
- pingpong/primitive
pingpong/primitive:
ros_topic: /pong/primitive
primitive: true
To run the client using configuration from a file enter the following command.
ros2 launch mqtt_client standalone.launch.ros2.xml params_file:="</PATH/TO/PARAMS.YAML>"
Public to ROS topic via MQTT- Open a terminal on the ROS machine and subscribe to a ros topic with this command.
ros2 topic echo /pong/primitive
- In an MQTT client, such as the MQTTX.app client app, publish any arbitrary message to the MQTT topic pingpong/primitive
Back in the terminal session on our ROS machine where we subscribed to the ROS topic /pong/primitive we see that the message sent to the MQTT topic pingpong/primitive was forwarded. This is because configured these topics to be bridged in the params.yaml file.
- In an MQTT client, such as the MQTTX.app test client, connect to the EMQX broker we created and subscribe to the topic pingpong/primitive.
- In a terminal on the ROS host machine, run the following command to publish to the ROS topic /ping/primitive
ros2 topic pub /ping/primitive std_msgs/msg/String "{data: \"Hello MQTT\"}"
We should now see the messages come through in our MQTT test client. The messages published to the ROS topic /ping/primitive are being forwarded to the MQTT topic pingpong/primitive.
We have an amazing community of open-source contributors and users of the EMQX project and cloud service. Join in on the discussions, share your projects, or feel free to ask any questions! We'd love to hear from you!
Comments