I’m wanting to create a simple push button device that allows me to track button interactions but at the same time use the sleep function to conserve battery power. Looking at the documentation I can use the DIGITAL_IN_WAKEUP command. But if I’m using a standard button, do I need to add an extra resistor and a power line to the button to get it to work with the WAKEUP. As it looks like the DIGITAL_IN_WAKEUP uses a pull-down resistor rather than a pull-up one?
The code:
`
function pin1changed() {
local buttonState = hardware.pin1.read();
// if buttonState is 0, the button is pushed
if (buttonState == 0) {
server.show("Button Pressed!");
server.log("Button Pressed");
} else {
server.show("Button Released!");
server.log("Button Released");
}
}
// Configure Pin1 to be wake up pin
hardware.pin1.configure(DIGITAL_IN_WAKEUP, pin1changed);
Hi all you need to do is connect one side of the switch 3v3 on the April (if thats what you are using) and the other to pin one. I do this with a PIR sensor (it’s a switch) which sleeps all the time. You might consider a bit of denounce in the handler imp.sleep(0.05);
You should also use imp.onidle(function() {server.sleepfor(3600);});
// Configure Pin1 to be wake up pin
hardware.pin1.configure(DIGITAL_IN_WAKEUP, pin1changed);
imp.configure(“Pin1 Waker”, [], []);
// Enter deep sleep
server.log(“Test sleep started”);
imp.onidle(function() { server.sleepfor(120.0); } );
`
The sleep part now works which is great, but now I’m not getting the button reading. Is this because by the time it’s woken up from sleep, it’s stopped reading the button function? Is there a way to read the button state at the same time as waking it up? And also is there a way to keep it awake for 5 mins then go back to deep sleep?
I will try your code, but you might just have to change the to Digital_in the top of handler to get it’s state then at the end revert to Digital_in_wakeup. When the sleepfor timer expires the imp does a warm boot and runs all the code again but the handler should run when it sees the rising edge of your switch. Some folk have reported issues with Sparkfun board pin1 not triggering think you need to push the imp card holder and that does the trick
Hi your code works
Tue May 21 2013 09:24:47 GMT+0100 (GMT Daylight Time): Test sleep started
Tue May 21 2013 09:24:47 GMT+0100 (GMT Daylight Time): Button Pressed
Tue May 21 2013 09:24:47 GMT+0100 (GMT Daylight Time): sleeping until 1369124810000
You won’t catch the button Release though the imp sleeeps after pin1changed() finished the code flow is this
pin1-high
warm boot - runs main code
pin1changed() - pin handler called
deepSleep
I haven’t used wakeup on pinchange yet, but as I understand it, there should be no need to check the pinstate. The onchange function will only be called after a 0 to 1 transition on the pin, so if its called, it automatically implies that the pin was 1.
Only if you want to ignore very short pulses, which might just be noise spikes, you might want to check if the pin is still high after say 10ms. To do this, you could put just a imp.wakeup(0.01, checkPinState), in your onchange handler, and let the checkPinState() function decide if further action should be taken.
hardware.pin1.configure(DIGITAL_IN_WAKEUP, pin1changed);
This doesn’t seem to trigger the function unless I press the button twice so have no way of triggering the first button press.
Yes I have a sparkfun board but it does work nicely with DIGITAL_IN_PULLDOWN. I think looking at some of the other posts, that unless the button is pressed for a few seconds, the function will get skipped. Hugo mentioned about a new version of the firmware which gives us hardware.wakereason() to use. Not sure when this will be released though.
Benjamin you might want to wait for rel 23 as it tells you what woke the imp up. I have monitor imp when it wakes from pin I don’t go back to deep sleep but stay in Shallow sleep for period. If there is no pin IO I go back to deep sleep. I can share this code but based on current API it will be a lot simpler with rel 23.
The only reason to hold the button high for 2 secs to allow the imp to connect and run code and read pins. In rel 23 you can wake and process in about 60ms if your in wifi off mode. You can then do some counting then send to cloud at a given point.
Thanks for helping I started to use: imp.wakeup(30, function(){server.sleepfor(6000.0)});
in the root which helped, but would be nice to be able to keep adding to the wakeup call if there any interactions detected on the pin.
Slowly getting my head around the nuances of Squirrel and Imp :-p I finally worked out how to declare a global var last night