From 97be2b6791143a2e0121ad8ff53caaf2da4ab798 Mon Sep 17 00:00:00 2001 From: Dirk Jahnke Date: Fri, 30 Nov 2018 12:46:26 +0100 Subject: [PATCH] Refactored and fixed power off handling. Still power off does power on again after deep sleep time is over (~1h). --- src/main.cpp | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 43b0f30..489673b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -212,6 +212,30 @@ void setup() { pinMode(POWER_OFF_PIN, INPUT); } +void checkForPowerOffRequest() { + static unsigned long powerOffButtonPressed_ts = 0; + static bool powerOffRequested = false; + + if (!digitalRead(POWER_OFF_PIN)) { // power off pressed + if (!powerOffRequested) { + Serial.println("Switching off? Keep button for 2 seconds to turn off."); + powerOffRequested = true; + powerOffButtonPressed_ts = millis(); + } + if (millis() - powerOffButtonPressed_ts > 2000) { + // pressed for longer than 2 seconds, now turn off + ESP.deepSleep(ESP.deepSleepMax()); + delay(200); + } + } else { + if (powerOffRequested) { + powerOffRequested = false; + Serial.println("Power off cancelled."); + powerOffButtonPressed_ts = 0; + } + } +} + bool requestToRegisterSent = false; void loop(void) @@ -226,23 +250,7 @@ void loop(void) return; } - static unsigned long powerOffButtonPressed_ts = 0; - static bool powerOffMessagePrinted = false; - if (!digitalRead(POWER_OFF_PIN)) { - if (!powerOffMessagePrinted) { - Serial.println("Switching off? Keep button for 2 seconds to turn off."); - powerOffMessagePrinted = true; - } - if (millis() - powerOffButtonPressed_ts > 2000) { - // pressed for longer than 2 seconds, now turn off - ESP.deepSleep(ESP.deepSleepMax()); - delay(200); - } - } else { - powerOffButtonPressed_ts = millis(); - if (powerOffMessagePrinted) Serial.println("Power off cancelled."); - powerOffMessagePrinted = false; - } + checkForPowerOffRequest(); #if defined(AUTOSTART_CLOCK_AFTER_ms) #if AUTOSTART_CLOCK_AFTER_ms > 0