MQTT (Message Queuing Telemetry Transport) Protocol is a M2M (Machine to Machine) lightweight messaging protocol widely used in IOT (Internet of Things) that provides a resource constrained network clients. Almost IOT cloud platform support MQTT protocol to send and receive data from smart objects in other words it is a simple way to distribute telemetry information which uses a publish & subscribe communication pattern together with your favorite microcontrollers & Open GPIO microcomputers.
MQTT was created and develop way back 1999 the main goal of this projects is to create a protocol that capable of transmitting an efficient, power saving, very simple to implement, low overhead, TCP stack as transmission substrate and the most important on MQTT is open source. All of this aspect contribute to its large adoption in IOT.
MQTT Message Pattern
MQTT protocol implements publish and subscribe paradigm the paradigm decouple as client that publishes a message to other clients that receive the message subscribers. MQTT is asynchronous protocol that means that it does not block the client while it waits for the messages in contrast to HTTP protocol, which is mainly a synchronous protocol another interesting property of MQTT protocol is that it does not require that the client subscribe and publisher are connected at the same time.
MQTT Publisher-subscriber pattern
As described above MQTT is a message based protocol that uses publisher-subscriber pattern. The key component is MQTT is the MQTT service broker. The main task of broker is to dispatching message the client.
The subscriber will receive a message from the publisher and dispatches these message to the subscribers. While it dispatches message the MQTT service provider uses a topic to filter the clients/subscriber that will receive the message.
The topic is a string and it is possible to combine the topics creating topic levels. A topic is like a virtual channel that connects a publisher to its subscribers this topic is managed by the MQTT service provider/broker. Through this virtual channel, the publisher is decouple from the subscribers and the client / publishers or subscribers does not have to know each other this make this protocol highly scalable without a direct dependency from message publisher.
The MQTT Architecture
Messaging and Topics
Installing MQTT with Raspberry Pi
There are other IOT protocol that you can use in your IOT projects like CoAP, HTTP, AMQP, Rest, XMPP, Stomp and others.
On this demonstration we will going to used Mosquitto an implementation developed by Eclipe for MQTT broker. Just follow the repository that holds the application.
[email protected]:~$ wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
Importing the key
[email protected]:~$ sudo apt-key add mosquitto-repo.gpg.key
Add the fil .list
[email protected]:~$ sudo wget http://repo.mosquitto.org/debian/mosquitto-wheezy.list
Installing Mosquitto MQTT Broker
[email protected]:~$ apt-get install mosquitto
MQTT server is installed on RPI. This server is now a MQTT broker specified above. The next step is we a client / publisher to subscriber.
Installing Mosquitto MQTT Client/Publisher or Subscriber
[email protected]:~$ apt-get install mosquitto-clients
Sending MQTT Message
MQTT broker & client has been installed, we can now register a subscriber to a specific topic and wait for the incoming message from the publisher.
[email protected]:~$ mosquitto_sub -d -t swa_news
As you can see our subscriber is waiting for the message. This example we used a top called “SWA_NEWS” we can now send a message using an MQTT publisher that uses the same topic swa_news.
The MQTT publisher sends a “HELLO PROTOCOL” message on the subscriber side as you can see in the CLI window.