thingsboard 演示地址:https://www.iotschool.com/topics/8
说明:该代码实现了 client side rpc 的演示,通过下面 python 方法,对 server 端 device 的 email 属性进行访问。服务器端响应的服务是通过 rule engine 实现的。
client_side_rpc.py
# this program illustrates the client side rpc on thingsboard iot platform
# paste your thingsboard iot platform ip and device access token
# client_side_rpc.py : this program will illustrates the client side
import os
import time
import sys
import json
import random
import paho.mqtt.client as mqtt
# thingsboard platform credentials
thingsboard_host = 'host_ip'
access_token = 'oudvzkzcs8b11x7omqjx'
request = {"method": "getemail", "params": ""}
# mqtt on_connect callback function
def on_connect(client, userdata, flags, rc):
print("rc code:", rc)
client.subscribe('v1/devices/me/rpc/response/ ')
client.subscribe('v1/devices/me/attributes')
client.publish('v1/devices/me/rpc/request/1',json.dumps(request), 1)
# mqtt on_message caallback function
def on_message(client, userdata, msg):
print('topic: ' msg.topic '\nmessage: ' str(msg.payload))
# start the client instance
client = mqtt.client()
# registering the callbacks
client.on_connect = on_connect
client.on_message = on_message
client.username_pw_set(access_token)
client.connect(thingsboard_host, 1883, 60)
try:
client.loop_forever()
except keyboardinterrupt:
client.disconnect()
tb 端新建立 rule enginer:总体效果图如下:
该 rule engine 的责任就是为了响应 client side rpc 的服务器端程序。最后把这个驱动加入到 root rule 中。
server side rpc 的代码分为两个部分,一个是遥测数据上传,一个是 rpc 响应。
server_side_rpc.py
# this program illustrates the simple server side rpc on thingsboard iot platform
# paste your thingsboard iot platform ip and device access token
# rpc server_side_rpc.py : this program will illustrates the server side rpc
import os
import time
import sys
import json
import random
import paho.mqtt.client as mqtt
# thingsboard platform credentials
thingsboard_host = 'host_ip'
access_token = 'oudvzkzcs8b11x7omqjx'
button_state = {"enabled": false}
def setvalue (params):
button_state['enabled'] = params
print("rx setvalue is : ",button_state)
# mqtt on_connect callback function
def on_connect(client, userdata, flags, rc):
print("rc code:",rc)
client.subscribe('v1/devices/me/rpc/request/ ')
# mqtt on_message caallback function
def on_message(client, userdata, msg):
print('topic: ' msg.topic '\nmessage: ' str(msg.payload))
if msg.topic.startswith('v1/devices/me/rpc/request/'):
requestid = msg.topic[len('v1/devices/me/rpc/request/'):len(msg.topic)]
print("requestid : ",requestid)
data = json.loads(msg.payload)
if data['method'] == 'getvalue':
print("getvalue request\n")
print("sent getvalue : ",button_state)
client.publish('v1/devices/me/rpc/response/' requestid, json.dumps(button_state), 1)
if data['method'] == 'setvalue':
print("setvalue request\n")
params = data['params']
setvalue(params)
client.publish('v1/devices/me/attributes', json.dumps(button_state), 1)
# start the client instance
client = mqtt.client()
# registering the callbacks
client.on_connect = on_connect
client.on_message = on_message
client.username_pw_set(access_token)
client.connect(thingsboard_host,1883,60)
try:
client.loop_forever()
except keyboardinterrupt:
client.disconnect()
temperature_controller_server_side_rpc.py
# this program illustrates the server side rpc on thingsboard iot platform
# paste your thingsboard iot platform ip and device access token
# temperature_controller_server_side_rpc.py : this program illustrates server side rpc using a simulated temperature controller
import os
import time
import sys
import json
import random
import paho.mqtt.client as mqtt
from threading import thread
# thingsboard platform credentials
thingsboard_host = 'host_ip' #change ip address
access_token = 'oudvzkzcs8b11x7omqjx'
sensor_data = {'temperature': 25}
def publishvalue(client):
interval = 2
print("thread started")
next_reading = time.time()
while true:
client.publish('v1/devices/me/telemetry', json.dumps(sensor_data),1)
next_reading = interval
sleep_time = next_reading - time.time()
if sleep_time > 0:
time.sleep(sleep_time)
def read_temperature():
temp = sensor_data['temperature']
return temp
# function will set the temperature value in device
def setvalue (params):
sensor_data['temperature'] = params
#print("rx setvalue is : ",sensor_data)
print("temperature set : ",params,"c")
# mqtt on_connect callback function
def on_connect(client, userdata, flags, rc):
#print("rc code:", rc)
client.subscribe('v1/devices/me/rpc/request/ ')
# mqtt on_message callback function
def on_message(client, userdata, msg):
#print('topic: ' msg.topic '\nmessage: ' str(msg.payload))
if msg.topic.startswith('v1/devices/me/rpc/request/'):
requestid = msg.topic[len('v1/devices/me/rpc/request/'):len(msg.topic)]
#print("requestid : ", requestid)
data = json.loads(msg.payload)
if data['method'] == 'getvalue':
#print("getvalue request\n")
#print("sent getvalue : ", sensor_data)
client.publish('v1/devices/me/rpc/response/' requestid, json.dumps(sensor_data['temperature']), 1)
if data['method'] == 'setvalue':
#print("setvalue request\n")
params = data['params']
setvalue(params)
# create a client instance
client = mqtt.client()
client.on_connect = on_connect
client.on_message = on_message
client.username_pw_set(access_token)
client.connect(thingsboard_host,1883,60)
t = thread(target=publishvalue, args=(client,))
try:
client.loop_start()
t.start()
while true:
pass
except keyboardinterrupt:
client.disconnect()
新建一个 dashboard,并同时执行上面的 2 个 python 代码:
通过 knob control 的调节,就可以在线调节温度了。在 chrome 通过 f12 进入 debug 模式,就可以看到 tb 调用 rpc 的 http 请求方法了。这个调用方式,和 doc 中描述的方法是一致的。
感谢:寒碧
原文: