Install Mosquitto on a Raspberry Pi

In this tutorial we’ll learn how to install Mosquitto on a Raspberry Pi. The goal is to use the Raspberry Pi as a MQTT broker in an IoT application. We’ll install Mosquitto, configure it and test it to make sure everything was correctly set up.

Note that I will be using a Raspberry Pi 3 running Raspbian Stretch Lite. You may have to change some commands if you are running another OS.

What is Mosquitto / MQTT

With IoT (Internet of Things), it’s extremely important to reduce the amount of data transferred between devices. With more and more objects connected to our local networks, and those objects often having very little resources, it’s crucial to limit the data transfers to the strict minimum. That’s part of what makes the MQTT protocol so useful.

This protocol uses one device as a “broker”, which will act as a master on the network and route the messages to the correct devices. Other devices will publish and/or subscribe to some types of messages, also called “topics”. When a device publishes a message on a topic, the broker will transfer it to all the devices subscribed to this topic. With this architecture, the amount of data transferred and resources needed on the devices is reduced, because:

  • Devices only need to know about the broker. If a device needs to send a message to 10 other devices, it doesn’t need to know any detail about those 10 devices. All it needs to do is publish a message, and the broker will handle the distribution.
  • Devices don’t receive messages that they didn’t subscribe to. This is much better than a basic “all broadcast” architecture for example.
  • The content of the messages is minimal. At least, a message consists of only a topic. If more information is needed, the message can include any kind of data (text, images, JSON, …).

Mosquitto (notice the two ‘T’s) is an open source message broker that implements MQTT, developed by Eclipse.

Install Mosquitto

Update the signing key for apt-get

Before we can install Mosquitto via apt-get, we need to update the signing key. First, download the key:


Then, add this key to apt-get:

Add the Mosquitto repository to apt-get

Mosquitto is not available in the basic Debian repositories. To add the repository, first go to the folder containing the repository lists for apt-get:


And then download the repository list file for Mosquitto:


Make sure to replace “stretch” by the version of Raspbian you’re using.

Install Mosquitto

First, update apt-get’s source lists:

Now we can install the package mosquitto, which is the MQTT broker, and mosquitto-clients, which we’ll use later to test the installation.

If you’re going to use MQTT in a Python project, you’ll have to install paho-mqtt, which replaces the old Mosquitto Python module. If python-pip is not installed on your Raspberry Pi yet, you’ll have to install it first:


Then install paho-mqtt using pip:

Configure Mosquitto

The configuration file for Mosquitto is located in /etc/mosquitto/mosquitto.conf. Before making any modification to this file, make a backup copy of it:


The original configuration of Mosquitto should be enough for starting to experiment with it. I only changed it to save every kind of log message to the log file:

For more information about the different configuration options available, you can take a look at /usr/share/doc/mosquitto/examples/mosquitto.conf

Test Mosquitto

The last step is to test our installation. We’re going to use 2 terminals. One will subscribe to the topic “test-mosquitto”, and the other will publish a message to this topic. The test will be successful if the message sent by the publisher is logged on the subscriber terminal.

To start the subscriber, run the command mosquitto_sub in the first terminal. The option -d is used to enable the debug mode, and -t lets us specify the topic:

Start the Mosquitto subscriber
Start the Mosquitto subscriber

To publish a message, use the command mosquitto_pub with the same options, and add a message with -m:

Publish a message
Publish a message

Now if everything is working fine, you should see the test message in the first terminal:

The subscriber received the test message
The subscriber received the test message

Conclusion

As we’ve seen in this article, it’s pretty easy to install and set up Mosquitto on a Raspberry Pi. The possibilities are endless for IoT applications, as Mosquitto allows you to easily interconnect devices on a local network and make them communicate between each other.

Feel free to leave a comment below if you have any question!

Be the first to comment

%d bloggers like this: