InfluxDB connection fix to prevent endless loop.

This commit is contained in:
Kai Lauterbach 2024-08-14 08:03:11 +02:00
parent 99711f6d76
commit aa1e3b0de1
2 changed files with 42 additions and 28 deletions

View file

@ -3,7 +3,7 @@
// config general setting and behavior of the weatherstation // 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_AUTOCONNECT_TIMEOUT_S 60
#define WIFI_CONFIG_PORTAL_TIMEOUT_S 120 #define WIFI_CONFIG_PORTAL_TIMEOUT_S 120

View file

@ -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"); // Timeout für Verbindung prüfen
do { uint32_t startTime = millis();
} while (client.validateConnection() and (_timeout + INFLUXDB_TIMEOUT_MS) <= millis()); while (!client.validateConnection()) {
if (millis() - startTime > INFLUXDB_TIMEOUT_MS) {
debug("Timeout validating connection");
break; // Timeout erreicht, aus Schleife ausbrechen
}
}
if (! client.validateConnection()) if (!client.validateConnection()) {
{ debug("Can't write to InfluxDB, connection validation failed on attempt " + String(attempt));
debug("Can't write to InfluxDB2, timeout validating connection"); continue; // Zum nächsten Versuch übergehen
return; }
}
_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"); if (!client.canSendRequest()) {
// wait unitl ready debug("Can't write to InfluxDB, canSendRequest failed on attempt " + String(attempt));
do { continue; // Zum nächsten Versuch übergehen
} while (client.canSendRequest() == false and (_timeout + INFLUXDB_TIMEOUT_MS) <= millis()); }
if (! client.canSendRequest()) // Datenpunkt schreiben
{ if (client.writePoint(sensor)) {
debug("Can't write to InfluxDB2, timeout canSendRequest"); success = true;
return; break; // Schreiben erfolgreich, Schleife verlassen
} } else {
debug("InfluxDB write failed on attempt " + String(attempt) + ": " + client.getLastErrorMessage() + " Err: " + String(client.getLastStatusCode()));
// Write point }
if (!client.writePoint(sensor)) }
{
debug("InfluxDB2 write failed: " + String(client.getLastErrorMessage()) + " Err: " + String(client.getLastStatusCode()));
}
if (!success) {
debug("Failed to write point to InfluxDB after " + String(maxRetries) + " attempts.");
}
} }
#endif // influxdb version 2 check #endif // influxdb version 2 check