Friday, January 26, 2018

Getting Started with MQTT using Mosquitto

What is MQTT?
The answer for this question is given on mqtt.org website and is as follow:
"MQTT stands for MQ Telemetry Transport. It is a publish/subscribe, extremely simple and lightweight messaging protocol, designed for constrained devices and low-bandwidth, high-latency or unreliable networks. The design principles are to minimize network bandwidth and device resource requirements whilst also attempting to ensure reliability and some degree of assurance of delivery. These principles also turn out to make the protocol ideal of the emerging “machine-to-machine” (M2M) or “Internet of Things” world of connected devices, and for mobile applications where bandwidth and battery power are at a premium".
The main objective of this post is not to teach you, What MQTT is, for that you can go to MQTT website and read it specification, which is probably the best place to understand each and every detail.
In this post, I will show, how to setup Mosquitto, which is an MQTT broker, and then Subscribe and Publish topics on your PC and then from your PC to CloudMQTT.
MQTT Publish/Subscribe Architecture

Watch the following video or follow the instruction given below.
 

Install Mosquitto
First go to Mosquitto website and find the download option, and after that find the mosquitto windows installation file, as highlighted below.
Mosquitto Broker Download
Now install the downloaded executable file and you will be instructed to download the Open-SSL and pthreadv2.dll file on the start screen as shown below.
OpenSSL and pthreadv2.dll download link
Download link for Open-SSL is given below:
http://slproweb.com/products/Win32OpenSSL.html
Download link for pthreadv2.dll file is given below:
ftp://sources.redhat.com/pub/pthreads-win32/dll-latest/dll/x86/

Make sure you download the Light Version of Open-SSL, and after downloading the Open-SSL and pthreadv2.dll, just install the Open-SSL and after this resume your mosquitto installation. After the mosquitto is installed just copy the pthreadv2.dll in the mosquitto installation directory. 
And finally, run the mosquitto.exe, this will include the Mosquitto Broker in Windows services.
To summarize follow the steps given below.
* Install Open-SSL
* Install Mosquitto
* Copy pthreadv2.dll inside the Mosquitto installation directory.
* Start Mosquitto.exe
* Restart Windows

Optional Steps:
* Include Mosquitto Installation path in PATH, Environment Variables of Windows, by this you can access the Mosquitto function directly from the command prompt.
Note:
Mosquitto installation must be finished without errors, in case you get any error, just note down the error message and search Google or ask here in the comments section.
Most of the time some of the dll's missing error is there, in that case just find that dll file and copy that in Mosquitto installation directory.
Most common errors are due to these dll's, "libeay32.dll", "ssleay32.dll" and "libeay32.dll". 
These dlls are present inside the Open-SLL installation directory, just copy these dll's into Mosquitto installation directory or update the Environment Variables PATH.

Once the installation is successful, you will see Mosquitto Broker running in Windows Services, as shown below.
Mosquitto Broker Running
Publish and Subscribe Using Mosquitto and CloudMQTT
Before going further, I would like to give a short description of CloudMQTT.
CloudMQTT have managed Mosquitto servers in the cloud. Mosquitto implements the MQ Telemetry Transport protocol, MQTT, which provides lightweight methods of carrying out messaging using a publish/subscribe message queuing model.
So as a first step, create an account and login to the control panel and press Create New Instance+ to create a new instance. Choose a name for the instance and the data-center to host the instance.
To get started you need to sign up for a customer plan. What plan you want to use depend on your needs. 
There are different plans and you can try CloudMQTT for free with the plan CuteCat.
The following is the information of the "Test" instances created for my account.
Details of my "Test" Instance

Now we will Publish and Subscribe from our PC to CloudMQTT.
Subscribe
To Subscribe a topic, we need to use command "mosquitto_sub" from our PC, this command has lots of options with it, but to keep things simple we will just use the basic, minimum required option, with this command.
The following is the command to subscribe to the topic named "topic".

mosquitto_sub -h "m14.cloudmqtt.com" 
-p "18410" -u "setsmjwc" -P 
"apDnKqHRgAjA" -t "topic/#" 
-h is used to specify the host-name.
-p is used to specify the port number.
-u is used to specify the user name.
-P is used to specify the password.
-t is used to specify the topic name.
Subscription Example
If you want to see the debug messages also, then you can add "-d" at the end of the above command.

mosquitto_sub -h "m14.cloudmqtt.com" 
-p "18410" 
-u "setsmjwc" -P "apDnKqHRgAjA" 
-t "topic#" 

Publish
To Publish a topic we need to use the command, "mosquitto_pub", from our PC, and the topic will get published since CloudMQTT is a broker, it will receive all the topics from all the publisher. The command to publish a topic is as follow:

mosquitto_pub -h "m14.cloudmqtt.com" 
-p "18410" 
-u "setsmjwc" -P "apDnKqHRgAjA" 
-m "Message" 
-h is used to specify the host-name.
-p is used to specify the port number.
-u is used to specify the user name.
-P is used to specify the password.
-t is used to specify the topic name.
-m is used to specify the message.


After sending the above command, we will see the following data on the WebSocket UI of CloudMQTT.
Publish Example
By using the above two commands one can Subscribe for particular topics and Publish particular topics.
This is a basic post to get Mosquitto broker setup on Windows PC, in the next post, we will use Python scripts to Subscribe and Publish topics in the same program.

Update 3rd April 2021

CloudMQTT free plan is not available now for new user, since I am an existing user my free plan will continue, but the new users need to purchase the plan in order to use their servers.
I have found one another free server which is public, so please don't use it for production environments as all data is public on this.
The free server is "HiveMQ" server. 

1 comment:

  1. Very informative blog... <a href="https://www.axonize.com/blog/iot-automation/frequently-asked-iot-questions/>MQTT service</a> was created with the goal of collecting data from many devices and then transporting that data to the IT infrastructure. Thanks

    ReplyDelete