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

 找回密码
 立即注册
查看: 9759|回复: 3

[教程系列] 【Node-RED】树莓派Raspbian安装nodered,使用MQTT控制ESP_Easy开关

[复制链接]

4

主题

15

帖子

125

积分

注册会员

Rank: 2

积分
125
金钱
110
HASS币
10
发表于 2019-11-27 00:36:43 | 显示全部楼层 |阅读模式
本帖最后由 hass.io 于 2020-12-17 20:42 编辑

【Node-RED】树莓派Raspbian安装nodered,使用MQTT控制ESP_Easy开关模块

#NODE-RED的安装很方便,写流程也很容易学。NODE-RED里面有MQTT服务器,不需要再配置mosquitto
#ESP_Easy固件自由度高,还能写自定义规则。

#1、首先准备树莓派Raspbian系统
#下载地址 https://www.raspberrypi.org/downloads/raspberry-pi-desktop/

1、新建虚拟机.png
2、安装Raspbian系统.png

#VBox新建虚拟机安装树莓派Raspbian系统
2、安装VBox增强功能.png
#安装完成建议备份一下初始化的快照

#安装VBox增强功能
#在终端输入代码安装
sudo sh /media/cdrom0/VBoxLinuxAdditions.run

#安装后重启
reboot



#更新软件包
sudo apt-get update
sudo apt-get upgrade


#NODE-RED在Raspberry Pi上运行 https://nodered.org/docs/getting-started/raspberrypi
#该脚本可在任何基于Debian的操作系统上使用,包括Ubuntu和Diet-Pi。
sudo apt-get install -y build-essential  curl libzmq3-dev
bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered | sed "s@install -y nodejs 2@install -y nodejs npm 2@g")


#安装npm8.16.2
sudo npm cache clean -f  
sudo npm install -g --unsafe-perm n
sudo n 8.16.2

#查看版本号
node -v
npm  -v
#更新模块

#作为服务运行
#Pi的安装脚本还将其设置为作为服务运行。这意味着它可以在后台运行,并可以在启动时自动启动。

#提供了以下命令来使用该服务:

node-red-start #-这将启动Node-RED服务并显示其日志输出。按Ctrl-C或关闭窗口并不能停止服务; 它一直在后台运行
node-red-stop #-这将停止Node-RED服务
node-red-restart #-这将停止并重新启动Node-RED服务
node-red-log #-这显示服务的日志输出
#您也可以通过选择Menu -> Programming -> Node-RED菜单选项在Raspbian Desktop上启动Node-RED服务。

#启动时自动启动
#如果要在打开或重新启动Pi时运行Node-RED,可以通过运行以下命令使服务自动启动:
sudo systemctl enable nodered.service
sudo systemctl start nodered.service

#要禁用该服务,请运行以下命令:
sudo systemctl stop nodered.service
sudo systemctl disable nodered.service


3、安装 Node-RED上的MQTT Broker服务器.png

#启动Node-RED服务后打开网页 http://192.168.2.209:1880
#右上角设置里面的节点设置安装2个节点模块:node-red-contrib-aedes 和 node-red-dashboard
#安装 Node-RED上的MQTT Broker服务器 https://flows.nodered.org/node/node-red-contrib-aedes
#安装 Node-RED的一组仪表板节点 https://flows.nodered.org/node/node-red-dashboard










#2、首先准备ESP8266模块,新手建议使用NodeMCU+面包板来调试设备

4、ESP8266-NodeMCU的GPIO.jpg

4、ESP8266-ESP01S的GPIO.png

#ESP8266-NodeMCU、ESP01S的GPIO

#下载ESPEasy最新版 https://github.com/letscontrolit/ESPEasy/releases
#刷机的教程看 https://www.jianshu.com/p/2401939de793 这里不再详细叙述(NodeMCU接USB装好驱动运行软件一键刷机,我使用ESP_Easy_mega-20191123_normal_ESP8266_1M)
#刷机重启后,接入ESP_Easy_0的WIFI,密码:configesp
#浏览器打开192.168.4.1设置网络

#完成网络设置后找到ESP8266模块的IP进入下一步设置: http://192.168.2.46/

5、设置设备的单位名称.png

#设置设备的单位名称(可按需自定义修改):ESP_Easy

6、设置设备的MQTT.png

#设置设备的MQTT,注意设置要和Node-RED里面的Mosca MQTT broker节点配置保持一致(192.168.2.209是Node-RED的服务器)

7、设置设备的开关或其他外部设备.png

#设置设备的开关或其他外部传感器
#三个红圈的名称设定是关联Node-RED的配置
#GPIO-12 (D6)是控制的开关,当MQTT收到命令就会在GPIO-12 (D6)输出3.3V电压(GPIO-12 (D6)和GND地线形成回路才有电压),这里接入继电器就能作为一个开关。

# 买设备刷第三方固件使用参考:控客小K刷入espeasy  https://bbs.hassbian.com/thread-2621-1-1.html
# 买设备刷第三方固件使用参考:SONOFF RF 刷入ESPEASY https://bbs.hassbian.com/thread-1769-1-1.html
# DIY开关设备使用参考:这里使用三个NEC UD2-3NU继电器来控制风扇的三个风速挡位。(继电器替换开关前注意测量电压电流是否会超继电器的负载)https://bbs.hassbian.com/thread-502-1-1.html

8、NEC UD2-3NU继电器工作原理.jpg

# NEC UD2-3NU继电器工作原理


#3、接下来设置Node-RED里面的流程配置

#打开网页 http://192.168.2.209:1880

9、Node-Red的流程.png

#右上角设置 - 导入 以下代码即可获得流程

[{"id":"4144fda5.a24df4","type":"tab","label":"NODE-RED ESP_Easy","disabled":false,"info":""},{"id":"d5769307.2a8d2","type":"mqtt in","z":"4144fda5.a24df4","name":"","topic":"ESP_Easy/DeviceName/ValueName","qos":"2","datatype":"auto","broker":"17b24401.fe3b9c","x":220,"y":360,"wires":[["6cf7075c.fa6b08"]]},{"id":"f3573895.ba88e8","type":"mqtt out","z":"4144fda5.a24df4","name":"","topic":"","qos":"","retain":"","broker":"17b24401.fe3b9c","x":610,"y":60,"wires":[]},{"id":"2cb15f66.f5ad2","type":"inject","z":"4144fda5.a24df4","name":"On","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":"","topic":"ESP_Easy/GPIO/12","payload":"1","payloadType":"str","x":130,"y":60,"wires":[["f3573895.ba88e8","e0a9cb51.a4ede8"]]},{"id":"f3a17e58.9b4b2","type":"inject","z":"4144fda5.a24df4","name":"Off","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":"","topic":"ESP_Easy/GPIO/12","payload":"0","payloadType":"str","x":130,"y":140,"wires":[["f3573895.ba88e8","e0a9cb51.a4ede8"]]},{"id":"bbca5ac9.f85e28","type":"http in","z":"4144fda5.a24df4","name":"","url":"/on","method":"get","upload":false,"swaggerDoc":"","x":140,"y":220,"wires":[["55b2774f.190518"]]},{"id":"e0a9cb51.a4ede8","type":"debug","z":"4144fda5.a24df4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":630,"y":180,"wires":[]},{"id":"122c9e93.8c1861","type":"http in","z":"4144fda5.a24df4","name":"","url":"/off","method":"get","upload":false,"swaggerDoc":"","x":130,"y":280,"wires":[["4b30fc1a.113b34"]]},{"id":"55b2774f.190518","type":"change","z":"4144fda5.a24df4","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"ESP_Easy/GPIO/12","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":220,"wires":[["e0a9cb51.a4ede8","f3573895.ba88e8"]]},{"id":"4b30fc1a.113b34","type":"change","z":"4144fda5.a24df4","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"ESP_Easy/GPIO/12","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"0","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":280,"wires":[["e0a9cb51.a4ede8","f3573895.ba88e8"]]},{"id":"6cf7075c.fa6b08","type":"ui_switch","z":"4144fda5.a24df4","name":"","label":"开关转换","tooltip":"","group":"98e7b38a.ecbcd","order":4,"width":0,"height":0,"passthru":false,"decouple":"true","topic":"ESP_Easy/GPIO/12","style":"","onvalue":"1","onvalueType":"str","onicon":"","oncolor":"","offvalue":"0","offvalueType":"str","officon":"","offcolor":"","x":440,"y":360,"wires":[["f3573895.ba88e8"]]},{"id":"3ea7759a.7579ca","type":"aedes broker","z":"4144fda5.a24df4","name":"","mqtt_port":1883,"mqtt_ws_port":"1882","cert":"","key":"","certname":"","keyname":"","dburl":"","usetls":false,"x":810,"y":60,"wires":[[]]},{"id":"17b24401.fe3b9c","type":"mqtt-broker","name":"","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"98e7b38a.ecbcd","type":"ui_group","name":"Default","tab":"78fe9673.6784f8","order":1,"disp":true,"width":"6","collapse":false},{"id":"78fe9673.6784f8","type":"ui_tab","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]



# 这个流程有手动测试开关,有web的UI开关,有http地址开关。
# Mosca MQTT broker节点 是MQTT服务器不需要连线

10、MQTT订阅和发布状态的主题.png

# 关于MQTT订阅和发布状态的主题:注意这里的主题开头是没有“ /” 的,有的教程有“ /”,只要改为匹配就行

#步骤2设置ESP8266模块的单位名称【ESP_Easy】就是上图的【%sysname%】
#这里说明这个设备只接收【ESP_Easy/】开头的MQTT里面的主题

#当ESP8266模块接收到MQTT里面的主题:【ESP_Easy/GPIO/12】;消息:【1】时,ESP8266模块就会在GPIO-12 (D6)输出3.3V电压(ON)
#当ESP8266模块接收到MQTT里面的主题:【ESP_Easy/GPIO/12】;消息:【0】时,ESP8266模块就会停止GPIO-12 (D6)的输出(OFF)

#当ESP8266模块改变GPIO-12 (D6)的状态,会在MQTT服务器发表主题:【ESP_Easy/DeviceName/ValueName】的消息
#上图的【%tskname%】就是步骤2设置的【DeviceName】
#上图的【%valname%】就是步骤2设置的【ValueName】
#这里 ESP_Easy/DeviceName/ValueName 的 ESP_Easy/

11、编辑inject节点.png

#编辑inject节点:我这里是作为一个手动测试开关,其实这个inject节点可以作为定时器使用,可以按需设置

12、编辑http in节点.png

#编辑http in节点:我这里是作为一个http开关(浏览器打开这个地址就会执行,类似hookapi),打开地址 http://192.168.2.209:1880/on 执行on流程,可以按需设置

13、编辑change节点.png

#编辑change节点:这里是作为一个http开关流程的一个中间件,用来定义准备发送MQTT的主题和消息,【topic】为主题,可以按需设置

14、编辑switch节点.png

#这个是Node-RED的一组仪表板节点 https://flows.nodered.org/node/node-red-dashboard
#编辑switch节点:这个是一个网页开关打开 http://192.168.2.209:1880/ui 即可控制,里面的主题【topic】:【ESP_Easy/GPIO/12】是对应GPIO-12 (D6),可以按需设置
#这里的开关也可以接收状态,需要在节点前面添加一个MQTT订阅来更新最新状态,注意要设置ESP8266模块里面的数据采集间隔设置。

15、编辑mqtt in节点.png

#编辑mqtt in节点:这个节点是接收ESP8266模块发布的状态,当收到数据就会转发数据到下一个节点。


#4、更多高级玩法
#ESP Easy 实现更多有意思的使用实例
#Domoticz查看温湿度 https://www.jianshu.com/p/2401939de793
#Node-RED的UI模块中看温湿度 https://diyprojects.io/esp-easy- ... board/#.XdpGZegzbhU
#使用node-red实现自动化 https://bbs.hassbian.com/forum.p ... =174&highlight=NODE

# 看完上面的教程,估计大家对MQTT的主题和消息的发送接收有一定认识了,很多外部传感器都是这样设定,举一反三即可。

# 射频遥控改造ESP8266模块控制 https://bbs.hassbian.com/thread-1081-1-1.html

# 上面教程的思路就是把遥控开关接入UD2-3NU继电器,但是这些都是【点动开关】需要触发后断开,这里就要用到ESP Easy高级玩法,写自定义规则脚本。
# ESP Easy自定义规则脚本的官方教程 https://www.letscontrolit.com/wi ... _Rules#Introduction

#开启方法如图:Tools -- Advanced Settings -- Rules √

16、开启脚本.png

#这里脚本的意思是当开关DeviceName开启时,一秒内马上关闭GPIO-12 (D6),可以按需修改

on DeviceName#ValueName=1 do
  timerSet,1,1
  gpio,12,0
endon

on Rules#Timer=1 do
if [DeviceName#ValueName]=1
  gpio,12,0
endif
endon



# 如果射频是不加密的可以把315/433射频的改造为ESP8266模块控制 https://bbs.hassbian.com/forum.p ... 6960&highlight=node
# 上面教程的思路是直接获取射频的数据,等需要时根据流程激活射频,适合一些能射频学习复制的设备。













回复

使用道具 举报

4

主题

15

帖子

125

积分

注册会员

Rank: 2

积分
125
金钱
110
HASS币
10
 楼主| 发表于 2019-11-27 00:39:32 | 显示全部楼层
ESP_Easy的配置页面是英文的,但可用浏览器的即时翻译功能来进行汉化。
回复

使用道具 举报

0

主题

7

帖子

39

积分

新手上路

Rank: 1

积分
39
金钱
32
HASS币
0
发表于 2019-12-6 13:44:55 | 显示全部楼层
大佬帮忙看看这是咋回事,我去目录里找了也没有找到这个文件。RED安装完毕后可以启动,但是浏览器打不开,运行日志我也没找到。大哭%>_<%

我我

我我

回复

使用道具 举报

0

主题

7

帖子

39

积分

新手上路

Rank: 1

积分
39
金钱
32
HASS币
0
发表于 2019-12-6 14:23:22 | 显示全部楼层
莫名其妙的重装了一遍居然成功了。。。
回复

使用道具 举报

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

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-12-4 16:33 , Processed in 0.086194 second(s), 29 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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