视频演示 https://v.douyin.com/6pkAjPMHCTM/NodeRed工作流[{id: d500fe07916bb488,type: tab,label: 02-温度传感器控制空调,disabled: false,info: ,env: []},{id: 7005f0477b947b5a,type: modbus-read,z: d500fe07916bb488,name: 读取温度传感器,topic: ,showStatusActivities: false,logIOActivities: false,showErrors: false,showWarnings: true,unitid: 1,dataType: HoldingRegister,adr: 600,quantity: 4,rate: 2,rateUnit: s,delayOnStart: false,enableDeformedMessages: false,startDelayTime: ,server: 9e84b83cd9fb6aea,useIOFile: false,ioFile: ,useIOForPayload: false,emptyMsgOnFail: false,x: 150,y: 60,wires: [[7499a01364a8131a],[]]},{id: cc476978e7fbc83b,type: debug,z: d500fe07916bb488,name: debug 1,active: true,tosidebar: true,console: false,tostatus: false,complete: false,statusVal: ,statusType: auto,x: 560,y: 60,wires: []},{id: 7499a01364a8131a,type: function,z: d500fe07916bb488,name: 解析温度值,func: // 获取Modbus返回的4个寄存器值\nconst regs msg.payload;\nif (regs.length 4) {\n msg.error \读取寄存器数量不足请检查接线和配置\;\n return msg;\n}\n\n// 解析温度CDAB格式高字在后低字在前\n// 对应说明书温度数据 第2个寄存器 第1个寄存器拼成32位大端浮点数\nconst tempRaw (regs[1] 16) | regs[0];\nconst tempBuf Buffer.alloc(4);\ntempBuf.writeUInt32BE(tempRaw, 0);\nconst temperature tempBuf.readFloatBE(0);\n\n// 解析湿度同温度格式\nconst humRaw (regs[3] 16) | regs[2];\nconst humBuf Buffer.alloc(4);\nhumBuf.writeUInt32BE(humRaw, 0);\nconst humidity humBuf.readFloatBE(0);\n\n// 保留2位小数输出结构化数据\nmsg.payload {\n temperature: Number(temperature.toFixed(2)),\n humidity: Number(humidity.toFixed(2)),\n unit: {\n temperature: \℃\,\n humidity: \%RH\\n }\n};\nmsg.topic \博为温湿度传感器数据\;\nreturn msg;,outputs: 1,timeout: 0,noerr: 0,initialize: ,finalize: ,libs: [],x: 350,y: 60,wires: [[cc476978e7fbc83b,29ba4ec7c3184af3]]},{id: 29ba4ec7c3184af3,type: switch,z: d500fe07916bb488,name: 温度判断,property: payload.temperature,propertyType: msg,rules: [{t: gte,v: 33,vt: str},{t: lte,v: 32,vt: str}],checkall: true,repair: false,outputs: 2,x: 160,y: 120,wires: [[32e7890342b79c9e],[1462adf5b582b3a8]]},{id: 32e7890342b79c9e,type: function,z: d500fe07916bb488,name: 开灯红外码,func: // airconStatus空调状态为关false时才进入if中去开空调\nif(!flow.get(\airconStatus\)) {\n // 把HEX字符串转串口需要的Buffer\n msg.payload Buffer.from(\6863000022DC08B20446D5014347454745464646464645464646464646D50143D50143D50143D50143D50143D50143D50143D5014446464646464646464943494346464646D50144D50144D50144D50143D50144D40144D40144D026E50895024B1B16\, \hex\);\n flow.set(\airconStatus\, true); // 将空调状态设置为开\n}\nreturn msg;,outputs: 1,timeout: 0,noerr: 0,initialize: ,finalize: ,libs: [],x: 400,y: 200,wires: [[eb6016fcede26454]]},{id: 1462adf5b582b3a8,type: function,z: d500fe07916bb488,name: 关灯红外码,func: // airconStatus空调状态为开true时才进入if中去关空调\nif (flow.get(\airconStatus\)) {\n // 把HEX字符串转串口需要的Buffer\n msg.payload Buffer.from(\6863000022DE08B50443D5014347454745474547454745474547454745D50143D50143D50143D60143D60143D60143D501424745D60143474547454745474547454745D501434745D50143D50143D50142D60142D50143D50143D326DD089902471616\, \hex\);\n flow.set(\airconStatus\, false); // 将空调状态设置为关\n}\nreturn msg;,outputs: 1,timeout: 0,noerr: 0,initialize: ,finalize: ,libs: [],x: 400,y: 320,wires: [[eb6016fcede26454]]},{id: eb6016fcede26454,type: serial out,z: d500fe07916bb488,name: COM7,serial: b0d421786c2f6e99,x: 590,y: 260,wires: []},{id: 8c51d683caf368e5,type: inject,z: d500fe07916bb488,name: 测试开灯,props: [{p: payload},{p: topic,vt: str}],repeat: ,crontab: ,once: false,onceDelay: 0.1,topic: ,payload: ,payloadType: date,x: 140,y: 200,wires: [[32e7890342b79c9e,535eb5a292ee28a1]]},{id: 888f378ebefbf5ed,type: inject,z: d500fe07916bb488,name: 测试关灯,props: [{p: payload},{p: topic,vt: str}],repeat: ,crontab: ,once: false,onceDelay: 0.1,topic: ,payload: ,payloadType: date,x: 140,y: 320,wires: [[1462adf5b582b3a8,6b821f2e41bd0f88]]},{id: 535eb5a292ee28a1,type: function,z: d500fe07916bb488,name: 设置状态为false,func: flow.set(\airconStatus\,false);\nreturn msg;,outputs: 1,timeout: 0,noerr: 0,initialize: ,finalize: ,libs: [],x: 180,y: 260,wires: [[]]},{id: 6b821f2e41bd0f88,type: function,z: d500fe07916bb488,name: 设置状态为true,func: flow.set(\airconStatus\,true);\nreturn msg;,outputs: 1,timeout: 0,noerr: 0,initialize: ,finalize: ,libs: [],x: 180,y: 380,wires: [[]]},{id: 9e84b83cd9fb6aea,type: modbus-client,name: COM28,clienttype: simpleser,bufferCommands: true,stateLogEnabled: false,queueLogEnabled: false,failureLogEnabled: true,tcpHost: 127.0.0.1,tcpPort: 502,tcpType: DEFAULT,serialPort: COM28,serialType: RTU-BUFFERD,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,showErrors: false,showWarnings: true,showLogs: true},{id: b0d421786c2f6e99,type: serial-port,name: COM7,serialport: COM7,serialbaud: 115200,databits: 8,parity: none,stopbits: 1,waitfor: ,dtr: none,rts: none,cts: none,dsr: none,newline: 10,bin: bin,out: interbyte,addchar: ,responsetimeout: 10000},{id: cd33721e7c96b796,type: global-config,env: [],modules: {node-red-contrib-modbus: 5.45.2,node-red-node-serialport: 2.0.3}}]