MQTT (Message Queue Telemetry Transport) is the protocol that is becoming increasingly important in the world of the Internet of Things.
This makes it very easy to exchange different data between different sensors and actuators. All clients connected to the MQTT server send and receive their data to or from a central server.
In the MQTT protocol, the individual connected clients do not speak directly to each other.
When registering on the MQTT server, each client can specify which data they would like to receive as soon as new ones are available. At the same time, each client can send data to the server. This data sent to the server is then automatically distributed to the clients who have previously registered to receive the data.
An MQTT message always consists of a name and data. The name (called topic) enables clients to log in to the server as “interested parties” for the data sent under this name.
As soon as the server receives data under this name, it is forwarded to all clients who have previously registered as “interested parties” for this data.
So much for the rough function of the MQTT protocol.
In order for you to be able to use the MQTT protocol in your network, you need an MQTT server. I will explain to you how to install this on the RaspberryPi in this article.
In addition to your own MQTT server, there is also the option of using external MQTT servers available over the Internet. Some of these are provided free of charge.
In my opinion, this has a few disadvantages:
– your data leaves the home network. The probability that mischief will be committed or can be committed at all is not high. Nevertheless, I somehow feel more comfortable when my data doesn't leave my “own four walls”.
– you are dependent on the external MQTT server. If this is not accessible, for example because it has failed or your internet connection cannot be established, your entire network-internal (MQTT) communication will also be lost.
Since installing your own MQTT server is quick and free, this is the better alternative in my opinion.
Safety instructions
I know the following notes are always kind of annoying and seem unnecessary. Unfortunately, many people who knew "better" have lost eyes, fingers or other things due to carelessness or injured themselves. Data loss is almost negligible in comparison, but even these can be really annoying. Therefore, please take five minutes to read the safety instructions. Because even the coolest project is not worth injury or other trouble.
https://www.nerdiy.de/sicherheitshinweise/
Affiliate links/advertising links
The links to online shops listed here are so-called affiliate links. If you click on such an affiliate link and make a purchase via this link, Nerdiy.de will receive a commission from the relevant online shop or provider. The price does not change for you. If you make your purchases via these links, you support Nerdiy.de in being able to offer other useful projects in the future. 🙂
Overview
Before you can start installing the MQTT server, you should have prepared the Raspberry Pi so that it can be accessed via Putty.
The following three articles describe what needs to be done to prepare the RaspberryPi:
RaspberryPi – setup for nerdiys!
RaspberryPI - The First Configuration!
RaspberryPi – Control the RaspberryPi via SSH
Required material:
-no-
Required material:
In the following list you will find all the parts you need to implement this article.
Log in to the RaspberryPi via SSH
To start, you must first log in to the Rasp Pi with Putty via SSH. How to do this is in the article RaspberryPi - Control the RaspberryPi via SSH described.
Update package management
The package management in Linux is a "central place" through which various software packages can be installed. In order for this to work reliably, the lists and sources of the package management should be updated before each installation of new packages.
sudo apt-get update && sudo apt-get upgrade
Install mosquito
You can use the free open source software “Mosquitto” as an MQTT server. This can be easily installed via the package manager with the following command:
sudo apt-get install mosquitto mosquitto-clients
Set up autostart for the MQTT server
So that the MQTT server starts automatically after restarting your Raspberry Pi, you now have to activate the autostart for it. This can be done with the following command.
sudo systemctl enable mosquitto.service
Check the status of the MQTT server
After installation, “Mosquitto” will start automatically. You can check whether this worked with the following command:
sudo service mosquito status
The MQTT server can be stopped with the following command:
sudo service mosquito stop
The MQTT server can be started with the following command:
sudo service mosquito start
Check function
In order to carry out a first test, we want to try to send a message to the Sevrer and receive it from it at the same time. To do this, we need to subscribe to a topic of the MQTT server and at the same time send a message to this subscribed topic from another window.
To start, you first have to enter the following command in Putty:
mosquitto_sub -h localhost -v -t testTopic
In this new Putty window you enter the following command:
mosquitto_pub -h localhost -t testTopic -m "A mosquito flies, a mosquito pricks."
You should now see the message you just sent appear in the first Putty window.
If this works, your MQTT server has passed the first test. 🙂
Security note: No encryption or user identification is yet active. This means anyone who has access to your network can log on to the MQTT server and send or receive data to it. I will explain how to close these two security holes in a later article.
Have fun with the project
I hope everything worked as described for you. If not or you have questions or suggestions please let me know in the comments. I will then add this to the article if necessary.
Ideas for new projects are always welcome. 🙂
PS Many of these projects - especially the hardware projects - cost a lot of time and money. Of course I do this because I enjoy it, but if you think it's cool that I share the information with you, I would be happy about a small donation to the coffee fund. 🙂