Bidirectional MQTT

How does AssetWolf send data to a device that only wakes up occasionally?

Many sensors are low energy devices; to save power, they sleep most of the time and only wake up occasionally. The sensor may not be awake when you want to send a command or other data to it.

AssetWolf can hold a queue of command messages that are sent to the asset when it connects. The standard AssetWolf portal allows you to send a command, by adding it to the queue of messages to be sent when the device connects.

If the device connects using MQTT, it wakes up, connects to the broker, and it should subscribe to receive at the same time. It publishes it "up" message to the broker, and the AssetWolf portal receives it. Then if there is something in the command queue for the device, Assetwolf sends it immediately afterwards:

MQTT diagram 2

Here's how to set up your asset, so as to listen for a message when it connects in.

1. Connect to the MQTT broker

The asset's communication device will need to make the usual connection to the broker, see here if you're not familiar with this.

2. Subscribe to commands

When your asset wakes up, the first thing it must do is to subscribe.

It must this before publishing. This is to avoid race conditions in which the portal sends back date before the asset is ready.

Subscribe to a topic like this:

$commandTopic = "down/client/clientId/asset/assetId";

The first part, "down" is important, replace clientId and assetId with the relevant strings.

3. Publish the data

Your device should publish its data in the usual way. To recap, this is as follows:

$sensorTopic = "up/client/clientId/asset/assetId";

4. Wait a short time

After publishing, the device should do a listen for a short time, this is typically 1 or 2 seconds, to allow for the server to respond and for network latency.

5. Process the command

If your device has received an MQTT payload, it can disconnect cleanly from the broker and process the payload.