简体   繁体   中英

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 .

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 . 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. I'd recommend rpi-gpio . After npm-installing it, you can control the GPIO21 with something along these lines (untested code):

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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