Tutorial: install Mosquitto MQTT on Raspberry Pi

Mosquitto: an easy to install MQTT server!

If you have followed our many home automation tutorials DIY, it is very likely that you have already heard of MQTT protocol. But késako?

This is what we are going to see today before explaining to you how to implement on its own, in just a few minutes and inexpensively, a server using this protocol, which is very interesting for all those who use a home automation box such as the new Jeedom atlas, the inexpensive Home Assistant on Raspberry Pi or the very efficient Homey Pro.

What is MQTT?

Definition

MQTT, or Message Queuing Telemetry Transport, is a communication protocol designed for M2M applications (Machine To Machine) and IoT (Internet of Things). Created in 1999 by Andy Stanford-Clark (IBM) and Arlen Nipper (Arcom), we cannot say that it is very new, but it still remains very popular in home automation due to its reliability and sobriety.

MQTT indeed allows fast exchanges with very little data - saving bandwidth - without having to query a server - less latency. It is therefore precious in our connected homes because it is 90 times faster than HTTP protocol. Moreover, it requires 10 times less energy to send messages and 170 times less to receive them.

Concretely, each MQTT message consists of a fixed header of only 2 bytes, an optional variable header, a message payload limited to 256 MB and a quality of service level ...

Quality of service (QoS)

MQTT offers Quality of Service (QoS) at 3 levels:

  • QoS 0 : message transmission without acknowledgment
  • QoS 1 : message transmission with acknowledgment of receipt
  • QoS 2 : message transmission with acknowledgment of receipt and verification of the message transmitted to prevent the same message from being transmitted more than once.

How does it work?

MQTT is a protocol called " publish / suscribe "Allowing devices" edge of network "To publish to a" broker ".

Well, OK, it's not very clear. In French, this means that the server called broker will publish topics to which customers will be able to subscribe. Each client can subscribe to different topics, and being a bidirectional protocol, the MQTT broker will notify all clients subscribed to the topic when one of them publishes a message there.

Example: if our customer is temperature sensor or thermostat, he will inform his MQTT broker of the temperature that it notes in a topic called " /temperature " to which our MQTT customers : mobile application and connected boiler. If our application will only record this data, our boiler will use it to decide whether or not it should turn on. Better yet, when the latter starts up, it will notify the broker who, in turn, will inform the other clients.

Basically, it's not very complicated, is it?

The necessary equipment

Now that we know everything, let's get down to practice and use some material we had in stock: a Raspberry Pi 3B + kit. You can obviously opt for a Raspberry Pi 4, but it is much more expensive and its extra power will not be of any use to us ...

Install a Mosquitto MQTT broker on Raspberry Pi

We will assume that you have already performed theinstalling Raspberry Pi OS on your nanocomputer of the same name. If this is not already the case, we invite you to consult our tutorial. Fear not, it is now surprisingly easy!

TO READ :
Raspberry Pi OS tutorial: installation always easier with Imager

MQTT installation on Raspberry Pi OS

We are going to use PuTTY, a small program allowing to very easily connect remotely to our Raspberry Pi by SSH.

Install it, launch it and enter the IP address of your Raspberry PI on the port 22.

Then click Open and a new window opens. This is the terminal in which you will enter your access credentials to the Raspberry Pi OS system.

Important: if you haven't already changed the default password, it's important to do so before proceeding further.

Type the following line and select 'Change User Password':

sudo raspi-config

Another important point, we need to check that our Raspberry Pi is up to date… Even if you have just installed it!

sudo apt-get update

Make the necessary updates and let's move on to the installation of our MQTT broker.

sudo apt-get install mosquitto

If you are familiar with Linux, you will not be surprised that we then check the correct execution by typing:

systemctl status mosquitto

Everything is OK? Your screen should look like this:

Well done, you are done, you are now the proud owner of a Mosquitto MQTT broker!

But we are not finished so far… As we are really going to use Mosquitto on a daily basis, and not just to do a tutorial, we have to force it to start each time we start our Raspberry Pi.

Enter this command:

sudo systemctl enable mosquitto.service

Secure your Mosquitto MQTT broker

It is also very important to secure our service by creating a Mosquitto identifier and an associated password (replace ID with the identifier of your choice):

sudo mosquitto_passwd -c / etc / mosquitto / passwd ID

We will then add two lines to the Mosquitto configuration file via the text editor:

sudo nano /etc/mosquitto/mosquitto.conf allow_anonymous false password_file / etc / mosquitto / passwd
Do not forget to do ctrl + o to save, to validate with Starter, and leave with ctrl + x
It's over, our Mosquitto MQTT broker is ready! All we have to do is use it ...

Use an MQTT client

We are going to relaunch our Mosquitto MQTT broker with:

sudo systemctl restart mosquitto

And we turn to a free MQTT client in order to read and publish topics very easily.

Our preference goes to MQTT Explorer, but you will find others if you prefer. Available for PC and Mac, this program will be very useful to us in the future by allowing us to view the topics and their activity, to manipulate them, and of course to publish MQTT topics.

  1. Install the client MQTT Explorer
  2. Fill in the address IP of your Raspberry Pi in "Host",
  3. Let him port 1883
  4. Enter your identifiers in 'Username' and 'Password' (those chosen for Mosquitto, not those for Raspberry Pi OS, of course).
You can now browse, create and modify your MQTT topics at will thanks to the Mosquitto broker and the MQTT Explorer client. What's the point ? Well that's what we'll see in the next home automation tutorials DIY...

For the more curious, you will better understand where we are going with this list of Mosquitto MQTT compatible applications:

Fascinated by Alexa since the day I received it in beta test, I gradually became passionate about the subject, before deciding to go further by creating a site with Jean-Christophe. An activity that allows me to quench my thirst for new technologies and share my discoveries about the nicest of communities: Les Alexiens.