Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble getting GPIO pin value in node.js

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

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?

like image 878
Jens Laur Avatar asked Nov 10 '22 08:11

Jens Laur


1 Answers

Maybe you could ensure you are connecting it to proper pins, as the GPIO and PIN numbers are not the same.

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

like image 118
moscaverd Avatar answered Nov 15 '22 11:11

moscaverd