What is MQTT ? How Machine Talks With Each Other Using MQTT?

Going further to explain What is MQTT ? How Machine Talks With Each Other Using MQTT?
MQTT stands for MQ Telemetry Transport. It is a publish/subscribe based very simple and lightweight messaging protocol for Internet of Things. It has been designed for constrained devices and low-bandwidth, high-latency unreliable networks. Because of it’s lightweight nature it is being used widely for Machine to Machine (M2M) and Internet of Things (IoT) communication where a very small code footprint is required.

MQTT was invented by Dr Andy Stanford-Clark of IBM, and Arlen Nipper of Arcom in 1999, to create a protocol for minimal battery loss and minimal bandwidth connecting oil pipelines over satellite connection. Where they have specified the some following goals which the future protocol should have :-

  • It should be simple to implement for anyone (It is very simple to implement).
  • It should be lightweight and bandwidth efficient (Extremely lightweight overhead 2-byte header)
  • It should be continuous session awareness ( We have complete control over session awareness)

What are the Advantages of MQTT?

  • Distribute information more efficiently and easy way
  • It increases scalability
  • It reduce network bandwidth because of being lightweight
  • It reduce update rates to seconds
  • Externally secure with permission-based security
  • It saves development time because it require very less code footprint

MQTT Publish/Subscribe Architecture and Control Packets:-

The MQTT messages are being delivered in asynchronous manner through publish subscribe architecture. The MQTT protocol works mostly on TCP/IP protocol (It also works on Web Socket) by exchanging a series of MQTT control packets. Each control packet has a specific purpose and every bit in the packet is carefully crafted to reduce the data transmitted over the network.
MQTT Publish/Subscribe Architecture and Control

Following table describes MQTT control packets and their usage :-

Control Packet Flow of Direction Usage
CONNECT Client To Server Used to connect to server
CONNACT Server To Client To get the connection acknowledgment whether connected or not
PUBLISH Client To Server

Or

Server to Client

To publish any message for a particular topic and it can be both way.
PUBACK Client To Server

Or

Server to Client

To get the published acknowledgment whether message has been published or not
PUBREC Client To Server

Or

Server to Client

It state publish received
PUBREL Client To Server

Or

Server to Client

It state publish release
PUBCOMP Client To Server

Or

Server to Client

It state publish complete
SUBSCRIBE Client To Server Client subscribe for a particular topic
SUBACK Server To Client To get the subscribe acknowledgement
UNSUBSCRIBE Client To Server To unsubscribe a particular request
UNSUBACK Server To Client To get the unsubscribe acknowledgment
PINGREQ Client To Server To send the ping request to server
PINGRESP Server To Client To get the ping response
DISCONNECT Client To Server To disconnect from the server

 

How Does MQTT Works?

MQTT protocol works on Client and Broker, Broker can be called server.

  • MQTT Client :- MQTT client can be any device from a micro-controller to fully fledged server,  which runs the MQTT library and is connected to MQTT broker over any network. MQTT client libraries are available across a wide variety of programming language platforms like C, C++, C#, Java, JavaScript, Android, iOS etc. .
  • MQTT Broker/Server :- MQTT broker is responsible for receiving all messages, filtering, decision making and sending messages to subscribed clients based on a particular topic. MQTT is based on TCP/IP, hence both client and broker is expected to have TCP/IP stack.

How Does MQTT Works

MQTT allows edge-of-network devices to publish to a broker.  Clients connect to this broker, which then mediates communication between the two devices. Each device can subscribe, or register, to particular topics. When another client publishes a message on a subscribed topic, the broker forwards the message to any client that has subscribed.

MQTT is bidirectional, and maintains state full session awareness. If an edge-of-network device loses connectivity, all subscribed clients will be notified with the “Last Will and Testament” feature of the MQTT server so that any authorized client in the system can publish a new value back to the edge-of-network device, maintaining bidirectional connectivity.

Who Uses MQTT?

MQTT was originally developed for the low-bandwidth, high-latency data links used in the oil and gas industry. Now MQTT being used in many applications beyond oil and gas – from controlling smart home systems to the Facebook Messenger . Now a days MQTT became first choice to communicate with M2M/Internet of Things. AWS recently announced that Amazon Internet of Things (IoT) is based on MQTT, as well. Overall, MQTT appears to be the protocol best suited for the control systems used by industrial organizations. Microsoft Windows Azure IoT Hub uses MQTT as its main protocol for telemetry messages.

URI pattern to connect with MQTT server :-

The format of MQTT server URI is :-

>mqtt[s]://[username][:password]@host.domain[:port]

A typical use for this is to provide the details of a MQTT server in an environment variable.

  • Scheme :– First section of the URI pattern is  scheme, describes the protocol to use to communicate with the server. It can either be “mqtt” for un-encrypted communication or “mqtts” to bind up to MQTT protocol in a secure TLS encrypted connection. Some implementations also supports “tcp” and “ssl” scheme name instead of “mqtt” and “mqtts” .
  • Username :- Second section of the URI pattern is the username, to be put into the CONNECT packet, send directly after opening the socket. If no username is given or it is an empty string, then the user name flag should not be set in the CONNECT packet.
  • Password :- Third section of the URI pattern is the password, that is optional it may be given in the URI. This is also passed into the CONNECT packet. A colon separates the username and password. If the username is an empty string, then the password should still be sent.
  • Host and Domain :- Forth section of the URI pattern  only required component of a MQTT URI. This is the DNS name or IP address of the MQTT server to connect with.
  • Port :- Fifth section of URL pattern TCP port of the MQTT server to connect to. If no port is given, then the default port number 1883 will be used for “mqtt” and 8883 port will be used for “mqtts” secured scheme.

 

What is MQTT ? How Machine Talks With Each Other Using MQTT?

What is AMQP ? How AMQP works for Internet of Things

 

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *