Today we are going to see how to install and configure Mosquitto, one of the most popular MQTT brokers, especially in the domestic 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.
As theory is enough, today we have to get into the details and start playing. For this, the first thing we need is to install a broker to which we can connect the 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 domestic projects.
Eclipse Mosquito is an Open Source Broker from the Eclipse Foundation distributed under the EPL/EDL license, compatible with the MQTT protocol in its 3.1, 3.1.1, and 5.0 versions. 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 being a lightweight broker, are some of the reasons why this broker has become so popular.
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 the installation we run a console with administrator rights and do. mosquitto install
If during the installation you get an error, you should 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 of them have it integrated into their repositories. This is the case with 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 when the system starts, in a console we run
sudo systemctl enable mosquitto.service
Now we are going to test that the installation has been done correctly and that, indeed, Mosquitto is ready to listen and distribute our MQTT messages.
For this Mosquito provides two utilities ‘mosquitto_sub’ and ‘mosquitto_pub’, very suitable to test that everything works.
We open two command consoles and go to the folder where we have 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 console, second, we publish a message ‘Hello World’
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.
The Mosquitto configuration is stored in the file ‘mosquitto.conf’ which, in the case of Windows, is in the location where you have installed Mosquitto, and in Linux in ‘etc/mosquito’.
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
In this post we have learned how to install Mosquitto, the popular MQTT broker, both on Windows and Linux. As we have seen, it is very easy to have a functional broker in a matter of minutes.
Now we can connect our clients, which can be written in C#, Python, from a web page or even from a microprocessor like the ESP8266. We will see all this in upcoming posts.