Introduction
Message Queuing Telemetry Transport or MQTTÂ is a lightweight, publish-subscribe network protocol that transports messages between devices.
MQTT is used for data exchange between constrained devices and server applications. It keeps bandwidth requirements to an absolute minimum, handles unreliable networks, requires little implementation effort for developers, and is, therefore, ideal for machine-to-machine (M2M) communication.
In this post, we will learn how to create a Mosquitto server and will make a MQTT network to publish and subscribe to messages with multiple Raspberry Pi based PLC.
Related links
What is MQTT and how does it work?
MQTT is a publishing/subscribe protocol that allows edge-of-network devices to publish to a broker. Clients connect to this broker, which then mediates communication among the devices. When another client publishes a message on a subscribed topic, the broker forwards the message to any client that has subscribed.Â
And how can the devices subscribe to a message? They can do it with the topics.
In MQTT, the word topic refers to a UTF-8 string that the broker uses to filter messages for each connected client. The topic consists of one or more topic levels. Each topic level is separated by a forward slash (topic level separator). In comparison to a message queue, MQTT topics are very lightweight.
With the example from the picture above, only the device that subscribes to a specific topic, will receive the message from that topic.
- In this example, the temperature sensor would publish the topic "bedroom/temp" and only the device 2 subscribed to that topic would receive the message.
- The humidity sensor would publish the topic "bedroom/humid" and only the device 1 subscribed to that topic would receive the message.
- And the device number 3 subscribed to the topic kitchen/temp, would not receive any message.
MQTT-Broker
In order to set an MQTT-Broker, let's follow the next steps:
1. Check your Raspbian model:
lsb_release -a
2. Download the repository:
sudo apt-key add mosquitto-repo.gpg.keycd /etc/apt/sources.list.d/
3. Select the repository for your version of Raspbian. In our case it would be the Buster version. Copy and paste only the line that corresponds to your Raspbian:Â
sudo wget http://repo.mosquitto.org/debian/mosquitto-wheezy.listsudo wget http://repo.mosquitto.org/debian/mosquitto-jessie.listsudo wget http://repo.mosquitto.org/debian/mosquitto-stretch.listsudo wget http://repo.mosquitto.org/debian/mosquitto-buster.list
Install Mosquitto
Installing Mosquitto on the main Linux distributions is just as easy, as most have it built into their repositories. This is the case with Ubuntu, Debian, or Raspbian (Raspberry Pi). So we just have to run the commands.Â
sudo apt updatesudo apt upgradesudo apt-get install mosquitto mosquitto-clients
If we want to execute Mosquitto when the system starts, run the following command:
sudo systemctl enable mosquitto.service
Testing MQTT
Now, we are going to publish and subscribe to a topic to check that the installation has been carried out correctly.
For this, Mosquitto provides two utilities 'mosquitto_sub' and 'mosquitto_pub', one for subscribing and the other one for publishing.
Connected to our industrial Raspberry Pi PLC controller, we are going to check the IP addresses:
ifconfig
We are going to set the MQTT-Broker to one of the open source PLC Raspberry Pi with the IP address: 192.168.1.4. The other ones will be the Mosquitto clients.
From the Raspberry Pi industrial PLC number 2, for example, subscribe to the topic hello/world.
mosquitto_sub -d -h 192.168.1.4 -p 1883 -t "hello/world"
From the industrial Raspberry PLC number 1, so the MQTT-broker, publish a message:
mosquitto_pub -d -h localhost -p 1883 -t "hello/world" -m "Alarm"
Finally, you will be able to get the message from the PLC which has been subscribed to the topic:
Troubleshooting
If you can not subscribe to the publisher with an "Error: Connection refused" message, you can add the following lines to the /etc/mosquitto/mosquitto.conf file:
allow_anonymous true
listener 1883 0.0.0.0
How to install Mosquitto: the MQTT-Broker on Raspberry PLC