无法在 node.js 中获取 GPIO 引脚值

Trouble getting GPIO pin value in node.js

我在使用 node.js 进行 RaspberryPi GPIO 编程时遇到问题。我正在使用 onoff 库。这是我的代码:

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);

问题是附加的按钮永远不会触发。当我使用 Python 读取按钮的值时,它起作用了。我已经尝试过其他 node.js 库,如 rpi-gpio、pi-gpio 和 wiring-pi。 None 其中对我有用。我正在使用安装了最新 Raspbian 的 Raspberry PI B+。 有什么想法吗?

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

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