Bidirectional MQTT

How to I send data to a remote asset that only wakes up occasionally?

Many "things" only wake up from time to time, so as to save power or bandwidth. This means that you can't always send data to the device as it's not listening most of the time.

Here's the idea, look at the red line, as this is the portal-to-device communication:

MQTT diagram 2

Assetwolf can hold a queue of messages that are sent to the asset when it connects. The standard Assetwolf portal allows you to send a message or command, adding it to the queue.

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.