On this page
网关API通信
一、环境准备
1.测试环境
1
2
3
4
5
6
7
8
9
10
| # ThingsBoard
http://192.168.202.188:8080/login
# Node-RED
http://192.168.202.188:1880
设备名称:Node-RED-gateway
访问令牌:y0b9w7P2dSIR2AKn7Dl4
Nodered流程: TB-gateway
|
2.创建网关

3.创建Node-RED流程

二、设备链接
1.Connect
1
2
3
4
| # 发布主题
v1/gateway/connect
{"device":"Device A"}
|






2.Disconnect
1
2
3
| v1/gateway/disconnect
{"device":"Device A"}
|


三、属性
1.发布属性到服务端
将客户端设备属性发布到ThingsBoard服务器节点。
1
2
3
| Topic: v1/gateway/attributes
Message: {"Device A":{"attribute1":"value1", "attribute2": 42}, "Device B":{"attribute1":"value1", "attribute2": 42}}
|




2.请求属性从服务端
向ThingsBoard服务器节点请求客户端或共享设备属性。
请向以下主题发送PUBLISH消息:
1
2
| Topic: v1/gateway/attributes/request
Message: {"id": $request_id, "device": "Device A", "client": true, "key": "attribute1"}
|
其中$request_id是您的整数请求标识符,设备A是您的设备名称,client标识客户端或共享属性范围,key是属性键。
在发送PUBLISH消息和请求之前,客户端需要订阅
1
| Topic: v1/gateway/attributes/response
|
并期望具有以下格式的结果的消息:
1
| Message: {"id": $request_id, "device": "Device A", "value": "value1"}
|





3.订阅属性更新从服务端
订阅共享设备属性更改,请向以下主题发送subscribe消息:
并期望具有以下格式的结果的消息:
1
| Message: {"device": "Device A", "data": {"attribute1": "value1", "attribute2": 42}}
|




四、遥测
将设备遥测发布到ThingsBoard服务器节点,请向以下主题发送publish消息:
1
| Topic: v1/gateway/telemetry
|
Message:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
| {
"Device A": [
{
"ts": 1483228800000,
"values": {
"temperature": 42,
"humidity": 80
}
},
{
"ts": 1483228801000,
"values": {
"temperature": 43,
"humidity": 82
}
}
],
"Device B": [
{
"ts": 1483228800000,
"values": {
"temperature": 42,
"humidity": 80
}
}
]
}
|




五、服务端RPC
从服务器订阅RPC命令,请向以下主题发送subscribe消息:
并期望具有以下格式的单个命令的消息:
1
| {"device": "Device A", "data": {"id": $request_id, "method": "toggle_gpio", "params": {"pin":1}}}
|
一旦设备处理了命令,网关就可以使用以下格式发回命令:
1
| {"device": "Device A", "id": $request_id, "data": {"success": true}}
|



六、总结
