How to install Mosquitto: the MQTT-Broker on Raspberry PLC
Learn what is MQTT and how it works on your Raspberry PLC!
3 September, 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 blog, we will learn how to create a Mosquitto server, and will make a MQTT network to publish and subscribe to messages with multiple Raspberry PLC.

 RELATED LINKS

How to

Connect Raspberry PLC to Wi-Fi

Read 

Raspberry PLC

Family products

See 

How to

Program Raspberry PLC Interrupt inputs with Python

Read 

Basics about

Raspberry Pi PLC Analog Outputs

Read 

Touchberry Pi

Family products

See 

How to

Find your perfect industrial PLC

Read 


 What is MQTT and how does it work?

MQTT (or Message Queuing Telemetry Transport) is a lightweight, publish-subscribe network protocol that transports messages between devices. 

MQTT is a publish/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 a MQTT network.
If the temperature sensor publishes a message. Those devices that subscribe to the message sent by the temperature sensor, will receive it.

Odoo • Image and Text

And how can the devices subscribe to a message? They can do it with the topics.

In MQTT, the word topic refers to an 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.

Odoo • Image and Text
  • 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 Raspberry PLC, we are going to check the IP addresses:

ifconfig
Odoo • Image and Text

We are going to set the MQTT-Broker to the one of the Raspberry PLC with the IP address: 192.168.1.4. The other ones, will be the Mosquitto clients.

From the Raspberry 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 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
Know more about Mosquitto.conf >>> 
How to install Mosquitto: the MQTT-Broker on Raspberry PLC
Boot & Work Corp. S.L., Fernandez Queralt Martinez
3 September, 2021
Share this post
Archive

Looking for your ideal PLC?

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 >>

Do you want more information?

Just fill the form!

Tell me more!