Download the latest binaries for windows from here and install them with the installation wizard. The standard path of installation used in this tutorial is C:\Program Files\Mosquitto
.
The installer of Mosquitto does not automatically add the installation path to the global PATH
variables. Search for PATH
in the Windows search and navigate to Environment Variables -> System Variables -> Path
and add the following entry to the PATH variables:
C:\Program Files\Mosquitto
This enables Windows to find the Mosquitto executables.
Open the Windows Defender firewall by pressing Win + R and launching wf.msc
. Add a new inbound rule with the following parameters:
This allows external clients to connect to the MQTT server on port 1883 once its running.
First, create an empty file called passwords.txt
in the Mosquitto installation folder. It will hold all users and their passowords in the next step.
In the installation directory, open the configuration file mosquitto.conf
. Two settings need to be activated (uncommented) for user authentication to work:
allow_anonymous false # Disable anonymous logins
password_file C:\Program Files\Mosquitto\passwords.txt # Path to the password file
Open the file passwords.txt
, that was created in the prior step, and add the desired users, one per line, to the file. The following format must be used for each line:
username:password
Once the user-password pairs are created, they need to be hashed. Run the following command in a terminal to hash all passwords in the password file:
mosquitto_passwd -U "C:\Program Files\Mosquitto\passwords.txt"
After this step, the plaintext passwords should now be ciphered.
This step tells Mosquitto on which interface to listen for MQTT messages. Uncomment the following line in the configuration file mosquitto.conf
:
listener 1883 <ip_address> # IP address and port of the interface to bind the broker to
When all settings are done, start (restart) the Mosquitto broker with the Windows Service panel. Press Win + R and run services.msc
to locate the Mosquitto service. Right click on it and choose Restart Service
.
The broker should now be ready to go.
Use a tool like MQTT Explorer
to connect to the broker and to send messages to it. Alternatively, Mosquitto supplies two helper programs, one for publishing and one for subscribing, that can be used on a remote machine to test the communication.
Note: Install Mosquitto on the remote machine with the same procedure for the following commands to work!
In one terminal, subscribe to the topic test
with the following command:
mosquitto_sub -h "<ip_address>" -p 1883 -t "test" -u "user_name" -P "password"
Use another terminal to send a message to the MQTT broker with the following command:
mosquitto_pub -h "<ip_address>" -p 1883 -t "test" -u "user_name" -P "password" -m "Hello World via MQTT!"
The message should now appear in the first console if everything is set up correctly.