简体   繁体   English

使用继电器打开和关闭灯

[英]Switch on and off a lamp using relay

I'm trying to switch a house lamp on and off using Raspberry Pi 3 and a 5V relay . 我正在尝试使用Raspberry Pi 3和5V继电器来打开和关闭房灯。

I'm able to switch it on and off using python code, but I'm not into python, i want to use node and js . 我可以使用python代码打开和关闭它,但是我不喜欢python, 我想使用node和js Been trying for a week to find a node example for doing this, but nothing works. 尝试了一周的时间来找到执行此操作的节点示例,但是没有任何效果。 This should be pretty simple for someone with more experience. 对于有更多经验的人来说,这应该很简单。

This is my setup: 这是我的设置: pi_gpio

There are many Node.js libraries to control GPIO. 有许多Node.js库可以控制GPIO。 I'd recommend rpi-gpio . 我建议rpi-gpio After npm-installing it, you can control the GPIO21 with something along these lines (untested code): npm安装后,您可以通过以下几行(未经测试的代码)控制GPIO21:

var gpio = require('rpi-gpio')
var gpiop = gpio.promise;

gpiop.setup(21, gpio.DIR_OUT)
    .then(() => {
        return gpiop.write(21, true); // ON
    })
    .catch((err) => {
        console.log('Error: ', err.toString());
    })

You can find other alternatives such as https://github.com/jperkin/node-rpio or https://github.com/jperkin/node-rpio in the npm registry. 您可以在npm注册表中找到其他替代方法,例如https://github.com/jperkin/node-rpiohttps://github.com/jperkin/node-rpio

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM