要求:
PC通过网线采集S7-200SMART数据,通过PC端部署的node-red,与云服务器部署的node-red通过MQTT通信,并将数据在本地或外网界面显示。
准备:
node.js
阿里云服务器
PC
参考:Node-red学习第8篇--关于function节点的详细介绍_Enl0ve的博客-CSDN博客_function node
JSON对象使用变量作为键名_lihefei_coder的博客-CSDN博客_json 变量
1-阿里云服务器端部署的node-red
[
{
"id": "c86dbba29484c7bc",
"type": "tab",
"label": "MQTTDATA",
"disabled": false,
"info": ""
},
{
"id": "16dc231f756b5cd4",
"type": "comment",
"z": "c86dbba29484c7bc",
"name": "接收运行情况",
"info": "",
"x": 570,
"y": 540,
"wires": []
},
{
"id": "d29fed4a8ed88cab",
"type": "comment",
"z": "c86dbba29484c7bc",
"name": "远程数据下传",
"info": "",
"x": 570,
"y": 400,
"wires": []
},
{
"id": "507781c8ee76c4f7",
"type": "function",
"z": "c86dbba29484c7bc",
"name": "",
"func": "var msg1 = {payload:msg.payload.U_dCpos};\nvar msg2 = {payload:msg.payload.U_dCspd};\nreturn [msg1,msg2];",
"outputs": 2,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 870,
"y": 780,
"wires": [
[
"45ddec103b3c0908",
"0425ae561a44dd72"
],
[
"b1a321e17188aad5"
]
]
},
{
"id": "a47efa30939c8969",
"type": "json",
"z": "c86dbba29484c7bc",
"name": "",
"property": "payload",
"action": "obj",
"pretty": false,
"x": 710,
"y": 720,
"wires": [
[
"507781c8ee76c4f7"
]
]
},
{
"id": "345847283f4f5b2f",
"type": "mqtt in",
"z": "c86dbba29484c7bc",
"name": "",
"topic": "CYQplc",
"qos": "0",
"datatype": "json",
"broker": "bf5fe1b1f396077f",
"nl": false,
"rap": true,
"rh": 0,
"x": 550,
"y": 720,
"wires": [
[
"a47efa30939c8969"
]
]
},
{
"id": "865106b81d4e628c",
"type": "mqtt out",
"z": "c86dbba29484c7bc",
"name": "",
"topic": "YunData",
"qos": "0",
"retain": "",
"respTopic": "",
"contentType": "",
"userProps": "",
"correl": "",
"expiry": "",
"broker": "bf5fe1b1f396077f",
"x": 720,
"y": 460,
"wires": []
},
{
"id": "45ddec103b3c0908",
"type": "ui_text",
"z": "c86dbba29484c7bc",
"group": "abc33c008a0186bd",
"order": 3,
"width": 0,
"height": 0,
"name": "",
"label": "位置1",
"format": "{
{msg.payload}}",
"layout": "row-spread",
"className": "",
"x": 1010,
"y": 760,
"wires": []
},
{
"id": "b1a321e17188aad5",
"type": "ui_text",
"z": "c86dbba29484c7bc",
"group": "abc33c008a0186bd",
"order": 2,
"width": 0,
"height": 0,
"name": "",
"label": "速度1",
"format": "{
{msg.payload}}",
"layout": "row-spread",
"className": "",
"x": 1010,
"y": 880,
"wires": []
},
{
"id": "0425ae561a44dd72",
"type": "debug",
"z": "c86dbba29484c7bc",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 1110,
"y": 820,
"wires": []
},
{
"id": "bf5fe1b1f396077f",
"type": "mqtt-broker",
"name": "wgj",
"broker": "121.196.179.60",
"port": "61613",
"clientid": "",
"usetls": false,
"protocolVersion": "4",
"keepalive": "60",
"cleansession": true,
"birthTopic": "",
"birthQos": "0",
"birthPayload": "",
"birthMsg": {},
"closeTopic": "",
"closeQos": "0",
"closePayload": "",
"closeMsg": {},
"willTopic": "",
"willQos": "0",
"willPayload": "",
"willMsg": {},
"sessionExpiry": ""
},
{
"id": "abc33c008a0186bd",
"type": "ui_group",
"name": "运行状态",
"tab": "6dd062cb1b09a3af",
"order": 1,
"disp": true,
"width": "6",
"collapse": false,
"className": ""
},
{
"id": "6dd062cb1b09a3af",
"type": "ui_tab",
"name": "调试板",
"icon": "dashboard",
"order": 1,
"disabled": false,
"hidden": false
}
]
2- PC端部署的NODE-RED
[
{
"id": "8ee22d9371723256",
"type": "tab",
"label": "DAQ",
"disabled": false,
"info": ""
},
{
"id": "e5d12a0e3ab9826c",
"type": "s7 control",
"z": "8ee22d9371723256",
"endpoint": "bea9ddf4f56cfeb2",
"function": "trigger",
"name": "",
"x": 370,
"y": 200,
"wires": [
[]
]
},
{
"id": "a443e97a2c9c3993",
"type": "ui_switch",
"z": "8ee22d9371723256",
"name": "",
"label": "正转",
"tooltip": "",
"group": "4fe48dc8aa7b2968",
"order": 1,
"width": 0,
"height": 0,
"passthru": true,
"decouple": "false",
"topic": "topic",
"topicType": "msg",
"style": "",
"onvalue": "true",
"onvalueType": "bool",
"onicon": "",
"oncolor": "",
"offvalue": "false",
"offvalueType": "bool",
"officon": "",
"offcolor": "",
"animate": false,
"className": "",
"x": 330,
"y": 520,
"wires": [
[
"8cc61f1c7c44a1c4"
]
]
},
{
"id": "8cc61f1c7c44a1c4",
"type": "s7 out",
"z": "8ee22d9371723256",
"endpoint": "bea9ddf4f56cfeb2",
"variable": "U_bJogP",
"name": "",
"x": 500,
"y": 520,
"wires": []
},
{
"id": "0b4738a5a6c94e87",
"type": "ui_switch",
"z": "8ee22d9371723256",
"name": "",
"label": "反转",
"tooltip": "",
"group": "4fe48dc8aa7b2968",
"order": 2,
"width": 0,
"height": 0,
"passthru": true,
"decouple": "false",
"topic": "topic",
"topicType": "msg",
"style": "",
"onvalue": "true",
"onvalueType": "bool",
"onicon": "",
"oncolor": "",
"offvalue": "false",
"offvalueType": "bool",
"officon": "",
"offcolor": "",
"animate": false,
"className": "",
"x": 330,
"y": 560,
"wires": [
[
"4b2296b1539f27ba"
]
]
},
{
"id": "4b2296b1539f27ba",
"type": "s7 out",
"z": "8ee22d9371723256",
"endpoint": "bea9ddf4f56cfeb2",
"variable": "U_bJogN",
"name": "",
"x": 500,
"y": 560,
"wires": []
},
{
"id": "ab2d6e52d06b2357",
"type": "s7 out",
"z": "8ee22d9371723256",
"endpoint": "bea9ddf4f56cfeb2",
"variable": "U_dGpos",
"name": "",
"x": 500,
"y": 720,
"wires": []
},
{
"id": "35a55fdaf9ec917a",
"type": "s7 out",
"z": "8ee22d9371723256",
"endpoint": "bea9ddf4f56cfeb2",
"variable": "U_dGspd",
"name": "",
"x": 500,
"y": 760,
"wires": []
},
{
"id": "518287261f915fcc",
"type": "comment",
"z": "8ee22d9371723256",
"name": "点动",
"info": "",
"x": 330,
"y": 480,
"wires": []
},
{
"id": "9a3bf0bee2ba6cc1",
"type": "comment",
"z": "8ee22d9371723256",
"name": "定位",
"info": "",
"x": 330,
"y": 680,
"wires": []
},
{
"id": "bcb2e21202d51493",
"type": "trigger",
"z": "8ee22d9371723256",
"name": "",
"op1": "",
"op2": "false",
"op1type": "pay",
"op2type": "bool",
"duration": "100",
"extend": false,
"overrideDelay": false,
"units": "ms",
"reset": "",
"bytopic": "all",
"topic": "topic",
"outputs": 1,
"x": 490,
"y": 800,
"wires": [
[
"8b29a9ee3d120e4a"
]
]
},
{
"id": "c0651f0965ba2668",
"type": "ui_button",
"z": "8ee22d9371723256",
"name": "",
"group": "625ff6e7b6bfef8b",
"order": 3,
"width": 0,
"height": 0,
"passthru": false,
"label": "定位触发",
"tooltip": "",
"color": "",
"bgcolor": "",
"className": "",
"icon": "",
"payload": "",
"payloadType": "date",
"topic": "topic",
"topicType": "msg",
"x": 340,
"y": 800,
"wires": [
[
"bcb2e21202d51493"
]
]
},
{
"id": "8b29a9ee3d120e4a",
"type": "s7 out",
"z": "8ee22d9371723256",
"endpoint": "bea9ddf4f56cfeb2",
"variable": "U_bGoTrig",
"name": "",
"x": 650,
"y": 800,
"wires": []
},
{
"id": "52dba1183273a58c",
"type": "ui_text_input",
"z": "8ee22d9371723256",
"name": "",
"label": "定位量",
"tooltip": "",
"group": "625ff6e7b6bfef8b",
"order": 2,
"width": 0,
"height": 0,
"passthru": true,
"mode": "number",
"delay": 300,
"topic": "topic",
"sendOnBlur": true,
"className": "",
"topicType": "msg",
"x": 330,
"y": 720,
"wires": [
[
"ab2d6e52d06b2357"
]
]
},
{
"id": "5a0e428e8281793f",
"type": "ui_text_input",
"z": "8ee22d9371723256",
"name": "",
"label": "速度",
"tooltip": "",
"group": "625ff6e7b6bfef8b",
"order": 1,
"width": 0,
"height": 0,
"passthru": true,
"mode": "number",
"delay": 300,
"topic": "topic",
"sendOnBlur": true,
"className": "",
"topicType": "msg",
"x": 330,
"y": 760,
"wires": [
[
"35a55fdaf9ec917a"
]
]
},
{
"id": "711bfb7f6645d998",
"type": "ui_text",
"z": "8ee22d9371723256",
"group": "abc33c008a0186bd",
"order": 3,
"width": 0,
"height": 0,
"name": "",
"label": "位置",
"format": "{
{msg.payload}}",
"layout": "row-spread",
"className": "",
"x": 470,
"y": 360,
"wires": []
},
{
"id": "749625cde1564895",
"type": "ui_text",
"z": "8ee22d9371723256",
"group": "abc33c008a0186bd",
"order": 2,
"width": 0,
"height": 0,
"name": "",
"label": "速度",
"format": "{
{msg.payload}}",
"layout": "row-spread",
"className": "",
"x": 470,
"y": 400,
"wires": []
},
{
"id": "896b6744e91bbf7e",
"type": "comment",
"z": "8ee22d9371723256",
"name": "运动状态",
"info": "",
"x": 340,
"y": 280,
"wires": []
},
{
"id": "411fdb23bca345f8",
"type": "s7 in",
"z": "8ee22d9371723256",
"endpoint": "bea9ddf4f56cfeb2",
"mode": "single",
"variable": "U_dCpos",
"diff": true,
"name": "Cpos",
"x": 330,
"y": 360,
"wires": [
[
"711bfb7f6645d998"
]
]
},
{
"id": "17a5a37a3b402ca3",
"type": "s7 in",
"z": "8ee22d9371723256",
"endpoint": "bea9ddf4f56cfeb2",
"mode": "single",
"variable": "U_dCspd",
"diff": true,
"name": "Cspd",
"x": 330,
"y": 400,
"wires": [
[
"749625cde1564895"
]
]
},
{
"id": "13522b11251c74eb",
"type": "s7 in",
"z": "8ee22d9371723256",
"endpoint": "bea9ddf4f56cfeb2",
"mode": "single",
"variable": "U_bCrunning",
"diff": true,
"name": "noRun",
"x": 330,
"y": 320,
"wires": [
[
"bdc32f6ccff802bb"
]
]
},
{
"id": "f27e6a8eb9277043",
"type": "ui_button",
"z": "8ee22d9371723256",
"name": "",
"group": "4fe48dc8aa7b2968",
"order": 3,
"width": 0,
"height": 0,
"passthru": false,
"label": "位置标零",
"tooltip": "",
"color": "",
"bgcolor": "",
"className": "",
"icon": "",
"payload": "",
"payloadType": "date",
"topic": "topic",
"topicType": "msg",
"x": 340,
"y": 600,
"wires": [
[
"31a3c24c41ccb1cb"
]
]
},
{
"id": "31a3c24c41ccb1cb",
"type": "trigger",
"z": "8ee22d9371723256",
"name": "",
"op1": "",
"op2": "false",
"op1type": "pay",
"op2type": "bool",
"duration": "100",
"extend": false,
"overrideDelay": false,
"units": "ms",
"reset": "",
"bytopic": "all",
"topic": "topic",
"outputs": 1,
"x": 490,
"y": 600,
"wires": [
[
"5a221991d211ae69"
]
]
},
{
"id": "bdc32f6ccff802bb",
"type": "ui_led",
"z": "8ee22d9371723256",
"order": 1,
"group": "abc33c008a0186bd",
"width": 0,
"height": 0,
"label": "状态",
"labelPlacement": "right",
"labelAlignment": "left",
"colorForValue": [
{
"color": "#f8f7f7",
"value": "false",
"valueType": "bool"
},
{
"color": "#008000",
"value": "true",
"valueType": "bool"
}
],
"allowColorForValueInMessage": false,
"shape": "circle",
"showGlow": true,
"name": "",
"x": 470,
"y": 320,
"wires": []
},
{
"id": "5a221991d211ae69",
"type": "s7 out",
"z": "8ee22d9371723256",
"endpoint": "bea9ddf4f56cfeb2",
"variable": "U_bClsPos",
"name": "",
"x": 650,
"y": 600,
"wires": []
},
{
"id": "6805b1b652c577ff",
"type": "ui_spacer",
"z": "8ee22d9371723256",
"name": "spacer",
"group": "bcdd867b59a3eac4",
"order": 2,
"width": 1,
"height": 1
},
{
"id": "fca3849530564772",
"type": "ui_spacer",
"z": "8ee22d9371723256",
"name": "spacer",
"group": "83791b88bc9ededf",
"order": 5,
"width": 1,
"height": 1
},
{
"id": "bea9ddf4f56cfeb2",
"type": "s7 endpoint",
"transport": "iso-on-tcp",
"address": "192.168.0.20",
"port": "102",
"rack": "0",
"slot": "2",
"localtsaphi": "10",
"localtsaplo": "02",
"remotetsaphi": "03",
"remotetsaplo": "01",
"connmode": "tsap",
"adapter": "",
"busaddr": "2",
"cycletime": "500",
"timeout": "1500",
"name": "ST20",
"vartable": [
{
"addr": "I0.0",
"name": "I00"
},
{
"addr": "I0.1",
"name": "I01"
},
{
"addr": "I0.2",
"name": "I02"
},
{
"addr": "I0.3",
"name": "I03"
},
{
"addr": "I0.4",
"name": "I04"
},
{
"addr": "I0.5",
"name": "I05"
},
{
"addr": "I0.6",
"name": "I06"
},
{
"addr": "I0.7",
"name": "I07"
},
{
"addr": "I1.0",
"name": "I10"
},
{
"addr": "I1.1",
"name": "I11"
},
{
"addr": "I1.2",
"name": "I12"
},
{
"addr": "I1.3",
"name": "I13"
},
{
"addr": "Q0.0",
"name": "Q00"
},
{
"addr": "Q0.1",
"name": "Q01"
},
{
"addr": "Q0.2",
"name": "Q02"
},
{
"addr": "Q0.3",
"name": "Q03"
},
{
"addr": "Q0.4",
"name": "Q04"
},
{
"addr": "Q0.5",
"name": "Q05"
},
{
"addr": "Q0.6",
"name": "Q06"
},
{
"addr": "Q0.7",
"name": "Q07"
},
{
"addr": "DB1,X1200.0",
"name": "U_bCrunning"
},
{
"addr": "DB1,X1200.1",
"name": "U_bJogP"
},
{
"addr": "DB1,X1200.2",
"name": "U_bJogN"
},
{
"addr": "DB1,X1200.3",
"name": "U_bClsPos"
},
{
"addr": "DB1,X1200.4",
"name": "U_bGoTrig"
},
{
"addr": "DB1,DI1300",
"name": "U_dCpos"
},
{
"addr": "DB1,DI1304",
"name": "U_dCspd"
},
{
"addr": "DB1,DI1308",
"name": "U_dMspd"
},
{
"addr": "DB1,DI1312",
"name": "U_dGspd"
},
{
"addr": "DB1,DI1316",
"name": "U_dGpos"
}
]
},
{
"id": "4fe48dc8aa7b2968",
"type": "ui_group",
"name": "点动操作",
"tab": "6dd062cb1b09a3af",
"order": 3,
"disp": true,
"width": "6",
"collapse": false,
"className": ""
},
{
"id": "625ff6e7b6bfef8b",
"type": "ui_group",
"name": "定位操作",
"tab": "6dd062cb1b09a3af",
"order": 5,
"disp": true,
"width": "6",
"collapse": false,
"className": ""
},
{
"id": "abc33c008a0186bd",
"type": "ui_group",
"name": "运行状态",
"tab": "6dd062cb1b09a3af",
"order": 1,
"disp": true,
"width": "6",
"collapse": false,
"className": ""
},
{
"id": "bcdd867b59a3eac4",
"type": "ui_group",
"name": "I",
"tab": "6dd062cb1b09a3af",
"order": 2,
"disp": false,
"width": 2,
"collapse": false,
"className": ""
},
{
"id": "83791b88bc9ededf",
"type": "ui_group",
"name": "II",
"tab": "6dd062cb1b09a3af",
"order": 4,
"disp": false,
"width": 2,
"collapse": false,
"className": ""
},
{
"id": "6dd062cb1b09a3af",
"type": "ui_tab",
"name": "调试板",
"icon": "dashboard",
"disabled": false,
"hidden": false
}
]
[
{
"id": "98a4d6e78c673268",
"type": "tab",
"label": "DataMQTT",
"disabled": false,
"info": ""
},
{
"id": "9a920a29961d0d56",
"type": "mqtt out",
"z": "98a4d6e78c673268",
"name": "",
"topic": "CYQplc",
"qos": "0",
"retain": "",
"respTopic": "",
"contentType": "",
"userProps": "",
"correl": "",
"expiry": "",
"broker": "e47e3b729550a5cc",
"x": 560,
"y": 560,
"wires": []
},
{
"id": "0d7a9216295512b6",
"type": "comment",
"z": "98a4d6e78c673268",
"name": "运行情况上传",
"info": "",
"x": 390,
"y": 520,
"wires": []
},
{
"id": "708a0dc7eaa9e784",
"type": "s7 in",
"z": "98a4d6e78c673268",
"endpoint": "bea9ddf4f56cfeb2",
"mode": "all",
"variable": "U_bCrunning",
"diff": true,
"name": "Data_to_Yun",
"x": 390,
"y": 560,
"wires": [
[
"9a920a29961d0d56"
]
]
},
{
"id": "80c418a72e186ea4",
"type": "mqtt in",
"z": "98a4d6e78c673268",
"name": "",
"topic": "YunData",
"qos": "0",
"datatype": "json",
"broker": "e47e3b729550a5cc",
"nl": false,
"rap": true,
"rh": 0,
"x": 380,
"y": 680,
"wires": [
[
"0a64c16a1581aa83"
]
]
},
{
"id": "ce1f04d2f6b341e1",
"type": "comment",
"z": "98a4d6e78c673268",
"name": "远程数据下传",
"info": "",
"x": 390,
"y": 640,
"wires": []
},
{
"id": "c23a9be21666b22b",
"type": "function",
"z": "98a4d6e78c673268",
"name": "",
"func": "\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 690,
"y": 680,
"wires": [
[]
]
},
{
"id": "0a64c16a1581aa83",
"type": "json",
"z": "98a4d6e78c673268",
"name": "",
"property": "payload",
"action": "",
"pretty": false,
"x": 550,
"y": 680,
"wires": [
[
"c23a9be21666b22b"
]
]
},
{
"id": "e47e3b729550a5cc",
"type": "mqtt-broker",
"name": "wgj",
"broker": "121.196.179.60",
"port": "61613",
"clientid": "",
"usetls": false,
"protocolVersion": "4",
"keepalive": "60",
"cleansession": true,
"birthTopic": "",
"birthQos": "0",
"birthPayload": "",
"birthMsg": {},
"closeTopic": "",
"closeQos": "0",
"closePayload": "",
"closeMsg": {},
"willTopic": "",
"willQos": "0",
"willPayload": "",
"willMsg": {},
"sessionExpiry": ""
},
{
"id": "bea9ddf4f56cfeb2",
"type": "s7 endpoint",
"transport": "iso-on-tcp",
"address": "192.168.0.20",
"port": "102",
"rack": "0",
"slot": "2",
"localtsaphi": "10",
"localtsaplo": "02",
"remotetsaphi": "03",
"remotetsaplo": "01",
"connmode": "tsap",
"adapter": "",
"busaddr": "2",
"cycletime": "500",
"timeout": "1500",
"name": "ST20",
"vartable": [
{
"addr": "I0.0",
"name": "I00"
},
{
"addr": "I0.1",
"name": "I01"
},
{
"addr": "I0.2",
"name": "I02"
},
{
"addr": "I0.3",
"name": "I03"
},
{
"addr": "I0.4",
"name": "I04"
},
{
"addr": "I0.5",
"name": "I05"
},
{
"addr": "I0.6",
"name": "I06"
},
{
"addr": "I0.7",
"name": "I07"
},
{
"addr": "I1.0",
"name": "I10"
},
{
"addr": "I1.1",
"name": "I11"
},
{
"addr": "I1.2",
"name": "I12"
},
{
"addr": "I1.3",
"name": "I13"
},
{
"addr": "Q0.0",
"name": "Q00"
},
{
"addr": "Q0.1",
"name": "Q01"
},
{
"addr": "Q0.2",
"name": "Q02"
},
{
"addr": "Q0.3",
"name": "Q03"
},
{
"addr": "Q0.4",
"name": "Q04"
},
{
"addr": "Q0.5",
"name": "Q05"
},
{
"addr": "Q0.6",
"name": "Q06"
},
{
"addr": "Q0.7",
"name": "Q07"
},
{
"addr": "DB1,X1200.0",
"name": "U_bCrunning"
},
{
"addr": "DB1,X1200.1",
"name": "U_bJogP"
},
{
"addr": "DB1,X1200.2",
"name": "U_bJogN"
},
{
"addr": "DB1,X1200.3",
"name": "U_bClsPos"
},
{
"addr": "DB1,X1200.4",
"name": "U_bGoTrig"
},
{
"addr": "DB1,DI1300",
"name": "U_dCpos"
},
{
"addr": "DB1,DI1304",
"name": "U_dCspd"
},
{
"addr": "DB1,DI1308",
"name": "U_dMspd"
},
{
"addr": "DB1,DI1312",
"name": "U_dGspd"
},
{
"addr": "DB1,DI1316",
"name": "U_dGpos"
}
]
}
]