Sunday, April 12, 2020

MQTT : THE MESSAGING PROTOCOL FOR IOT

What is MQTT and why do we need it in IIoT? Description of MQTT ...


Internet of Things (IoT) describes the practice of connecting devices through the use of the Internet. For this rapid increase in the number of connected devices IoT need a protocol by which the connected devices may communicate when required.

WHAT IS A PROTOCOL?

A Protocol is a set of rules which both the sender and receiver and all intermediate devices need to follow to be able to communicate effectively.
The main features of these protocols are:
· Low Power Consumption
· Small Code Footprint
· Low Latency
· Use of Pub/Sub Pattern
· Low Bandwidth Consumption
MQTT protocol complements the necessities of IoT.

WHAT IS MQTT?

Message Queue Telemetry Transport(MQTT) is alight-weight messaging protocol based on the publish/subscribe model, launched by IBM. MQTT uses a client-server architecture where the client(such as an IoT device) connects to the server(also called MQTT Broker) and publishes messages to topics on the server. The broker then routes the messages to the clients subscribed to topics. This protocol basically operates over TCP/IP; however, any network protocol that provides lossless, ordered, bi-directional connections can support MQTT.

Now, lets look at the architecture of IoT,

MQTT is based on clients and a server. Clients are the connected devices in MQTT, which communicates with the server(Broker).
When a client(Publisher) wants to send information to the broker, it will be published to a particular topic. The client(Subscriber) which is subscribed to that topic receives all the messages on that topic.
The brokers pass the messages from the publishers to the subscribers. Publisher and Subscriber only communicate with MQTT broker which manages the receiving of messages from publishers and the sending of messages to the subscribers. Clients can be publisher, subscriber, or both.
 
Other than routing the messages, the Broker has some additional responsibilities which are discussed briefly below:
· Quality of Service: 
It is an agreement between the sender of a message and the receiver of a message that defines the guarantee of delivery for a specific message. There are 3 QoS levels in MQTT:

QoS 0 - at most once

There is no guarantee that the message will be delivered successfully. It is like “fire and forget” type.

QoS 1 - at least once

It guarantees that the message will be delivered at least once. But the message can be sent more than once.


QoS 2 - exactly once

This guarantees that the message is sent only once.
 

· Security: MQTT broker requires username and password authentication from clients to connect for security.

MQTT IN IOT 

MQTT is the most widely used protocol in IOT. Let’s look at one example to understand its working in IOT. 
Let’s take the example of Plant Monitoring System. In this system we have a moisture sensor and a humidity sensor. And we want to the data collected by the sensors to be send to your mobile phone. This is possible with the help of MQTT. First, you need to set up an MQTT broker service. Then, you can connect the sensors on the broker as clients and set them up to send data on topics – “Humid” and “Moist”. After that, you need to integrate the mobile phone with the broker and subscribe the mobile to “Humid” and “Moist”. Consequently, the mobile phone will receive messages about the humidity and the moisture content whenever the sensors publish the required information to the broker.

MOSQUITTO MQTT BROKER

Mosquitto is a lightweight open source message broker that implements MQTT versions 3.1.0, 3.1.1 and version 5.0.

Now let see Windows Command prompt as Publisher and Subscriber;
1) Install MQTT broker(mosquitto) on Windows machine
2) Open Command Prompt as Administrator.
3) Navigate to Mosquitto installed folder. For navigation use following command 
cd C:\Program Files (x86)\mosquitt
4) Execute the following Command to Subscribe topic test.
mosquitto_sub -t test -h localhost
At the moment, you won’t receive the data asthere is no publisher,to receive data follow the following publisher steps in another command prompt and then check the subscriber terminal to see received data.
Open another Command Prompt as Administrator.
5) Navigate to mosquitto installed folder or path.
6) Execute the following Commands to Publish message HELLO!! WORLD & MY NAME IS XYZ”
mosquitto_pub -t test -h localhost -m “HELLO!! WORLD
mosquitto_pub -t test -h localhost -m “MY NAME IS XYZ
 
 

But why do we need MQTT for communication when HTTP & HTTPS protocols are so popular and widely used for communication?

Even though these protocols are widely used, the amount of overheads data that is send over the Internet for managing the communication is quite a lot. Overhead data is the data which is sent along with the actual message/data which conveys the extra information required to understand the message/data sent. The overhead data varies from protocol to protocol.This is fine in case of systems such as mobile phones, laptops, desktop computers that have the hardware capabilities and the network capabilities to send the extra overhead data.
Most IoT devices and sensors contain limited processing capabilities and constrained Internet bandwidth. Due to these limitations, they send data over the Internet only when required and the data sent has very low bandwidth usage. Hence protocols such as HTTP,HTTPS are not feasible where the overhead data is more than the actual data itself. MQTT contains very low overhead and hence becomes ideal got IoT communication

CONCLUSION :

MQTT plays a major role in Internet of Things. It provides lots of functions for the IOT. It is a very light weight protocol that can work with every types of devices and work using a minimum bandwidth.It helps in providing a great performance and create new area for messaging and  handling billion of things connected through the internet.

















MQTT : THE MESSAGING PROTOCOL FOR IOT

Internet of Things (IoT) describes the practice of connecting devices through the use of the Internet. For this rapid incre...