Use the MQTT protocol to communicate data between 2 Raspberry Pi.

Mohamed El Hlafi
7 min readJun 20, 2020

--

IoT connected objects invade our world more and more every day. Good or bad thing ? I will not comment. To better understand how it works, I offer this article to discover MQTT.

Why this article?

I don’t know about you (you tell me), but when I look for an article to test MQTT, I find plenty. Great ! But when it is necessary to implement, one realizes that the articles are written by users who master this protocol. They tell you what to do, yes. But they don’t say HOW TO. More clearly ? a detailed procedure, a recipe that works. What to start with a set that works and then modify it to go further … I put in the sources part of the links that I explored. I’ll let you judge. So I decided to propose a solution with Raspberry Pi.

Specifications :

A first Raspberry Pi captures Temperature + Humidity data. It sends them via Wifi (MQTT protocol) to a second Raspberry Pi, responsible for receiving messages, storing them in a database, and then displaying them in real time.

Chosen solution :

After many (and long) researches, tests (more or less fruitful) I finally retained the solution which I propose to you.

Transmitter:

  • A Raspberry Pi 2 under Raspbian Stretch version 04–2018.
  • An “official” WiFi USB key.
  • A DHT22 — AM2302 sensor mounted on a breadboard (prototyping plate).

Receiver:

  • A Raspberry Pi 3 under Raspbian Stretch version 04–2018.
  • To process MQTT data, the Telegraf / Influxdb / Chronograf pack.

The MQTT protocol :

MQTT (Message Queuing Telemetry Transport) is a publication-subscription messaging protocol based on the TCP / IP protocol. It was originally developed by Andy Stanford-Clark (IBM) and Arlen Nipper (EuroTech). (wikipedia).

This diagram shows the operating principle of MQTT. On the left are the “publishers”. These are machines (Arduino, ESP8266, Raspberry Pi, etc.) that capture values (temperature, humidity, pressure, electricity consumption, water consumption, etc.) and send them to a server called “Broker”.
“Subscriber” clients are connected to the “Broker”. They requested that certain data be sent. When the Broker receives this data, it retransmits it to clients who have subscribed to this data flow. For example, one of the subscribing customers will receive the temperature and humidity to make a graph in real time. The other customer will receive the water consumption and will trigger an alert if it exceeds a determined value.

A first local MQTT test :

For this first test there was no question of sending data … just a string, to see if “it goes” between the two Raspberry Pi.
I selected this article from Felix, on tutorials-raspberrypi. He offers to install Mosquitto, which is probably the easiest way to get started.

Start by knowing your two Raspberry Pi in WiFi on your Box, and note their IP addresses (mouse on the network icon or ifconfig in a terminal).

First install Mosquitto on the two Raspberry Pi:

sudo apt-get install -y mosquitto mosquitto-clients

After installation, a Mosquitto server is started automatically. Open a subscriber in the “test_channel” channel awaiting messages on one of the Raspberry Pi. In a terminal type:

mosquitto_sub -h localhost -v -t test_channel

Mosquitto is waiting for a message on this channel.

The channel here is like an FM frequency, on which we listen to a station. For example, different data can be sent on different channels (temperature, humidity, brightness, etc.).
In order to simply transfer the data, we will first use the same Raspberry Pi. This will test that Mosquitto works on this Raspberry Pi. Open a new terminal / new SSH connection on the same Raspberry Pi and type:

mosquitto_pub -h localhost -t test_channel -m “Hello Raspberry Pi”

Here the data leaves from a test_channel publisher, joins the broker and is sent to the subscriber. In the first terminal window (or in your first SSH session) you should see Hello Raspberry Pi appear. This proves that your Mosquitto broker is working properly on this machine.
If not, you don’t have to go any further. You have to make this manipulation work first!

In the example above you see two putty sessions open on the same machine. First launch the subscriber (receiver) in the bottom window. The upper window receives the publisher. When you validate the mosquitto_pub command line, the text appears in the bottom window. This is proof that MQTT works on this machine.

Do the same on the other Raspberry Pi. This will confirm that the two Raspberry Pi’s work locally, each with its own Mosquitto.

Send data with MQTT between two Raspberry Pi :

Now that we’re certain that MQTT is up and running on both Raspberry Pi’s, it’s time to send data from one Raspberry Pi to the other.

No big change. My Raspberry Pi have respectively 192.168.#.# for the publisher and 192.168.#.# for the subscriber. First launch the subscriber (bottom window) to wait for reception. You will then need to tell the publisher (top window) the address of the broker (server) instead of localhost. The publisher will send the message to the second Raspberry Pi, to the broker. The broker notes the presence of a subscriber on the same channel. It sends the data to it and the subscriber displays the text.Now, you are now able to send data in MQTT from one Raspberry Pi to another. For less primary use, we will want to send values of temperature, pressure, humidity, etc. To gain flexibility and vary our content, we will have to use a programming language, for example Python.

Transmitting data with MQTT: the publisher:

The sensor

This is an article from thingsboard.io which served as my starting point for setting up the publisher. In this article, the author uses a DHT22 or AM2302 sensor to measure temperature and relative humidity. These values are then sent in JSON via MQTT to the broker. JSON allows you to transport values with a label to identify them.

The Python publishing program:

Here is the Python program extracted from this article and modified to meet the specifications and the desired operation. In this use the broker will be on the Raspberry Pi 3B + (address 192.168.1.16) with the subscriber. The Raspberry Pi 2 at 192.168.1.20 will be the publisher. It uses paho-mqtt. It is a Paho MQTT client library for Python, which implements versions 3.1 and 3.1.1.1 of the MQTT protocol. This code provides a client class that allows applications to connect to an MQTT broker to publish messages (this is what we will use), subscribe to channels, and receive published messages. It supports Python 2.7.9+ or 3.4+.

Start by installing paho-mqtt :

sudo pip install paho-mqtt

then the Adafruit library which manages the sensors:

Then enter this program (or download it) :

The data is sent to the broker by client publish on the test_channel channel.To test launch mosquitto on the other Raspberry Pi, you will see the data arrive:

We are ready on the publication side, we will go to the broker side.

The broker, his client, the database and the display :

It is this architecture that I chose for the broker. It is presented on the influxdata.com site. For my specifications, I only need Telegraf to receive the data, InfluxDB to store them and Chronograf to display them on a web page.

To the left of this image, we see the Pi2 that we have just configured, with its DHT22 probe. It is connected by WiFi to the box. The second Raspberry Pi is a Raspberry Pi 3. It is also connected by WiFI to the Box.
Telegraf is responsible for recovering the data sent by the Pi2, and received by the broker. It stores them in InfluxDB whose particularity is to record the data with a “timestamp”, a time stamp. This is very useful when recording climate data, for example. Each piece of data is precisely dated.
Chronograf generates a web page for administering influxdb and managing dashboards presenting the data.

Conclusion:

Here you should now have an installation transmitting climate measurements from one Raspberry Pi to another using the MQTT protocol.You can have fun and start modifying (that’s good!) Remember anyway (when it is operational) to make an image of each of the two SD cards, to be able to restart quickly in case of a crash.It remains to transform the Raspberry Pi receiver into an access point so that the other Raspberry Pi (ESP8266) can connect directly to it without going through a box.

Do not hesitate to return in the comments of this article, it is always rewarding (and for the author and for other readers).

Sources:

--

--