ThingsBoard Gateway 连接 Thingsboard
修改tb_gateway.yaml
配置文件
1 | thingsboard: //用于连接ThingsBoard平台的配置 |
开启OPC-UA
修改tb_gateway.yaml
配置文件
1 | connectors: //连接器阵列及其使用的配置 |
配置OPC-UA
修改opcua.json
配置文件
1 | { |
模拟OPC-UA server
- 安装python 的OPC-UA 库
1
pip install opcua
- 编写OPC-UA server,创建
tbOpcServer.py
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51# encoding=utf-8
import sys
sys.path.insert(0, "..")
import time
from opcua import ua, Server
if __name__ == "__main__":
def multiply(parent, x, y):
return x * y
# setup our server
server = Server()
server.set_endpoint("opc.tcp://0.0.0.0:4840/freeopcua/server/")
uri = "http://127.0.0.1"
idx = server.register_namespace(uri)
# get Objects node, this is where we should put our nodes
objects = server.get_objects_node()
# populating our address space
device = objects.add_object(idx, "Device1")
name = device.add_variable(idx, "SerialNumber", "TEST")
temperature_and_humidity = device.add_object(idx, "TemperatureAndHumiditySensor")
temperature = temperature_and_humidity.add_variable(idx, "Temperature", 56.7)
temperature.set_writable()
humidity = temperature_and_humidity.add_variable(idx, "Count", 68.7)
humidity.set_writable()
battery = device.add_object(idx, "Battery")
battery_level = battery.add_variable(idx, "BatteryLevel", 24)
battery_level.set_writable()
device.add_method(idx, "multiply", multiply, [ua.VariantType.Int64, ua.VariantType.Int64], [ua.VariantType.Int64])
# starting!
server.start()
try:
count = 1
while True:
time.sleep(1)
count += 1
humidity.set_value(count)
finally:
# close connection, remove subcsriptions, etc
server.stop() - 启动OPC-UA Server
1
python tbOpcServer.py
- 安装OPC-UA可视化工具
1
pip3 install opcua-client
- 注意:PyQT 5 is required*
安装好后效果图 - ThingsBoard 设备可以看到自动新增设备Device TEST,并且已经存在属性数据和遥测数据
模拟设备OPC-UA client 推送数据
- 编写OPC-UA设备方客户端,创建
opcClient.py
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
28
29import sys
sys.path.insert(0, "..")
from opcua import Client, ua
if __name__ == "__main__":
client = Client("opc.tcp://localhost:4840/freeopcua/server/")
# client = Client("opc.tcp://admin@localhost:4840/freeopcua/server/") #connect using a user
try:
client.connect()
# Client has a few methods to get proxy to UA nodes that should always be in address space such as Root or Objects
root = client.get_root_node()
print("Objects node is: ", root)
print("Children of root are: ", root.get_children())
# 获取节点
var = client.get_node("ns=2;i=7")
print("ns=2;i=7 is: ", var.get_value())
var.set_value(4.8) # 设置节点的值
print("new ns=2;i=7 is: ", var.get_value())
finally:
client.disconnect() - 运行python客户端脚本,修改batteryLevel为4.8
1
python opcClient.py
- 在Thingsboard 查看执行结果