Thingsboard gateway 3.4 连接opc_ua

安装opc_ua服务

opcua_server安装

  • 下载代码
    git clone https://gitlab.com/latosai/opcua_server.git
  • 安装环境变量
    python3 -m venv venv
    source venv/bin/activate
  • 安装依赖
    pip install -r requirements.txt
  • 运行服务
    cd code
    python3 server.py
  • 使用UaExpert连接测试

KEPServerEX安装

  • KEPServerEX 下载
  • 项目属性设置在这里插入图片描述
    服务器接口:
    “启用”选“是” 将启用OPC UA服务器接口
    “日志诊断”用于记录 OPC UA 连接及通讯信息(可选)
    客户端会话:
    “允许匿名登录”选“是” 此属性指定在建立连接时是否需要用户名及密码
  • OPC UA配置管理器设置
    在这里插入图片描述
    在这里插入图片描述
    选择使用的本机网卡,端口号,安全策略(本测试端口设置为49320,安全策 略全选,本服务器站点URL opc.tcp://192.168.1.32:49320)
  • 重启Kepware服务 停止Kepware服务,再启动Kepware服务,使OPC UA Server端设置生效
  • 关闭防火墙
    在这里插入图片描述

使用UaExpert测试服务端

下载

https://www.unified-automation.com/downloads/opc-ua-clients.html

安装配置

  • 添加服务端
    在这里插入图片描述
  • 连接服务端
    在这里插入图片描述
  • 选择属性查看
    在这里插入图片描述
    至此,如果效果与图一致,则表明ocp ua 服务启动都正常,再使用程序语言进行下一步连接测试

程序测试连接

from queue import PriorityQueue
import time

from opcua import Client, ua


class SubHandler(object):
    def datachange_notification(self, node, val, data):
        print("Python: New data change event", node, val, data)

def get_text():
    client = Client('opc.tcp://192.168.0.74:49320/', timeout=10)
    # client.set_user('will')
    # client.set_password('123456') # 需要在连接前调用
    # client.set_security_string(r"Basic256,SignAndEncrypt,C:\Users\42082\Desktop\certificate-example.der,C:\Users\42082\Desktop\private-key-example.pem") # 只支持Basic128Rsa15, Basic256 or Basic256Sha256三种生成证书的方法
    client.connect()
    objects = client.get_objects_node()
    print("objects:")
    print(objects)
    print("\n")

    root = client.get_root_node()
    print("children node:")
    print(root.get_children()[0].get_children())
    print("\n")

    namespaces = client.get_namespace_array()
    print("namespaces:")
    print(namespaces)
    print("\n")
    endpoints = client.get_endpoints()
    print("endpoints")
    print(endpoints)
    print("\n")
    snode = client.get_server_node()
    print("snode:")
    print(snode)
    print("\n")
    try:
        node = client.get_node('ns=2;s=通道 1.设备 1.标记 1')
        print("\n")
        print(node.get_value())
        dv = ua.DataValue(ua.Variant(200.0, ua.VariantType.Float))
        node.set_value(dv)
        handler = SubHandler()
        sub = client.create_subscription(500, handler)
        sub.subscribe_data_change(node)
        time.sleep(1000)
    except Exception as e:
        print(e)
    finally:
        client.disconnect()

if __name__ == "__main__":
    get_text()

在这里插入图片描述

import asyncio
import logging

from asyncua import Client, ua
from future.moves import sys


class SubHandler(object):
    def datachange_notification(self, node, val, data):
        idNode = node.nodeid.Identifier.split('.', maxsplit=2)
        idDevice = idNode[1]
        tag = idNode[2]
        print(idDevice, tag, val)

async def main():
    url = "opc.tcp://192.168.0.74:49320"
    # url = "opc.tcp://localhost:53530/OPCUA/SimulationServer/"
    # url = "opc.tcp://olivier:olivierpass@localhost:53530/OPCUA/SimulationServer/"
    async with Client(url,10) as client:
        #print("Root children are", await client.nodes.root.get_children())

        tag1 = client.get_node("ns=2;s=通道 1.设备 1.标记 1")
        tag1Val = await tag1.read_value()
        print(f"tag1:{tag1}, VALUE={tag1Val}, TYPE={type(tag1Val)}")

        tag2 = client.get_node("ns=2;s=通道 1.设备 1.标记 2")
        tag2Val = await tag2.read_value()
        print(f"tag2:{tag2}, VALUE={tag2Val}, TYPE={type(tag2Val)}")

if __name__ == "__main__":
    logging.basicConfig(level=logging.WARN)
    asyncio.run(main())

在这里插入图片描述

gateway连接

tb_gateway.json配置

"connectors": [
    {
        "name": "OPC-UA Connector",
        "type": "opcua",
        "configuration": "opcua.json"
    }
  ],

opcua.json配置

{
  "server": {
    "name": "OPC-UA Default Server",
    "url": "192.168.0.74:49320",
    "timeoutInMillis": 5000,
    "scanPeriodInMillis": 5000,
    "disableSubscriptions": false,
    "subCheckPeriodInMillis": 100,
    "showMap": false,
    "security": "Basic128Rsa15",
    "identity": {
      "type": "anonymous"
    },
    "mapping": [
      {
        "deviceNodePattern": "Root\\.Objects\\.Device1",
        "deviceNamePattern": "OPC001",
        "attributes": [
          {
            "key": "temperature",
            "path": "${ns=2;s=Device1.OPC001.temperature}"
          }
        ],
        "timeseries": [
          {
            "key": "humidity",
            "path": "${Root\\.Objects\\.Device1\\.OPC001\\.Humidity}"
          }
        ],
        "rpc_methods": [
          {
            "method": "multiply",
            "arguments": [
              2,
              4
            ]
          }
        ],
        "attributes_updates": [
          {
            "attributeOnThingsBoard": "deviceName",
            "attributeOnDevice": "Root\\.Objects\\.Device1\\.OPC001\\.temperature"
          }
        ]
      }
    ]
  }
}

server根据配置新建通道与属性

  • 新建通道
    在这里插入图片描述
  • 选择通道类型
    在这里插入图片描述
  • 指定通道名称

按照配置文件中的名字定义,如"deviceNodePattern": “Root\.Objects\.Device1”,通道名称为Device1
在这里插入图片描述

  • 添加设备

根据设备名称字段定义,如"deviceNamePattern": “Device ${Root\.Objects\.Device1\.OPC001}”
在这里插入图片描述

  • 添加标记

设备的属性名称以及属性地址,对应
“attributes”: [
{
“key”: “temperature”,
“path”: “${Root\.Objects\.Device1\.OPC001\.temperature}”
}
在这里插入图片描述
地址值说明
在这里插入图片描述

  • 查看添加好的值
    在这里插入图片描述
    在这里插入图片描述
    获取到NodeId,写入到Path中

ns=2;s=Device1.OPC001.temperature
在这里插入图片描述

  • 启动gateway

代码拿下来后运行是存在问题的,无法获取到属性值,源码讲解以及如何修改后续补上

启动成功

  • 查看Thingsboard页面中是否有名称为"deviceNamePattern": "OPC001"的设备注册上去
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_三石_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值