diff --git a/src/lib/config.cpp b/src/lib/config.cpp index 98911e4..19b8132 100644 --- a/src/lib/config.cpp +++ b/src/lib/config.cpp @@ -9,6 +9,7 @@ Adafruit_MCP23X17 mcp2; #endif std::vector screenNameMap(SCREEN_COUNT); std::mutex mcpMutex; +uint lastTimeSync; void setup() { @@ -46,7 +47,7 @@ void setup() setupWebserver(); // setupWifi(); - setupTime(); + syncTime(); finishSetup(); setupTasks(); @@ -193,7 +194,7 @@ void tryImprovSetup() // queueLedEffect(LED_EFFECT_WIFI_CONNECT_SUCCESS); } -void setupTime() +void syncTime() { configTime(preferences.getInt("gmtOffset", TIME_OFFSET_SECONDS), 0, NTP_SERVER); @@ -206,6 +207,8 @@ void setupTime() delay(500); Serial.println(F("Retry set time")); } + + lastTimeSync = esp_timer_get_time() / 1000000; } void setupPreferences() @@ -300,7 +303,7 @@ void setupHardware() if (!LittleFS.open("/index.html.gz", "r")) { - Serial.println("Error loading WebUI"); + Serial.println(F("Error loading WebUI")); } setupLeds(); @@ -568,25 +571,25 @@ void WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) switch (event) { case ARDUINO_EVENT_WIFI_READY: - Serial.println("WiFi interface ready"); + Serial.println(F("WiFi interface ready")); break; case ARDUINO_EVENT_WIFI_SCAN_DONE: - Serial.println("Completed scan for access points"); + Serial.println(F("Completed scan for access points")); break; case ARDUINO_EVENT_WIFI_STA_START: - Serial.println("WiFi client started"); + Serial.println(F("WiFi client started")); break; case ARDUINO_EVENT_WIFI_STA_STOP: - Serial.println("WiFi clients stopped"); + Serial.println(F("WiFi clients stopped")); break; case ARDUINO_EVENT_WIFI_STA_CONNECTED: - Serial.println("Connected to access point"); + Serial.println(F("Connected to access point")); break; case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: { if (!first_connect) { - Serial.println("Disconnected from WiFi access point"); + Serial.println(F("Disconnected from WiFi access point")); queueLedEffect(LED_EFFECT_WIFI_CONNECT_ERROR); uint8_t reason = info.wifi_sta_disconnected.reason; if (reason) @@ -596,7 +599,7 @@ void WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) break; } case ARDUINO_EVENT_WIFI_STA_AUTHMODE_CHANGE: - Serial.println("Authentication mode of access point has changed"); + Serial.println(F("Authentication mode of access point has changed")); break; case ARDUINO_EVENT_WIFI_STA_GOT_IP: { @@ -608,33 +611,33 @@ void WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) break; } case ARDUINO_EVENT_WIFI_STA_LOST_IP: - Serial.println("Lost IP address and IP address is reset to 0"); + Serial.println(F("Lost IP address and IP address is reset to 0")); queueLedEffect(LED_EFFECT_WIFI_CONNECT_ERROR); WiFi.reconnect(); break; case ARDUINO_EVENT_WIFI_AP_START: - Serial.println("WiFi access point started"); + Serial.println(F("WiFi access point started")); break; case ARDUINO_EVENT_WIFI_AP_STOP: - Serial.println("WiFi access point stopped"); + Serial.println(F("WiFi access point stopped")); break; case ARDUINO_EVENT_WIFI_AP_STACONNECTED: - Serial.println("Client connected"); + Serial.println(F("Client connected")); break; case ARDUINO_EVENT_WIFI_AP_STADISCONNECTED: - Serial.println("Client disconnected"); + Serial.println(F("Client disconnected")); break; case ARDUINO_EVENT_WIFI_AP_STAIPASSIGNED: - Serial.println("Assigned IP address to client"); + Serial.println(F("Assigned IP address to client")); break; case ARDUINO_EVENT_WIFI_AP_PROBEREQRECVED: - Serial.println("Received probe request"); + Serial.println(F("Received probe request")); break; case ARDUINO_EVENT_WIFI_AP_GOT_IP6: - Serial.println("AP IPv6 is preferred"); + Serial.println(F("AP IPv6 is preferred")); break; case ARDUINO_EVENT_WIFI_STA_GOT_IP6: - Serial.println("STA IPv6 is preferred"); + Serial.println(F("STA IPv6 is preferred")); break; default: break; @@ -651,4 +654,8 @@ String getMyHostname() snprintf(hostname, sizeof(hostname), "%s-%02x%02x%02x", hostnamePrefix, mac[3], mac[4], mac[5]); return hostname; +} + +uint getLastTimeSync() { + return lastTimeSync; } \ No newline at end of file diff --git a/src/lib/config.hpp b/src/lib/config.hpp index cd4b262..4f5a83c 100644 --- a/src/lib/config.hpp +++ b/src/lib/config.hpp @@ -33,7 +33,8 @@ #define DEFAULT_BG_COLOR GxEPD_BLACK void setup(); -void setupTime(); +void syncTime(); +uint getLastTimeSync(); void setupPreferences(); void setupWebsocketClients(void *pvParameters); void setupHardware(); diff --git a/src/lib/ota.cpp b/src/lib/ota.cpp index 945a4d4..ea82348 100644 --- a/src/lib/ota.cpp +++ b/src/lib/ota.cpp @@ -127,7 +127,7 @@ void downloadUpdate() { } void onOTAError(ota_error_t error) { - Serial.println("\nOTA update error, restarting"); + Serial.println(F("\nOTA update error, restarting")); Wire.end(); SPI.end(); delay(1000); @@ -135,7 +135,7 @@ void onOTAError(ota_error_t error) { } void onOTAComplete() { - Serial.println("\nOTA update finished"); + Serial.println(F("\nOTA update finished")); Wire.end(); SPI.end(); delay(1000); diff --git a/src/main.cpp b/src/main.cpp index fb7148a..4968eda 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -49,12 +49,12 @@ extern "C" void app_main() if (!wifiLostConnection) { wifiLostConnection = currentUptime; - Serial.println("Lost WiFi connection, trying to reconnect..."); + Serial.println(F("Lost WiFi connection, trying to reconnect...")); } if ((currentUptime - wifiLostConnection) > 600) { - Serial.println("Still no connection after 10 minutes, restarting..."); + Serial.println(F("Still no connection after 10 minutes, restarting...")); delay(2000); ESP.restart(); } @@ -64,33 +64,35 @@ extern "C" void app_main() else if (wifiLostConnection) { wifiLostConnection = 0; - Serial.println("Connection restored, reset timer."); + Serial.println(F("Connection restored, reset timer.")); } - else if (getPriceNotifyInit() && !preferences.getBool("fetchEurPrice", false) && !isPriceNotifyConnected()) + + if (getPriceNotifyInit() && !preferences.getBool("fetchEurPrice", false) && !isPriceNotifyConnected()) { priceNotifyLostConnection++; - Serial.println("Lost price data connection..."); + Serial.println(F("Lost price data connection...")); queueLedEffect(LED_DATA_PRICE_ERROR); // if price WS connection does not come back after 6*5 seconds, destroy and recreate if (priceNotifyLostConnection > 6) { - Serial.println("Restarting price handler..."); + Serial.println(F("Restarting price handler...")); stopPriceNotify(); setupPriceNotify(); priceNotifyLostConnection = 0; } } - else if (getBlockNotifyInit() && !isBlockNotifyConnected()) + + if (getBlockNotifyInit() && !isBlockNotifyConnected()) { blockNotifyLostConnection++; - Serial.println("Lost block data connection..."); + Serial.println(F("Lost block data connection...")); queueLedEffect(LED_DATA_BLOCK_ERROR); // if mempool WS connection does not come back after 6*5 seconds, destroy and recreate if (blockNotifyLostConnection > 6) { - Serial.println("Restarting block handler..."); + Serial.println(F("Restarting block handler...")); stopBlockNotify(); setupBlockNotify(); @@ -106,7 +108,7 @@ extern "C" void app_main() // if more than 5 price updates are missed, there is probably something wrong, reconnect if ((getLastPriceUpdate() - currentUptime) > (preferences.getUInt("minSecPriceUpd", DEFAULT_SECONDS_BETWEEN_PRICE_UPDATE) * 5)) { - Serial.println("Detected 5 missed price updates... restarting price handler."); + Serial.println(F("Detected 5 missed price updates... restarting price handler.")); stopPriceNotify(); setupPriceNotify(); @@ -117,13 +119,13 @@ extern "C" void app_main() // If after 45 minutes no mempool blocks, check the rest API if ((getLastBlockUpdate() - currentUptime) > 45 * 60) { - Serial.println("Long time (45 min) since last block, checking if I missed anything..."); + Serial.println(F("Long time (45 min) since last block, checking if I missed anything...")); int currentBlock = getBlockFetch(); if (currentBlock != -1) { if (currentBlock != getBlockHeight()) { - Serial.println("Detected stuck block height... restarting block handler."); + Serial.println(F("Detected stuck block height... restarting block handler.")); // Mempool source stuck, restart stopBlockNotify(); setupBlockNotify(); @@ -133,6 +135,11 @@ extern "C" void app_main() } } + if (currentUptime - getLastTimeSync() > 24 * 60 * 60) { + Serial.println(F("Last time update is longer than 24 hours ago, sync again")); + syncTime(); + }; + vTaskDelay(pdMS_TO_TICKS(5000)); } } \ No newline at end of file