Archive for September 3rd, 2015
MQTT Protocol for Internet of Things (IoT)
I was thinking about how to control my Aeroponic system remotely via internet. The Raspberry Pi controlling the system is connected to internet via a router. I could access RaspberryPi by Port forwarding and stuff like that but it’s complicated. Next option could be using Websockets but I felt it’s an overkill for the applications running in Pi.
Recently I received a Refcard from Dzone regarding a protocol called MQTT. I was not aware of this Protocol before. So thought of doing some experiment with it. I am not going much deeper into the protocol, Dzone refcard did a great job of explaining it well.
In a nutshell, MQTT consist of three parts.
-
Broker
-
Subscribers
-
Publishers
Publisher, publish message to a specific topic and any Subscriber subscribes for that topic receives the message. Broker is the central hub, both Publishers and Subscribers are connected to the Broker and it take care of delivering the message to all the subscribers subscribed for the topic.
Brokers
We can implement our own broker using RabitMQ or Mosca plugin for Node js or any other MQTT brokers available in the market. To experiment it, I used CloudMQTT addon in Heroku. I used Heroku just to manage every thing from one central place.
Dev Environment
I created two set off Node js application, one running in my computer as a publisher and another running in my RaspberryPi as a subscriber. Both have no direct connection instead they are connected to CoudMQTT broker. Below is a test code and nothing related to my Aeroponic system.
Publisher Code
var mqtt = require('mqtt'); var client = mqtt.createClient('<<PortNumber>>', 'm11.cloudmqtt.com', { username: '<<UserName>>', password: '<<Password>>' }); client.on('connect', function () { // When connected // subscribe to a topic client.subscribe('TEMPERATURE_READING', function () { // when a message arrives, do something with it client.on('message', function (topic, message, packet) { console.log("Received '" + message + "' on '" + topic + "'"); }); }); // publish a message to a topic client.publish('SET_TEMPERATURE', '24', function () { console.log("Message is published"); }); });
The above code act as a Publisher as well as a Subscriber. For e.g. the above code can be a piece running in Internet and the Pi’s can Publish the Temperature readings in a periodic interval and logged in a central db. We can see the readings via a webapp or which ever the way we need. Also if required we can set a temperature to all the connected RPi’s by publishing a message to topic ‘SET_TEMPERATURE’.
Subscriber Code
var mqtt = require('mqtt'), url = require('url'); var client = mqtt.createClient('<<Portnumber>>', 'm11.cloudmqtt.com', { username: '<<UserName>>', password: '<<Password>>' }); client.on('connect', function () { // When connected // subscribe to a topic client.subscribe('SET_TEMPERATURE', function () { // when a message arrives, do something with it client.on('message', function (topic, message, packet) { console.log("Received '" + message + "' on '" + topic + "'");
// set the temperature. }); }); });
The code is very minimal and we could achieve an easy communication to all the connected devices. In the above scenario clients are always connected. If you want to end the connection then call ‘client.end()’.
Later I implemented a Broker using Mosca, both scenarios the system worked really well.