HowTo: RaspberryPi – Install MQTT server on the RaspberryPi

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


Safety instructions

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/


Requirements

Helpful Articles:

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.

The following articles describe what to do to prepare the RaspberryPi:
RaspberryPi – Setting up for Nerdiys!
RaspberryPi – The first configuration
RaspberryPi – Controlling the RaspberryPi via SSH

Required tools:
-none-

Required material:

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.

After entering your username and password you can start to enter the first commands.

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.

To start the package management update you have to enter the following command.
sudo apt-get update && sudo apt-get upgrade
Depending on how long your last update of the package management is, this process can take some time. First, the lists are updated in which the individual repositories are referenced.
Then the packages themselves are updated. As this additional memory is occupied, you will again asked for your consent. You have to confirm this with a "J"(in German) or a "Y" and "Enter".
When the update is complete, you will see a small summary of the duration and scope of the update.

Mosquitto install

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
It should be readable in gürn “active (running)”. To leave this status view you have to press CTRL + C.

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

Check function

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
This starts the MQTT Subscriber. So a client that connects to the just created server (that’s why localhost) and listen to the topic “testTopic”. As soon as data is sent to the server under the topic “testTopic” by another MQTT client, these are displayed here.
Now you open a new window Putty (But leave the old one open).

In this new Putty window type in the following command:

mosquitto_pub -h localhost -t testTopic -m "Ein Mosquitto fliegt, ein Mosquitto piekst. "
This command sends the text “A mosquito flies, a mosquito pokes.” To the topic “testTopic” that you have previously subscribed to in the other Putty window.

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

Fab

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

Buy Me a Coffee at ko-fi.com

Leave a Reply

Your email address will not be published. Required fields are marked *