1

I've following this tutorial but using the setup shown below. I can get my LED working fine, but the button.watch() doesn't seem to listen to the button.

When I run this python script & then rerun my node app button.watch() works as expected... so I'm guessing the python script is opening a connection & then it's staying open for the node app to use.

What am I doing wrong that when I first run my node app I can't get button.watch() to actually listen to button events?

node - v4.2.1 Linux 4.1.13+ #826 PREEMPT Fri Nov 13 20:13:22 GMT 2015 armv6l GNU/Linux

enter image description here

var Gpio = require('onoff').Gpio,
led = new Gpio(18, 'out'),
button = new Gpio(23, 'in', 'both');
console.log('boot');

led.writeSync(1);
function exit() {
    led.unexport();
    button.unexport();
    process.exit();
}
console.log(
//the value of this is 0 right now
button.readSync()
);

button.watch(function(err, value) {
    //these console.log commands never get fired
    console.log(value);
    led.writeSync(value);
    console.log('button valued changed');

});

setInterval(function(){

    led.writeSync(led.readSync() ^ 1);

}, 1000);

process.on('SIGINT', exit);

1 Answer 1

1

Apparently I just didn't RTFM.

I'm totally new to hardware & electronics so I'll explain what I did for newbies like I was all of 10 min. ago....

In order for you button to function properly you need to configure a resistor aka a "pull up" or "pull down" via the OS. In my case I needed a "pull up"

Read this - I mean really read it, don't just skim it. Following these instructions got it working for me.

4
  • Justin, I read it and it did not clear the issue for me. How do you know if your button needs a "pull up" or "pull down" resister? Can you please include here the "tree" you created and what you did with it ? Thx.
    – Ric
    Commented Jan 11, 2016 at 5:38
  • ...In my case I have 3 lights that work on GPIO 14, 15, and 18. All fine there. I have 3 On-Off buttons on GPIO 2, 3, and 4 that seem non-functional in that the onoff module's watch() function never triggers for any of them. :(
    – Ric
    Commented Jan 11, 2016 at 5:53
  • From what I understand you should just us a pull up. Essentially that pin is set with current & when you press your button, it completes the circuit to "ground" & disconnecting the power makes the pi reboot. So pretty much even once you do a shutdown it's not actually powered off. It goes into low power mode - when you break that current it reboots. Make sense? Commented Jan 11, 2016 at 5:54
  • Using a button as power on only works on GPIO3 I think and maybe a couple other pins that get set to high to begin with. Not sure which they are, like 16 & 18 maybe. Search for default GPIO values & you should find some info about which pins are set high/low. Also, you have to use a .dtb file to set your pins as high for on/off to be able to "listen" for a change. Does that answer your question? Commented Jan 11, 2016 at 5:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.