How to send data using MQTT

Recall our structure of sending data from an asset, via the MQTT broker, to Assetwolf:

MQTT diagram 1

Publish to send data

Assuming your device is connected using MQTT, it should publish its data to:

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

The topic is structured in this way:

  • up means device to cloud
  • clientID is your client identifier)
  • deviceId the communication device identifier.

The JSON payload should look like this (but you can remove newlines and spaces):

{
    "timestamp": timestamp,
    "field1": "boolean",
    "field2": integer,
    "field3: float,
    "latitude": latitude,
    "longitude": longitude,
    "field4": "string",
    "field5": "state_indicator"
}

Notes on field types

You do not have to send all fields that are defined on the schema for this asset. It's quite normal to send partial data, or that the full set of field data is only sent occasionally.

timestamp

Optional! You only need to send this if you want to be specific about the time that the current data refers to, or if you wish to send data for sensor readings taken in the past. Must be a Unix microtime (milliseconds since 1970).

boolean

Must be either "true" or "false" (with double quotes).

integer, float

You can send integer and floating-point numbers, no quotes are necessary.

string

Be sure to use double-quotes (not single quotes) around all alphanumeric fields. Numeric fields can have quotes but they are optional.

state_indicator

If the asset has a state indicator, such as a run mode, you can send a state indicator, such as "1", or "on" for example. This can be used to display an icon within the portal (on a schematic floorplan, a list of assets, or on an asset's status panel).

Checking your data is arriving

On the Dashboard on your portal, and look at your assets. If all is well, you should see some data has arrived.

Google ChromeScreenSnapz780.jpg 

Publish to send data

Assuming your device is connected using MQTT, it should publish its data to:

$sensorTopic = "up/client/clientId/asset/deviceId/fieldname";

The topic is structured in this way:

  • up means device-to-cloud
  • clientID is your client identifier (replace with your client ID)
  • deviceId the communication device identifier (replace with your device's unique ID)
  • fieldname is the code name of the field in the asset's schema (replace with your field name).

The payload should be just the value of the field at that time. For example for a floating-point field:

2.2

or for a Boolean field:

true

Note that there is no way to specify a timestamp using this method; the server will use the time at which the data is received.

Minimising data transfer by using aliases

When editing the schema for the asset, you will notice a small "+" sign in the list of "Incoming data from asset". You can use this to specify an alias, i.e. a short form of a field name.

Here for example a floating-point field is called "lightlevel", but to the Superuser is adding a alias "L":

Google ChromeScreenSnapz2227.jpg

In the data transmission, the value of this field can now be sent with a reduced data size. Without the alias the format would be like this:

{
    "lightlevel": 499
}

With the alias defined, data can now be sent like this:

{
    "L": 499
}