Node-Red使用MQTT协议接收及发送消息到ESP32单片机


以下方法经本人验证通过,环境如下:

Node-RED V0.20.5

ESP32-CAM + HC-SR04 + Arduino IDE

mosquitto version 2.0.11

MatrixDB


前言:Node-RED 是构建物联网(IOT, Internet of Things)应用程序的一个强大工具,而MQTT则是当下使用广泛的物联网通信协议之一,为此我使用一块ESP-32单片机作为客户端,连接HC-SR04超声波测距模块做了两个简单的实验。

  • 实验一:获取物体距离数据,使用MQTT协议发送消息,并在Node-Red端进行接收,将数据保存在MatrixDB数据库中
  • 实验二:Node-Red使用MQTT协议发送消息,ESP32收到消息后对LED灯进行控制

实验中我使用了同一块ESP-32,实际应用中他们既可以是相同的物联网设备,当然也可以是不同的。实验目的是抛砖引玉,实际物联网中各类复杂场景都可以此为基础进行实现。

本实验的环境我在开篇已经列出,对于环境的准备,你可根据自己的情况参考下列文章做出调整:

第一步: 连接HC-SR04超声波测距模块

HC-SR04 有4个针脚,分别是Vcc, Trig, Echo, Gnd。Vcc接5V供电,Trig 和 Echo接IO口,Gnd自然就是接地。我将Trig连接到IO12,Echo连接到IO13,连接示意图如下:

第二步: 编写实验所需的程序,并烧录到ESP32

HC-SR04 的测距原理是向 Trig 口发一个10US 以上的高电平, 模块会发送8 个40khz 的方波并自动检测是否有信号返回,如有就通过 Echo 口输出一高电平,高电平持续的时间就是超声波从发射到返回的时间。

因此我们只需要将连接Trig口的 IO12 设置为OUTPUT,发一个持续时间为10US 的高电平,然后获取连接着Echo 口的 IO13 (设置为INPUT)高电平的持续时间,将取得的持续时间 *声速(340M/S)/2 就得到了距离。

实验中的LED为板载,使用的是IO4,将其设置为OUTPUT,通过高低电平即可控制开启和关闭

MQTT我使用的是PubSubClient库,如果你没安装,可通过库管理器进行安装加载

部分程序代码图如下:

引用的库和部分配置
连接wifi和群晖上的mosquittoMQTT服务
使用HC-SR04计算距离

代码编译通过后烧录到ESP32,上传烧录的方法在 win10利用arduino + esp32-cam搭建网络摄像头 文章中有写,不再啰嗦。

第三步: Node-Red 编写实验流程

实验一:拖入mqtt输入节点,双击,添加mqtt broke服务端

添加后选择该服务端,订阅主题。我在单片机上设置的发送的主题是Distance,因此这里也要输入主题为Distance

接着我们使用节点管理器安装node-red-contrib-postgresql,安装后将postgresql节点拖入

配置MatrixDB数据库

接着在Server中选择配置好的数据库,在Query中编写插入语句(我事前已经在数据库中添加了测试表MQTT_Test,包含payload,msg_time两个字段)

最后将两个节点连接起来,部署即可

实验二:这个比较简单,拖入两个inject节点,一个控制开启LED,一个控制关闭LED。开启的节点设置如下,内容为“ON”,这个是ESP32程序中我设置的命令,当接收到主题为LED的消息,且内容为ON时,开启LED。同理关闭设置内容为“OFF”

接着再拖入一个mqtt输出节点,将它与两个inject连接起来,该节点只需要配置好服务端和主题即可。配置如下:

至此流程编写完毕

导出的流程如下:

[{"id":"81d18949.9b6738","type":"mqtt in","z":"b8dd355c.40cd78","name":"","topic":"Distance","qos":"2","datatype":"auto","broker":"fcb81ef4.a2021","x":520,"y":320,"wires":[["14feeb2c.6dfff5"]]},{"id":"23f6b97e.a38776","type":"comment","z":"b8dd355c.40cd78","name":"实验一:接收MQTT消息,将数据写入数据库","info":"","x":630,"y":260,"wires":[]},{"id":"af6c9b1b.dab788","type":"mqtt out","z":"b8dd355c.40cd78","name":"","topic":"LED","qos":"","retain":"","broker":"fcb81ef4.a2021","x":760,"y":540,"wires":[],"inputLabels":["ON"]},{"id":"4718d956.78af68","type":"inject","z":"b8dd355c.40cd78","name":"开灯","topic":"Test 2.1","payload":"ON","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":530,"y":500,"wires":[["af6c9b1b.dab788"]]},{"id":"2fc63b12.cba2d4","type":"inject","z":"b8dd355c.40cd78","name":"关灯","topic":"Test 2.2","payload":"OFF","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":530,"y":580,"wires":[["af6c9b1b.dab788"]]},{"id":"c3cfd91.f709f28","type":"comment","z":"b8dd355c.40cd78","name":"实验二:发送MQTT消息,控制板载LED","info":"","x":610,"y":440,"wires":[]},{"id":"14feeb2c.6dfff5","type":"postgresql","z":"b8dd355c.40cd78","name":"保存收到消息","query":"INSERT INTO \"MQTT_Test\" (payload,msg_time) VALUES ( '{{{msg.payload}}}' ,now());","postgreSQLConfig":"8e100f3e.01c4b","split":false,"rowsPerMsg":"1","outputs":1,"x":789.1667709350586,"y":317.66671657562256,"wires":[[]]},{"id":"fcb81ef4.a2021","type":"mqtt-broker","z":"","name":"feeus","broker":"10.0.0.2","port":"18831","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"8e100f3e.01c4b","type":"postgreSQLConfig","z":"","name":"MatrixDB","host":"10.0.0.214","hostFieldType":"str","port":"5432","portFieldType":"num","database":"feeus","databaseFieldType":"str","ssl":"false","sslFieldType":"bool","max":"10","maxFieldType":"num","min":"1","minFieldType":"num","idle":"1000","idleFieldType":"num","connectionTimeout":"10000","connectionTimeoutFieldType":"num","user":"mxadmin","userFieldType":"str","password":"feeus.com","passwordFieldType":"str"}]

第四步:验证实验结果


如果您喜欢这篇文章,或者它给您带来了帮助,您可以请我们喝一杯咖啡,我们将非常感谢您的支持!

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

15 + 15 =