Close

Tech Blog: MQTT-SN

A project log for Iota: Internet of things for all

A low cost Internet of things platform to build your own smart house

nathanandrewwilliamsnathan.andrew.williams 05/27/2014 at 21:220 Comments

Iota makes use of a number of different technologies, and I wanted to explain some of them along the way.

This week will be about MQTT-SN, what it is, how it works and why it was chosen for Iota.

MQTT-SN & MQTT

To start with, MQTT-SN is simply a version of MQTT with additional considerations for sensor networks.

It aims to reduce the amount of data transferred and introduces the ability for nodes to sleep without being disconnected, an important feature for battery powered nodes.

MQTT was also designed to work over a TCP/IP connection, while MQTT-SN is targeted at wireless radio links.

A gateway allows nodes using the different protocols to communicate, acting as a bridge between the TCP/IP network and the radio links.

Apart from these differences, the protocols are largely the same, so the rest of this post will focus on MQTT.

MQTT

MQTT stands for MQ Telemetry Transport and was invented by IBM which has since made the protocol open source.

MQTT is a publish / subscribe protocol designed for Machine to Machine (m2m) communications.

What that means is that it has been designed for machines (e.g. sensors) to create information that is targeted at other machines to read and act on (computers, displays, actuators etc).

Publish / Subscribe

The publish / subscribe model lets you build a network of nodes that don’t need to know each other to function.

Instead, nodes only know of topics to publish or subscribe to.

For example, you might have a topic structure like this:

inside/bedroom/temperature

inside/kitchen/temperature

inside/bathroom/temperature

The various temperature sensors in your house would publish to their own topic, while a display showing temperatures in the house might subscribe to something like:

inside/+/temperature

The “+” acts as a wildcard, allowing a system to subscribe to a group of similar topics.

Topics and wildcards

As mentioned above, messages are addressed through the use of topics.

Topics are separated by a “/“ allowing topics to be grouped in a tree structure.

How you design your topics is important to allow efficient use of wildcard matching.

There are two wildcard characters “#” and “+”

“#” is the multi-level wildcard and matches zero or more levels. It can only be used at the end of a topic tree like so:

inside/bedroom/#

This would match topics such as:

inside/bedroom

inside/bedroom/humidity

inside/bedroom/temperature

“+” is a single level wildcard and can be used anywhere in the topic tree.

An example:

inside/+/temperature

Would match:

inside/bedroom/temperature

inside/kitchen/temperature

inside/bathroom/temperature

Wildcards can also be combined to create more complex combinations:

inside/+/#

Would match:

inside/bedroom/humidity

inside/bedroom/temperature

inside/kitchen/humidity

inside/kitchen/temperature

inside/bathroom/humidity

inside/bathroom/temperature

Brokers

To let nodes publish and subscribe to topics, without having to know about each other needs a broker.

Brokers act as a central point in a group of nodes and keep track of subscriptions and incoming messages.

Brokers do not need to know about possible topics ahead of time.

Nodes can subscribe and publish to new topics and the broker will start tracking them.

Brokers also provide a “Last Will and Testament” feature, where a node can specify a message and topic to publish it to if the node is disconnected.

You might use this to raise an alert that a node needs fresh batteries for example.

Conclusion

This post was just a quick skim over MQTT, but as you can see, it is a simple concept that allows a lot of flexibility in how you design your system.

If you want more details on MQTT or MQTT-SN, check out the protocol specifications, they are quite easy to follow.

MQTT Spec: http://public.dhe.ibm.com/software/dw/webservices/ws-mqtt/mqtt-v3r1.html

MQTT-SN Spec: MQTT-SN v1.2 specification

Discussions