What is MQTT Protocol | A deep dive approach for MQTT | MQTT installation Step-by-step

cloudyrathor.com

What is MQTT Protocol | A deep dive approach for MQTT | MQTT installation Step-by-step

If you are IoT enthusiast and trying to explore the possibilities of IoT, then you have to explore the MQTT and COAP Protocol. Take a long breath and ready for the deep dive…….

MQTT ( Message Queuing and Telemetry Transport ) is an Open standard and it is OASIS( Organization for the Advancement of structured information standards) standardized which is royalty-free.It's also ISO standard.

cloudyrathor.com

WHAT is MQTT ?

In simple its just an Publish-Subscribe messaging protocol which works on top of TCP/IP protocol having very small “coding footprint”.MQTT protocol is a scalable and cost-efficient protocol to connect your IoT device with the internet.

WHY MQTT is used for ?

MQTT is used for delivering messages which is nearly real-time in nature and having guarantee of delivery. If you like to connect thousands of device in your project and hoping to send/update or broadcast the notification of any changes then your choice of this protocol is right. The first good thing about this protocol is, its light weighted.

The protocol is almost adopted by most of the company worldwide.MQTT is works on lower bandwidth and able to deal with unreliable networks with minimum error and complexity.

In Simple Words, if you have many devices to collect the information and want to update the central server for the further processing and decision making then this protocol will do good for you.

HOW MQTT works ?

To understand the working of this Protocol we need to know the basics elements of this protocol. So Basically it have

  • Publish/Subscribe
  • Message
  • Topics
  • Broker

Here at first, a device can publish the message to your devices and the device which subscribe for the particular device is able here it or receive signal from that device topic wise.So we can say it's just an alternative for the conventional client-server network.where client is able to communicate with the server. But here in IoT world some time devices are not directly communicate with the central server but it decouple a client. The devices which send a particular message called as publisher and devices which get the message is called a subscriber.But here publisher and subscriber are unaware about each other and here a third component comes into existence, that is broker.

WHY broker ?

A broker is an essential part of any Publish/Subscribe protocol, Because broker handle number of concurrent connected MQTT clients. The broker is responsible for receiving the messages and filter the same. Broker also decide who is interested in that signal and according to the interest/Subscription, it send the data to the subscriber device. Broker maintain the session of all the devices which are connected to it. It check the authenticity of the devices and authorized them to send and receive the data/signal.Due to broker in the system one can use their own authentication logic for the authorization.So if the broker of the system is rigid in nature, then your system can handle the problem very well.

How Broker Works ?

The working of this protocol have just five verbs…..

  • Connect
  • Disconnect
  • Publish
  • Subscribe
  • Ping

The part of publishing and subscribing is an event-driven based. Which trigger the Push service of messages to the client. The heart of this system is the broker which is responsible for dispatching the messages between the sender and the receiver.

Every time Client which like to publish, sends  a message to the broker which include the topic also. Basically a Topic is the routing information which is essential for the broker to work well.

Now if one of the client willing to get the particular messages from the broker have to subscribe for the same and broker will send all the relevant message to it. And here client does not required to know about the origin of the sender.

Each client that wants to receive messages subscribes to a certain topic and the broker delivers all messages with the matching topic to the client. Therefore the clients don’t have to know each other, they only communicate over the topic. This architecture enables highly scalable solutions without dependencies between the data producers and the data consumers.

WHO uses the MQTT Protocol ?

As per the above description we know that, this protocol is best for low bandwidth and for the project which required high accuracy. It have good performance in high-latency or unreliable network. The Projects which required machine to machine communication just like vehicle to vehicle communication can use this protocol.

cloudyrathor.com

Lets get Practical :

If you want to start your IoT project using MQTT protocol from the scratch then I will suggest you two Broker for your project where first broker is cloud-based ( CLOUDMQTT ) which can be access with just small configuration and internet connection but if you want to make your own setup then you have standalone setup then go for the ( Mosquitto ).

CLOUDMQTT :

https://www.cloudmqtt.com/
https://www.cloudmqtt.com/

It provide some nice services for your project where it is very lightweight messaging protocol which is best suited for embedded systems, sensors and mobile applications having very strong delivery hit rate with three mode

  1. Fire-and-forget
  2. At-least-one
  3. Exactly-once

On the console of CLOUDMQTT you just need to fulfill some configuration like

  • Server
  • User
  • Password
  • Port
  • SSL port
  • WebSocket port
  • Connection limit

This platform provide two library MQTT.h and PubSubClient.h which you need to include and just configure the same. For more details you can visit to this official website of  CLOUDMQTT. Or for configuration, you can go for this too Conf

Mosquitto :

Eclipse Mosquitto is an open source broker service for MQTT protocol which works with version 3.1 and 3.1.1 MQTT protocol. It also available with the library written in C which is good for implementing the IoT projects.

cloudyrathor.com

How to install and test it :

To install the mosquitto and test it on Ubuntu (You can also install it on Windows and Raspberry pi too) follow this steps…..

To install it :

sudo apt-get update

sudo apt-get install mosquitto

Once you install the server you need to install its client too….to install the client

Sudo apt-get install mosquitto-clients

We are installing a client for testing purpose, we will use two terminal to test our setup

Now fire this command mosquitto_sub -t “demo” here mosquitto_sub is subscribed client and demo followed by “-t” is a “topic” name.

Once we have done with the installation of the client then it’s time to publish a message to the topic, to do so log in to new terminal ctrl+t and type

Mosquitto_pub -m “Hello from cloudyrathor’s mosquitto_pub client” -t “demo”

Here we publish the message by command “mosquito_pub” followed by -m and this message will be received by the subscriber what we have seen in the previous command you can enjoy this setup in the video below.

But the message we send was not secured, to make this communication secure and robust we need to set the password to it.

To set up the password fire this command

sudo mosquitto_passwd -c /etc/mosquitto/passwd demo

Password: demo (you can give your own password)

Now you need to create one conf file to save this configuration

So hit this command

sudo nano /etc/mosquitto/conf.d/default.conf  ( it will be an empty file )

Now type this in an empty file

allow_anonymous false

password_file /etc/mosquitto/passwd

Now hit ctrl+o then enter and ctrl+x

Now we are ready to test the secured setup…..just restart the mosquitto server to see the changes

sudo systemctl restart mosquitto

Now open your subscriber terminal hit ctrl+c to exit and restart with this command

mosquitto_sub -t “demo” -u “demo” -P “demo”

here I gave the user and password is “demo”

Now go to your client terminal and try to send the message….

mosquitto_pub -t “demo” -m “hello from cloudyrathor mosquitto_pub client” and hit the enter

Definitely, it will give this error  message with rejection (coz you didn't give password)

Connection Refused: not authorized.

Error: The connection was refused.

Now try the same command with the password and hit the enter key…..

mosquitto_pub -t “demo” -m “hello from cloudyrathor mosquitto_pub client -u “demo” -P “demo”

I hope you enjoy this blog on MQTT protocol...if you like it share it with other...Have an innovative time for you.