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

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

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.

12 Responses

  1. hi,
    can i communicate using my internet router (Dlink).
    what changes should i make ?

    thank you

      • through the router.
        and one more thing how do i subscribe to multiple topics.


      • hi,
        i wanted one more help .
        how do we connect to multiple ip address ?
        i have 2 topics on different raspberry pi
        and im connecting them to a laptop
        i am just able to connect to the first client.connect(ip1)
        and not the client.connect(ip2)

  2. hi,
    can i communicate rpi3 and app phone via wifi? I need stream data with phone accelerometr to rpi3, it is possible?

    • When I tried the code on this page, I used the app MQTTool on my iPhone 5S, – and it worked!
      There are several MQTT apps for iPhone, but so far I have just tested MQTTool, Mqtt buddy (a bit hard to understand), and IoT Manager (crashes sometimes and hard to get to connect).
      So try search for “mqtt client” at the App Store.

      For now I’m trying to find/learn how to get a notification-signal on my iPhone when I get a message from the mqtt-server/broker. No luck yet.

      Felix: Thank you for the guide! You made me understand how mqtt works!

  3. Hi I need complete information about using mqtt established msg send and received using telegram app with raspberry pi can u pls send the full coding part pls….


Leave a Comment

Your email address will not be published.

Subscribe to Raspberry Pi Tutorials and don't miss any new Tutorial!