Mojtaba Kamyabi
Mojtaba Kamyabi

Reputation: 3620

how to read from a GPIO pin when it changes?

I create an app that must detect when the lights(LEDs) changes their status (on->off or off->on) monitor this to a site.I create this app in client side with node.js and use rpi.GPIO package from here.
But I can't use change event in this package here is my code:

var gpio = require('rpi-gpio');
function alert()
{
    console.log("detected !");
}

gpio.on('change', function(channel, value)
{
    //send monitoring data to server for monitor on site
});
gpio.setup(7, gpio.DIR_IN, alert);

change event never called !! even if I change the state of LED. is there any way to do this without use setInterval ?

Upvotes: 0

Views: 1945

Answers (2)

JapAlekhin
JapAlekhin

Reputation: 31

I realize that this two years old, but in case some people come by for answers.

Notice that in your setup you just specified pin 7 as input and a callback, you need to also specify the edge a pin must trigger an interrupt.

gpio.setup(7, gpio.DIR_IN, gpio.EDGE_BOTH, alert);

You can use EDGE_NONE, EDGE_RISING, EDGE_FALLING, and EDGE_BOTH, default is EDGE_NONE.

Upvotes: 2

moscaverd
moscaverd

Reputation: 106

I'm not sure why it's not working, but you could also consider onoff package.

Upvotes: 0

Related Questions