Node.js 中使用 Node RED 进行物联网开发的步骤和配置技巧
什么是 Node RED?
Node-RED 是一个低代码工具,可以轻松的连接硬件设备、API 和在线服务。它基于 Node.js,并提供一个易于使用的 Web 界面,方便用户可视化的连接各种设备,进行自动化流程的编排和管理。
为什么要使用 Node RED 进行物联网开发?
相较于传统的物联网方案,Node-RED 可以极大的简化设备和服务的连接和编排,减少开发人员的工作量。同时,Node-RED 内置了大量的节点,支持多种设备和协议,无需开发人员自行开发驱动和服务接口,具有更高的开发效率和更快的上市时间。
如何使用 Node-RED 进行物联网开发?
安装 Node-RED
首先需要安装 Node.js,然后可以通过 npm 命令安装 Node-RED。安装完成后,使用 node-red
命令启动 Node-RED。
$ npm install -g --unsafe-perm node-red $ node-red
连接设备和服务
在 Node-RED 中,设备和服务可以通过节点进行连接。一个节点代表一个设备或服务,通过连接不同的节点,可以构建出整个自动化流程。
例如,连接一个 MQTT 节点和一个 LED 设备节点,可以实现远程控制 LED 灯的功能。下面是一个示例代码:
[{"id":"a6d5e6ae.8076c","type":"mqtt in","z":"c893348f.03bf2","name":"","topic":"led","qos":"2","broker":"","x":200,"y":180,"wires":[["4e11109.bbfdd32"]]},{"id":"4e11109.bbfdd32","type":"rpi-gpio out","z":"c893348f.03bf2","name":"","pin":"7","state":"state","pinstate":"0","outputs":"","board":"f4a4a4a4.588108","pulse":"","pulseWidth":"","repulseInterval":"","startupState":"","statePinning":"false","boardRev":"2","x":470,"y":180,"wires":[]},{"id":"f4a4a4a4.588108","type":"rpi-board","z":"","name":"Raspberry Pi"}]
编辑自动化流程
打开 Node-RED 的 Web 界面,在 Flow 编辑器中,可以添加、删除和连接各种节点,构建自动化流程。
例如,连接一个 HTTP 输入和一个 MQTT 输出节点,可以实现远程控制 LED 灯的功能,下面是一个示例代码:
[{"id":"753c9b92.217b8","type":"http in","z":"823715f3.a33cd8","name":"","url":"/led","method":"get","upload":false,"swaggerDoc":"","x":130,"y":220,"wires":[["1138d4e4.837fe7"]]},{"id":"1138d4e4.837fe7","type":"function","z":"823715f3.a33cd8","name":"Set LED State","func":"msg.payload = msg.payload == 1 ? \"on\" : \"off\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":310,"y":220,"wires":[["93f8d9f9.08b238"]]},{"id":"93f8d9f9.08b238","type":"mqtt out","z":"823715f3.a33cd8","name":"","topic":"led","qos":"","retain":"","broker":"","x":480,"y":220,"wires":[]}]
配置和管理节点
Node-RED 提供了丰富的节点和配置选项,可以根据需求灵活配置和管理节点。例如,可以配置 MQTT 节点的服务器地址和端口号,设置消息的 QoS 和 Retain 属性。
同时,Node-RED 还提供了 Dashboard 节点,可以方便用户构建自定义的面板,展示和控制设备数据。
[{"id":"4710540a.d4683c","type":"mqtt in","z":"cb414e03.5de6f8","name":"","topic":"led-state","qos":"2","datatype":"utf8","broker":"","x":180,"y":220,"wires":[["47fed310.a2c854"]]},{"id":"47fed310.a2c854","type":"ui_switch","z":"cb414e03.5de6f8","name":"","label":"LED","tooltip":"","group":"a966fcbc.23c7b","order":0,"width":0,"height":0,"passthru":false,"decouple":"false","topic":"led-state","style":"","onvalue":"1","onvalueType":"num","onicon":"","oncolor":"","offvalue":"0","offvalueType":"num","officon":"","offcolor":"","x":370,"y":220,"wires":[["90b3876f.970a68"]]},{"id":"90b3876f.970a68","type":"mqtt out","z":"cb414e03.5de6f8","name":"","topic":"led","qos":"2","retain":"true","broker":"","x":580,"y":220,"wires":[]},{"id":"a966fcbc.23c7b","type":"ui_group","name":"LED Control","tab":"111be3a3.694d91","order":1,"disp":true,"width":"6","collapse":false},{"id":"111be3a3.694d91","type":"ui_tab","name":"Home","icon":"dashboard","order":1,"disabled":false,"hidden":false}]
总结
Node-RED 是一个功能强大的低代码工具,可以极大的简化物联网开发的工作量,提高开发效率和上市时间。在使用 Node-RED 进行物联网开发时,需要注意理解和掌握节点和配置选项的使用,尤其要注意设备和服务的安全性,避免数据泄露和攻击。
来源:JavaScript中文网 ,转载请注明来源 https://www.javascriptcn.com/post/64e96601f6b2d6eab34b0360