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.
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.
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:
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. Using custom binary format or some serialization framework is also possible. See protocol customization for more details. For example:
{
"stringKey":"value1",
"booleanKey":true,
"doubleKey":42.0,
"longKey":73,
"jsonKey": {
"someNumber": 42,
"someArray": [1,2,3],
"someNestedObject": {"key": "value"}
}
}
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’
resources/mosquitto-telemetry.sh |
---|
|
resources/mqtt-js-telemetry.sh |
---|
|
resources/telemetry-data-as-object.json |
---|
|
resources/telemetry-data-as-array.json |
---|
|
resources/telemetry-data-with-ts.json |
---|
|
ThingsBoard attributes API allows devices to
In order to publish client-side device attributes to ThingsBoard server node, send PUBLISH message to the following topic:
v1/devices/me/attributes
resources/mosquitto-attributes-publish.sh |
---|
|
resources/mqtt-js-attributes-publish.sh |
---|
|
resources/new-attributes-values.json |
---|
|
In order to 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. Before sending PUBLISH message with the request, client need to subscribe to
v1/devices/me/attributes/response/+
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.
resources/mqtt-js-attributes-request.sh |
---|
|
resources/mqtt-js-attributes-request.js |
---|
|
resources/attributes-response.json |
---|
|
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.
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"}
resources/mosquitto-attributes-subscribe.sh |
---|
|
resources/mqtt-js-attributes-subscribe.sh |
---|
|
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
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.
resources/mqtt-js-rpc-from-server.sh |
---|
|
resources/mqtt-js-rpc-from-server.js |
---|
|
In order to send RPC commands to server, 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
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.
resources/mqtt-js-rpc-from-client.sh |
---|
|
resources/mqtt-js-rpc-from-client.js |
---|
|
Please see the corresponding article to get more information about the Claiming devices feature.
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).
MQTT transport can be fully customized for specific use-case by changing the corresponding module.
Getting started guides - These guides provide quick overview of main ThingsBoard features. Designed to be completed in 15-30 minutes.
Installation guides - Learn how to setup ThingsBoard on various available operating systems.
Data visualization - These guides contain instructions how to configure complex ThingsBoard dashboards.
Data processing & actions - Learn how to use ThingsBoard Rule Engine.
IoT Data analytics - Learn how to use rule engine to perform basic analytics tasks.
Hardware samples - Learn how to connect various hardware platforms to ThingsBoard.
Advanced features - Learn about advanced ThingsBoard features.
Contribution and Development - Learn about contribution and development in ThingsBoard.