# Publish data as an object without timestamp (server-side timestamp will be used). Replace $THINGSBOARD_HOST_NAME and $ACCESS_TOKEN with corresponding values.
mosquitto_pub -d-q 1 -h"$THINGSBOARD_HOST_NAME"-t"v1/devices/me/telemetry"-u"$ACCESS_TOKEN"-m"{"temperature":42}"# For example, $THINGSBOARD_HOST_NAME reference your local installation, $ACCESS_TOKEN is ABC123:
mosquitto_pub -d-q 1 -h"127.0.0.1"-t"v1/devices/me/telemetry"-u"ABC123"-m"{"temperature":42}"# Publish data as an object without timestamp (server-side timestamp will be used) using data from file. Replace $THINGSBOARD_HOST_NAME and $ACCESS_TOKEN with corresponding values.
mosquitto_pub -d-q 1 -h"$THINGSBOARD_HOST_NAME"-t"v1/devices/me/telemetry"-u"$ACCESS_TOKEN"-f"telemetry-data-as-object.json"# For example, $THINGSBOARD_HOST_NAME reference your local installation, $ACCESS_TOKEN is ABC123:
mosquitto_pub -d-q 1 -h"127.0.0.1"-t"v1/devices/me/telemetry"-u"ABC123"-f"telemetry-data-as-object.json"# Publish data as an array of objects without timestamp (server-side timestamp will be used) using data from file. Replace $THINGSBOARD_HOST_NAME and $ACCESS_TOKEN with corresponding values.
mosquitto_pub -d-q 1 -h"$THINGSBOARD_HOST_NAME"-t"v1/devices/me/telemetry"-u"$ACCESS_TOKEN"-f"telemetry-data-as-array.json"# For example, $THINGSBOARD_HOST_NAME reference your local installation, $ACCESS_TOKEN is ABC123:
mosquitto_pub -d-q 1 -h"127.0.0.1"-t"v1/devices/me/telemetry"-u"ABC123"-f"telemetry-data-as-array.json"# Publish data as an object with timestamp (telemetry timestamp will be used) using data from file. Replace $THINGSBOARD_HOST_NAME and $ACCESS_TOKEN with corresponding values.
mosquitto_pub -d-q 1 -h"$THINGSBOARD_HOST_NAME"-t"v1/devices/me/telemetry"-u"$ACCESS_TOKEN"-f"telemetry-data-with-ts.json"# For example, $THINGSBOARD_HOST_NAME reference your local installation, $ACCESS_TOKEN is ABC123:
mosquitto_pub -d-q 1 -h"127.0.0.1"-t"v1/devices/me/telemetry"-u"ABC123"-f"telemetry-data-with-ts.json"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Publish data as an object without timestamp (server-side timestamp will be used). Replace $THINGSBOARD_HOST_NAME and $ACCESS_TOKEN with corresponding values.
mqtt pub -v-q 1 -h"$THINGSBOARD_HOST_NAME"-t"v1/devices/me/telemetry"-u'$ACCESS_TOKEN'-m"{"temperature":42}"# For example, $THINGSBOARD_HOST_NAME reference your local installation, $ACCESS_TOKEN is ABC123:
mqtt pub -v-q 1 -h"127.0.0.1"-t"v1/devices/me/telemetry"-u'ABC123'-m"{"temperature":42}"# Publish data as an object without timestamp (server-side timestamp will be used). Replace $THINGSBOARD_HOST_NAME and $ACCESS_TOKEN with corresponding values.cat telemetry-data-as-object.json | mqtt pub -v-h"$THINGSBOARD_HOST_NAME"-t"v1/devices/me/telemetry"-u'$ACCESS_TOKEN'-s-m""# For example, $THINGSBOARD_HOST_NAME reference your local installation, $ACCESS_TOKEN is ABC123:cat telemetry-data-as-object.json | mqtt pub -v-h"127.0.0.1"-t"v1/devices/me/telemetry"-u'ABC123'-s-m""# Publish data as an array of objects without timestamp (server-side timestamp will be used). Replace $THINGSBOARD_HOST_NAME and $ACCESS_TOKEN with corresponding values.cat telemetry-data-as-array.json | mqtt pub -v-h"$THINGSBOARD_HOST_NAME"-t"v1/devices/me/telemetry"-u'$ACCESS_TOKEN'-s-m""# For example, $THINGSBOARD_HOST_NAME reference your local installation, $ACCESS_TOKEN is ABC123:cat telemetry-data-as-array.json | mqtt pub -v-h"127.0.0.1"-t"v1/devices/me/telemetry"-u'ABC123'-s-m""# Publish data as an object with timestamp (telemetry timestamp will be used). Replace $THINGSBOARD_HOST_NAME and $ACCESS_TOKEN with corresponding values.cat telemetry-data-with-ts.json | mqtt pub -v-h"$THINGSBOARD_HOST_NAME"-t"v1/devices/me/telemetry"-u'$ACCESS_TOKEN'-s-m""# For example, $THINGSBOARD_HOST_NAME reference your local installation, $ACCESS_TOKEN is ABC123:cat telemetry-data-with-ts.json | mqtt pub -v-h"127.0.0.1"-t"v1/devices/me/telemetry"-u'ABC123'-s-m""
# Publish client-side attributes update. Replace $THINGSBOARD_HOST_NAME and $ACCESS_TOKEN with corresponding values.
mosquitto_pub -d-h"$THINGSBOARD_HOST_NAME"-t"v1/devices/me/attributes"-u"$ACCESS_TOKEN"-m"{"attribute1": "value1", "attribute2": true}"# For example, $THINGSBOARD_HOST_NAME reference your local installation, $ACCESS_TOKEN is ABC123:
mosquitto_pub -d-h"127.0.0.1"-t"v1/devices/me/attributes"-u"ABC123"-m"{"attribute1": "value1", "attribute2": true}"# Publish client-side attributes update from file. Replace $THINGSBOARD_HOST_NAME and $ACCESS_TOKEN with corresponding values.
mosquitto_pub -d-h"$THINGSBOARD_HOST_NAME"-t"v1/devices/me/attributes"-u"$ACCESS_TOKEN"-f"new-attributes-values.json"# For example, $THINGSBOARD_HOST_NAME reference your local installation, $ACCESS_TOKEN is ABC123:
mosquitto_pub -d-h"127.0.0.1"-t"v1/devices/me/attributes"-u"ABC123"-f"new-attributes-values.json"
1
2
3
4
# Publish client-side attributes update. Replace $THINGSBOARD_HOST_NAME and $ACCESS_TOKEN with corresponding values.cat new-attributes-values.json | mqtt pub -d-h"$THINGSBOARD_HOST_NAME"-t"v1/devices/me/attributes"-u'$ACCESS_TOKEN'-s-m""# For example, $THINGSBOARD_HOST_NAME reference your local installation, $ACCESS_TOKEN is ABC123:cat new-attributes-values.json | mqtt pub -d-h"127.0.0.1"-t"v1/devices/me/attributes"-u'ABC123'-s-m""
# Subscribes to attribute updates. Replace $THINGSBOARD_HOST_NAME and $ACCESS_TOKEN with corresponding values.
mosquitto_sub -d-h"$THINGSBOARD_HOST_NAME"-t"v1/devices/me/attributes"-u"$ACCESS_TOKEN"# For example, $THINGSBOARD_HOST_NAME reference your local installation, $ACCESS_TOKEN is ABC123:
mosquitto_sub -d-h"127.0.0.1"-t"v1/devices/me/attributes"-u"ABC123"
1
2
3
4
# Subscribes to attribute updates. Replace $THINGSBOARD_HOST_NAME and $ACCESS_TOKEN with corresponding values.
mqtt sub -v-h"$THINGSBOARD_HOST_NAME"-t"v1/devices/me/attributes"-u'$ACCESS_TOKEN'# For example, $THINGSBOARD_HOST_NAME reference your local installation, $ACCESS_TOKEN is ABC123:
mqtt sub -v-h"127.0.0.1"-t"v1/devices/me/attributes"-u'ABC123'
支持JSON
We added support of JSON data structures to telemetry and attributes API to simplify work with device configuration.
JSON support allows you to both upload from the device, and push to device nested objects.
You can store one configuration JSON as a shared attribute and push it to the device. You can also process the JSON data in the rule engine and raise alarms, etc.
Therefore, this improvement minimizes the number of Database operations when ThingsBoard stores the data.
For example, “temperature” and “humidity” would be stored as separate rows in SQL or NoSQL databases in order to efficiently aggregate this data for visualization.
Since there is no need to aggregate JSON data, we can store all the content as one row instead of separate rows for each configuration item.
In some of our environments, it is possible to decrease the number of database operations more than 10 times by aggregating multiple parameters within one JSON.
Learn more about JSON value support with the video.
varmqtt=require('mqtt');varclient=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());varrequestId=topic.slice('v1/devices/me/rpc/request/'.length);//client acts as an echo serviceclient.publish('v1/devices/me/rpc/response/'+requestId,message);});