当前位置:   article > 正文

NodeRED学习(七)MQTT/Modbus网关_node-red对接传感器

node-red对接传感器

简述:

Node-RED 搭建一个MQTT网关,网关通过Modbus总线连接一个水位传感器和一个温度传感器,采集水位和温度数据,再通过MQTT客户端把水位和温度信息发布到MQTT代理。

网关:

协议转化:网关可以通过Modbus总线把多个485串口的传感器,以轮询的方式采集,通过MQTT协议发布给信息平台。
信息汇聚:网关可以把多个传感器数据,聚合为一个对象,为数据附加语义描述,使信息具有结构化,易可识别。
虚拟串口软件VSPD配置两对虚拟串口:
在这里插入图片描述
Modbus Slave模拟两个传感器:
在这里插入图片描述
在这里插入图片描述

配置MQTT代理:
在这里插入图片描述

通过Modbus分别采集水箱的水位和温度,合并为水箱参数,由MQTT客户端发布:
在这里插入图片描述
在这里插入图片描述

添加注入(inject)节点和延迟(delay)节点配置轮询:
在这里插入图片描述
在这里插入图片描述

添加函数节点,为消息添加主题:
在这里插入图片描述
在这里插入图片描述

[
    {
        "id": "47697d9d8a564c16",
        "type": "tab",
        "label": "流程 2",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "67b13033ddc0d223",
        "type": "modbus-getter",
        "z": "47697d9d8a564c16",
        "name": "水位",
        "showStatusActivities": true,
        "showErrors": false,
        "logIOActivities": false,
        "unitid": "1",
        "dataType": "HoldingRegister",
        "adr": "0",
        "quantity": "1",
        "server": "ce08db4b7d9f20f0",
        "useIOFile": false,
        "ioFile": "",
        "useIOForPayload": false,
        "emptyMsgOnFail": false,
        "keepMsgProperties": false,
        "x": 310,
        "y": 180,
        "wires": [
            [
                "d4cba5fe6569d8c3"
            ],
            []
        ]
    },
    {
        "id": "844ad47ff9474f12",
        "type": "modbus-getter",
        "z": "47697d9d8a564c16",
        "name": "水温",
        "showStatusActivities": true,
        "showErrors": false,
        "logIOActivities": false,
        "unitid": "1",
        "dataType": "HoldingRegister",
        "adr": "0",
        "quantity": "1",
        "server": "c3bdb25d487433e4",
        "useIOFile": false,
        "ioFile": "",
        "useIOForPayload": false,
        "emptyMsgOnFail": false,
        "keepMsgProperties": false,
        "x": 350,
        "y": 280,
        "wires": [
            [
                "06514971ca3ad6ed"
            ],
            []
        ]
    },
    {
        "id": "36e7a1c989d23266",
        "type": "inject",
        "z": "47697d9d8a564c16",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "5",
        "crontab": "",
        "once": true,
        "onceDelay": "10",
        "topic": "",
        "payload": "",
        "payloadType": "str",
        "x": 150,
        "y": 220,
        "wires": [
            [
                "67b13033ddc0d223",
                "cb911d4fa59973ac"
            ]
        ]
    },
    {
        "id": "16aa2453141586b1",
        "type": "debug",
        "z": "47697d9d8a564c16",
        "name": "debug 4",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 420,
        "y": 380,
        "wires": []
    },
    {
        "id": "cb911d4fa59973ac",
        "type": "delay",
        "z": "47697d9d8a564c16",
        "name": "",
        "pauseType": "delay",
        "timeout": "10",
        "timeoutUnits": "milliseconds",
        "rate": "1",
        "nbRateUnits": "1",
        "rateUnits": "second",
        "randomFirst": "1",
        "randomLast": "5",
        "randomUnits": "seconds",
        "drop": false,
        "allowrate": false,
        "outputs": 1,
        "x": 210,
        "y": 280,
        "wires": [
            [
                "844ad47ff9474f12"
            ]
        ]
    },
    {
        "id": "d4cba5fe6569d8c3",
        "type": "function",
        "z": "47697d9d8a564c16",
        "name": "水位",
        "func": "msg={topic:\"WaterLevel\",payload:msg.payload[0]}\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 450,
        "y": 180,
        "wires": [
            [
                "47bd8c57dc676aeb"
            ]
        ]
    },
    {
        "id": "06514971ca3ad6ed",
        "type": "function",
        "z": "47697d9d8a564c16",
        "name": "水温",
        "func": "msg={topic:\"WaterTemp\",payload:msg.payload[0],complete:true}\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 470,
        "y": 280,
        "wires": [
            [
                "47bd8c57dc676aeb"
            ]
        ]
    },
    {
        "id": "2229838c3375166e",
        "type": "mqtt out",
        "z": "47697d9d8a564c16",
        "name": "",
        "topic": "WaterTank",
        "qos": "0",
        "retain": "false",
        "respTopic": "",
        "contentType": "",
        "userProps": "",
        "correl": "",
        "expiry": "",
        "broker": "0222eb30f00ea5a5",
        "x": 750,
        "y": 220,
        "wires": []
    },
    {
        "id": "d731c49d3fbee150",
        "type": "aedes broker",
        "z": "47697d9d8a564c16",
        "name": "",
        "mqtt_port": 1883,
        "mqtt_ws_bind": "port",
        "mqtt_ws_port": "",
        "mqtt_ws_path": "",
        "cert": "",
        "key": "",
        "certname": "",
        "keyname": "",
        "dburl": "",
        "usetls": false,
        "x": 210,
        "y": 80,
        "wires": [
            [],
            []
        ]
    },
    {
        "id": "5323672ec49be0fd",
        "type": "mqtt in",
        "z": "47697d9d8a564c16",
        "name": "",
        "topic": "WaterTank",
        "qos": "0",
        "datatype": "auto-detect",
        "broker": "0222eb30f00ea5a5",
        "nl": false,
        "rap": true,
        "rh": 0,
        "inputs": 0,
        "x": 140,
        "y": 380,
        "wires": [
            [
                "16aa2453141586b1"
            ]
        ]
    },
    {
        "id": "47bd8c57dc676aeb",
        "type": "join",
        "z": "47697d9d8a564c16",
        "name": "",
        "mode": "custom",
        "build": "object",
        "property": "payload",
        "propertyType": "msg",
        "key": "topic",
        "joiner": "\\n",
        "joinerType": "str",
        "accumulate": true,
        "timeout": "",
        "count": "",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "",
        "reduceFixup": "",
        "x": 610,
        "y": 220,
        "wires": [
            [
                "2229838c3375166e"
            ]
        ]
    },
    {
        "id": "7b17b1f6c871629b",
        "type": "comment",
        "z": "47697d9d8a564c16",
        "name": "1. 用于演示的MQTT代理",
        "info": "",
        "x": 190,
        "y": 40,
        "wires": []
    },
    {
        "id": "0a22dc0691ff8231",
        "type": "comment",
        "z": "47697d9d8a564c16",
        "name": "2. 通过Modbus分别采集水箱的水位和温度,合并为水箱参数,由MQTT客户端发布",
        "info": "",
        "x": 370,
        "y": 140,
        "wires": []
    },
    {
        "id": "c3e9a93a84b8059c",
        "type": "comment",
        "z": "47697d9d8a564c16",
        "name": "3. 订阅水箱参数",
        "info": "",
        "x": 160,
        "y": 340,
        "wires": []
    },
    {
        "id": "ce08db4b7d9f20f0",
        "type": "modbus-client",
        "name": "",
        "clienttype": "simpleser",
        "bufferCommands": true,
        "stateLogEnabled": false,
        "queueLogEnabled": false,
        "failureLogEnabled": true,
        "tcpHost": "127.0.0.1",
        "tcpPort": "502",
        "tcpType": "DEFAULT",
        "serialPort": "com4",
        "serialType": "RTU",
        "serialBaudrate": "9600",
        "serialDatabits": "8",
        "serialStopbits": "1",
        "serialParity": "none",
        "serialConnectionDelay": "100",
        "serialAsciiResponseStartDelimiter": "0x3A",
        "unit_id": 1,
        "commandDelay": 1,
        "clientTimeout": 1000,
        "reconnectOnTimeout": true,
        "reconnectTimeout": 2000,
        "parallelUnitIdsAllowed": true
    },
    {
        "id": "c3bdb25d487433e4",
        "type": "modbus-client",
        "name": "",
        "clienttype": "simpleser",
        "bufferCommands": true,
        "stateLogEnabled": false,
        "queueLogEnabled": false,
        "failureLogEnabled": true,
        "tcpHost": "127.0.0.1",
        "tcpPort": "502",
        "tcpType": "DEFAULT",
        "serialPort": "com6",
        "serialType": "RTU",
        "serialBaudrate": "9600",
        "serialDatabits": "8",
        "serialStopbits": "1",
        "serialParity": "none",
        "serialConnectionDelay": "100",
        "serialAsciiResponseStartDelimiter": "0x3A",
        "unit_id": "1",
        "commandDelay": "1",
        "clientTimeout": "1000",
        "reconnectOnTimeout": true,
        "reconnectTimeout": "2000",
        "parallelUnitIdsAllowed": true
    },
    {
        "id": "0222eb30f00ea5a5",
        "type": "mqtt-broker",
        "name": "",
        "broker": "localhost",
        "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": ""
    }
]
  • 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
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/218105
推荐阅读
相关标签
  

闽ICP备14008679号