智能家居填坑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

}] 

原文地址:https://www.cnblogs.com/kiddfu/p/12350037.html

时间: 2024-10-31 10:48:55

智能家居填坑004 Modbus接入逻辑篇的相关文章

智能家居填坑003 Modbus设备接入篇

Modbus设备 格控TCP系列507E.508M.508D 1.IO端口多:508D有32个DI口.508M有24个DO口.507E有8个DI和8个DO口. 2.原生支持有线Modbus-TCP协议. 3.价格便宜,差评少. 4.支持二次开发:后期计划将普通设备逻辑写入里面,就可以脱离服务器. 5.产品相关资料 :格控资料 准备 格控设备在说明书里详细的说明了设备的设置方式,在此就不多加叙述(不同品牌的设置方式存在差异,均需详细参考说明书). 1.给设备分配与你所在网段匹配的ip地址. eg.

智能家居填坑005 开关复用

背景 餐厅规划了一组主灯.两组灯带(一组3000k.一组6000k).一组筒灯. 在后期装修中餐厅主灯换成了风扇灯,筒灯换成了飞利浦智睿筒灯.但是在餐厅只设置了一个两位自复位开关. 常规情况下不能满足主灯.风扇.灯带.筒灯的开关. 思路 为满足灯具的控制需求,需要对两位开关实现一些功能上的复用. 设:左边开关为 开关1,右边为 开关2 点击 开关1 :打开主灯 三秒内 再次 点击 开关1 :打开风扇低档 三秒内 再次 点击 开关1 :打开风扇中档 三秒内 再次 点击 开关1 :打开风扇高档 三秒

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

这是完整版,现在发现可以直接就精简原先项目的方法了,硬盘不够用的你们看看吧,别在生产环境用这个--

支付宝和微信支付的各种填坑

填坑 支付宝填坑是每个接入支付宝必经之路,下面是我接入支付宝遇到的问题汇总,希望大家在接入的路上少一点弯路 问题1. Util/base64.h:63:21: Cannot find interface declaration for ‘NSObject’, superclass of ‘Base64’ 解决办法: 这是base64.h中没有加入#import 系统库文件导致,这个错误报错方法直接想喷它一脸.报错方式太恶心. 1 2 解决办法: 这是base64.h中没有加入#import  系

10分钟搞定支付宝和微信支付 的 各种填坑

填坑   支付宝填坑是每个接入支付宝必经之路,下面是我接入支付宝遇到的问题汇总,希望大家在接入的路上少一点弯路 问题1. Util/base64.h:63:21: Cannot find interface declaration for ‘NSObject’, superclass of ‘Base64’ 解决办法: 这是base64.h中没有加入#import  系统库文件导致,这个错误报错方法直接想喷它一脸.报错方式太恶心. 问题2.截图告知你什么问题 解决办法: 这个问题可以同上的,心情

10分钟搞定支付宝和微信支付的各种填坑

支付宝填坑是每个接入支付宝必经之路,下面是我接入支付宝遇到的问题汇总,希望大家在接入的路上少一点弯路. 问题1. Util/base64.h:63:21: Cannot find interface declaration for ‘NSObject’, superclass of ‘Base64’ 解决办法: 这是base64.h中没有加入#import 系统库文件导致,这个错误报错方法直接想喷它一脸.报错方式太恶心. 问题2.截图告知你什么问题 解决办法: 这个问题可以同上的,心情好,截图再

10分钟搞定支付宝支付 的 各种填坑

填坑支付宝填坑是每个接入支付宝必经之路,下面是我接入支付宝遇到的问题汇总,希望大家在接入的路上少一点弯路 问题1. Util/base64.h:63:21: Cannot find interface declaration for ‘NSObject’, superclass of ‘Base64’ 解决办法: 这是base64.h中没有加入#import <Foundation/Foundation.h> 系统库文件导致,这个错误报错方法直接想喷它一脸.报错方式太恶心. 问题2.截图告知你

Google活动 | 智能家居开发者工作坊诚邀您参加

过去一年里,Google 在确保 Google Assistant 能与各类智能家居设备连接互动方面取得了重要的进展,现在美国的每个主要品牌的设备都能通过 Google Assistant 控制. 那么这到底有多少设备呢? 今天 Google Assistant 已能与超过5000款智能家居设备连接互动 - 这比今年 1 月增加了 1500 款.这其中包括相机.洗碗机.门铃.烘干机.灯具.插头.温控器.保安系统.开关.吸尘器.洗衣机.风扇.门锁.传感器.取暖器.空调.空气净化器.冰箱.烤箱 -

以太坊私链与智能合约部署学习(博主修正篇)—— 第一篇

以太坊(Ethereum)自2017年5月诞生以来,发展迅速.作为了一个小白,博主初步打算上个车,过个瘾.那什么是以太坊?请参看度娘的解释(https://baike.baidu.com/item/%E4%BB%A5%E5%A4%AA%E5%9D%8A/20865117?fr=aladdin). 事物是个新事特,也是个好东西,但资料不多,所以小白博主也只能慢慢找寻资料,慢慢记录自己填坑的过程.  第一篇的主题是:以太坊私链的创建:)    一.配置环境与软件安装 1.安装geth 以下的资料来自