Refactored and fixed power off handling. Still power off does power on again after deep sleep time is over (~1h).
This commit is contained in:
		
							
								
								
									
										42
									
								
								src/main.cpp
									
									
									
									
									
								
							
							
						
						
									
										42
									
								
								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
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user