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.
This commit is contained in:
		
							
								
								
									
										23
									
								
								src/main.cpp
									
									
									
									
									
								
							
							
						
						
									
										23
									
								
								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;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user