Node-RED 是一个流程编程工具,可让您轻松地创建数据流和自动化流程。node-red-contrib-voltage_undivider
是一个 Node-RED 中的 npm 包,可帮助您进行电压测量和电压除法计算等操作。本文将详细介绍该 npm 包的使用方法。
安装
在 Node-RED 的工作目录中,使用 npm 安装 node-red-contrib-voltage_undivider
。
cd ~/.node-red # 进入 Node-RED 的工作目录 npm install node-red-contrib-voltage_undivider
使用
测量电压
我们可以使用 voltage measure
节点来测量电压。将该节点拖到工作区,在设置选项中指定输入引脚以及测量范围。如果您想测量的电压范围在 0-5V 之间,您可以将测量范围设置为 5V。在将节点与硬件连接后,将能够通过 Node-RED 的 debug 窗口查看测量到的电压值。
电压除法计算
电压除法公式为 Vout = Vin * R2 / (R1 + R2)
,其中 R1 和 R2 分别为电阻器的阻值。我们可以使用 voltage divider
节点来执行电压除法计算。将该节点拖到工作区,并设置输入引脚、输出引脚以及电阻值。将节点连接到 voltage measure
节点,就可以开始计算了。
示例代码
下面是一个简单的 Node-RED 流程示例,演示了如何使用 node-red-contrib-voltage_undivider
执行电压测量和电压除法计算。
[{"id":"6c32f034.e9ba8","type":"voltage measure","z":"3c4a4dfa.baf032","name":"Voltage Measure","pin":"A0","range":"5V","x":313,"y":160,"wires":[["621758e6.c2512"]]},{"id":"722f0a31.c8d1f8","type":"inject","z":"3c4a4dfa.baf032","name":"Inject 5V","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"5","payloadType":"num","x":93,"y":160,"wires":[["80f03edc.a3eba"]]},{"id":"80f03edc.a3eba","type":"delay","z":"3c4a4dfa.baf032","name":"Delay 1s","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":209,"y":160,"wires":[["6c32f034.e9ba8"]]},{"id":"621758e6.c2512","type":"voltage divider","z":"3c4a4dfa.baf032","name":"Voltage Divider","in_pin":"A0","out_pin":"O0","r1":"1000","r2":"1000","x":502,"y":160,"wires":[["f9eb7e31.a2c908"]]},{"id":"f9eb7e31.a2c908","type":"debug","z":"3c4a4dfa.baf032","name":"","active":true,"console":"false","complete":"payload","x":702,"y":160,"wires":[]}]
该示例将模拟 5V 信号,使用 voltage measure
节点测量电压,并使用 voltage divider
节点计算电压除法。最后,将结果显示在 Node-RED 的 debug 窗口中。
总结
node-red-contrib-voltage_undivider
是一个功能强大的 npm 包,可用于 Node-RED 中的电压测量和电压除法计算。在本文中,我们介绍了如何安装和使用该 npm 包。我们提供了一个简单的示例流程,演示了如何将 voltage measure
和 voltage divider
节点集成到 Node-RED 中。我们希望本文能帮助您了解如何使用该 npm 包,以便您可以在自己的项目中轻松地进行电压测量和电压除法计算。
来源:JavaScript中文网 ,转载请注明来源 https://www.javascriptcn.com/post/60066b5251ab1864dac668e5