From aa1e3b0de1f15c759ac61cf4399c6b89e29f99ad Mon Sep 17 00:00:00 2001 From: Kai Lauterbach Date: Wed, 14 Aug 2024 08:03:11 +0200 Subject: [PATCH] InfluxDB connection fix to prevent endless loop. --- firmware/config.h | 2 +- firmware/influxdb.ino | 68 ++++++++++++++++++++++++++----------------- 2 files changed, 42 insertions(+), 28 deletions(-) diff --git a/firmware/config.h b/firmware/config.h index 7d5b88b..d7c9b7f 100644 --- a/firmware/config.h +++ b/firmware/config.h @@ -3,7 +3,7 @@ // config general setting and behavior of the weatherstation -#define VERSION_STRING "1.1.3" +#define VERSION_STRING "1.1.5" #define WIFI_AUTOCONNECT_TIMEOUT_S 60 #define WIFI_CONFIG_PORTAL_TIMEOUT_S 120 diff --git a/firmware/influxdb.ino b/firmware/influxdb.ino index 5e3a023..adcbdba 100644 --- a/firmware/influxdb.ino +++ b/firmware/influxdb.ino @@ -179,40 +179,54 @@ void pushToInfluxDB(String device, float sensorValues[]) { } -void _writePoint() -{ +void _writePoint() { + const uint8_t maxRetries = 3; // Maximale Anzahl der Versuche + uint8_t attempt = 0; + bool success = false; - uint32_t _timeout = millis(); + while (attempt < maxRetries) { + attempt++; - //debug("InfluxDB2: check connection"); - do { - } while (client.validateConnection() and (_timeout + INFLUXDB_TIMEOUT_MS) <= millis()); + // Timeout für Verbindung prüfen + uint32_t startTime = millis(); + while (!client.validateConnection()) { + if (millis() - startTime > INFLUXDB_TIMEOUT_MS) { + debug("Timeout validating connection"); + break; // Timeout erreicht, aus Schleife ausbrechen + } + } - if (! client.validateConnection()) - { - debug("Can't write to InfluxDB2, timeout validating connection"); - return; - } + if (!client.validateConnection()) { + debug("Can't write to InfluxDB, connection validation failed on attempt " + String(attempt)); + continue; // Zum nächsten Versuch übergehen + } - _timeout = millis(); + // Timeout für canSendRequest prüfen + startTime = millis(); + while (!client.canSendRequest()) { + if (millis() - startTime > INFLUXDB_TIMEOUT_MS) { + debug("Timeout waiting for canSendRequest"); + break; // Timeout erreicht, aus Schleife ausbrechen + } + } - //debug("InfluxDB2: waiting for write ready"); - // wait unitl ready - do { - } while (client.canSendRequest() == false and (_timeout + INFLUXDB_TIMEOUT_MS) <= millis()); + if (!client.canSendRequest()) { + debug("Can't write to InfluxDB, canSendRequest failed on attempt " + String(attempt)); + continue; // Zum nächsten Versuch übergehen + } - if (! client.canSendRequest()) - { - debug("Can't write to InfluxDB2, timeout canSendRequest"); - return; - } - - // Write point - if (!client.writePoint(sensor)) - { - debug("InfluxDB2 write failed: " + String(client.getLastErrorMessage()) + " Err: " + String(client.getLastStatusCode())); - } + // Datenpunkt schreiben + if (client.writePoint(sensor)) { + success = true; + break; // Schreiben erfolgreich, Schleife verlassen + } else { + debug("InfluxDB write failed on attempt " + String(attempt) + ": " + client.getLastErrorMessage() + " Err: " + String(client.getLastStatusCode())); + } + } + if (!success) { + debug("Failed to write point to InfluxDB after " + String(maxRetries) + " attempts."); + } } #endif // influxdb version 2 check