MQTT (Message Queue Telemetry Transport) is the protocol that is becoming more and more important in the world of the Internet of Things.
It is 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.
Each client can specify when logging in to the MQTT server which data he would like to receive as soon as new ones are available. At the same time, every client can send data to the server. These data sent to the server are then automatically distributed to the clients who have previously reported to receive the data.
An MQTT message therefore always consists of a name and the data. The name (called “topic”) allows clients to log in to the server as “interested” in the data sent under that name.
As soon as the server receives data under this name, these are forwarded to all clients who have previously logged in as “interested parties” for this data.
So much for the rough function of the MQTT protocol.
In order to use the MQTT protocol in your network you need an MQTT server. How you install this on the RaspberryPi is explained in this article.
In addition to its own MQTT server, there is also the option of using external, via the Internet available MQTT server. These are partly provided free of charge.
In my opinion, this has a few disadvantages:
– your data leaves the domestic network. The likelihood that this is mischief or can be driven at all is not high. Nevertheless, I feel somehow more comfortable, if my data does not leave the “own four walls”.
– You depend on the external MQTT server. Should this not be attainable, because it failed for example or also your Internet connection can not be made, then also your complete network-internal (MQTT) communication falls out.
Since installing your own MQTT server is quick and free, this is the better alternative in my opinion.
Hints for our lovely english readers: Basically, many of the articles on Nerdiy.de are translations from the original german articles. Therefore, it may happen here and there that some illustrations are not available in english and that some translations are weird/strange/full of mistakes or generally totaly wrong. So if you find some obvious (or also not obvious) mistakes don't hesitate to leave us a hint about that in the comment section.
Also please don't get confused, that instead of a "dot" often a "comma" is used as decimal separator. 🙂
I know the following hints are always a bit annoying and seem unnecessary. But unfortunately, many people who knew it "better" from carelessness lost their eyes, fingers or other things or hurt themselves. In comparison, a loss of data is almost not worth mentioning, but even these can be really annoying. Therefore, please take five minutes to read the safety instructions. Even the coolest project is worth no injury or other annoyance. https://www.nerdiy.de/sicherheitshinweise/
The links to online shops listed here are so-called affiliate links. If you click on such an affiliate link and shop via this link, Nerdiy.de receives a commission from the online shop or provider concerned. The price doesn't change for you. If you do your purchases via these links, you will support Nerdiy.de in being able to offer further useful projects in the future. 🙂
Before you start with this article, you should have prepared the RaspberryPi so far that it can be reached via the network and controlled by SSH.
In the following list you will find all the parts you need to implement this article.
Log in via SSH on the RaspberryPi
To get started, you first need to log in to RasPi with SSH on Putty. How to do it is described in the article RaspberryPi - Controlling the RaspberryPi via SSH.
Update package management
The package management in Linux is a "central point" over which various software packages can be installed. For this to work reliably, the lists and sources of package management should be updated before installing any new packages.
sudo apt-get update && sudo apt-get upgrade
As an MQTT server you can use the free open-source software “Mosquitto”. This can be easily installed via the package manager with the following command:
sudo apt-get install mosquitto mosquitto-clients
Check the status of the MQTT server
After installation “Mosquitto” will be started automatically. If this worked can be checked with the following command:
sudo service mosquitto status
The MQTT server can be stopped with the following command:
sudo service mosquitto stop
The MQTT server can be started with the following command:
sudo service mosquitto start
Set up autostart for the MQTT server
In order for the MQTT server to start automatically after a restart of your RaspberryPi, you have to activate the autostart for it. This is done with the following command.
sudo systemctl enable mosquitto.service
In order to carry out a first test we want to try to send a message to the server and receive it at the same time. To do this, we must subscribe to a topic from the MQTT server and at the same time send a message to this subscribed topic from another window.
To start, you must first enter the following command in Putty:
mosquitto_sub -h localhost -v -t testTopic
In this new Putty window type in the following command:
mosquitto_pub -h localhost -t testTopic -m "Ein Mosquitto fliegt, ein Mosquitto piekst. "
You should now see the message just sent in the first Putty window.
If this works, your MQTT server passed the first test. 🙂
Security note: There is no encryption or user recognition active yet. That means, anyone who has access to your network can log on to the MQTT server and send or receive data from it. I will explain in a later article how to close these two vulnerabilities.
I hope everything worked as described. If not or you have any other questions or suggestions, please let me know in the comments. Also, ideas for new projects are always welcome. 🙂
P.S. 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 appreciate it that I share these information with you, I would be happy about a small donation to the coffee box. 🙂