Installing MQTT on Windows - d-sanchezl/xware GitHub Wiki
Firewall considerations
When installing MQTT, you might be asked to allow the mosquitto program to communicate past your firewall. Make sure to allow mosquitto communication rights, otherwise issues will come up. You may allow mosquitto to communicate only on private or only on public networks.
Installation
This MQTT for Windows guide was heavily based on this guide.
- Head to Mosquitto's downloads page and download the binary installer that suits your system - most likely the 64-bit build.
- Execute the installer. If a Windows Smartscreen window appears to prevent installation, click on "More info" and then on "Run Anyway":
-
If prompted to choose install components, install both the Files and the Service Mosquitto components.
-
Once installed, go to Mosquitto's install directory and open the 'README-windows.txt' file. Head to the 'Dependencies' section; as of the writing of thos guide, the only extra requirement is to install OpenSSL:
- Head to the Open SSL website indicated in the README under the Download Win32/Win64 OpenSSL section and download the 'Light' binary for your Windows version - again, you will most likely get the 'Win64 OpenSSL v1.1.1i Light' file. Install this file normally.
- With OpenSSL installed, head to its installation directory and into the 'bin' subfolder, and copy the following files to the Mosquitto installation folder:
libssl-1_1.dll, libcrypto-1_1.dll or libssl-1_1-x64.dll, libcrypto-1_1-x64.dll
- Mosquitto is now successfully installed. Every time you wish to run the Mosquitto server, you must open a Command Prompt window (type cmd in the Start Menu search bar) and:
a) Change the current directory to the Mosquitto installation folder b) Execute the mosquitto -v command
cd C:\Program Files\mosquitto
mosquitto -v
- Add the Mosquitto install directory to the PATH variable so that the 'change directory' command can be omitted, and the 'mosquitto' command (along with other commands) can be executed directly in any Command Prompt. For a guide on adding a directory to PATH as found in Stack Overflow, click here.
More installation steps are required (see the Installation (contd.) section below) but a quick verification of the correct installation of MQTT thus far can be done.
Verification
To verify that MQTT was installed correctly, two clients can be executed on a single Windows machine, which in turn also holds the server.
-
Start the server in a Command Prompt using
mosquitto -v
-
Then, open a second terminal window that will act as a 'subscriber' client. The standard syntax for 'subbing' to a topic is:
mosquitto_sub -h [address] -t [topic name in ""]
- Where -h indicates the broker to which the client is connecting, 'address' stands for the IP address of the server machine ('localhost' can be used to refer to this same machine), -t indicates the Topic, and the topic name is written in quotes. Type and execute this example into the terminal:
mosquitto_sub -h localhost -t "TestTopic/SubTopic"
- Open a third terminal that will act as the 'publisher' client. The syntax for publishing a message to a given topic is:
mosquitto_pub -h [address] -t [topic name in ""] -m [message in ""]
- Here, the ‘mosquitto_pub’ command is used instead, and a message is added with -m. Type and execute this example into the terminal:
mosquitto_pub -h localhost -t "TestTopic/SubTopic" -m "This is a message"
- If MQTT was installed correctly, messages sent by the publisher should reach the subscriber. The following image contains three resulting Command Prompt windows:
- The uppermost window is the MQTT server, which regulates communications between MQTT clients.
- the rightmost window is used to send messages as the publisher
- the lower-left window receives said messages as the subscriber
Installation (contd.)
By default, MQTT on Windows can only communicate locally. If you run the Mosquitto server in a command window, you will get the following message:
Note the Starting in local only mode.
message. For Mosquitto to communicate to other devices (such as a Raspberry Pi client), a listener must be configured.
- Create a new notepad file and write the following contents on it:
# Config file for mosquitto
# Plain MQTT protocol
listener 1883
protocol mqtt
allow_anonymous true
- We will now save this file to the "default" Command Prompt location so it is always visible to Mosquitto. Open a new command prompt window, and see what the default folder is:
- In this case, the default cmd location is "C:\Users\User". Save this notepad file in this location and name it mosquitto.conf. Make sure it is saved as mosquitto.conf rather than mosquitto.conf.txt. If you use the default Windows Notepad, make sure to change the Type option from
Text document (\*.txt)
toAll files (\*.\*)
.
- Open a new command prompt and enter
mosquitto -c mosquitto.conf -v
to verify that the local mode message does not appear anymore:
Summary
When using Windows as an MQTT server, remember to run the following in a command prompt window:
mosquitto -c mosquitto.conf -v
This window must remain open; closing it will shut down the MQTT server.
To create a subscriber or to send a message as a publisher, use the mosquitto_sub
and mosquitto_pub
commands as explained in the Verification section of this page. above