esp_clock: disable WiFi for 1 hour

This commit is contained in:
Nicu Hodos 2020-05-13 21:28:28 +02:00
parent 611124d166
commit b1952bebe4

View File

@ -39,6 +39,8 @@ byte displayHours[24] = {12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4,
bool blinkColon = false; // Track colon status to blink once/sec bool blinkColon = false; // Track colon status to blink once/sec
int status = WL_IDLE_STATUS; int status = WL_IDLE_STATUS;
bool shouldUpdate = true; bool shouldUpdate = true;
String currentSSID;
String currentPsk;
ESP8266WiFiMulti wifiMulti; ESP8266WiFiMulti wifiMulti;
WiFiUDP ntpUDP; WiFiUDP ntpUDP;
@ -55,6 +57,8 @@ void setup() {
delay(500); delay(500);
} }
Serial.println("Connected to network."); Serial.println("Connected to network.");
currentSSID = WiFi.SSID();
currentPsk = WiFi.psk();
printWiFiStatus(); // Display WiFi status data printWiFiStatus(); // Display WiFi status data
setupOTA(); setupOTA();
@ -74,6 +78,7 @@ void loop() {
minutes = timeClient.getMinutes(); minutes = timeClient.getMinutes();
seconds = timeClient.getSeconds(); seconds = timeClient.getSeconds();
Serial.println(timeClient.getFormattedTime()); Serial.println(timeClient.getFormattedTime());
WiFi.forceSleepBegin();
} else { } else {
incrementTime(); incrementTime();
} }
@ -82,14 +87,28 @@ void loop() {
} }
void incrementTime() { void incrementTime() {
if (++seconds == 60) { if (++seconds >= 60) {
seconds = 0; seconds = 0;
if (++minutes == 60) { if (++minutes == 60) {
minutes = 0; minutes = 0;
Serial.println("Minutes set to zero - should query NTP on next loop()"); Serial.println("Minutes set to zero - should query NTP on next loop()");
if (++hours == 24) hours = 0; if (++hours == 24) hours = 0;
} }
if (((minutes % 5) == 0)) shouldUpdate = true; if ((minutes == 0)) {
WiFi.forceSleepWake();
WiFi.begin(currentSSID.c_str(), currentPsk.c_str());
for (int i = 0; i < 4; i++) {
Serial.println("Reconnecting to WiFi netowrk...");
delay(1000);
seconds++;
}
if (WiFi.status() == WL_CONNECTED) {
shouldUpdate = true;
} else {
WiFi.forceSleepBegin();
}
printWiFiStatus();
}
} }
} }
@ -105,6 +124,9 @@ void displayTime() {
// which can look confusing. Go in and explicitly add these zeros. // which can look confusing. Go in and explicitly add these zeros.
if (displayHour == 0) { if (displayHour == 0) {
clockDisplay.writeDigitNum(1, 0); clockDisplay.writeDigitNum(1, 0);
if (minutes < 10) {
clockDisplay.writeDigitNum(3, 0);
}
} }
// Blink the colon by flipping its value every loop iteration // Blink the colon by flipping its value every loop iteration
@ -117,41 +139,27 @@ void displayTime() {
} }
void setupOTA() { void setupOTA() {
ArduinoOTA.onStart([]() { ArduinoOTA.onStart([]() {
String type; Serial.println("Start");
if (ArduinoOTA.getCommand() == U_FLASH) { });
type = "sketch"; ArduinoOTA.onEnd([]() {
} else { // U_FS Serial.println("\nEnd");
type = "filesystem"; });
} ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
// NOTE: if updating FS this would be the place to unmount FS using FS.end() });
Serial.println("Start updating " + type); ArduinoOTA.onError([](ota_error_t error) {
}); Serial.printf("Error[%u]: ", error);
ArduinoOTA.onEnd([]() { if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
Serial.println("\nEnd"); else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
}); else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
Serial.printf("Progress: %u%%\r", (progress / (total / 100))); else if (error == OTA_END_ERROR) Serial.println("End Failed");
}); });
ArduinoOTA.onError([](ota_error_t error) { ArduinoOTA.begin();
Serial.printf("Error[%u]: ", error); Serial.println("Ready");
if (error == OTA_AUTH_ERROR) { Serial.print("IP address: ");
Serial.println("Auth Failed"); Serial.println(WiFi.localIP());
} else if (error == OTA_BEGIN_ERROR) {
Serial.println("Begin Failed");
} else if (error == OTA_CONNECT_ERROR) {
Serial.println("Connect Failed");
} else if (error == OTA_RECEIVE_ERROR) {
Serial.println("Receive Failed");
} else if (error == OTA_END_ERROR) {
Serial.println("End Failed");
}
});
ArduinoOTA.begin();
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
} }
void printWiFiStatus() { void printWiFiStatus() {