How MQTT is helping drive the Internet of Things

This blog post contributed by Markus Van Kempen, a Canadian SAP Mobile leader.

How does sensor integration with the Message Queue Telemetry Transport (MQTT) protocol and the Internet of Things cloud work, and what makes it so special?

My colleagues keep telling me how cool MQTT is—it’s the protocol of the Internet of Things (IoT). So I went on a quest to understand what’s so great about MQTT and investigate how it works with devices, sensors and the Internet of Things cloud. To do this, I built a scenario to see if I could get some of my devices to communicate with the IBM Internet of Things Foundation. I also visualized the sensor data in a graph using a local application on my notebook.

First, let’s talk about a couple of basics.

What is MQTT?

MQTT is a lightweight messaging protocol that supports publish/subscribe messaging. With MQTT, an application publishes messages to a topic. These messages may then be received by another application that is subscribed to that topic. This allows for a detached messaging network in which the subscribers and publishers do not need to be aware of each other. It’s like the electronic newspaper publishing model: there are publishers of newspapers or topics and many possible subscribers or clients.

Getting started with my MQTT project

I needed the following components for my project:

An MQTT broker or an IoT cloud (like IBM IoT Foundation)

MQTT clients or applications

Devices

There are several brokers (for example, mosquitto.org and other IoT clouds available on the Internet that you could also use. The task for the broker is the registration of publishers or devices and the distribution of messages to applications that subscribe to the specific message topic. I used our IBM IoT cloud, also known as the IBM Internet of Things Foundation, for my prototype as the MQTT broker.

As an MQTT test client, I used Paho, which is a commonly used Eclipse-based MQTT client.

The general information flow

A device that is registered with IBM IoT Foundation may publish event data or consume command data using the MQTT protocol. The Eclipse Paho MQTT client is used to publish and subscribe to the IoT cloud.

Now I needed a device or sensor that I could pull or publish data from. I looked around my desk and found my Pebble smartwatch, the Moto 360, Google Glass, Tile, Electric Imp, Leap Motion and my smartphone, but all these would require more coding to get some results. (By the way, there is also an Android sensor app from IBM you could use to get data like an accelerometer.) I decided to use one of our Nest thermostats as a sensor, hoping for the easiest route, with less coding.

After browsing GitHub, I found a simple Node.js project that allowed me to pull data from Nest (note that there is an official application programming interface, API, from Nest that offers a lot more functionality). I also found an MQTT Node.js implementation that allowed me to publish or subscribe using the MQTT protocol to a broker. After I mashed the two JavaScript files together (the MQTT client and the Nest project), I was able to pull the data from my Nest devices and publish to the IoT cloud. In order to use the IBM IoT cloud I had to sign up and register my notebook (MAC address) as a device. After that, I was able to see the message from my Nest thermostat in the IoT cloud.

Now I had a virtual device using my notebook that would publish Nest temperatures every five seconds or so to the IoT cloud, with the following JSON messages format:

The hardest part so far was registering the device (my notebook) on the IoT cloud and dealing with the authentication tokens, which took a couple of tries. My example code in GitHub should be very self-explanatory.

Since I now had the data in the IoT cloud, I wanted another client to subscribe to the message/topic and visualize the Nest temperature data. I found a visualization tool that was part of the IBM IoT Starter that allows you to run locally and pulls the IoT cloud device’s data to visualize the message from a specific registered device.

If I change the temperature using the Nest app, my MQTT client would be notified of the change and display the change in the target temperature.

There was one more thing I wanted to accomplish: sending a command from one of my MQTT clients to the device, which would essentially be doing the same as the Nest app could do.

I added some JavaScript code to my MQTT proxy run on my notebook, which subscribed to a command topic like the following code snippet:

client.subscribe('iot-2/cmd/settemp/fmt/json');

When I received a message event like client.on('message', …)I would check to see if the JSON message had a target temperature and device ID that I could use to set the new temperature in Nest with the following function:

nest.setTemperature(deviceId, myNewTargetTemp);

Now I had basically the mechanics in place to do what the Nest app already does using MQTT and IBM IoT Foundation.

My conclusions about MQTT

It was fun to tinker with the MQTT protocol and to try to find a new way of pulling information and making devices talk to each other. One of the coolest features was the subscription that would automatically notify me of events. In the past I would have to constantly ask the server for an update. MQTT also shines in its concept simplicity and in various implementations. I think MQTT will be the protocol of the Internet, but it may also be the future of messaging in mobile and web apps and has the potential of being the next Ajax!