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
#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

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");
do {
} while (client.validateConnection() and (_timeout + INFLUXDB_TIMEOUT_MS) <= millis());
if (! client.validateConnection())
{
debug("Can't write to InfluxDB2, timeout validating connection");
return;
// 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
}
}
_timeout = millis();
//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 InfluxDB2, timeout canSendRequest");
return;
if (!client.validateConnection()) {
debug("Can't write to InfluxDB, connection validation failed on attempt " + String(attempt));
continue; // Zum nächsten Versuch übergehen
}
// Write point
if (!client.writePoint(sensor))
{
debug("InfluxDB2 write failed: " + String(client.getLastErrorMessage()) + " Err: " + String(client.getLastStatusCode()));
// 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
}
}
if (!client.canSendRequest()) {
debug("Can't write to InfluxDB, canSendRequest failed on attempt " + String(attempt));
continue; // Zum nächsten Versuch übergehen
}
// 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