From 6eeb9177f91310bccb00e6f3ee74b8bf39da1b95 Mon Sep 17 00:00:00 2001 From: Dirk Jahnke Date: Thu, 15 Nov 2018 21:39:21 +0100 Subject: [PATCH] Added deep sleep to save battery -- but this solution is not clean, as deep sleep is possible for an hour only and peripherals (display) is not turned off. --- src/main.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/main.cpp b/src/main.cpp index e03276e..1492277 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -16,6 +16,7 @@ #include "radio.h" #define AUTOSTART_CLOCK_AFTER_ms 10000 +#define POWER_OFF_PIN 0 //define your default values here, if there are different values in config.json, they are overwritten. //length should be max size + 1 @@ -134,9 +135,12 @@ void setupWifiConnection() { } void setup() { + // if coming from deep sleep, we just go to sleep again + // put your setup code here, to run once: Serial.begin(115200); Serial.println("Starting *** FastclockMasterESP8266"); + Serial.println(ESP.getResetReason()); display.begin(); display.showLog(); @@ -205,6 +209,7 @@ void setup() { radio.begin(); fastclock.begin(); + pinMode(POWER_OFF_PIN, INPUT); } bool requestToRegisterSent = false; @@ -221,6 +226,24 @@ 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; + } + #if defined(AUTOSTART_CLOCK_AFTER_ms) #if AUTOSTART_CLOCK_AFTER_ms > 0 static unsigned long autostart_ms = 0;