本文将介绍如何使用 npm 包 @unipitechnology/node-red-contrib-unipi-evok 来连接 UniPi Evok 设备,并在 Node-RED 中实现控制和监测。该 npm 包支持以下 UniPi 设备:
- UniPi 1.1
- UniPi Lite
- UniPi Neuron
安装
首先,你需要安装 Node.js 和 Node-RED。在安装完成后,打开终端并执行以下命令来安装 npm 包:
npm install @unipitechnology/node-red-contrib-unipi-evok
当安装完成后,启动 Node-RED 并在浏览器打开它。点击右上角的菜单按钮(三个横线),选择“管理面板”然后选择“Install”。在搜索栏中搜索“unipi”,然后选择“node-red-contrib-unipi-evok”来安装该 npm 包。
连接 UniPi 设备
在将 UniPi 设备连接到你的电脑之前,你需要确保 UniPi Evok 服务正在运行。你可以在 UniPi 设备上使用以下命令来启动该服务:
sudo service evok start
从 UniPi 设备的官方网站上下载并安装适当的驱动器,根据不同的设备选择相应的驱动器。安装完成后,你需要连接你的 UniPi 设备到你的电脑上。
连接 UniPi 设备后,在 Node-RED 编辑器中拖放“UniPi In”节点和“UniPi Out”节点。
UniPi In 和 UniPi Out 节点
“UniPi In”节点用于读取 UniPi 设备的输入端口。该节点接收来自 Evok 服务中 UniPi 设备输入端口的消息,并将其转化为 Node-RED 中可用的消息。
“UniPi Out”节点用于控制 UniPi 设备的输出端口。该节点接收来自 Node-RED 中的消息,并将其转化为 Evok 服务中 UniPi 设备输出端口的消息。
在配置“UniPi In”和“UniPi Out”节点时,你需要选择你所连接的 UniPi 设备和其相应的端口。此外,你可以指定节点使用的 Evok 服务和其相应的端口。
示例
下面是一个示例,展示如何在 Node-RED 中监测 UniPi 设备的输入端口并控制其输出端口。该示例利用“UniPi In”节点和“UniPi Out”节点来实现这一过程。
[{"id":"a1d222af.bf5a5","type":"UniPi In","z":"2f5cbb2d.112a1c","name":"Read DI","unipi_type":"1.1","unipi_port":"I1","unipi_multiplexer":false,"unipi_evok_host":"localhost","unipi_evok_port":"8080","x":270,"y":180,"wires":[["2e3212d8.d039ea"]]},{"id":"c96f71aa.885bb8","type":"inject","z":"2f5cbb2d.112a1c","name":"Read DI","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"1","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":130,"y":180,"wires":[["a1d222af.bf5a5"]]},{"id":"651a6a90.6cd23c","type":"UniPi Out","z":"2f5cbb2d.112a1c","name":"Write DO","unipi_type":"1.1","unipi_port":"Q1","unipi_multiplexer":false,"unipi_evok_host":"localhost","unipi_evok_port":"8080","x":540,"y":360,"wires":[]},{"id":"246eeaad.ea9b5a","type":"inject","z":"2f5cbb2d.112a1c","name":"Write DO 1","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":120,"y":360,"wires":[["651a6a90.6cd23c"]]},{"id":"2e3212d8.d039ea","type":"debug","z":"2f5cbb2d.112a1c","name":"debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":490,"y":180,"wires":[]},{"id":"9d4c4a4a.7ad38","type":"inject","z":"2f5cbb2d.112a1c","name":"Write DO 0","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"false","payloadType":"bool","x":120,"y":420,"wires":[["651a6a90.6cd23c"]]}]
该示例使用“UniPi In”节点监测 UniPi 设备的 I1 输入端口,并使用“UniPi Out”节点控制 UniPi 设备的 Q1 输出端口。在本示例中,“Read DI”注入节点每秒将触发一次“UniPi In”节点,并将 I1 端口的状态发送到“debug”节点。同样地,“Write DO”和“Write DO 0”注入节点可以分别控制 Q1 端口的状态。
结论
npm 包 @unipitechnology/node-red-contrib-unipi-evok 使得将 UniPi 设备与 Node-RED 连接变得非常简单。在使用该 npm 包时,请记住设置正确的 Evok 服务和端口,并确保你的 UniPi 设备处于可用状态。当你掌握了该 npm 包后,你将能够使用 Node-RED 完成各种任务,从而提高工作效率。
来源:JavaScript中文网 ,转载请注明来源 https://www.javascriptcn.com/post/60067382890c4f72775842e5