HowTo: RaspberryPi – Install MQTT Server on the RaspberryPi

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.

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, 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 in being able to offer other useful projects in the future. 🙂 


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:

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.

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

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.

To start the update of the package management you have to enter the following command.
sudo apt-get update && sudo apt-get upgrade
Depending on how long ago your last update of the package management was, this process can now take a while. The lists that refer to the individual package sources are updated first.
After that, the packages themselves are updated. Since additional memory is occupied, you will be asked again for your consent. You have to confirm this with a "J" and "Enter".
Once the update is complete, you will see a small summary of the duration and scope of the update.

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
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 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
This starts the MQTT Subscriber. So a client that connects to the server you just created (hence localhost) and listens for the topic “testTopic”. As soon as data is sent from another MQTT client to the server under the topic “testTopic”, it will be displayed here.
Now open a new Putty window (but leave the old one open).

In this new Putty window you enter the following command:

mosquitto_pub -h localhost -t testTopic -m "A mosquito flies, a mosquito pricks."
This command sends the text “A mosquito flies, a mosquito bites.” To the topic “testTopic” that you previously subscribed to in the other Putty window.

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

Buy Me a Coffee at       

Kommentar hinterlassen

Your email address will not be published. Erforderliche Felder sind mit * markiert

This site uses Akismet to reduce spam. Learn how your comment data is processed.