So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By ezcGman
#88314 Hey there,

this is my first post about my first ESP8266 project, so bear with me :) I am a software engineer, but never ended up doing some micro controller programming. Now with doing more and more home automation, I also started making devices smart, which are not close to be smart :)
Like my doorbell, which is the project I have a question about :)

First, the code: https://github.com/gman-php/esp8266-ske ... orbell.ino

I live in a pretty new apartment in Germany and I have one of these doorbell phones most people have. Luckily, it has an interface to connect external signals / doorbells, so that's where you can capture the doorbell signal. It's simply sending a 5V signal on one contact if somebody is ringing the doorbell.

So the whole code and the project / hardware wiring works. No problem, everything cool :)
My question is about power consumption. Unfortunately, I have no power source near the phone and the phone itself really doesn't like it when you steal from it's 24V output, pull it down to 5V and connect it to the ESP. For some reason, you get a huge noise on the phone, even with capacitors. So I need to live with a battery pack (for now!).

But as you know, power consumption is pretty high, but there are sleep modes. I was reading through them a bit, but didn't 100% understand how they work and if I can make use of them.

Why I think I can't use them? Because I have a case where the ESP needs to check for a signal on GPIO4 all the time, because somebody could ring the door. I don't know when this will happen. So I always need to plan with it.

And it seems that in every sleep mode, it also looses the Wi-Fi and MQTT connection!? So even if I do 1 second modem/light/deep sleep, I will still then need to go through Wi-Fi and MQTT connection before I can inform my Home Assistant (HA) about the poor person, standing out there in the rain ringing the doorbell, probably seconds ago!?

So I'm asking straightforward: Do you see any chance for using any sleep mode in my use case and also having a few hints/links/example code lines showing how to use them? I can live with around 1-3 seconds of total delay for the doorbell signal to reach my HA. But I think more than that is a bit long.

Thank you so much already and greetings,

Andy!
User avatar
By btidey
#88317 This is a case where you want to use the doorbell signal to wake the esp out of deep sleep.

To do this you tap the 5V signal down to 3.3V and wire it to the EN signal so that when it goes high then the device wakes up and performs whatever action / notification you want before entering a deep sleep state and waiting for the next wake up.

The battery consumption is then very low most of the time and the device just consumes current for the few seconds whilst it is active every time there is a door bell ring.

One thing you have to deal with is that the doorbell 5V signal might go away before the action has completed. One can deal with this by using a GPIO to feedback a holding signal to EN a soon as the device starts up and then this is released just before going back into deep sleep. You have to use diodes etc to isolate the actions of the doorbell signal and the hold feedback.

Other items to consider are what module you use. The development type boards consume a bit extra in deep sleep due to the usb / serial interface. The bare-boards like ESP12-F minimise the current in deep sleep.

Using these techniques a battery powered sensor like this can last a long time (e.g. 12 months) even when powered by something like a small 400mAh Lion battery.

For a similar example see
https://github.com/roberttidey/SecuritySensor

In this case the wake up is active low but can be modified to be fed by an active high fairly easily.
User avatar
By ezcGman
#88321 Wow, thanks already for the very detailed answer! I actually have to work through it a bit, as I don't (yet) understand everything and don't have the time today to play around.

This is a case where you want to use the doorbell signal to wake the esp out of deep sleep.

First question directly: Doesn't the ESP loose it's Wi-Fi connection in deep sleep? So whenever it wakes up, it first needs to go through Wi-Fi conenction and MQTT connection. That already I think takes 3 seconds :) I probably just have to test it how long it takes :)

To do this you tap the 5V signal down to 3.3V and wire it to the EN signal so that when it goes high then the device wakes up and performs whatever action / notification you want before entering a deep sleep state and waiting for the next wake up.

Brace yourself for a wave of questions :)
1. I googled a bit and now learned that the EN PIN is called "Chip Enable", right? So just pulling it HIGH wakes it up? That actually makes sense, compared to what I read on some pages: A few guides tell you to conenct D0 to RST. Without that deep sleep doesn't work or something; I honestly didn't fully understand it.
So that wiring is not required? Simply a 3V3 on EN is enough to wake it up?
2. Do I really need to pull it HIGH? Is it possible to have it configured PULLUP and then just pull LOW to wake the ESP up? That's what I actually do with the doorbell / GPIO4: I have the 5V connected to a transistors base and emitter and collector go to GPIO4 and GND on the ESP. This way, I don't have to deal with pulling the 5V down to 3V3 ;)
3. Is it correct, that I would basically then have nothing / could have nothing in my loop() function, as the ESP goes through setup() every time it wakes up? Or is setup() not called on wakeup?

One thing you have to deal with is that the doorbell 5V signal might go away before the action has completed.

Yeah, I already thought that. But this may be fine, as the board will only wake u if there is somebody at the door. I would basically not need the current setup I have; I can just always on wakeup assume that somebody is at the door and let my HA know over MQTT.

One can deal with this by using a GPIO to feedback a holding signal to EN a soon as the device starts up and then this is released just before going back into deep sleep. You have to use diodes etc to isolate the actions of the doorbell signal and the hold feedback.

That I have to work through when I have more time, hopefully tomorrow :) Don't really understand that (yet!) ;)

For a similar example see
https://github.com/roberttidey/SecuritySensor

Thanks for the link! Again: I need some time to work this through; it's more code than I expected/thought :D

Thank you so much for the help already! That's awesome!

Greetings,

Andy!
User avatar
By btidey
#88327 Yes. Wifi connection is not maintained in deep sleep. So it has to reconnect when it wakes up. This does take about 3 seconds normally so the overall wake active time is normally about 1 second longer as you perform an action like mqtt. You can reduce the 3 seconds quite a bit if you are prepared to set the ip address locally on the esp8266 instead of using dhcp for auto ip allocation as that does take quite a large slice of this 3 seconds.

One thing to realise about deep sleep is that effectively the chip needs to be reset to wake it up and it then goes through the same process as a power up. There are 3 ways to perform this reset. One is by connecting GPIO16 to reset. This is used when wanting to have a timed deep sleep period. A low power timer is kept going and will produce a pulse on GPIO16 when the sleep period expires. The second way is to externally pulse the reset pin. The third way is to use the EN pin. When this is low then the chip is off and has a similar power draw to deep sleep. When brought high the chip will start up. The other advantage of using EN rather than RESET is that it is a high impedance logic input whereas RESET normally has a pull up and a timing capacitor making it more tricky to drive from an external signal.

So using the EN method the sequence goes like... External signal goes high, chip wakes up, first action before even connecting to wifi is to set a GPIO high to keep EN high even if external signal goes away, connect wifi and perform app actions, remove EN hold and put chip into deep sleep(not timed). The reason for doing this last step is to make sure the chip goes low power even if the external signal stays high. It must go low and then go high again to trigger another cycle.

The hold is necessary as if the doorbell external signal goes away and EN goes low then the chip stops processing immediately even if it has not completed its action.

It is easiest to use the active high method from the doorbell signal. You can just use a simple potential divider like a 22K / 33k resistor to move from the 5V signal to 3.3V. As I say the EN is very high input impedance and in this case the doorbell signal is driving the resistors so that is not consuming any current from the esp8266 supply.

You could code it so it is all in setup. However, I prefer to split it so that GPIO, hold and wifi connection is in set up and the actions are in loop. Normally loop will only get executed once as it falls into deep sleep, but it does mean you can easily put in checking and retry logic if needed.

Reason why the example code is quite long is that it is has wifi management to set up wifi credentials initially, OTA updating of the firmware, and remote fetching of configuration parameters, database reporting and sending notifications to phones via a service. That can obviously be simplified a lot.