Wireless communication between Raspberry Pi’s via MQTT broker/client
MQTT is a protocol that specifically sends data from devices of the Internet of Things and is supported by most microcontrollers and systems. To use Raspberry Pi MQTT communication, not much is needed, which is why this type of transmission is very interesting. In addition, it is really easy to use.
In this tutorial, we install a Raspberry Pi MQTT broker, whereupon we receive and send data. We can either use several Raspberry Pi’s for testing or just use a single device.
This tutorial works without much additional hardware. However, you either have to have at least one Raspberry Pi 3 (alternatively a Model Zero W) or connect a wifi stick to your Raspberry Pi to use MQTT.
Other devices such as an ESP8266 can also communicate using MQTT. This is possible with Raspberry Pi’s as well as other devices. Also for the NodeMCU there are ready-made packages.
What is MQTT?
Some may wonder what MQTT should be. To quote the official MQTT page:
MQTT is a machine-to-machine (M2M)/”Internet of Things” connectivity protocol. It was designed as an extremely lightweight publish/subscribe messaging transport. It is useful for connections with remote locations where a small code footprint is required and/or network bandwidth is at a premium.
It is therefore a standardized network protocol with which short messages / commands can be transmitted. The big advantage is that the built-in wifi adapters (for example in the Raspberry Pi or in the ESP8266) are used for the internet connection. More accessories and complicated wiring is not necessary! This makes it possible to send the data via the local network or the Internet.
In detail, the transfer consists of three different components:
- Publisher: Sends messages.
- Broker: Forwards messages to registered subscribers.
- Subscriber: Receives messages through the broker.
Raspberry Pi MQTT Server – Install and test Mosquitto
There are several applications that can be used to send and receive through MQTT, but the simplest on the Raspberry Pi is probably Mosquitto. We will install this on the Raspberry Pi first:
sudo apt-get install -y mosquitto mosquitto-clients
After installation, a Mosquitto server is started automatically. We open a subscriber in the channel “test_channel” waiting for messages:
mosquitto_sub -h localhost -v -t test_channel
The channel is here like a frequency, on which one hears. For example, different data may be sent in different channels (e.g., temperature, humidity, brightness, etc.).
In order to simply transfer data, we can either use the same Raspberry Pi (open new terminal / SSH connection) or send the data from another Pi. If we use the same Raspberry Pi, use is easily possible. For this we simply send a test message (as publisher) in the same channel:
mosquitto_pub -h localhost -t test_channel -m "Hello Raspberry Pi"
Otherwise you have to specify the internal IP address (eg 192.168.1.5) of the recipient instead of “localhost”. On the receiver side, the message should appear soforn.
Raspberry Pi MQTT data exchange with Python
The communication is super easy, as we have seen. In order for us to be able to use the whole thing from scripts, we want to make it available to Python. For this purpose, we first install a library via the Python package manager (for Python3 also use pip3):
sudo pip install paho-mqtt
Alternatively (for example, if the package could not be properly installed) you can also install it manually:
git clone https://github.com/eclipse/paho.mqtt.python.git cd paho.mqtt.python python setup.py install
We start with the receiver. For this we create a new file with the content:
sudo nano mqtt_subscriber.py
import paho.mqtt.client as mqtt
MQTT_SERVER = "localhost"
MQTT_PATH = "test_channel"
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
# more callbacks, etc
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(MQTT_SERVER, 1883, 60)
# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
After saving and closing (CTRL + O, CTRL + X) we execute the file:
sudo python mqtt_subscriber.py
Either you send a command as usual by Shell Command, or we also create a Python Sender / Publisher.
The publisher’s code looks like this (create a new file and then run it, keeping in mind the correct IP address):
import paho.mqtt.publish as publish
MQTT_SERVER = "192.168.1.5"
MQTT_PATH = "test_channel"
publish.single(MQTT_PATH, "Hello World!", hostname=MQTT_SERVER)
That’s it! Now you can include the appropriate parts in your scripts and receive or send MQTT messages from other Raspberry Pi’s. Of course, this is not just limited to Raspberry Pi’s because the messages can also come from or be addressed to other devices.
Other ways to communicate with Raspberry Pi’s
Of course, MQTT is far from the only way to exchange data. If you have a system with its own OS such. Raspbian can be established using SSH connections and, for example, send via SCP files. However, the devices must have a network connection.
Another possibility without internet connection are normal radio transmitters and receivers. Here is e.g. The 433 MHz radio transmission is very popular because the modules are very cheap and many devices such as radio-controlled sockets or garage doors are compatible with it. However, this transfer is limited to 24 bits, which is not very much.
More data per message can be transmitted via 2.4 GHz radio transmission. This transfer differs from MQTT because extra modules are used. However, it is also possible to send commands or messages to Arduinos.