Help with deep sleep code

Hello all, i have a device that i will use as a gate switch and i would like to save as much energy as possible.
When the gate is closed the read state is 1, when opened its 0
I would like my device to wake up when the gate opens and send a message to the agent (the agent part i have)
It would be nice afterwards to go so sleep but i dont mind cause its one of these gates that close if no one keeps it open
Basically it would be nice to wake up on any state change send a message to the agent depending the state and then go back to sleep.

What i have so far is this but doesnt work so good for me:
`#require “Button.class.nut:1.1.0”

z1_input <- hardware.pin1;
z1 <- Button(z1_input, DIGITAL_IN_WAKEUP, Button.NORMALLY_LOW);

// Contacts actions

z1.onPress(function() {
server.log(“Contact closed!”);
z1.onRelease(function() {
server.log(“Contact open!”);

function TrySleep() {
if ( == 1) {
// if pin1 is high, we can sleep
server.log(“Going to sleep”);
imp.onidle(function() { imp.deepsleepfor(3600.0); });
} else {
// if we can’t go to sleep yet, wait a while, then try again
imp.wakeup(0.5, TrySleep);

// Call the ping function
function ping()
server.log(“MAC: " + imp.getmacaddress() +” NTWK: “+ imp.getssid() +” @ " + imp.rssi() + " dBm");
imp.wakeup(5, ping); // Send ping every 5 sec
// Get states initially

// go to sleep loop
// End of code.`

If i change the line if ( == 1) { from ==1 to ==0 it works but i want the opposite.
Like i said ultimately i would like to have it as it wakes up on state change and goes back to sleep after that.
Thanks in advance!

The wake up pin triggers on 1, but you have the same pin set to 1 as its default state (button pressed = gate closed; button unpressed = gate open, I presume). As such you need the imp to wake on pin1 = 0, but that’s not how it works.

Ditch the button class. Set pin1 to DIGITAL_IN_WAKEUP. Connect your switch between ground, pin1 and 3V3 with a big pull-down resistor (1M Ohm). When the switch is pressed, ie. gate is shut, current runs to ground and pin1 is kept at 0; when the switch opens, current runs to pin1, which goes high and wakes the imp. Note that pin1 has to go low again before it can detect a high and re-awaken the imp

Great info here! I need to go home and fine some 1 MOhm resistors then and will test!
Thanks for that i will let you know soon

That suggestion doesn’t quite work. DIGITAL_IN_WAKEUP enables a ~40kΩ pulldown on Pin1 (even when sleeping). That means that Pin1 will stay low no matter what the state of the gate is. You would need to us a ~4kΩ pullup resistor which would burn a lot of power when the gate is shut.

From your description it’s a little unclear how you’ve connected the switch to get a 1. If the switch is open (no current flows) when the gate is closed then you can just connect the switch from 3V3 to Pin1. This will cause the pin to low when the gate is closed and high when the gate opens which is what you want.

If the switch is closed (current flows) when the gate is closed and you want to wake on the gate opening you are going to have to invert it if you want reasonable battery life. I’ve attached a circuit drawing that should do what you want with just a single resistor and PFET transistor.

@brandon, thanks for the instructions. Yes, so when the gate is closed, the switch is closed (current is flowing). Any idea what PFET transistor i would need? Or i can just look for another contact sensor that will do the opposite (current doesn’t flow whn gate is closed)
So basically from what i have experimented and understand reading threads and documentation, there is no way you can just sleep and wake up on change of state. Doesnt really matter to me since the gate is 99% closed or closes alone, but if someone would leave it on it would basically waste power…
I will try to look for PFETs at fry’s otherwise i will just use a different contact sensor
Thanks again

hello there, so i actually implemented this circuit and it look like it works…
what do you guys think?

That will work, but when the switch is closed (which sounds like it’s most of the time) the power drawn will be 3.3v / 10k = 330uA (or about 50x what the imp is using). Brandon’s circuit allows you to drop this down significantly.

The simplest solution is your circuit and a sensor that works the other way round, otherwise any logic-level PFET should be fine. You’re looking for a Vgs(th) of less than -3.0v. The big ones you’ll see are -4v or higher.

i see, thanks Hugo, i will implement differently then