2

In a project powered by batteries, the ESP8266 should go to deep sleep for long time, having RF disabled. After some number of sleep cycles, it shall restart with RF enabled, synchronize time via NTP, and again go to sleep. 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)?

Edit: Using external circuitry this might be possible. To avoid this, we switch to ESP32, which is an 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).

4
  • 1
    Does this answer your question? Wake up ESP8266 from deepSleep with a button
    – Juraj
    Jan 23, 2021 at 6:41
  • Well, not really. Resetting the ESP with a button is not the problem; in both cases /by button/by timer the ESP will be reset, running through setup() code. But how to know why it was reset? I read something about rtc_get_reset_reason() and ESP.getResetReason(); will try that and report on success.
    – ridgy
    Jan 23, 2021 at 11:14
  • In case of wakeup from deep sleep, rtc_get_reset_reason() always responds with "wakeup from deep sleep" - that is correct, but does not help here.
    – ridgy
    Jan 24, 2021 at 17:00
  • 1
    it is the same reset with button and with io 16. the software can't distinguish it. the reset with io 16 is a workaround for a bug in ROM code of the esp8266
    – Juraj
    Jan 24, 2021 at 17:05

2 Answers 2

1

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 reads the state of the pushbutton switch as it's 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 10's of millisec no matter how quickly it is pressed and released.

2
  • 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
    Jan 27, 2021 at 17:38
  • 1
    Ah, 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
    Jan 28, 2021 at 2:37
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.