文章目录
- 安装及配置
- 下载安装Node-RED
- Node-RED下载节点
- 实验
- 1. 串口显数
- 2. MQTT
- JSON源码
- 1. 串口显示实验
- 2. MQTT实验
安装及配置
下载安装Node-RED
下载安装Node.js(并配置环境变量)后,且node安装版本需要在v14.17.2.0以上,npm版本安装版本在v6.14.13以上。
- 设置npm默认安装依赖位置
npm set cache H:\nodejs\node_cache
npm set prefix H:\nodejs\node_global
- 换源
npm config set registry https://registry.npm.taobao.org
- Node.js安装Node-RED
npm install -g --unsafe-perm node-red
- 或是Docker安装Node-RED
docker run -it -p 1880:1880 --name mynodered -d nodered/node-red
- 启动
node-red
Node-RED下载节点
打开浏览器访问localhost:1880,点击右上方,在节点管理>>控制板>>安装中即可下载以下组件:
- node-red-dashboard,用于部署后形成控制面板(访问localhost:1880/ui);
- node-red-node-serialport,添加串口组件。
实验
1. 串口显数
- 收集Zigbee和WiFi网关数据。
- 左为Zigbee网关接入串口,右为WiFi网关接入串口。
- 流程并部署
- 面板效果
- 硬件连接图
(需按硬件实际情况,传输的数据格式,修改JSON中的自定义函数)
2. MQTT
- 首先需要有本地的MQTT Broker
- 下载安装EMQX
https://www.emqx.com/zh/try?product=broker - 或是用Docker安装
docker run -d --name emqx -p 1883:1883 -p 8081:8081 -p 8083:8083 -p 8084:8084 -p 8883:8883 -p 18083:18083 emqx/emqx:4.3.11
- 启动
emqx start
- 访问localhost:18083
- Node-RED设置及流程
- 设置节点
- 流程并部署
- 结果
关闭EMQX
emqx stop
JSON源码
1. 串口显示实验
[
{
"id": "a271f746b8b78baf",
"type": "tab",
"label": "串口显数流程",
"disabled": false,
"info": ""
},
{
"id": "23256dc00e20c5e6",
"type": "debug",
"z": "a271f746b8b78baf",
"name": "测试打印",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 340,
"y": 260,
"wires": []
},
{
"id": "8e429633a17d9fc4",
"type": "serial in",
"z": "a271f746b8b78baf",
"name": "Zigbee串口COM7输入",
"serial": "854e53dd8b178f91",
"x": 120,
"y": 140,
"wires": [
[
"e399fd2c31a733ad"
]
]
},
{
"id": "77dba539c59a386f",
"type": "ui_text",
"z": "a271f746b8b78baf",
"group": "edb315d0aa0404ce",
"order": 0,
"width": 0,
"height": 0,
"name": "",
"label": "超声波传感器",
"format": "{{msg.payload}}",
"layout": "row-spread",
"className": "",
"x": 1100,
"y": 80,
"wires": []
},
{
"id": "04165a40f0638664",
"type": "switch",
"z": "a271f746b8b78baf",
"name": "WiFi节点判断",
"property": "topic",
"propertyType": "msg",
"rules": [
{
"t": "eq",
"v": "三轴",
"vt": "str"
},
{
"t": "eq",
"v": "震动",
"vt": "str"
},
{
"t": "else"
}
],
"checkall": "false",
"repair": false,
"outputs": 3,
"x": 660,
"y": 380,
"wires": [
[
"93cce256dae79ec0"
],
[
"2714bf9cf36752b8"
],
[
"b26f54754a4e36ef"
]
]
},
{
"id": "6594423759956339",
"type": "ui_text",
"z": "a271f746b8b78baf",
"group": "edb315d0aa0404ce",
"order": 0,
"width": 0,
"height": 0,
"name": "",
"label": "烟雾传感器",
"format": "{{msg.payload}}",
"layout": "row-spread",
"className": "",
"x": 1090,
"y": 160,
"wires": []
},
{
"id": "4536daee149e96d2",
"type": "ui_text",
"z": "a271f746b8b78baf",
"group": "edb315d0aa0404ce",
"order": 0,
"width": 0,
"height": 0,
"name": "",
"label": "其它Zigbee节点",
"format": "{{msg.payload}}",
"layout": "row-spread",
"className": "",
"x": 860,
"y": 200,
"wires": []
},
{
"id": "07acf3dda54fa7e2",
"type": "ui_text",
"z": "a271f746b8b78baf",
"group": "2c4daaf625693205",
"order": 4,
"width": 0,
"height": 0,
"name": "",
"label": "三轴传感器",
"format": "{{msg.payload}}",
"layout": "row-spread",
"className": "",
"x": 1130,
"y": 320,
"wires": []
},
{
"id": "27e9f9f58a22f1f6",
"type": "ui_text",
"z": "a271f746b8b78baf",
"group": "2c4daaf625693205",
"order": 4,
"width": 0,
"height": 0,
"name": "",
"label": "震动传感器",
"format": "{{msg.payload}}",
"layout": "row-spread",
"className": "",
"x": 1090,
"y": 420,
"wires": []
},
{
"id": "b26f54754a4e36ef",
"type": "ui_text",
"z": "a271f746b8b78baf",
"group": "2c4daaf625693205",
"order": 4,
"width": 0,
"height": 0,
"name": "",
"label": "其它WiFi节点",
"format": "{{msg.payload}}",
"layout": "row-spread",
"className": "",
"x": 820,
"y": 460,
"wires": []
},
{
"id": "f047fd0de0c8644c",
"type": "ui_chart",
"z": "a271f746b8b78baf",
"name": "",
"group": "edb315d0aa0404ce",
"order": 3,
"width": 0,
"height": 0,
"label": "超声波传感器监视",
"chartType": "bar",
"legend": "false",
"xformat": "HH:mm:ss",
"interpolate": "linear",
"nodata": "",
"dot": false,
"ymin": "0",
"ymax": "5",
"removeOlder": "5",
"removeOlderPoints": "",
"removeOlderUnit": "1",
"cutout": 0,
"useOneColor": false,
"useUTC": false,
"colors": [
"#1f77b4",
"#aec7e8",
"#ff7f0e",
"#2ca02c",
"#98df8a",
"#d62728",
"#ff9896",
"#9467bd",
"#c5b0d5"
],
"outputs": 1,
"useDifferentColor": false,
"className": "",
"x": 1110,
"y": 40,
"wires": [
[]
]
},
{
"id": "909bafbb628444e1",
"type": "ui_chart",
"z": "a271f746b8b78baf",
"name": "",
"group": "2c4daaf625693205",
"order": 3,
"width": 0,
"height": 0,
"label": "震动传感器监视",
"chartType": "line",
"legend": "false",
"xformat": "HH:mm:ss",
"interpolate": "linear",
"nodata": "",
"dot": false,
"ymin": "-1",
"ymax": "12",
"removeOlder": "5",
"removeOlderPoints": "",
"removeOlderUnit": "1",
"cutout": 0,
"useOneColor": false,
"useUTC": false,
"colors": [
"#1f77b4",
"#aec7e8",
"#ff7f0e",
"#2ca02c",
"#98df8a",
"#d62728",
"#ff9896",
"#9467bd",
"#c5b0d5"
],
"outputs": 1,
"useDifferentColor": false,
"className": "",
"x": 1100,
"y": 380,
"wires": [
[]
]
},
{
"id": "ccf9848aae2ae4ce",
"type": "function",
"z": "a271f746b8b78baf",
"name": "处理超声波传感器数据",
"func": "var node = msg.payload\nvar shortAddr = node[0]\nreturn [\n {topic: shortAddr, payload: node[1]},\n {topic: shortAddr, payload: \"\"+ (node[1] / 100) +\" m\"}\n]",
"outputs": 2,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 880,
"y": 80,
"wires": [
[
"f047fd0de0c8644c"
],
[
"77dba539c59a386f"
]
]
},
{
"id": "9de3a936b607a633",
"type": "serial in",
"z": "a271f746b8b78baf",
"name": "WiFi串口COM8输入",
"serial": "804b073ba5bac0a9",
"x": 130,
"y": 380,
"wires": [
[
"5598f41421735b24"
]
]
},
{
"id": "5598f41421735b24",
"type": "function",
"z": "a271f746b8b78baf",
"name": "处理串口数据函数并判读节点类型",
"func": "var frame = msg.payload\n// 端节点地址\nvar endDeviceAddr = frame[3]+frame[4]\n// 传感器类型\nvar sensorNo = frame[5]\nvar sensor = [\"温湿度\", \"人体红外\", \"光照\", \"震动\", \n \"烟雾\", \"酒精\", \"超声波\", \"三轴\",\n \"水流量\", \"雨滴\", \"门磁\"]\n// 数据\nvar data = [frame[6], frame[7], frame[8]]\nreturn {topic: sensor[sensorNo], payload: [endDeviceAddr, data]}",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 400,
"y": 380,
"wires": [
[
"04165a40f0638664"
]
]
},
{
"id": "93cce256dae79ec0",
"type": "function",
"z": "a271f746b8b78baf",
"name": "处理三轴传感器数据",
"func": "var node = msg.payload\n// 节点短地址\nvar endDevice = node[0]\n// 三轴值\nvar x = node[1][0]\nvar y = node[1][1]\nvar z = node[1][2]\nvar data = \"x: \" + x +\",y: \" + y + \",z: \" + z\n// 三轴x值\nreturn [\n {topic: \"x\", payload: x},\n {topic: \"y\", payload: y},\n {topic: \"z\", payload: z},\n {topic: \"data\", payload: data}\n]\n",
"outputs": 4,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 880,
"y": 320,
"wires": [
[
"01017c20c0854b66"
],
[
"01017c20c0854b66"
],
[
"01017c20c0854b66"
],
[
"07acf3dda54fa7e2"
]
]
},
{
"id": "01017c20c0854b66",
"type": "ui_chart",
"z": "a271f746b8b78baf",
"name": "",
"group": "2c4daaf625693205",
"order": 3,
"width": 0,
"height": 0,
"label": "三轴传感器监视",
"chartType": "pie",
"legend": "false",
"xformat": "HH:mm:ss",
"interpolate": "linear",
"nodata": "",
"dot": false,
"ymin": "-20",
"ymax": "50",
"removeOlder": "5",
"removeOlderPoints": "",
"removeOlderUnit": "1",
"cutout": 0,
"useOneColor": false,
"useUTC": false,
"colors": [
"#1f77b4",
"#aec7e8",
"#ff7f0e",
"#2ca02c",
"#98df8a",
"#d62728",
"#ff9896",
"#9467bd",
"#c5b0d5"
],
"outputs": 1,
"useDifferentColor": false,
"className": "",
"x": 1140,
"y": 280,
"wires": [
[]
]
},
{
"id": "2714bf9cf36752b8",
"type": "function",
"z": "a271f746b8b78baf",
"name": "处理震动传感器数据",
"func": "var node = msg.payload\n// 节点短地址\nvar endDeviceAddr = node[0]\n// 震动值\nvar val = node[1][0]\nreturn {topic: endDeviceAddr, payload: val}\n",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 880,
"y": 400,
"wires": [
[
"909bafbb628444e1",
"27e9f9f58a22f1f6"
]
]
},
{
"id": "e399fd2c31a733ad",
"type": "function",
"z": "a271f746b8b78baf",
"name": "处理串口数据函数并判读节点类型",
"func": "var frame = msg.payload\n// 节点短地址\nvar shortAddr = frame[5]+frame[6]\n// 传感器类型\nvar sensorNo = frame[10]\nvar sensor\nif (sensorNo == 0x86)\n{\n // 超声波\n sensor = \"超声波\"\n} \nelse if (sensorNo == 0x84) {\n // 烟雾\n sensor = \"烟雾\"\n}\n// 数据\nvar data = frame[11]\nreturn {topic: sensor, payload: [shortAddr, data]}",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 400,
"y": 140,
"wires": [
[
"41c18eac9586b069"
]
]
},
{
"id": "41c18eac9586b069",
"type": "switch",
"z": "a271f746b8b78baf",
"name": "Zigbee节点判断",
"property": "topic",
"propertyType": "msg",
"rules": [
{
"t": "eq",
"v": "超声波",
"vt": "str"
},
{
"t": "eq",
"v": "烟雾",
"vt": "str"
},
{
"t": "else"
}
],
"checkall": "false",
"repair": false,
"outputs": 3,
"x": 660,
"y": 140,
"wires": [
[
"ccf9848aae2ae4ce"
],
[
"8a390ebd777bfc59"
],
[
"4536daee149e96d2"
]
]
},
{
"id": "8a390ebd777bfc59",
"type": "function",
"z": "a271f746b8b78baf",
"name": "处理烟雾传感器数据",
"func": "var node = msg.payload\nvar shortAddr = node[0]\nvar data = node[1]\nreturn {topic: shortAddr, payload: data}",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 880,
"y": 140,
"wires": [
[
"83c29137012ce704",
"6594423759956339"
]
]
},
{
"id": "83c29137012ce704",
"type": "ui_chart",
"z": "a271f746b8b78baf",
"name": "",
"group": "edb315d0aa0404ce",
"order": 3,
"width": 0,
"height": 0,
"label": "烟雾传感器监视",
"chartType": "line",
"legend": "false",
"xformat": "HH:mm:ss",
"interpolate": "linear",
"nodata": "",
"dot": false,
"ymin": "-20",
"ymax": "50",
"removeOlder": "5",
"removeOlderPoints": "",
"removeOlderUnit": "1",
"cutout": 0,
"useOneColor": false,
"useUTC": false,
"colors": [
"#1f77b4",
"#aec7e8",
"#ff7f0e",
"#2ca02c",
"#98df8a",
"#d62728",
"#ff9896",
"#9467bd",
"#c5b0d5"
],
"outputs": 1,
"useDifferentColor": false,
"className": "",
"x": 1100,
"y": 120,
"wires": [
[]
]
},
{
"id": "854e53dd8b178f91",
"type": "serial-port",
"serialport": "COM7",
"serialbaud": "115200",
"databits": "8",
"parity": "none",
"stopbits": "1",
"waitfor": "",
"dtr": "none",
"rts": "none",
"cts": "none",
"dsr": "none",
"newline": "13",
"bin": "bin",
"out": "count",
"addchar": "",
"responsetimeout": "10000"
},
{
"id": "edb315d0aa0404ce",
"type": "ui_group",
"name": "Zigbee节点数据",
"tab": "9426268fa5f427a2",
"order": 1,
"disp": true,
"width": "6",
"collapse": false,
"className": ""
},
{
"id": "2c4daaf625693205",
"type": "ui_group",
"name": "WiFi节点数据",
"tab": "9426268fa5f427a2",
"order": 2,
"disp": true,
"width": "6",
"collapse": false,
"className": ""
},
{
"id": "804b073ba5bac0a9",
"type": "serial-port",
"serialport": "COM8",
"serialbaud": "9600",
"databits": "8",
"parity": "none",
"stopbits": "1",
"waitfor": "",
"dtr": "none",
"rts": "none",
"cts": "none",
"dsr": "none",
"newline": "10",
"bin": "bin",
"out": "count",
"addchar": "",
"responsetimeout": "10000"
},
{
"id": "9426268fa5f427a2",
"type": "ui_tab",
"name": "数据采集系统",
"icon": "dashboard",
"disabled": false,
"hidden": false
}
]
2. MQTT实验
[
{
"id": "8cfa40f205178e09",
"type": "tab",
"label": "MQTT流程",
"disabled": false,
"info": "",
"env": []
},
{
"id": "786c122f2dca298c",
"type": "inject",
"z": "8cfa40f205178e09",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "test-MQTT",
"payload": "这是一个测试",
"payloadType": "str",
"x": 210,
"y": 120,
"wires": [
[
"3dfcbbceac74d7a9"
]
]
},
{
"id": "3dfcbbceac74d7a9",
"type": "mqtt out",
"z": "8cfa40f205178e09",
"name": "test-Mqtt-publisher",
"topic": "test-MQTT",
"qos": "0",
"retain": "false",
"respTopic": "",
"contentType": "",
"userProps": "",
"correl": "",
"expiry": "",
"broker": "cfa3263c5fc61d26",
"x": 490,
"y": 120,
"wires": []
},
{
"id": "cfa3263c5fc61d26",
"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": {},
"sessionExpiry": ""
}
]