Mosquito installation
In this article we will see how to install an MQTT server on Raspberry Pi, step by step. First of all we must install the software to run an MQTT broker (server). mosquito It is the most used and the one we are going to use in this case.
Don't stop looking at this Article, where I talk about the differences between MQTT and HTTP and which is the best option in each case. And if you want to add encryption to communication, check out this Article where I explain how to do it.
To install it we connect by console (ssh) to the raspberry and execute the following commands. Everything described here assumes that we are using raspbian or some other debian-based operating system.
Before installing Mosquitto on our Raspberry, we update the packages
sudo apt-get update
sudo apt-get upgrade
Then we install mosquitto and its client software
sudo apt-get install mosquitto mosquitto-clients

Configuring Mosquito
Once this is done we already have everything installed and a basic configuration, but before running the broker we will make some configurations.
The default generated configuration file (mosquitto.conf) is located in the /etc/mosquitto directory.
If we open or read this file we will find the following
# Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example
pid_file /var/run/mosquitto.pid
persistence true
persistence_location /var/lib/mosquitto/
log_dest file /var/log/mosquitto/mosquitto.log
include_dir /etc/mosquitto/conf.d
All options that are not specified in the configuration file take default values. As seen in the text, there is an example file in the /usr/share/doc/mosquitto/examples/ directory. However, the mosquitto.conf.example file is compressed in the gz format, so we must uncompress it with the gunzip utility, for which we execute the following:
sudo gunzip mosquitto.conf.gz
The zip file actually contains four sample files: aclfile.example, mosquitto.conf, pskfile.example, and pwfile.example. These files are access lists, configuration, encryption key and passwords respectively.
These files can be opened and modified with any text editor, in my case I use elder brother. We will now see some basic configurations.
One of these settings is the port that the server will be listening on, which is TCP 1883 by default. We will leave this unchanged, but it is important to know it in order to connect to the broker.
Another setting is the interface that the server will be listening on. If not set, it will listen on all interfaces, but can be set to one by specifying the IP address of that interface. This is totally optional and has to do with restricting access to the broker from other interfaces.
For example: listener 1883 192.168.0.101

Other settings
The configuration file has many more options, but in this tutorial we will leave everything as default. If you want to modify something, you should include it in the mosquitto.conf file in the /etc/mosquitto directory or add an additional configuration file in the /etc/mosquitto/conf.d directory, which is referenced in the above file.
Finally we will define a username and password. As many users as needed can be defined based on the number of clients and the permissions they have.
To generate the username and password we execute the following command:
sudo mosquitto_passwd -c /etc/mosquitto/pwfile user
Where “user” is the name of the user that we are going to use.
This command will ask to enter, in a line break, the corresponding password for that user. It should be noted that the broker can also be configured to be used anonymously (without authentication).
Would you like to try this for free on a cloud server? Here I leave you a video-tutorial and a $100 credit to use on Digital Ocean.
In addition, encryption can be used in the authentication and sending of messages, but we will not cover this in this tutorial.
Access the free course “The 4 fundamentals in managing IoT devices”
Testing the communication with the broker
Now it's time to test that everything is working correctly, for which we will run the client software as follows.
In a window we subscribe to the topic in which the messages are going to be written.
mosquitto_sub -h -Yo -or -P' ' -t topic/value -q 2
In another window we execute the publication command
mosquitto_pub -h -Yo -or -P' ' -t topic/value -m "hello world"
When making this publication, the published message should appear in the first window.
Feel free to write your questions or comments below.
3 Comments
MQTT vs HTTP - What to choose for your IoT project | IoT Consulting · 11 December, 2019 at 10:28 AM
[…] purpose, here is an article on how to install an MQTT server on a Raspberry […]
How to add encryption to MQTT - IoT Consulting · 20 March, 2020 at 10:28 AM
[…] If you still haven't seen how to install an MQTT server on a Raspberry Pi, I invite you to read this article. […]
Raspberry Pi IoT Applications - Your IoT Expert Source · 24 June, 2020 at 10:28 AM
[…] you are interested in this topic, I invite you to also see this other publication, where I explain how to install an MQTT server on a Raspberry […]