Improve lost data detection

This commit is contained in:
Djuri Baars 2024-04-12 00:17:40 +02:00
parent 91fc474a1f
commit 2ef56c1938
4 changed files with 49 additions and 34 deletions

View file

@ -9,6 +9,7 @@ Adafruit_MCP23X17 mcp2;
#endif #endif
std::vector<std::string> screenNameMap(SCREEN_COUNT); std::vector<std::string> screenNameMap(SCREEN_COUNT);
std::mutex mcpMutex; std::mutex mcpMutex;
uint lastTimeSync;
void setup() void setup()
{ {
@ -46,7 +47,7 @@ void setup()
setupWebserver(); setupWebserver();
// setupWifi(); // setupWifi();
setupTime(); syncTime();
finishSetup(); finishSetup();
setupTasks(); setupTasks();
@ -193,7 +194,7 @@ void tryImprovSetup()
// queueLedEffect(LED_EFFECT_WIFI_CONNECT_SUCCESS); // queueLedEffect(LED_EFFECT_WIFI_CONNECT_SUCCESS);
} }
void setupTime() void syncTime()
{ {
configTime(preferences.getInt("gmtOffset", TIME_OFFSET_SECONDS), 0, configTime(preferences.getInt("gmtOffset", TIME_OFFSET_SECONDS), 0,
NTP_SERVER); NTP_SERVER);
@ -206,6 +207,8 @@ void setupTime()
delay(500); delay(500);
Serial.println(F("Retry set time")); Serial.println(F("Retry set time"));
} }
lastTimeSync = esp_timer_get_time() / 1000000;
} }
void setupPreferences() void setupPreferences()
@ -300,7 +303,7 @@ void setupHardware()
if (!LittleFS.open("/index.html.gz", "r")) if (!LittleFS.open("/index.html.gz", "r"))
{ {
Serial.println("Error loading WebUI"); Serial.println(F("Error loading WebUI"));
} }
setupLeds(); setupLeds();
@ -568,25 +571,25 @@ void WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info)
switch (event) switch (event)
{ {
case ARDUINO_EVENT_WIFI_READY: case ARDUINO_EVENT_WIFI_READY:
Serial.println("WiFi interface ready"); Serial.println(F("WiFi interface ready"));
break; break;
case ARDUINO_EVENT_WIFI_SCAN_DONE: case ARDUINO_EVENT_WIFI_SCAN_DONE:
Serial.println("Completed scan for access points"); Serial.println(F("Completed scan for access points"));
break; break;
case ARDUINO_EVENT_WIFI_STA_START: case ARDUINO_EVENT_WIFI_STA_START:
Serial.println("WiFi client started"); Serial.println(F("WiFi client started"));
break; break;
case ARDUINO_EVENT_WIFI_STA_STOP: case ARDUINO_EVENT_WIFI_STA_STOP:
Serial.println("WiFi clients stopped"); Serial.println(F("WiFi clients stopped"));
break; break;
case ARDUINO_EVENT_WIFI_STA_CONNECTED: case ARDUINO_EVENT_WIFI_STA_CONNECTED:
Serial.println("Connected to access point"); Serial.println(F("Connected to access point"));
break; break;
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
{ {
if (!first_connect) if (!first_connect)
{ {
Serial.println("Disconnected from WiFi access point"); Serial.println(F("Disconnected from WiFi access point"));
queueLedEffect(LED_EFFECT_WIFI_CONNECT_ERROR); queueLedEffect(LED_EFFECT_WIFI_CONNECT_ERROR);
uint8_t reason = info.wifi_sta_disconnected.reason; uint8_t reason = info.wifi_sta_disconnected.reason;
if (reason) if (reason)
@ -596,7 +599,7 @@ void WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info)
break; break;
} }
case ARDUINO_EVENT_WIFI_STA_AUTHMODE_CHANGE: 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; break;
case ARDUINO_EVENT_WIFI_STA_GOT_IP: case ARDUINO_EVENT_WIFI_STA_GOT_IP:
{ {
@ -608,33 +611,33 @@ void WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info)
break; break;
} }
case ARDUINO_EVENT_WIFI_STA_LOST_IP: 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); queueLedEffect(LED_EFFECT_WIFI_CONNECT_ERROR);
WiFi.reconnect(); WiFi.reconnect();
break; break;
case ARDUINO_EVENT_WIFI_AP_START: case ARDUINO_EVENT_WIFI_AP_START:
Serial.println("WiFi access point started"); Serial.println(F("WiFi access point started"));
break; break;
case ARDUINO_EVENT_WIFI_AP_STOP: case ARDUINO_EVENT_WIFI_AP_STOP:
Serial.println("WiFi access point stopped"); Serial.println(F("WiFi access point stopped"));
break; break;
case ARDUINO_EVENT_WIFI_AP_STACONNECTED: case ARDUINO_EVENT_WIFI_AP_STACONNECTED:
Serial.println("Client connected"); Serial.println(F("Client connected"));
break; break;
case ARDUINO_EVENT_WIFI_AP_STADISCONNECTED: case ARDUINO_EVENT_WIFI_AP_STADISCONNECTED:
Serial.println("Client disconnected"); Serial.println(F("Client disconnected"));
break; break;
case ARDUINO_EVENT_WIFI_AP_STAIPASSIGNED: case ARDUINO_EVENT_WIFI_AP_STAIPASSIGNED:
Serial.println("Assigned IP address to client"); Serial.println(F("Assigned IP address to client"));
break; break;
case ARDUINO_EVENT_WIFI_AP_PROBEREQRECVED: case ARDUINO_EVENT_WIFI_AP_PROBEREQRECVED:
Serial.println("Received probe request"); Serial.println(F("Received probe request"));
break; break;
case ARDUINO_EVENT_WIFI_AP_GOT_IP6: case ARDUINO_EVENT_WIFI_AP_GOT_IP6:
Serial.println("AP IPv6 is preferred"); Serial.println(F("AP IPv6 is preferred"));
break; break;
case ARDUINO_EVENT_WIFI_STA_GOT_IP6: case ARDUINO_EVENT_WIFI_STA_GOT_IP6:
Serial.println("STA IPv6 is preferred"); Serial.println(F("STA IPv6 is preferred"));
break; break;
default: default:
break; break;
@ -652,3 +655,7 @@ String getMyHostname()
mac[3], mac[4], mac[5]); mac[3], mac[4], mac[5]);
return hostname; return hostname;
} }
uint getLastTimeSync() {
return lastTimeSync;
}

View file

@ -33,7 +33,8 @@
#define DEFAULT_BG_COLOR GxEPD_BLACK #define DEFAULT_BG_COLOR GxEPD_BLACK
void setup(); void setup();
void setupTime(); void syncTime();
uint getLastTimeSync();
void setupPreferences(); void setupPreferences();
void setupWebsocketClients(void *pvParameters); void setupWebsocketClients(void *pvParameters);
void setupHardware(); void setupHardware();

View file

@ -127,7 +127,7 @@ void downloadUpdate() {
} }
void onOTAError(ota_error_t error) { void onOTAError(ota_error_t error) {
Serial.println("\nOTA update error, restarting"); Serial.println(F("\nOTA update error, restarting"));
Wire.end(); Wire.end();
SPI.end(); SPI.end();
delay(1000); delay(1000);
@ -135,7 +135,7 @@ void onOTAError(ota_error_t error) {
} }
void onOTAComplete() { void onOTAComplete() {
Serial.println("\nOTA update finished"); Serial.println(F("\nOTA update finished"));
Wire.end(); Wire.end();
SPI.end(); SPI.end();
delay(1000); delay(1000);

View file

@ -49,12 +49,12 @@ extern "C" void app_main()
if (!wifiLostConnection) if (!wifiLostConnection)
{ {
wifiLostConnection = currentUptime; wifiLostConnection = currentUptime;
Serial.println("Lost WiFi connection, trying to reconnect..."); Serial.println(F("Lost WiFi connection, trying to reconnect..."));
} }
if ((currentUptime - wifiLostConnection) > 600) 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); delay(2000);
ESP.restart(); ESP.restart();
} }
@ -64,33 +64,35 @@ extern "C" void app_main()
else if (wifiLostConnection) else if (wifiLostConnection)
{ {
wifiLostConnection = 0; 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++; priceNotifyLostConnection++;
Serial.println("Lost price data connection..."); Serial.println(F("Lost price data connection..."));
queueLedEffect(LED_DATA_PRICE_ERROR); queueLedEffect(LED_DATA_PRICE_ERROR);
// if price WS connection does not come back after 6*5 seconds, destroy and recreate // if price WS connection does not come back after 6*5 seconds, destroy and recreate
if (priceNotifyLostConnection > 6) if (priceNotifyLostConnection > 6)
{ {
Serial.println("Restarting price handler..."); Serial.println(F("Restarting price handler..."));
stopPriceNotify(); stopPriceNotify();
setupPriceNotify(); setupPriceNotify();
priceNotifyLostConnection = 0; priceNotifyLostConnection = 0;
} }
} }
else if (getBlockNotifyInit() && !isBlockNotifyConnected())
if (getBlockNotifyInit() && !isBlockNotifyConnected())
{ {
blockNotifyLostConnection++; blockNotifyLostConnection++;
Serial.println("Lost block data connection..."); Serial.println(F("Lost block data connection..."));
queueLedEffect(LED_DATA_BLOCK_ERROR); queueLedEffect(LED_DATA_BLOCK_ERROR);
// if mempool WS connection does not come back after 6*5 seconds, destroy and recreate // if mempool WS connection does not come back after 6*5 seconds, destroy and recreate
if (blockNotifyLostConnection > 6) if (blockNotifyLostConnection > 6)
{ {
Serial.println("Restarting block handler..."); Serial.println(F("Restarting block handler..."));
stopBlockNotify(); stopBlockNotify();
setupBlockNotify(); 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 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)) 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(); stopPriceNotify();
setupPriceNotify(); setupPriceNotify();
@ -117,13 +119,13 @@ extern "C" void app_main()
// If after 45 minutes no mempool blocks, check the rest API // If after 45 minutes no mempool blocks, check the rest API
if ((getLastBlockUpdate() - currentUptime) > 45 * 60) 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(); int currentBlock = getBlockFetch();
if (currentBlock != -1) if (currentBlock != -1)
{ {
if (currentBlock != getBlockHeight()) 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 // Mempool source stuck, restart
stopBlockNotify(); stopBlockNotify();
setupBlockNotify(); 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)); vTaskDelay(pdMS_TO_TICKS(5000));
} }
} }