From b1952bebe4a98d32310246dfec2f2953f8249371 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Wed, 13 May 2020 21:28:28 +0200 Subject: [PATCH] esp_clock: disable WiFi for 1 hour --- esp_clock.ino | 82 ++++++++++++++++++++++++++++----------------------- 1 file changed, 45 insertions(+), 37 deletions(-) diff --git a/esp_clock.ino b/esp_clock.ino index d7db4d9..8a4952d 100644 --- a/esp_clock.ino +++ b/esp_clock.ino @@ -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 int status = WL_IDLE_STATUS; bool shouldUpdate = true; +String currentSSID; +String currentPsk; ESP8266WiFiMulti wifiMulti; WiFiUDP ntpUDP; @@ -55,6 +57,8 @@ void setup() { delay(500); } Serial.println("Connected to network."); + currentSSID = WiFi.SSID(); + currentPsk = WiFi.psk(); printWiFiStatus(); // Display WiFi status data setupOTA(); @@ -74,6 +78,7 @@ void loop() { minutes = timeClient.getMinutes(); seconds = timeClient.getSeconds(); Serial.println(timeClient.getFormattedTime()); + WiFi.forceSleepBegin(); } else { incrementTime(); } @@ -82,14 +87,28 @@ void loop() { } void incrementTime() { - if (++seconds == 60) { + if (++seconds >= 60) { seconds = 0; if (++minutes == 60) { minutes = 0; Serial.println("Minutes set to zero - should query NTP on next loop()"); 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. if (displayHour == 0) { clockDisplay.writeDigitNum(1, 0); + if (minutes < 10) { + clockDisplay.writeDigitNum(3, 0); + } } // Blink the colon by flipping its value every loop iteration @@ -117,41 +139,27 @@ void displayTime() { } void setupOTA() { - ArduinoOTA.onStart([]() { - String type; - if (ArduinoOTA.getCommand() == U_FLASH) { - type = "sketch"; - } else { // U_FS - type = "filesystem"; - } - - // NOTE: if updating FS this would be the place to unmount FS using FS.end() - Serial.println("Start updating " + type); - }); - ArduinoOTA.onEnd([]() { - Serial.println("\nEnd"); - }); - ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { - Serial.printf("Progress: %u%%\r", (progress / (total / 100))); - }); - ArduinoOTA.onError([](ota_error_t error) { - Serial.printf("Error[%u]: ", error); - if (error == OTA_AUTH_ERROR) { - Serial.println("Auth Failed"); - } 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()); + ArduinoOTA.onStart([]() { + Serial.println("Start"); + }); + ArduinoOTA.onEnd([]() { + Serial.println("\nEnd"); + }); + ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { + Serial.printf("Progress: %u%%\r", (progress / (total / 100))); + }); + ArduinoOTA.onError([](ota_error_t error) { + Serial.printf("Error[%u]: ", error); + if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed"); + 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() {