In a project powered by batteries, the ESP8266 should go to deep sleep for a long time, having RF disabled. After some number of sleep cycles, it should restart with RF enabled, synchronize time via NTP, and again go to sleep. I think this could work with a counter saved in RTC Memory or SPIFFS.
Additionally, s.o. should be able to wake up via push button (at any time), which should result in different behaviour (running as WiFi AP, allowing for configuration of new WiFi credentials).
The question is: Is it possible to find out what did wake the ESP (timer or pushbutton)?
Using external circuitry this might be possible. To avoid this, we switch to ESP32, which is overkill for the project, but easy to find out the source of the wakeup (and with the possibility to use touch sensors). Additionally, ESP32 allows for easy use of "Preferences" in NVR and variables in RTC memory (when using the Arduino IDE), which will survive deep sleep (RTC) and even reset/power loss (NVR).
3 Answers 3
Place a capacitor on the ESP side of the button to one power rail or the other (my preference is to ground, but either should work), and have the setup()
routine read the state of the pushbutton switch as its first instruction. You probably don't even need the capacitor, but if you don't have it, you'll have to read the switch state multiple times to account for possible contact bounce.
If the ESP wakes up due to a timer, there will be no activity on the PB line; if it wakes up due to the PB being pressed, there will be activity on the PB line for several 10s of millisecinds no matter how quickly it is pressed and released.
-
The only button I know to wakeup an ESP8266 is the reset button - and this one as well has to be used by the timer. Or am I mistaken?ridgy– ridgy2021年01月27日 17:38:17 +00:00Commented Jan 27, 2021 at 17:38
-
1Ah, I thought the OP was referring to an external button. This solution still might work if the state of the reset button can be read programmatically during setup().starship15– starship152021年01月28日 02:37:37 +00:00Commented Jan 28, 2021 at 2:37
The capacitor solution works, but I wanted to expand on what I did in particular and why I found it beneficial. Here's my circuit: enter image description here
At wakeup, the first thing I do is read D1. After that, I then set D1 to an output and set it high to keep the capacitor charged. That prevents a second button press from resetting the ESP.
Then, when going to sleep, I set the pin low for a few ms to discharge the capacitor.
I picked the smallest capacitor possible because there's no downside to charging quickly.
Note that the ESP will reset as the button is pressed down - the pin will go low, and soon after go high as the capacitor charges, all before the button is released. This is ideal in circumstances where you want the act of pushing the button to cause the wake up, not the release.
If D0 is positive and D1 is negative, your shorting the power, BAD.... That will reset the chip all right, but burn the chip up... I would simply use an "and gate" like the MC74VHC1G08... I have not tried your Idea, But connecting 2 chip outputs together is never a good idea...
-
it is just TTL. It is how TTL works.2024年01月31日 06:30:40 +00:00Commented Jan 31, 2024 at 6:30
rtc_get_reset_reason()
andESP.getResetReason()
; will try that and report on success.rtc_get_reset_reason()
always responds with "wakeup from deep sleep" - that is correct, but does not help here.