『瀚思彼岸』» 智能家居技术论坛

 找回密码
 立即注册
查看: 27434|回复: 35

[教程系列] 通过Node-RED 将RS485 4路继电器接入HA,带状态反馈

  [复制链接]

3

主题

53

帖子

381

积分

论坛分享达人

积分
381
金钱
328
HASS币
0
发表于 2018-4-14 21:58:02 | 显示全部楼层 |阅读模式
本帖最后由 youhaishan 于 2018-4-14 22:07 编辑

       通过向群里各位大神的学习,及测试,终于实现RS485 4路继电器板接入HA控制并实现状态反馈,目前只找到通过字符串反馈的方法,16进制数据反馈的暂时没学会怎么判断(群里如有Node js的高手望指点下),控制采用的是继电器板16进制的协议进行控制。
IMG_8065~1.gif

设备连接



机械面板接4路继电器板的IO输入口,继电器RS485接网络模块的RS485接口,网络模块接入网络。(设备都是网上淘的)
TIM截图20180414215250.jpg

接入原理


HA采用的是MQTT协议,Node-RED上设置MQTT组件与HA通信,设置TCP Server服务,网络模块工作方式设置为TCP Client连接到TCP Server进行通信。Node-RED上用 switch 进行判断,function进行数据转换,change进行状态反馈。


控制原理


HA通过MQTT将OFF、ON指令反馈到Node-RED MQTT组件,Node-RED接收到OFF\ON指令后转换成继电器的控制协议发送到网络模块上,网络模块在发送到继电器板上实现控制。
TIM截图20180414214753.jpg





反馈原理


当继电器状态改变时,将相对应的字符串状态信息发送到TCP Server,switch 进行判断后通过change将OFF\ON反馈到MQTT。
TIM截图20180414214805.jpg




Node-RED代码



[{"id":"32f04645.76c51a","type":"function","z":"a3219be1.5eef08","name":"S1on","func":"msg.payload = new Buffer([0x55,0x01,0x32,0x00,0x00,0x00,0x03,0x8B]);\nreturn msg;","outputs":1,"noerr":0,"x":450,"y":140,"wires":[["fddbb240.55fb7"]]},{"id":"8545cae2.40f3b","type":"function","z":"a3219be1.5eef08","name":"S1off","func":"msg.payload = new Buffer([0x55,0x01,0x31,0x00,0x00,0x00,0x03,0x8a]);\nreturn msg;","outputs":1,"noerr":0,"x":450,"y":180,"wires":[["fddbb240.55fb7"]]},{"id":"341926d2.225802","type":"mqtt in","z":"a3219be1.5eef08","name":"","topic":"room1/1/2","qos":"0","broker":"c5ca30b6.ddee48","x":80,"y":160,"wires":[["2038d7f0.521cb8"]]},{"id":"2038d7f0.521cb8","type":"switch","z":"a3219be1.5eef08","name":"S1","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"ON","vt":"str"},{"t":"eq","v":"OFF","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":270,"y":160,"wires":[["32f04645.76c51a"],["8545cae2.40f3b"]]},{"id":"9ede0feb.a8759","type":"change","z":"a3219be1.5eef08","name":"S1on","rules":[{"t":"set","p":"payload","pt":"msg","to":"ON","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":450,"y":420,"wires":[["deb69e8f.64b0f8"]]},{"id":"51dd564a.a85b48","type":"change","z":"a3219be1.5eef08","name":"S1off","rules":[{"t":"set","p":"payload","pt":"msg","to":"OFF","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":450,"y":460,"wires":[["deb69e8f.64b0f8"]]},{"id":"9f639c28.5755c8","type":"change","z":"a3219be1.5eef08","name":"S2on","rules":[{"t":"set","p":"payload","pt":"msg","to":"ON","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":450,"y":500,"wires":[["27f71cc8.6fb10c"]]},{"id":"68d95b98.c7af34","type":"change","z":"a3219be1.5eef08","name":"S2off","rules":[{"t":"set","p":"payload","pt":"msg","to":"OFF","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":450,"y":540,"wires":[["27f71cc8.6fb10c"]]},{"id":"deb69e8f.64b0f8","type":"mqtt out","z":"a3219be1.5eef08","name":"","topic":"room1/1/1","qos":"0","retain":"true","broker":"c5ca30b6.ddee48","x":750,"y":440,"wires":[]},{"id":"27f71cc8.6fb10c","type":"mqtt out","z":"a3219be1.5eef08","name":"","topic":"room1/2/1","qos":"0","retain":"true","broker":"c5ca30b6.ddee48","x":750,"y":520,"wires":[]},{"id":"83af934c.ec50c","type":"mqtt in","z":"a3219be1.5eef08","name":"","topic":"room1/2/2","qos":"0","broker":"c5ca30b6.ddee48","x":80,"y":260,"wires":[["aab43bdf.ec03e"]]},{"id":"aab43bdf.ec03e","type":"switch","z":"a3219be1.5eef08","name":"S2","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"ON","vt":"str"},{"t":"eq","v":"OFF","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":270,"y":260,"wires":[["dc196f34.1903"],["c7f2cb7.1198638"]]},{"id":"c7f2cb7.1198638","type":"function","z":"a3219be1.5eef08","name":"S2off","func":"msg.payload = new Buffer([0x55,0x01,0x31,0x00,0x00,0x00,0x04,0x8b]);\nreturn msg;","outputs":1,"noerr":0,"x":450,"y":280,"wires":[["fddbb240.55fb7"]]},{"id":"dc196f34.1903","type":"function","z":"a3219be1.5eef08","name":"S2on","func":"msg.payload = new Buffer([0x55,0x01,0x32,0x00,0x00,0x00,0x04,0x8c]);\nreturn msg;","outputs":1,"noerr":0,"x":450,"y":240,"wires":[["fddbb240.55fb7"]]},{"id":"2a8e9a06.c48da6","type":"switch","z":"a3219be1.5eef08","name":"S1","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"room131","vt":"str"},{"t":"eq","v":"room130","vt":"str"},{"t":"eq","v":"room141","vt":"str"},{"t":"eq","v":"room140","vt":"str"}],"checkall":"true","repair":false,"outputs":4,"x":290,"y":480,"wires":[["9ede0feb.a8759"],["51dd564a.a85b48"],["9f639c28.5755c8"],["68d95b98.c7af34"]]},{"id":"c29bc05d.4b3d1","type":"tcp in","z":"a3219be1.5eef08","name":"","server":"server","host":"","port":"8080","datamode":"stream","datatype":"buffer","newline":"","topic":"","base64":false,"x":70,"y":480,"wires":[["2a8e9a06.c48da6"]]},{"id":"fddbb240.55fb7","type":"tcp out","z":"a3219be1.5eef08","host":"","port":"8080","beserver":"server","base64":false,"end":false,"name":"","x":720,"y":200,"wires":[]},{"id":"c5ca30b6.ddee48","type":"mqtt-broker","z":"","name":"测试","broker":"192.168.1.110","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""}]




评分

参与人数 1金钱 +12 收起 理由
+ 12 谢谢分享!

查看全部评分

回复

使用道具 举报

14

主题

660

帖子

3973

积分

论坛元老

Rank: 8Rank: 8

积分
3973
金钱
3308
HASS币
10
发表于 2018-4-15 00:03:55 | 显示全部楼层
前来膜拜楼主……
回复

使用道具 举报

24

主题

885

帖子

5069

积分

论坛元老

Rank: 8Rank: 8

积分
5069
金钱
4179
HASS币
20
发表于 2018-4-17 00:50:21 | 显示全部楼层
Node-RED的应用应该还很宽广,比如plc的控制接入,希望坛里大神继续研究开发
回复

使用道具 举报

4

主题

208

帖子

1408

积分

金牌会员

Rank: 6Rank: 6

积分
1408
金钱
1199
HASS币
0
发表于 2018-4-20 14:53:37 | 显示全部楼层
膜拜楼主!好文章啊!!
回复

使用道具 举报

0

主题

29

帖子

94

积分

注册会员

Rank: 2

积分
94
金钱
65
HASS币
0
发表于 2018-6-4 05:37:18 来自手机 | 显示全部楼层
好文章!!!希望楼主视频来一个!!
回复

使用道具 举报

1

主题

44

帖子

210

积分

中级会员

Rank: 3Rank: 3

积分
210
金钱
166
HASS币
0
发表于 2018-6-27 14:43:41 | 显示全部楼层
这个太SIX,感谢大腿
回复

使用道具 举报

1

主题

7

帖子

77

积分

注册会员

Rank: 2

积分
77
金钱
70
HASS币
0
发表于 2018-8-7 13:44:09 | 显示全部楼层
你好,请问继电器与机械开关是怎样一个关系,或者是怎样连接的呢?
回复

使用道具 举报

6

主题

171

帖子

1063

积分

论坛技术达人

积分
1063
金钱
882
HASS币
20
发表于 2018-8-7 14:46:35 | 显示全部楼层
膜拜大神!支持原创
回复

使用道具 举报

5

主题

356

帖子

1411

积分

金牌会员

Rank: 6Rank: 6

积分
1411
金钱
1055
HASS币
0
发表于 2018-8-24 19:40:39 | 显示全部楼层
如果用N1 只能通过TCP/IP?
回复

使用道具 举报

3

主题

53

帖子

381

积分

论坛分享达人

积分
381
金钱
328
HASS币
0
 楼主| 发表于 2018-8-25 10:21:33 | 显示全部楼层
tanghuafu 发表于 2018-8-24 19:40
如果用N1 只能通过TCP/IP?

N1没485接口吧,你就买个网络模块转换下
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-12-22 22:42 , Processed in 0.129264 second(s), 35 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表