ThingsBoard MQTT Device API

Introduction

See ThingsBoard API reference.

ThingsBoard API consists of two main parts: Device API and Server-side API.

../_images/f9c9ec01307484175496373df9fba33e1d53f7d8b58ba9ccffefbb9a6708bfb4.svg

Getting started

See MQTT Device API Reference.

MQTT basics

MQTT is a lightweight publish-subscribe messaging protocol which probably makes it the most suitable for various IoT devices. You can find more information about MQTT here.

ThingsBoard server nodes act as an MQTT Broker that supports QoS levels 0 (at most once) and 1 (at least once) and a set of predefined topics.

Client libraries setup

You can find a large number of MQTT client libraries on the web. Examples in this article will be based on Mosquitto and MQTT.js. In order to setup one of those tools, you can use instructions in our Hello World guide.

MQTT Connect

We will use access token device credentials in this article and they will be referred to later as $ACCESS_TOKEN. The application needs to send MQTT CONNECT message with username that contains $ACCESS_TOKEN. Possible return codes and their reasons during connect sequence:

  • 0x00 Connected - Successfully connected to ThingsBoard MQTT server.

  • 0x04 Connection Refused, bad user name or password - Username is empty.

  • 0x05 Connection Refused, not authorized - Username contains invalid $ACCESS_TOKEN.

Key-value format

By default, ThingsBoard supports key-value content in JSON. Key is always a string, while value can be either string, boolean, double, long or JSON. For example:

{
   "stringKey":"value1",
   "booleanKey":true,
   "doubleKey":42.0,
   "longKey":73,
   "jsonKey": {
      "someNumber": 42,
      "someArray": [1,2,3],
      "someNestedObject": {"key": "value"}
   }
}

Telemetry upload API

../_images/35f6ff74327b801d99b7cbb725eb38cf94f101fef13269aa848dc2d4c948685a.svg

In order to publish telemetry data to ThingsBoard server node, send PUBLISH message to the following topic:

v1/devices/me/telemetry

The simplest supported data formats are:

{"key1":"value1", "key2":"value2"}

or

[{"key1":"value1"}, {"key2":"value2"}]

Please note that in this case, the server-side timestamp will be assigned to uploaded data!

In case your device is able to get the client-side timestamp, you can use following format:

{"ts":1451649600512, "values":{"key1":"value1", "key2":"value2"}}

In the example above, we assume that β€œ1451649600512” is a unix timestamp with milliseconds precision. For example, the value β€œ1451649600512” corresponds to β€œFri, 01 Jan 2016 12:00:00.512 GMT”

Example

Client library

Shell file

JSON file

Mosquitto

mosquitto-telemetry.sh

MQTT.js

mqtt-js-telemetry.sh

mosquitto-telemetry.sh

# Publish data as an object without timestamp (server-side timestamp will be used)
mosquitto_pub -d -h "127.0.0.1" -t "v1/devices/me/telemetry" -u "$ACCESS_TOKEN" -f "telemetry-data-as-object.json"
# Publish data as an array of objects without timestamp (server-side timestamp will be used)
mosquitto_pub -d -h "127.0.0.1" -t "v1/devices/me/telemetry" -u "$ACCESS_TOKEN" -f "telemetry-data-as-array.json"
# Publish data as an object with timestamp (server-side timestamp will be used)
mosquitto_pub -d -h "127.0.0.1" -t "v1/devices/me/telemetry" -u "$ACCESS_TOKEN" -f "telemetry-data-with-ts.json"

mqtt-js-telemetry.sh

# Publish data as an object without timestamp (server-side timestamp will be used)
cat telemetry-data-as-object.json | mqtt pub -v -h "127.0.0.1" -t "v1/devices/me/telemetry" -u '$ACCESS_TOKEN' -s
# Publish data as an array of objects without timestamp (server-side timestamp will be used)
cat telemetry-data-as-array.json | mqtt pub -v -h "127.0.0.1" -t "v1/devices/me/telemetry" -u '$ACCESS_TOKEN' -s
# Publish data as an object with timestamp (server-side timestamp will be used)
cat telemetry-data-with-ts.json | mqtt pub -v -h "127.0.0.1" -t "v1/devices/me/telemetry" -u '$ACCESS_TOKEN' -s

telemetry-data-as-object.json

{
   "stringKey": "value1",
   "booleanKey": true,
   "doubleKey": 42.0,
   "longKey": 73,
   "jsonKey": {
      "someNumber": 42,
      "someArray": [1,2,3],
      "someNestedObject": {"key": "value"}
   }
}

telemetry-data-as-array.json

[{"key1":"value1"}, {"key2":true}]

telemetry-data-with-ts.json

{
   "ts": 1451649600512,
   "values": {
      "stringKey": "value1",
      "booleanKey": true,
      "doubleKey": 42.0,
      "longKey": 73,
      "jsonKey": {
         "someNumber": 42,
         "someArray": [1, 2, 3],
         "someNestedObject": {
         "key": "value"
         }
      }
   }
}

Attributes API

ThingsBoard attributes API allows devices to

  • Request client-side and shared device attributes from the server.

  • Upload client-side device attributes to the server.

  • Subscribe to shared device attributes from the server.

Request attribute values from the server

../_images/1f362cd658b3bd8b94fa8f570f1e6f8796648dbe38aaee3f10aca892a5e2cf2a.svg

Before sending PUBLISH message with the attributes request, client need to subscribe to:

v1/devices/me/attributes/response/+

Once subscribed, the client may request client-side or shared device attributes to ThingsBoard server node, send PUBLISH message to the following topic:

v1/devices/me/attributes/request/$request_id

where $request_id is your integer request identifier.

The client should receive the response to the following topic:

v1/devices/me/attributes/response/$request_id

Example

The following example is written in javascript and is based on mqtt.js. Pure command-line examples are not available because subscribe and publish need to happen in the same mqtt session.

Client library

Shell file

JavaScript file

Result (JSON file)

MQTT.js

mqtt-js-attributes-request.sh

mqtt-js-attributes-request.js

attributes-response.json

mqtt-js-attributes-request.sh
export TOKEN=$ACCESS_TOKEN
node mqtt-js-attributes-request.js
mqtt-js-attributes-request.js
var mqtt = require('mqtt')
var client  = mqtt.connect('mqtt://127.0.0.1',{
   username: process.env.TOKEN
})

client.on('connect', function () {
   console.log('connected')
   client.subscribe('v1/devices/me/attributes/response/+')
   client.publish('v1/devices/me/attributes/request/1', '{"clientKeys":"attribute1,attribute2", "sharedKeys":"shared1,shared2"}')
})

client.on('message', function (topic, message) {
   console.log('response.topic: ' + topic)
   console.log('response.body: ' + message.toString())
   client.end()
})
attributes-response.json
{"key1":"value1"}

Please note, the intersection of client-side and shared device attribute keys is a bad practice! However, it is still possible to have same keys for client, shared or even server-side attributes.

Publish attribute update to the server

../_images/6f68ecb5dfceec90754ce65ec6e685129b1da784bdd6d4b37942b9e0d4b6667d.svg

In order to publish client-side device attributes to ThingsBoard server node, send PUBLISH message to the following topic:

v1/devices/me/attributes

Example

Client library

Shell file

JSON file

Mosquitto

mosquitto-attributes-publish.sh

new-attributes-values.json

MQTT.js

mqtt-js-attributes-publish.sh

mosquitto-attributes-publish.sh
# Publish client-side attributes update
mosquitto_pub -d -h "127.0.0.1" -t "v1/devices/me/attributes" -u "$ACCESS_TOKEN" -f "new-attributes-values.json"
mqtt-js-attributes-publish.sh
# Publish client-side attributes update
cat new-attributes-values.json | mqtt pub -d -h "127.0.0.1" -t "v1/devices/me/attributes" -u '$ACCESS_TOKEN' -s
new-attributes-values.json
{
   "stringKey": "value1",
   "booleanKey": true,
   "doubleKey": 42.0,
   "longKey": 73,
   "jsonKey": {
      "someNumber": 42,
      "someArray": [1,2,3],
      "someNestedObject": {"key": "value"}
   }
}

Subscribe to attribute updates from the server

../_images/8965056795e4787b33b45e7bd7841222f9efce0bb1f829f3f4f599614c3ef9e6.svg

In order to subscribe to shared device attribute changes, send SUBSCRIBE message to the following topic:

v1/devices/me/attributes

When a shared attribute is changed by one of the server-side components (such as the REST API or the Rule Chain), the client will receive the following update:

{"key1":"value1"}

Example

Client library

Shell file

Mosquitto

mosquitto-attributes-subscribe.sh

MQTT.js

mqtt-js-attributes-subscribe.sh

mosquitto-attributes-subscribe.sh
# Subscribes to attribute updates
mosquitto_sub -d -h "127.0.0.1" -t "v1/devices/me/attributes" -u "$ACCESS_TOKEN"
mqtt-js-attributes-subscribe.sh
# Subscribes to attribute updates
mqtt sub -v "127.0.0.1" -t "v1/devices/me/attributes" -u '$ACCESS_TOKEN'

PRC API

Server-side RPC

../_images/b6a3ba27f8bc1a1a6c4ece7317f6407aede9f1bce6a04e278c1473026b26223a.svg

In order to subscribe to RPC commands from the server, send SUBSCRIBE message to the following topic:

v1/devices/me/rpc/request/+

Once subscribed, the client will receive individual commands as a PUBLISH message to the corresponding topic:

v1/devices/me/rpc/request/$request_id

where $request_id is an integer request identifier.

The client should publish the response to the following topic:

v1/devices/me/rpc/response/$request_id

Example

The following example is written in javascript and is based on mqtt.js. Pure command-line examples are not available because subscribe and publish need to happen in the same mqtt session.

Client library

Shell file

JavaScript file

MQTT.js

mqtt-js-rpc-from-server.sh

mqtt-js-rpc-from-server.js

mqtt-js-rpc-from-server.sh
export TOKEN=$ACCESS_TOKEN
node mqtt-js-rpc-from-server.js
mqtt-js-rpc-from-server.js
var mqtt = require('mqtt');
var client  = mqtt.connect('mqtt://127.0.0.1',{
   username: process.env.TOKEN
});

client.on('connect', function () {
   console.log('connected');
   client.subscribe('v1/devices/me/rpc/request/+')
});

client.on('message', function (topic, message) {
   console.log('request.topic: ' + topic);
   console.log('request.body: ' + message.toString());
   var requestId = topic.slice('v1/devices/me/rpc/request/'.length);
   //client acts as an echo service
   client.publish('v1/devices/me/rpc/response/' + requestId, message);
});

Client-side RPC

../_images/e979f283a029406684ebebd0d8ecf6284d448cab043562a554b448ff3600b0f3.svg

In order to subscribe to client-side RPC response from the server, send SUBSCRIBE message to the following topic:

v1/devices/me/rpc/response/+

Once subscribed, the client may send PUBLISH message to the following topic:

v1/devices/me/rpc/request/$request_id

where $request_id is an integer request identifier. The response from server will be published to the following topic:

v1/devices/me/rpc/response/$request_id

Example

The following example is written in javascript and is based on mqtt.js. Pure command-line examples are not available because subscribe and publish need to happen in the same mqtt session.

Client library

Shell file

JavaScript file

MQTT.js

mqtt-js-rpc-from-client.sh

mqtt-js-rpc-from-client.js

mqtt-js-rpc-from-client.sh
export TOKEN=$ACCESS_TOKEN
node mqtt-js-rpc-from-client.js
mqtt-js-rpc-from-client.js
var mqtt = require('mqtt');
var client = mqtt.connect('mqtt://127.0.0.1', {
   username: process.env.TOKEN
});

client.on('connect', function () {
   console.log('connected');
   client.subscribe('v1/devices/me/rpc/response/+');
   var requestId = 1;
   var request = {
      "method": "getTime",
      "params": {}
   };
   client.publish('v1/devices/me/rpc/request/' + requestId, JSON.stringify(request));
});

client.on('message', function (topic, message) {
   console.log('response.topic: ' + topic);
   console.log('response.body: ' + message.toString());
});

Claiming API

Please see the corresponding article to get more information about the Claiming devices feature.

../_images/bf2b139903e81df28764e7f9d266fc91037740b070661d5f8ee36e6e1e40dad9.svg

In order to initiate claiming device, send PUBLISH message to the following topic:

v1/devices/me/claim

The supported data format is:

{"secretKey":"value", "durationMs":60000}

Please note that the above fields are optional. In case the secretKey is not specified, the empty string as a default value is used. In case the durationMs is not specified, the system parameter device.claim.duration is used (in the file /etc/thingsboard/conf/thingsboard.yml ).

Firmware API

../_images/f3c3703fb78316c2080b3444f0acf84ab665b4e91fbc9c8219eff2fe8060cb09.svg../_images/3bc0675ba93ddff052c239f24446ec9d65332cbcb1240535219c3170359cba36.svg../_images/1d66d0bcbb6f84886b055f4bee950cd176927f910ab635502dc07f7b28fb81ad.svg

Replace 8192 with your chunk size.

When ThingsBoard initiates an MQTT device firmware update, it sets the fw_title, fw_version, fw_checksum, fw_checksum_algorithm shared attributes. To receive the shared attribute updates, the device has to subscribe to:

v1/devices/me/attributes/response/+

Where

+ is the Wildcard character.

When the MQTT device receives updates for fw_title and fw_version shared attributes, it has to send PUBLISH message to:

v2/fw/request/${requestId}/chunk/${chunkIndex}

Where

${requestId} - number corresponding to the number of firmware updates. The ${requestId} has to be different for each firmware update.

${chunkIndex} - number corresponding to the index of firmware chunks. The ${chunkID} are counted from 0. The device must increment the chunk index for each request until the received chunk size is zero. And the MQTT payload should be the size of the firmware chunk in bytes.

For each new firmware update, you need to change the request ID and subscribe to:

v2/fw/response/+/chunk/+

Where

+ is the Wildcard character.

Device MQTT Topic

Device MQTT Topic

Function Topic

Subscribe

Tx

Rx

Telemetry

β‘  v1/devices/me/telemetry

Request attributes

β‘  v1/devices/me/attributes/response/+

β‘‘ v1/devices/me/attributes/request/${requestId}

β‘’ v1/devices/me/attributes/response/${requestId}

Publish attributes

β‘  v1/devices/me/attributes

Subscribe attributes update

β‘  v1/devices/me/attributes

β‘‘ v1/devices/me/attributes

Server-Side RPC

β‘  v1/devices/me/rpc/request/+

β‘’ v1/devices/me/rpc/response/${requestId}

β‘‘ v1/devices/me/rpc/request/${requestId}

Client-Side RPC

β‘  v1/devices/me/rpc/response/+

β‘‘ v1/devices/me/rpc/request/${requestId}

β‘’ v1/devices/me/rpc/response/${requestId}

Claiming device

β‘  v1/devices/me/claim

Firmware updates*

β‘  v2/fw/response/+/chunk/+

β‘‘ v2/fw/request/${requestId}/chunk/${chunkIndex}

β‘’ v2/fw/response/${requestId}/chunk/${chunkIndex}

Note

  • β‘ β‘‘β‘’ The order in which topics are performed.

  • Firmware updates needs the support of Telemetry, Request attributes and Subscribe attributes update.