Author Topic: Door alarm: deep sleep  (Read 5041 times)

davide

  • Newbie
  • *
  • Posts: 21
Door alarm: deep sleep
« on: November 03, 2016, 11:41:32 am »
Hello! I try to explain my idea to ask some help: I'd like to use my Oak to check my apartment door state, using as less battery as possible.
Thanks to some of you, I can now receive an email (with IFTTT service) everytime my door state changes. I'll have to add some filter on the signal of the door switch but I think I'll be able to do it. The problem now is to spare some battery usage. I read a lot on this forum about deepsleep but I can't say I understood everything. I found out that I must connect pin 10 to RST but I didn't understand if i can use the switch I have on my door to wake my Oak up and send it to sleep. Can anybody suggest anything, please? As I said I read a lot about it but perhaps I'm too stupid to understand your previous posts...
The "flowchart" could be something like:
- As the oak starts, it publishes "oak started" on Particle
- If the door is closed, Oak goes to sleep
- When the door is opened, Oak wakes up and publishes "door open"
- It stays awake until the door stays open
- When the door is closed, oak publishes "door closed" and goes to sleep.
Is it possible?

PeterF

  • Hero Member
  • *****
  • Posts: 881
Re: Door alarm: deep sleep
« Reply #1 on: November 03, 2016, 06:42:33 pm »
Hello again! ;D

Firstly your flowchart makes perfect sense... so no worries there!  8)

I think as far as deepsleep is concerned, it is unfortunately NOT an option, because as I understand it, when the ESP8266 (Oaks brain) goes into deepSleep mode... the only thing running is a clock that wakes it up again - so it can't monitor pins for changes, etc. The only way I know you'd be able to pull that off would be to use another microcontroller (DigiSpark to keep it in the family?  :P) to do that actual door monitoring, and it wakes the Oak up to do the actual wifi/particle stuff. That would probably be the most power effecient route, but not the simplest.

However, it may be possible to use other sleep modes on the Oak to bring it's power consumption level down to a more practical level. I fiddled around with that on some other Oaks, so I'll see if I kept the code. Basically, you can shut down the wifi module when you don't need it, and can also have the CPU go into standby mode, and it can be woken up by a interrupt (pin change), and then you can restart the wifi, and send stuff as needed. I'll see if I can knock something up between now and Sunday.




davide

  • Newbie
  • *
  • Posts: 21
Re: Door alarm: deep sleep
« Reply #2 on: November 04, 2016, 12:48:30 am »
Hello again, PeterF.

You are my Guru  :D

Thanks again for your help. I have some other questions/ideas... Can I use my switch to close pin 10 to RST? I mean: I send Oak to sleep for (let's say) 1 sec then I wake it up but pin 10 is not connected to RST. Then I open the door and that closes pin 10 to RST and (perhaps) this wakes the Oak up. I send the mail "door open" and as soon as pin 10 is open again, I send the mail "door closed" and I send Oak to sleep again. Do you think it can work? Or if pin 10 is open when Oak receive a "wakeup" order, Oak will never wake up again?

Another idea could be on the hardware side: Oak is off. When I open the door, the switch connects Oak to the battery and it sends the email. To manage the "door closed" state I think I need a condenser (I'm not an expert) to keep Oak on for a while when it's disconnected from the battery. When Oak is disconnected from the battery, it sends an email "door closed" (maybe I need a double switch that sends also a signal to a digital pin) and then will go off (when the condenser is discharged). Could this be an idea?

exeng

  • Sr. Member
  • ****
  • Posts: 454
Re: Door alarm: deep sleep
« Reply #3 on: November 04, 2016, 10:34:37 am »
Pete, Haven't given this a lot of thought but couldn't the RST (active low) be triggered by and external one shot (555) circuit plus transistor that pulls reset low for a short duration when the door is open. Obviously it would take some design effort to tie into the monitored door signal but seems like it could be done. I'm I missing something?

davide, You should also be aware that the use of deep sleep may require some structural changes in your code because after a reset (wakeup) occurs, the code enters setup() again.

PeterF

  • Hero Member
  • *****
  • Posts: 881
Re: Door alarm: deep sleep
« Reply #4 on: November 04, 2016, 05:23:49 pm »
@exeng: Yeah, that is basically what I think you need to do to trigger the reset externally / when in deep sleep. I was avoiding going that way as it isn't 'simple'. :-D I think the main issue with using reset is that IIRC, it needs a pulse - not just pulled high or low, hence why it can't be simply tied to a switch.

@davide: If you read what I wrote for exeng above, you'll probably be thinking that your Plan A won't work, as it needs a pulse, which the reed switch won't provide. Plus deep sleep resets the Oak, so you won't be able to track the door state with one switch. Plan B is probably the better option - use the reed switch itself to switch the Oak on and off. It probably offerers the best power option only, as if your door is closed most of the time, the Oak will have no power. And for power when the door has just been closed, it is probably overkill, but something like this would do the trick - once they've charged up, it can run the Oak for about 30 seconds on it's own! ;)

davide

  • Newbie
  • *
  • Posts: 21
Re: Door alarm: deep sleep
« Reply #5 on: November 05, 2016, 01:42:45 am »
Thanks to both for your comments and ideaas. I think I'll choose a condenser as suggested by PeterF (thanks a lot) but I have another question: when the battery is disconnected, the condenser gives power to the Oak until it is discharged (or discharged to a %). But when the battery is connected will the Oak get power? I know the condenser will not give electricity until it's fully charged. When it's fully charged it gives power but the battery is still connected... Will this circuit work?
Another idea is a relays: the switch powers the oak on, Oak closes the relays that connects it's Vin to the battery; so if I open the switch, the Oak will remain connected to the battery through the relay. This mean that when I open the switch, the Oak will send the "door closed" message and then will open the relay, killing itself...

PeterF

  • Hero Member
  • *****
  • Posts: 881
Re: Door alarm: deep sleep
« Reply #6 on: November 05, 2016, 07:51:33 pm »
Yes, a capacitor across the power line of the Oak would a) charge as soon as power is applied whilst b) the Oak does it's thing and then c) power the oak for a period (relative to its capacity).

I was actually thinking later of something similar to the relay idea as a better option, which was to use a mosfet to bypass the reed switch so the Oak could control it's own power. It shouldn't be to hard to arrange it so that the reed switch connects the power, and then the Oak can then monitor the switch to find out when it disconnects in order to send the door closed message. It would probably need a diode also in order to prevent the Oak's taking over of the relay/mosfet from masking the signal from the reed switch. Hm... interesting! ;)

davide

  • Newbie
  • *
  • Posts: 21
Re: Door alarm: deep sleep
« Reply #7 on: November 07, 2016, 08:52:27 am »
Thanks, PeterF.

I thought to use a mechanical switch. I have one with 3 pins: 1 common, 1 NO and NC. When the door is opened, the NO is closed to the common (GND) and when the door is closed again, the NC will be closed do GND (and it will be the signal for the Oak to start the power down procedure).

Why do you talk about a Reed switch? Is it better?

Another question: do you have an idea of the capacity of the capacitor I would need? I'm absolutely ignorant in this field, but this is what I found out: the Oak uses about 100mA at 5V Right? That means that its Resistance (R=V/I) could be 50 Ohm. I read that the discharge time of a capacitor [sec] is about equal to its capacity [F] x resistance [Ohm]. It means that 10[sec]=C[F].50[Ohm] and that C=10/50=0,2[F] and that's a huge capacitor, even if I supposed that the Oak can stay on until the capacitor is fully discharged (which is wrong)... Am I right? In this case, the capacitor is not anymore an option.
« Last Edit: November 08, 2016, 12:08:07 am by davide »

PeterF

  • Hero Member
  • *****
  • Posts: 881
Re: Door alarm: deep sleep
« Reply #8 on: November 07, 2016, 10:27:44 pm »
A reed switch is a magnetic switch - you attach the sensor side to the door jamb or something stationary, and the magnet goes on the moving bit (the door). When the door (magnet) is in close proximity to the sensor/switch, it will either open or close, depending on what type it is. Good point there is no contact is needed - the magnet can be 1cm or more away from the switch and still trigger it. It's the type of switch that was (and still is?) commonly used in security systems for doors and windows.

Capacity of the capacitor, I just went with a 1F capacitor  - I linked this one earlier - which is overkill, but will run the Oak for about 20-30 seconds on it's own once charged.

davide

  • Newbie
  • *
  • Posts: 21
Re: Door alarm: deep sleep
« Reply #9 on: November 08, 2016, 12:28:16 am »
I ordered the capacitor (your link doesn't ship to europe but I found something similar) and thanks for the suggestion.
I have some questions about your other ideas: do you have a suggestion about the mosfet? any part number? If you use a Reed switch how do you tell Oak that the door has been closed? I think you need a second switch. Isn't it?

PeterF

  • Hero Member
  • *****
  • Posts: 881
Re: Door alarm: deep sleep
« Reply #10 on: November 08, 2016, 05:12:00 am »
I need to think about  / test before I say with certainty, but I'm thinking you can use a diode to allow the oak to still monitor the reed switch once it's bypassed it for power. Otherwise, there are reed switches that have normally open AND normally closed contacts, so that could be used if needed.

davide

  • Newbie
  • *
  • Posts: 21
Re: Door alarm: deep sleep
« Reply #11 on: November 08, 2016, 12:49:12 pm »
Probably you are right...
I just thought something about the capacitor... Probably it won't work. I read that its charging time is the same than the discharging time. Let's assume that this time is 15sec. If I open and close the door within 15 seconds (which is possible), what will happen? The Oak will start when the door is already closed... Perhaps it will publish "door open" "door closed", but I'm not sure about it...

PeterF

  • Hero Member
  • *****
  • Posts: 881
Re: Door alarm: deep sleep
« Reply #12 on: November 08, 2016, 04:39:01 pm »
Charge time depends on the power source... capacitors can charge nearly instantly given the right power source. More realistically though,  I can charge a 5.5v 1F capacitor in less than 5 seconds on a USB power bank that can deliver 1A, and then run a 5mm red LED for more than half an hour, or an Oak for 20 seconds. On other power supplies, it may take longer. The catch with capacitors is that they will discharge if not used, so you really need to "charge" them and use them - they're not good for "charging" and storing for later use.

davide

  • Newbie
  • *
  • Posts: 21
Re: Door alarm: deep sleep
« Reply #13 on: November 09, 2016, 02:40:42 am »
I would like to power it with 4 aaa batteries (6V). I'll tell you that happens...