Today we are going to see how to install and configure Mosquitto, one of the most popular MQTT brokers, especially in the home sector, which you can add to your IoT projects.
In previous posts we have talked about communication protocols for IoT, where we saw the publisher-subscriber pattern, the MQTT protocol and its importance, and some of the main MQTT brokers available.
That’s enough theory for now, today it’s time to get our hands dirty and start playing. For this, the first thing we need is to install a broker to which we can connect our devices.
In this post we are going to install Mosquitto, which as we said is one of the most popular brokers, and probably the most used in home projects.
Eclipse Mosquitto is an Open Source Broker from the Eclipse Foundation distributed under the EPL/EDL license, compatible with the MQTT protocol in versions 3.1, 3.1.1, and 5.0. It is programmed in C and is compatible with Windows, Linux, and Mac. The code is here https://github.com/eclipse/mosquitto
The fact that it is Open Source and its licensing, as well as being cross-platform and a lightweight broker, are some of the reasons why this broker has gained so much popularity.
Installing Mosquitto
Installing Mosquitto on Windows is very simple, as we have an installer on the downloads page https://mosquitto.org/download/. We just have to download it and install it, like any other program.
If we want to run Mosquitto as a Windows service, after installation we run a console with administrator rights and do: mosquitto install

If you get any errors during installation, you will need to install Visual C++ Redistributable for Visual Studio 2015 available on the Microsoft website. https://www.microsoft.com/es-es/download/details.aspx?id=48145
Installing Mosquitto on the main Linux distros is equally simple, as most have it integrated into their repositories. This is the case for Ubuntu, Debian, or Raspbian (Raspberry Pi). So we just have to run the commands.
sudo apt update sudo apt upgrade sudo apt-get install mosquitto mosquitto-clients
If we want to run mosquitto at system startup, in a console we run
sudo systemctl enable mosquitto.service
Testing Mosquitto
Now let’s test that the installation was done correctly and that, indeed, Mosquitto is ready to listen and distribute our MQTT messages.
For this, Mosquitto provides two utilities, ‘mosquitto_sub’ and ‘mosquitto_pub’, which are very appropriate for testing that everything works.
We open two command consoles and go to the folder where we installed Mosquitto. In one of them we subscribe to the topic “mimqtt/test” (for example) with:
mosquitto_sub -d -h localhost -p 1883 -t “mimqtt/test”
In the other, second console, we publish a ‘Hello World’ message
mosquitto_pub -d -h localhost -p 1883 -t “mimqtt/test” -m “Hello World”
We will see that the message has been sent correctly from the publisher

And in the first window, the subscriber, we will see that the message has been correctly received.

Mosquitto Configuration
Mosquitto’s configuration is saved in the ‘mosquitto.conf’ file, which in the case of Windows is in the location where you installed Mosquitto, and in Linux in ‘etc/mosquitto’.
The ‘mosquitto.conf’ file is divided into sections, which control the main aspects of the broker. For more information, consult the documentation at https://mosquitto.org/man/mosquitto-conf-5.html

