Using MQTT to connect a "thing" to the cloud

In many internet of things scenarios, an IoT device in the field spends the majority of its time "asleep". The device needs to minimize internet bandwidth and power — data transfer may be expensive, and it may be battery powered — and anyway the application may not need to send data continuously.

MQTT is an ideal messaging protocol for these situations. It is lightweight and so messaging transport is ideal for mobile devices.

So how should you use MQTT to send data to an IoT cloud platform? And how can you send data back to a device that spends most of its time asleep?

Asset, awake!

The first action is when an asset "wakes up". Depending on the application, this could be every few minutes, or hours or days.

The asset will be configured to connect to a given MQTT broker, in this example it would be the Assetwolf MQTT broker.

MQTT diagram 1

 

The asset will need to send a set of authentication parameters to the broker, such as a username and password, and this may be encrypted using SSL.

MQTT Topics

MQTT works by using topics. Assetwolf typically uses a topic with a name like:

up/client/[clientId]/asset/[assetId]

clientID and assetID would be replaced by client-specific and asset-specific identifiers.

The broker will be configured to listen on that topic, and so the asset just needs to publish its data on that topic.

Publish data

When the asset publishes its data on that topic, the broker receives it and distributes it to other clients that are subscribed. There are security mechanisms to restrict what each client can subscribe to, but importantly the Assetwolf portal itself is itself a client of the broker and subscribes to the distributed data.

When the portal sees data coming from the broker, it verifies it, then records it into the Assetwolf database.

Sending commands back

Assetwolf has the capability to send data to assets, which may be commands for the asset to do something. It queues the outgoing data internally, and uses MQTT in just the same way, with the publish-subscribe mechanism working the other way round.

What's required of the asset is that it subscribes to a topic of a similar name, but slightly differently, i.e.:

down/client/[clientId]/asset/[assetId]

The asset needs to subscribe to the "down" topic for a brief time after it has published the "up" data. During this time, Assetwolf will send a command to the asset if it has one.

MQTT diagram 2

Back to sleep

As soon as the asset has received the command data, if present, it has finished for this cycle. It can go back to its sleep, or standby, mode again.