How to install Mosquitto: the MQTT-Broker on Raspberry PLC

Learn what is MQTT and how it works on your Raspberry PLC for Raspberry Pi automation!
September 3, 2021 by
How to install Mosquitto: the MQTT-Broker on Raspberry PLC
Boot & Work Corp. S.L., Fernandez Queralt Martinez

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

How to

Connect Raspberry PLC to Wi-Fi

Read 

Raspberry PLC

Family products for industrial automation

See 

How to

Program Raspberry PLC Interrupt inputs with Python

Read 

Basics about

Raspberry Pi PLC Analog Outputs

Read 

Touchberry Pi

Family products for industrial control

See 

How to

Find your perfect industrial Programmable Logic Controller

Read 


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. 

In the picture below, you can see a sample diagram of an MQTT network. 
If the temperature sensor publishes a message, those devices that subscribe to the message, sent by the temperature sensor, will receive it.

Diagram of a MQTT network-What is MQTT and how does it work-How to install Mosquitto: the MQTT-Broker on Raspberry PLC

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.

Device subscribes to a specific topic-Diagram of a MQTT network-What is MQTT and how does it work-How to install Mosquitto: the MQTT-Broker on Raspberry PLC
  • 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.key
cd /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.list
sudo wget http://repo.mosquitto.org/debian/mosquitto-jessie.list
sudo wget http://repo.mosquitto.org/debian/mosquitto-stretch.list
sudo 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 update
sudo apt upgrade
sudo 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
Testing MQTT-What is MQTT and how does it work-How to install Mosquitto: the MQTT-Broker on Raspberry PLC

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

​Search in our Blog

How to install Mosquitto: the MQTT-Broker on Raspberry PLC
Boot & Work Corp. S.L., Fernandez Queralt Martinez September 3, 2021

Looking for your ideal Programmable Logic Controller?

Take a look at this product comparison with other industrial controllers Arduino-based. 

We are comparing inputs, outputs, communications and other features with the ones of the relevant brands.


Industrial PLC comparison >>>