How to Set Up MQTT for a Robotics Project

mqtt-setup-robotics-ros2

MQTT (Message Queuing Telemetry Transport) is a lightweight, publish-subscribe network protocol that transports messages between devices. It is particularly useful in robotics projects, including those using ROS 2 (Robot Operating System 2), due to its efficiency in handling real-time data streams and its ability to work well in low-bandwidth, high-latency environments.

In a ROS 2 robotics project, MQTT can complement the existing communication framework by:

  • Enabling easy integration with IoT devices and sensors
  • Providing a standardized way to communicate with cloud services
  • Facilitating communication between robots and external systems
  • Offering a lightweight alternative for certain types of messages

This tutorial will guide you through setting up MQTT for your ROS 2 robotics project.

Setting Up the MQTT Broker (Mosquitto)

We’ll use Mosquitto, a popular open-source MQTT broker. Here’s how to set it up on an Intel NUC, Raspberry Pi or Jetson board:

Update your system:

sudo apt-get update
sudo apt-get upgrade

Install Mosquitto broker and clients:

sudo apt-get install mosquitto
sudo apt-get install mosquitto-clients

Check the status of Mosquitto:

systemctl status mosquitto

Manage Mosquitto services:

Stop:

sudo systemctl stop mosquitto or sudo service mosquitto stop

Start:

sudo systemctl start mosquitto or sudo service mosquitto start

Restart:

sudo systemctl restart mosquitto or sudo service mosquitto restart

Verify that Mosquitto is listening on the default port (1883):

netstat -an | grep 1883 

Configuring Mosquitto for Network Access

By default, Mosquitto only listens on localhost. To allow connections from other devices:

Create a new configuration file:

sudo nano /etc/mosquitto/conf.d/default_listener.conf

Add the following lines:

listener 1883
allow_anonymous true

Save the file and exit the editor.

Restart Mosquitto:

sudo systemctl restart mosquitto

Verify the new settings:

sudo netstat -tuln | grep 1883

Note: Allowing anonymous access may not be secure for all use cases. Consider setting up authentication for production environments.

Testing MQTT Communication

To test MQTT communication, open four terminals:

Replace localhost with the IP address of your MQTT broker if running on a different machine.

Terminal 1 (Subscriber for kitchen):

mosquitto_sub -h localhost -t /home/sensors/temp/kitchen

Terminal 2 (Publisher for kitchen):

mosquitto_pub -h localhost -t /home/sensors/temp/kitchen -m "Kitchen Temperature: 26°C"

Installing the Paho MQTT Python Client

To interact with MQTT from your Python scripts:

Install pip for Python 3:

sudo apt install python3-pip

Install the Paho MQTT client:

pip3 install paho-mqtt

With these steps completed, you’re now ready to integrate MQTT into your ROS 2 robotics project. You can use the Paho MQTT client in your Python scripts to publish and subscribe to topics, allowing your robot to communicate with various devices and services using the MQTT protocol.