MQTT直连接口
设备使用mqtt发送遥测数据,从官网api查看发送路径
MQTT Device API Reference
连接协议 | API | 具体作用 | 请求路径 |
---|---|---|---|
MQTT | 遥测API | 上传设备遥测 | v1/devices/me/telemetry |
属性API | 上传设备属性 | v1/devices/me/attributes | |
从服务器请求属性值(客户端和共享设备属性) | v1/devices/me/attributes/request/$request_id | ||
从服务器订阅属性更新 | v1/devices/me/attributes | ||
服务端RPC | 1 从服务器订阅RPC命令 | v1/devices/me/rpc/request/+ | |
2 订阅后,客户端将接收单独的命令,将其作为发布消息发送到相应的主题:其中$request_id是整数请求标识符。 | v1/devices/me/rpc/request/$request_id | ||
3 客户端应发布对以下主题的响应 | v1/devices/me/rpc/response/$request_id | ||
客户端RPC | 1 发送RPC命令到服务器,其中,$REQUEST_ID是整数请求标识符 | v1/devices/me/rpc/request/$request_id | |
2 来自服务器的响应将发布到以下主题 | v1/devices/me/rpc/response/$request_id | ||
认领设备 | 发起申请设备 | v1/devices/me/claim |
Telemetry upload API 遥测上传 API
In order to publish telemetry data to ThingsBoard server node, send PUBLISH message to the following topic:
要将遥测数据发布到 ThingsBoard 服务器节点,请向以下主题发送 PUBLISH 消息:
v1/devices/me/telemetry
The simplest supported data formats are:
支持的最简单的数据格式为:
{"key1":"value1", "key2":"value2"}
or
[{"key1":"value1"}, {"key2":"value2"}]
Attributes API
ThingsBoard attributes API allows devices to
ThingsBoard 属性 API 允许设备
- Upload client-side device attributes to the server.
将客户端设备属性上传到服务器。 - Request client-side and shared device attributes from the server.
从服务器请求客户端和共享设备属性。 - Subscribe to shared device attributes from the server.
从服务器订阅共享设备属性。
Publish attribute update to the server
将客户端属性更新发布到服务器(设备特有的)
In order to publish client-side device attributes to ThingsBoard server node, send PUBLISH message to the following topic:
要将客户端设备属性发布到 ThingsBoard 服务器节点,请向以下主题发送 PUBLISH 消息:
v1/devices/me/attributes
例如
{"hardWareVersion":"0.1", "softWareVersion":"1.2"}
上面默认是发到客户端设备属性里的,
1 如果要发到服务端/公共属性,可以修改规则链节点Post attributes之后指向的节点类型,例如改成save attributes(选择服务端节点)
2 如果post attributes要传到遥测timeseries,会报错
java.lang.IllegalArgumentException: Unsupported msg type: POST_ATTRIBUTES_REQUEST(不支持节点类型)
解决办法:既然遥测timeseries不支持post attributes,那把postattributes传的节点数据类型改变成,把数据类型转化成遥测节点类型POST_TELEMETRY_REQUEST(return {msg: msg, metadata: metadata, msgType: “POST_TELEMETRY_REQUEST”};)
Request attribute values from the server
从服务器请求属性值
In order to request client-side or shared device attributes to ThingsBoard server node, send PUBLISH message to the following topic:
要向 ThingsBoard 服务器节点请求客户端或共享设备属性,请向以下主题发送 PUBLISH 消息:
v1/devices/me/attributes/request/$request_id
where r e q u e s t i d ∗ ∗ i s y o u r i n t e g e r r e q u e s t i d e n t i f i e r . B e f o r e s e n d i n g P U B L I S H m e s s a g e w i t h t h e r e q u e s t , c l i e n t n e e d t o s u b s c r i b e t o 其中 ∗ ∗ request_id** is your integer request identifier. Before sending PUBLISH message with the request, client need to subscribe to 其中 ** requestid∗∗isyourintegerrequestidentifier.BeforesendingPUBLISHmessagewiththerequest,clientneedtosubscribeto其中∗∗request_id 是整数请求标识符。在发送带有请求的 PUBLISH 消息之前,客户端需要订阅
v1/devices/me/attributes/response/+
发送数据示例
示例
订阅v1/devices/me/attributes/response/+
请求v1/devices/me/attributes/request/1
{"clientKeys":"hardWareVersion,softWareVersion", "sharedKeys":"hello"}
Subscribe to attribute updates from the server
从服务器订阅属性更新
In order to subscribe to shared device attribute changes, send SUBSCRIBE message to the following topic:
要订阅共享设备属性更改,请向以下主题发送 SUBSCRIBE 消息:
v1/devices/me/attributes
发一条数据测试
当其中一个服务器端组件(如 REST API 或规则链)更改共享属性时,客户端将收到更新:
RPC API
Server-side RPC
In order to subscribe to RPC commands from the server, send SUBSCRIBE message to the following topic:
要从服务器订阅 RPC 命令,请向以下主题发送 SUBSCRIBE 消息:
v1/devices/me/rpc/request/+
Once subscribed, the client will receive individual commands as a PUBLISH message to the corresponding topic:
订阅后,客户端将接收单个命令作为相应主题的 PUBLISH 消息:
v1/devices/me/rpc/request/$request_id
where
r
e
q
u
e
s
t
i
d
∗
∗
i
s
a
n
i
n
t
e
g
e
r
r
e
q
u
e
s
t
i
d
e
n
t
i
f
i
e
r
.
其中
∗
∗
request_id** is an integer request identifier. 其中 **
requestid∗∗isanintegerrequestidentifier.其中∗∗request_id 是整数请求标识符。
The client should publish the response to the following topic:
客户端应发布对以下主题的响应:
v1/devices/me/rpc/response/$request_id
dashboard创建小部件rpc debug terminal
实际发送了请求为(twoway表示是一个双向的rpc,后缀是设备的id)
发送的数据(与terminal发的数据不同,实际转成了json发送)
通过mqttx手动回复请求
认领设备
为了发起申请设备,向以下主题发送PUBLISH消息:
v1/devices/me/claim
The supported data format is:
支持的数据格式为:
{"secretKey":"value", "durationMs":60000}
请注意,上述字段是可选的。如果未指定secretKey,则使用空字符串作为默认值。如果没有指定durationMs,则使用系统参数device.claim.duration(在文件/etc/thingsboard/conf/thingsboard.yml中)。