Trouble getting GPIO pin value in node.js

Jen*_*aur 5 node.js gpio raspberry-pi

I've a problem with using node.js for RaspberryPi GPIO Programming. I am using the onoff library. This is my code:

var Gpio = require('onoff').Gpio,
    led = new Gpio(20, 'out'),
    button = new Gpio(18, 'in', 'both');

button.watch(function (err, value) {
    if (err) {
        throw err;
    }
    led.writeSync(value);
});

function exit() {
    button.unexport();
    }
process.on('SIGINT', exit);
Run Code Online (Sandbox Code Playgroud)

The problem is that the attached button never triggers. When I use Python to read the button's value it works. I have already tried other node.js libraries like rpi-gpio, pi-gpio and wiring-pi. None of them worked for me. I'm using an Raspberry PI B+ with the latest Raspbian installed. Any ideas?

小智 1

也许您可以确保将其连接到正确的引脚,因为 GPIO 和 PIN 号不相同。

http://data.designspark.info/uploads/images/53bc258dc6c0425cb44870b50ab30621