2

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).

4
  • 1
    Does this answer your question? Wake up ESP8266 from deepSleep with a button
    – Juraj
    Commented 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
    Commented 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
    Commented 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
    Commented Jan 24, 2021 at 17:05

3 Answers 3

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 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.

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
    Commented 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
    Commented 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.

0

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...

1
  • it is just TTL. It is how TTL works.
    – Juraj
    Commented Jan 31 at 6:30

Your Answer

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

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