桃之夭夭,灼灼其华

Thingsboards Gateway 接入 OPC-UA

Word count: 798Reading time: 4 min
2021/03/02 Share

ThingsBoard Gateway 连接 Thingsboard

修改tb_gateway.yaml配置文件

1
2
3
4
5
6
7
8
thingsboard: //用于连接ThingsBoard平台的配置
host: 127.0.0.1 //thingsboard ip
port: 1883 //thingsboard mqtt 端口
remoteShell: false
remoteConfiguration: false
security:
accessToken: WQjc205EBB9meDI4lOP0 //设备访问令牌
qos: 1

开启OPC-UA

修改tb_gateway.yaml配置文件

1
2
3
4
5
connectors: //连接器阵列及其使用的配置
-
name: OPC-UA Connector
type: opcua
configuration: opcua.json

配置OPC-UA

修改opcua.json配置文件

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
{
"server": {
"name": "OPC-UA Default Server",
"url": "localhost:4840/freeopcua/server/", //opc-ua Server地址
"timeoutInMillis": 5000,
"scanPeriodInMillis": 5000,
"disableSubscriptions":false,
"subCheckPeriodInMillis": 100,
"showMap": false,
"security": "Basic128Rsa15",
"identity": {
"type": "anonymous"
},
"mapping": [
{
"deviceNodePattern": "Root\\.Objects\\.Device1", //opc-ua server 设备节点
"deviceNamePattern": "Device ${Root\\.Objects\\.SerialNumber}", //设备名称
"attributes": [ //对应Thingsboard属性
{
"key": "temperature", //对应Thingsboard属性名称
"path": "${ns=2;i=4}" //opc-ua server数据节点位置
}
],
"timeseries": [ //对应Thingsboard遥测数据
{
"key": "count", //对应Thingsboard遥测数据名称
"path": "${ns=2;i=5}" //opc-ua server数据节点位置
},
{
"key": "batteryLevel", //对应Thingsboard遥测数据名称
"path": "${ns=2;i=7}" //opc-ua server数据节点位置
}
]
}
]
}
}

模拟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*
    安装好后效果图
    1.png
  • ThingsBoard 设备可以看到自动新增设备Device TEST,并且已经存在属性数据和遥测数据
    2.png

    模拟设备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
    29
    import 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://[email protected]: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
    3.png
  • 在Thingsboard 查看执行结果
    4.png
CATALOG
  1. 1. ThingsBoard Gateway 连接 Thingsboard
  2. 2. 开启OPC-UA
  3. 3. 配置OPC-UA
  4. 4. 模拟OPC-UA server
  5. 5. 模拟设备OPC-UA client 推送数据