Running Mosquitto MQTT on Windows 10 super easy

29 Mar 2018 | Running Mosquitto MQTT on Windows 10 super easy |

TTGO esp32 LoRa Modules using MQTT

So I’ve been playing with IoT lately,  using platforms such as  Arduino ,ESP32, Raspberry Pi and when it comes to being able to have these devices communicate with a hub or server, the de facto standard is to use a MQTT  server and protocol  . Unfortunately, prior to Windows 10  finding a  native Windows  version is complicated.

In this post I’ll describe the super-easy steps to getting it up and running with Windows 10 . The trick on Windows 10 (yes only on Windows 10 or later) is to Install Windows Linux Subsytem (WSL)

While there are a bunch of MQTT implementations, one very popular implementation of the MQTT Broker is the Eclipse Mosquitto™ An open source MQTT broker, it runs on multiple platforms, Linux is by far the preferred platform, and while it does have a Windows variation unfortunately it requires too many dependencies  to make it practical to just run the install script and get it up and running.

View / Inspect MQTT Traffic

MQTT Explorer

On a  side note once you get the MQTT Mosquitto server up and running , a good way to see MQTT traffic and inspect it is with MQTT Explorer . Directly from their website ” MQTT Explorer is a comprehensive MQTT client that provides a structured view of your MQTT topics and makes working with devices/services on your broker dead-simple.”

Challenges of Running Mosquitto on earlier Windows Vista,7 ,8 …

Basically if you have a version of Windows prior to Windows 10, you will need to perform the following steps  , or a variation of these (Some sites use Cygwin for the Linux layer)..

As you can see it’s a bit of an involved process., plus some of those binaries or dll’s  must be a certain 32bit version for overall library compatibility so it’s a hassle, again be careful where you get the binaries/dll from , many of the links are outdated or compromised. Be careful. For complete details visit this post.

  1. . Download and install OpenSSL for Windows , make sure you grab the older version Win32 OpenSSL v1.0.2o as newer versions are missing critical dll.
  2. Download and unzip Win32  Pthread dll , and copy them to you mosquitto binary folder,
  3. Run and test mosquitto Server..
  4. WARNING  : remember double check the links above , be careful and make sure these are from reputable sources, scan all downloads for malicious software,  I inadvertently downloaded some malware attached to some of the DLL files. don’t just blindly clicking other sites  links,..
  5. Just forget the above steps and do the simple steps below.

Windows 10 Runs  Linux shell  via Windows 10’s Windows Subsystem for Linux (WSL)

If you have Windows 10 then you’re in luck! it’s soo much easier.. the reason things are much easier in Windows 10, is because Microsoft has made it so you can install a Windows Subsytem for Linux (Basically Microsoft created a Linux friendly container/vm within Windows 10)   Simply run the command below as an administrator in Powershell and then reboot.

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

Installing Ubuntu (or other Linux) from Microsoft Store (free)

Once your system reboots, no head on over to the Microsoft Store (a trusted source) and choose a Linux distribution, that is available and you prefer, I went with Ubuntu .

Microsoft Store available linux packages

 

After that installs , just run it  and it will put you into the Ubuntu Linux Shell, and from here you can proceed to install good ole Linux Mosquitto MQTT, don’t forget to update the software distribution  repos first..

sudo apt-get update

then install mosquitto.

sudo apt-get install mosquitto

and run it, it will run on the default port 1883. You can of course configure it just refer to the usual  Linux Mosquitto docs.

Run Mosquitto in Windows Linux SubsystemWe’re almost done! we need to do one more step. You need to  open up your ports to allow the Linux subsystem to be reachable from the outside and act as a server. Steps are…

Windows 10 Firewall Advanced settings

  • Go to Windows Firewall with Advanced Security  (choose Advanced Settings in Firewall prompt)
  • Click Inbound Rules then on right hand pane  add a New Rule 
  • This will bring up a  New Rules wizard, just follow the prompt entering the following
  • Set rule type as Port.
  • Enter a Specific local port your server is running under WSL, in this case, 1883
  • Choose TCP port
  • in the next tab Action tab, choose Allow the connection
  • next where this rule Applies choose all appropriate Domains, such as Public (entire Internet) or Private (just my local LAN traffic) or check all for complete access from outside.
  • and finally just name the Rule (Mosquitto port 1883) or something similar and press Finish.

Now all MQTT traffic to this server should be responded to as expected.

Also when your run WSL you  can access your normal  Windows drive  and folders check under the /mnt/  folder.

One drawback to be aware of… (reboots)

Note: this approach has  one drawback, running it as a server after rebooting, that is the server because its part of the Windows Linux Subsystem will need to be started automatically each time windows re-boots, and this may require more fiddling,

I found a post here that may be of help.  Starting a linux bash application as a service, since you need to have windows automatically boot the WSL for mosquitto to run in this environment, I gather its possible , but requires more tweaking.. if any windows folks have any good suggestions let me know.

Conclusion

While it may seem like a lot of steps , I can tell you it will only take a few minutes compared to all the time needed to try and get Mosquitto running as a native windows application.. Hope this helps some of you guys, leave comments and share your experiences.

26 thoughts on “Running Mosquitto MQTT on Windows 10 super easy

  1. Reply RajTowdry Apr 17,2018 6:32 pm

    Excellent works perfectly for me and so much easier than other examples

  2. Reply sanjayPtel Apr 21,2018 3:11 pm

    Awesome, yes , finally a straightforward way to run Mosquitto on Windows, thanks abrandao.com you saved me a bunch of time

  3. Reply JaniceRose May 17,2018 1:40 pm

    Yes!, this is awesomely easy literally cut-n-paste the above command and then just install Linux from the MS Store.. nice!

  4. Reply Becca White Aug 8,2018 2:13 pm

    Super easy thanks a bunch this makes it so much easier, just a little pain to open up the firewall on mywindows install but it worked.

  5. Reply Seamus o Sep 3,2018 9:06 pm

    I’m confused, I didn’t see this option under windows 7, where can I find it?

    • Reply Tony B. Sep 26,2018 3:45 pm

      WSL is NOT available in Windows versions prior to Windows 10.. Recommend upgrading as MSFT is retiring most older versions of windows.

  6. Reply Pavani Oct 30,2018 12:46 am

    When it’s working as explained, when I run netstat or try to find status, it doesn’t show if mosquitto is running.

  7. Reply Robert Auer Nov 28,2018 9:39 am

    Hey great article – thank you!

    I need to run it as secure – how can I set up for port 8883?

    Thanks.

  8. Reply Whiskey Jan 5,2019 9:19 am

    Finding this tutorial has changed my life on so many levels. Thank you so much for sharing.

  9. Reply C Delgado Feb 12,2019 10:31 pm

    Excellent!

  10. Reply Atul Dwivedi Jul 11,2019 12:10 pm

    Excellent Work!!! I have successfully installed the mosquitto broker. Now, How to connect my esp8266 as a client to it? How to get the IP address of this broker?

    • Reply Tony B. Jul 11,2019 3:14 pm

      First , make sure the Windows Firewall is letting the MQTT port (Typically) 1883 through, verify this.. The Code for the ESP8266 looks something like this…

      setup_wifi();
      client.setServer(mqtt_server, 1883);

      //Now do the normal LoopCode here
      if (!client.connected()) {
      reconnect();
      }
      client.loop();

      //Now send the MQTT message
      client.publish(“/sensor/door”, String(doorState).c_str(), false); //false means don’t retain messages
      client.publish(“/sensor/rssi”,String(rssi).c_str() , false); // wifi rssi = signal strenght 0 – 100 , <-80 is poor delay(1000); //give MQTT broker a chance to get message - before disconnecting wifi WiFi.disconnect(); For a detailed explanation of connecting MQTT from Arduino and ESP8xxx hardware , take a look at my Github https://github.com/acbrandao/Arduino-Projects

  11. Pingback: Appium MQTT backdoor

  12. Reply Amal Jun 6,2020 12:30 am

    Can we run a python script saved in window file when a massage come from mqtt by WSL?

  13. Reply Stuart Jun 24,2020 6:25 am

    I would also like to know how to work with this set-up and python scripts. Thanks to this guide I’ve been able to send messages with the mosquitto_pub and _sub tools between two WSL terminals but I can’t seem to send anything from my python script using paho-mqtt. Tried the exact same thing on my linux machine and it works fine. I’m new to WSL and MQTT so there might be some obvious that I’m missing. Thanks in advance

Leave a Reply to Tony B. Cancel Reply