Node-Red系列教程——NodeRed订阅opcua数据,并通过mqtt发布

目录

1.nodered 订阅opcua数据

2. nodered发布为mqtt

3. 部署运行结果,通过emqx接收数据

4. nodered的json代码


nodered搭配结果如下图:

1.nodered 订阅opcua数据

前言:opcua数据来自kepware模拟数据,具体nodered的OPCUA安装,mqtt安装参考下文使用kepware配置opcua服务端,并使用UaExpert作为opc客户端进行连接(匿名和用户名密码)_鲸鱼姐的博客-CSDN博客_kepware opc ua服务器配置

Node-Red系列——NodeRed通过OPC UA读取数据并写入mysql_鲸鱼姐的博客-CSDN博客

Node-Red系列——NodeRed创建mqtt broker(mqtt服务器),并使用mqttx进行消息发送验证_鲸鱼姐的博客-CSDN博客_mqttx 发送消息

扫描二维码关注公众号,回复: 14921370 查看本文章

(1)inject节点配置

(2)OPCUA-Item节点配置

(3)OPC client配置

action采用subscribe方式,Interval为1 second(s)  

注意订阅间隔的单位,可以为毫秒,秒,小时,分钟

2. nodered发布为mqtt

(1)function节点

将msg.topic作为device的键值,msg的payload作为value的键值。这样接收的mqtt端可以通过device进行设备区分。

msg.payload = {
    'device': msg.topic,
    'value': msg.payload
};
return msg;

(2)mqtt out配置

配置服务端和主题

(3)debug节点

3. 部署运行结果,通过emqx接收数据

(1)nodered运行结果

(2) 运行mqtt服务端——emqx,接收nodered发送的mqtt消息

使用emqx的websocket客户端连接,并订阅plc/data主题

 订阅后,emqx可以收到nodered发出的mqtt消息

4. nodered的json代码

[
    {
        "id": "b521b512ccf51472",
        "type": "tab",
        "label": "流程 3",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "a16131868eeaedda",
        "type": "OpcUa-Item",
        "z": "b521b512ccf51472",
        "item": "ns=2;s=通道 2.设备 1.tt1",
        "datatype": "Int32",
        "value": "",
        "name": "tt1",
        "x": 190,
        "y": 140,
        "wires": [
            [
                "ba7429bb4b39cf36"
            ]
        ]
    },
    {
        "id": "ba7429bb4b39cf36",
        "type": "OpcUa-Client",
        "z": "b521b512ccf51472",
        "endpoint": "058a91456e27a0bc",
        "action": "subscribe",
        "deadbandtype": "a",
        "deadbandvalue": 1,
        "time": "1",
        "timeUnit": "s",
        "certificate": "n",
        "localfile": "",
        "localkeyfile": "",
        "securitymode": "None",
        "securitypolicy": "None",
        "folderName4PKI": "",
        "name": "",
        "x": 380,
        "y": 120,
        "wires": [
            [
                "98aeb1313adf6976"
            ]
        ]
    },
    {
        "id": "702470447b1bbcbe",
        "type": "inject",
        "z": "b521b512ccf51472",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": true,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "str",
        "x": 50,
        "y": 120,
        "wires": [
            [
                "a16131868eeaedda",
                "efd47eaa4a986b84"
            ]
        ]
    },
    {
        "id": "0bd1292815c00b3a",
        "type": "mqtt out",
        "z": "b521b512ccf51472",
        "name": "mqtt out",
        "topic": "plc/data",
        "qos": "1",
        "retain": "",
        "respTopic": "",
        "contentType": "",
        "userProps": "",
        "correl": "",
        "expiry": "",
        "broker": "019d2a5c6d879e6a",
        "x": 620,
        "y": 220,
        "wires": []
    },
    {
        "id": "5116c26ea1dcb1e6",
        "type": "debug",
        "z": "b521b512ccf51472",
        "name": "debug 24",
        "active": false,
        "tosidebar": true,
        "console": true,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 630,
        "y": 300,
        "wires": []
    },
    {
        "id": "98aeb1313adf6976",
        "type": "function",
        "z": "b521b512ccf51472",
        "name": "function 10",
        "func": "msg.payload = {\n    'device': msg.topic,\n    'value': msg.payload\n};\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 430,
        "y": 220,
        "wires": [
            [
                "5116c26ea1dcb1e6",
                "0bd1292815c00b3a"
            ]
        ]
    },
    {
        "id": "efd47eaa4a986b84",
        "type": "OpcUa-Item",
        "z": "b521b512ccf51472",
        "item": "ns=2;s=通道 2.设备 1.tt2",
        "datatype": "Int32",
        "value": "",
        "name": "tt2",
        "x": 190,
        "y": 100,
        "wires": [
            [
                "ba7429bb4b39cf36"
            ]
        ]
    },
    {
        "id": "058a91456e27a0bc",
        "type": "OpcUa-Endpoint",
        "endpoint": "opc.tcp://192.168.31.88:49320",
        "secpol": "None",
        "secmode": "None",
        "none": true,
        "login": false,
        "usercert": false,
        "usercertificate": "",
        "userprivatekey": ""
    },
    {
        "id": "019d2a5c6d879e6a",
        "type": "mqtt-broker",
        "name": "",
        "broker": "127.0.0.1",
        "port": "1883",
        "clientid": "",
        "autoConnect": true,
        "usetls": false,
        "protocolVersion": "4",
        "keepalive": "60",
        "cleansession": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthPayload": "",
        "birthMsg": {},
        "closeTopic": "",
        "closeQos": "0",
        "closePayload": "",
        "closeMsg": {},
        "willTopic": "",
        "willQos": "0",
        "willPayload": "",
        "willMsg": {},
        "userProps": "",
        "sessionExpiry": ""
    }
]

猜你喜欢

转载自blog.csdn.net/zhaolulu916/article/details/127148195