智能家居填坑004 Modbus接入逻辑篇

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了智能家居填坑004 Modbus接入逻辑篇相关的知识,希望对你有一定的参考价值。

背景

自复位开关->modbus DI模块->node-red->modbus DO模块->继电器->设备(灯具或插座)

参照003篇,已经成功接入两个模块。

思路

在nr里定义一个变量用来记录设备状态,所有的操控都是直接操作这个变量,然后用变量去控制相应的设备。这样,无论控制源是开关、语音助手、传感器、场景、人工智能等都转化为对变量的直接操控。变量可以储存为全局变量或者写入数据库。

var switchCFD = {payload:{power:true,lm:100,colortemperature:100}};

var socketTV = {payload:{power:true}};

var fanNewAir = {payload:{power:true,grade:1}};

然后通过写modbus操作统一改变设备状态。

包括接入的米家设备、mqtt设备等都是通过这个思路来更改状态。

演示

整体流预览

1、初始化,定义全局变量,写入初始状态。

2、获取按键状态、改变全局变量

3、预览

https://v.youku.com/v_show/id_XNDU1ODA2NTU4MA==.html

https://v.youku.com/v_show/id_XNDU1ODA2NTU4MA==.html

https://v.youku.com/v_show/id_XNDU1ODA2NTU4MA==.html

4、演示流

[

{

"id": "57b89f19.5a761",

"type": "inject",

"z": "f62cb442.d1aef",

"name": "",

"topic": "",

"payload": "",

"payloadType": "date",

"repeat": "0.2",

"crontab": "",

"once": true,

"onceDelay": "0",

"x": 100,

"y": 360,

"wires": [

[

"e82fb085.3fbf8",

"821476c.4284d88"

]

]

},

{

"id": "5aceaa2f.2006ac",

"type": "inject",

"z": "f62cb442.d1aef",

"name": "初始化",

"topic": "",

"payload": "",

"payloadType": "date",

"repeat": "",

"crontab": "",

"once": true,

"onceDelay": "0",

"x": 100,

"y": 300,

"wires": [

[

"2874b18e.b524ce"

]

]

},

{

"id": "2874b18e.b524ce",

"type": "function",

"z": "f62cb442.d1aef",

"name": "定义状态数组",

"func": "var VswitchCFD = {payload:{power:false,lm:100,colortemperature:100}};\\nvar VsocketTV = {payload:{power:false}};\\nvar VfanNewAir = {payload:{power:false,grade:0}};\\nglobal.set(\'switchCFD\',VswitchCFD);\\nglobal.set(\'socketTV\',VsocketTV);\\nglobal.set(\'fanNewAir\',VfanNewAir);\\n\\nreturn;",

"outputs": 0,

"noerr": 0,

"x": 280,

"y": 300,

"wires": []

},

{

"id": "e82fb085.3fbf8",

"type": "function",

"z": "f62cb442.d1aef",

"name": "Read 0-2 on Unit 1 FC2",

"func": "msg.payload = { value: msg.payload, \'fc\': 2, \'unitid\': 1, \'address\': 0 , \'quantity\': 3 };\\nreturn msg;",

"outputs": 1,

"noerr": 0,

"x": 310,

"y": 420,

"wires": [

[

"5e3cb9b1.efd778"

]

]

},

{

"id": "821476c.4284d88",

"type": "function",

"z": "f62cb442.d1aef",

"name": "Write 0-2 on Unit 1 FC15",

"func": "var Value = [global.get(\'switchCFD\').payload.power,global.get(\'socketTV\').payload.power,global.get(\'fanNewAir\').payload.power];\\nmsg.payload = { value: Value, \'fc\': 15, \'unitid\': 1, \'address\': 0 , \'quantity\': 3 };\\nreturn msg;",

"outputs": 1,

"noerr": 0,

"x": 310,

"y": 360,

"wires": [

[

"2f8d6d5b.7aa85a"

]

]

},

{

"id": "dc4ccbea.224f8",

"type": "rbe",

"z": "f62cb442.d1aef",

"name": "",

"func": "rbe",

"gap": "",

"start": "",

"inout": "out",

"property": "payload",

"x": 710,

"y": 420,

"wires": [

[

"5ad43590.d6765c"

]

]

},

{

"id": "5e3cb9b1.efd778",

"type": "modbus-flex-getter",

"z": "f62cb442.d1aef",

"name": "获取按键状态",

"showStatusActivities": false,

"showErrors": false,

"logIOActivities": false,

"server": "54b233c9.eea30c",

"useIOFile": false,

"ioFile": "",

"useIOForPayload": false,

"x": 540,

"y": 420,

"wires": [

[

"dc4ccbea.224f8"

],

[]

]

},

{

"id": "2f8d6d5b.7aa85a",

"type": "modbus-flex-write",

"z": "f62cb442.d1aef",

"name": "更改设备状态",

"showStatusActivities": false,

"showErrors": false,

"server": "54b233c9.eea30c",

"x": 540,

"y": 360,

"wires": [

[

"7170acc1.c51154"

],

[]

]

},

{

"id": "5ad43590.d6765c",

"type": "function",

"z": "f62cb442.d1aef",

"name": "",

"func": "var value = msg.payload;\\n\\nvar msg0={payload:value[0]};\\nvar msg1={payload:value[1]};\\nvar msg2={payload:value[2]};\\n\\nreturn [msg0,msg1,msg2];",

"outputs": 3,

"noerr": 0,

"x": 250,

"y": 560,

"wires": [

[

"e1e76528.64858"

],

[],

[]

]

},

{

"id": "e1e76528.64858",

"type": "function",

"z": "f62cb442.d1aef",

"name": "00 厨房灯",

"func": "var Value = global.get(\'switchCFD\');\\nif(msg.payload === true)\\nValue.payload.power = !Value.payload.power;\\nglobal.set(\'switchCFD\',Value);\\nreturn ;",

"outputs": 0,

"noerr": 0,

"x": 400,

"y": 540,

"wires": []

},

{

"id": "9fad2eef.2d3f2",

"type": "comment",

"z": "f62cb442.d1aef",

"name": "初始化 断电、重启后执行,初始设备状态。只在最初执行一次",

"info": "",

"x": 260,

"y": 260,

"wires": []

},

{

"id": "7170acc1.c51154",

"type": "debug",

"z": "f62cb442.d1aef",

"name": "反馈设备状态",

"active": true,

"tosidebar": true,

"console": false,

"tostatus": false,

"complete": "payload",

"targetType": "msg",

"x": 740,

"y": 360,

"wires": []

},

{

"id": "2c486cf.a83a494",

"type": "modbus-server",

"z": "f62cb442.d1aef",

"name": "虚拟modbus",

"logEnabled": false,

"hostname": "127.0.0.1",

"serverPort": 10502,

"responseDelay": 100,

"delayUnit": "ms",

"coilsBufferSize": 10000,

"holdingBufferSize": 10000,

"inputBufferSize": 10000,

"discreteBufferSize": 10000,

"showErrors": false,

"x": 290,

"y": 100,

"wires": [

[

"770f8ac1.9ad934"

],

[

"91ad517b.724b78"

],

[

"7e004d83.580e64"

],

[

"2cbdcd7d.7e36ea"

]

]

},

{

"id": "770f8ac1.9ad934",

"type": "modbus-response",

"z": "f62cb442.d1aef",

"name": "",

"registerShowMax": 20,

"x": 630,

"y": 20,

"wires": []

},

{

"id": "91ad517b.724b78",

"type": "modbus-response",

"z": "f62cb442.d1aef",

"name": "",

"registerShowMax": 20,

"x": 630,

"y": 80,

"wires": []

},

{

"id": "7e004d83.580e64",

"type": "modbus-response",

"z": "f62cb442.d1aef",

"name": "",

"registerShowMax": 20,

"x": 630,

"y": 140,

"wires": []

},

{

"id": "2cbdcd7d.7e36ea",

"type": "modbus-response",

"z": "f62cb442.d1aef",

"name": "",

"registerShowMax": 20,

"x": 630,

"y": 200,

"wires": []

},

{

"id": "f455317d.4ec03",

"type": "inject",

"z": "f62cb442.d1aef",

"name": "",

"topic": "",

"payload": "",

"payloadType": "date",

"repeat": "",

"crontab": "",

"once": true,

"onceDelay": 0.1,

"x": 100,

"y": 100,

"wires": [

[

"2c486cf.a83a494"

]

]

},

{

"id": "1c1973de.98f044",

"type": "comment",

"z": "f62cb442.d1aef",

"name": "虚拟modbus 演示用",

"info": "",

"x": 130,

"y": 40,

"wires": []

},

{

"id": "54b233c9.eea30c",

"type": "modbus-client",

"z": "",

"name": "",

"clienttype": "tcp",

"bufferCommands": true,

"stateLogEnabled": false,

"tcpHost": "127.0.0.1",

"tcpPort": "10502",

"tcpType": "DEFAULT",

"serialPort": "/dev/ttyUSB",

"serialType": "RTU-BUFFERD",

"serialBaudrate": "9600",

"serialDatabits": "8",

"serialStopbits": "1",

"serialParity": "none",

"serialConnectionDelay": "100",

"unit_id": "1",

"commandDelay": "1",

"clientTimeout": "1000",

"reconnectOnTimeout": true,

"reconnectTimeout": "2000",

"parallelUnitIdsAllowed": true

}] 

 

以上是关于智能家居填坑004 Modbus接入逻辑篇的主要内容,如果未能解决你的问题,请参考以下文章

智能家居填坑006 新风接入

Modbus转Profinet网关把大棚内温湿度变送器接入到Profinet网络

2020-12-06 labview用I/O Server连接modbus(填坑)

虚幻填坑004:减少starter content占用空间,只保留使用的assets

Modbus转Profinet网关应用温湿度变送器接入到1200PLC

Modbus设备通过边缘网关接入阿里云IoT平台