From 3e8d65d121b1eb4a8aff8f8f69f840a85dba399d Mon Sep 17 00:00:00 2001 From: Kai Lauterbach Date: Thu, 15 Sep 2022 13:04:42 +0200 Subject: [PATCH] Improved InfluxDB code to prevent deadlock --- firmware/config.h | 1 + firmware/firmware.ino | 7 +++++-- firmware/influxdb.ino | 41 ++++++++++++++++++++++++---------------- firmware/sensor_wind.ino | 3 +++ 4 files changed, 34 insertions(+), 18 deletions(-) diff --git a/firmware/config.h b/firmware/config.h index 59e4a63..33a45ad 100644 --- a/firmware/config.h +++ b/firmware/config.h @@ -14,6 +14,7 @@ #define WIND_SENSOR_MEAS_TIME_S 15 #define WATCHDOG_TIMEOUT_MS 30000 #define WIFI_CHECK_INTERVAL_MS 120000 +#define INFLUXDB_TIMEOUT_MS 1000 #define ENERGY_SAVING_ITERATIONS 30 diff --git a/firmware/firmware.ino b/firmware/firmware.ino index 0e7cd74..6edc75d 100644 --- a/firmware/firmware.ino +++ b/firmware/firmware.ino @@ -41,7 +41,7 @@ const String wifiName = "oko-weather-" + DEVICE_NAME; WiFiManager wifiManager; uint8_t fsm_state = FSM_STATE_1; - +start_measure_wind uint8_t sensor_cnt = 0; boolean validData = false; @@ -360,9 +360,12 @@ void _fsm_loop() // reset the wait timer to get a value every HTTP_CALL_ON_WINDSPEED_INTERVAL_S independently to the runtime of the measurement update_windspeed_exceed_cnt = millis(); - start_measure_wind(); // start measurement of wind speed + // start measurement of wind speed + start_measure_wind(); + fsm_state = FSM_STATE_11; // wait untile the wind meas time exceeded break; // abort case here to prevent read of next sensor in list + } #endif fsm_state = FSM_STATE_2; diff --git a/firmware/influxdb.ino b/firmware/influxdb.ino index 524d798..07f38a4 100644 --- a/firmware/influxdb.ino +++ b/firmware/influxdb.ino @@ -176,26 +176,35 @@ void pushToInfluxDB(String device, float sensorValues[]) { void _writePoint() { + uint32_t _timeout = millis(); + + debug("InfluxDB: 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 = millis(); + + debug("InfluxDB: waiting for write ready"); // wait unitl ready do { -#ifdef DEBUG - Serial.print("InfluxDB: waiting for write ready\n"); -#endif - } while (client.canSendRequest() == false); + } while (client.canSendRequest() == false and (_timeout + INFLUXDB_TIMEOUT_MS) <= millis()); + + if (! client.canSendRequest()) + { + debug("Can't write to InfluxDB2, timeout canSendRequest"); + return; + } // Write point - if (!client.writePoint(sensor)) { -#ifdef DEBUG - Serial.print("InfluxDB write failed: "); - Serial.println(client.getLastErrorMessage()); - Serial.print("\nErrorcode: "); - Serial.println(client.getLastStatusCode()); - Serial.print("\n"); -#endif - } else { -#ifdef DEBUG - Serial.print("InfluxDB write done\n"); -#endif + if (!client.writePoint(sensor)) + { + debug("InfluxDB write failed: " + String(client.getLastErrorMessage()) + " Err: " + String(client.getLastStatusCode())); } } diff --git a/firmware/sensor_wind.ino b/firmware/sensor_wind.ino index b0320b6..bdb50c3 100644 --- a/firmware/sensor_wind.ino +++ b/firmware/sensor_wind.ino @@ -25,9 +25,11 @@ float wind_speed() void start_measure_wind() { + start_meas_wind_time = millis(); anemometerRotations = 0; interruptNumber = digitalPinToInterrupt(ANEMOMETER_PIN); + } boolean check_measure_wind_done() @@ -42,5 +44,6 @@ boolean check_measure_wind_done() float measure_wind_result() { + start_meas_wind_time = 0; return (float)anemometerRotations * WINDSPEED_FACTOR; }