From 0b84725d78736b7960c4df5298d96701d02706c9 Mon Sep 17 00:00:00 2001 From: Kai Lauterbach Date: Mon, 12 Sep 2022 20:27:42 +0200 Subject: [PATCH] Updated timing checks to use millis() function, added some links to web updater main page. --- firmware/config.h | 3 +++ firmware/firmware.ino | 41 +++++++++++++++++++---------------------- firmware/webUpdater.ino | 15 +++++++++++++-- 3 files changed, 35 insertions(+), 24 deletions(-) diff --git a/firmware/config.h b/firmware/config.h index 5509e72..8889ee7 100644 --- a/firmware/config.h +++ b/firmware/config.h @@ -14,8 +14,11 @@ #define WIND_SENSOR_MEAS_TIME_S 15 #define WATCHDOG_TIMEOUT_MS 30000 #define WIFI_CHECK_INTERVAL_MS 60000 + #define ENERGY_SAVING_ITERATIONS 30 + #define WIFI_MINIMUM_SIGNAL_QUALITY 10 // percent + #define BAT_LOW_VOLTAGE 3.6 #define BAT_EMERGENCY_DEEPSLEEP_VOLTAGE 3.5 diff --git a/firmware/firmware.ino b/firmware/firmware.ino index e6cc805..1e15453 100644 --- a/firmware/firmware.ino +++ b/firmware/firmware.ino @@ -32,11 +32,9 @@ float currentSensorData[VALUES] = { nanf("no value"), nanf("no value"), nanf("no nanf("no value"), nanf("no value") }; float (*sensors[VALUES])() = {}; -uint16_t update_sensor_cnt = 0; -uint16_t update_webserver_cnt = 0; -uint16_t update_windspeed_exceed_cnt = 0; - -boolean validData = false; +uint32_t update_sensor_cnt = 0; +uint32_t update_webserver_cnt = 0; +uint32_t update_windspeed_exceed_cnt = 0; const String wifiName = "oko-weather-" + DEVICE_NAME; @@ -175,7 +173,7 @@ void initSensors() void wifiConnectionCheck() { - if (millis() - wifi_check_interval_counter < WIFI_CHECK_INTERVAL_MS) + if (wifi_check_interval_counter + WIFI_CHECK_INTERVAL_MS > millis() or WiFi.status() == WL_CONNECTED) { // if check interval is not exceeded abort check return; @@ -183,6 +181,8 @@ void wifiConnectionCheck() wifi_check_interval_counter = millis(); + debug("no connection or time to check " + String(WiFi.status() == WL_CONNECTED)); + wifiConnect(); } @@ -252,14 +252,14 @@ void loop() // Needed to give WIFI time to function properly delay(DELAY_LOOP_MS); - update_sensor_cnt++; + update_sensor_cnt == millis(); #ifdef WEBUPDATER_FEATURE - update_webserver_cnt++; + update_webserver_cnt == millis(); #endif #ifdef HTTP_CALL_ON_WINDSPEED_EXCEED - update_windspeed_exceed_cnt++; + update_windspeed_exceed_cnt == millis(); #endif } @@ -268,6 +268,7 @@ void loop() void _loop() { + boolean validData = false; switch (fsm_state) { @@ -275,10 +276,10 @@ void _loop() case FSM_STATE_WU: debug("web updater call if required"); #ifdef WEBUPDATER_FEATURE - if (UPDATE_WEBSERVER_INTVERVAL_S * 1000 / DELAY_LOOP_MS <= update_webserver_cnt) + if (update_webserver_cnt + (UPDATE_WEBSERVER_INTVERVAL_S * 1000) <= millis()) { debug("web updater call"); - update_webserver_cnt = 0; + update_webserver_cnt = millis(); doWebUpdater(); } #endif @@ -288,7 +289,7 @@ void _loop() case FSM_STATE_WSE: debug("wind speed exceeded check if required"); #ifdef HTTP_CALL_ON_WINDSPEED_EXCEED - if (HTTP_CALL_ON_WINDSPEED_INTERVAL_S * 1000 / DELAY_LOOP_MS <= update_windspeed_exceed_cnt) + if (update_windspeed_exceed_cnt + (HTTP_CALL_ON_WINDSPEED_INTERVAL_S * 1000) <= millis()) { debug("reading wind sensor because of exceed call functionality"); if (sensors[SENSOR_WINDSPEED]) @@ -323,7 +324,7 @@ void _loop() } else { currentSensorData[SENSOR_WINDSPEED] = nan("no value"); } - update_windspeed_exceed_cnt = 0; + update_windspeed_exceed_cnt = millis(); } #endif fsm_state = FSM_STATE_RS; @@ -361,7 +362,7 @@ void _loop() case FSM_STATE_US: debug("read sensor data check"); - if (UPDATE_SENSOR_INTERVAL_S * 1000 / DELAY_LOOP_MS <= update_sensor_cnt) + if (update_sensor_cnt + (UPDATE_SENSOR_INTERVAL_S * 1000) <= millis()) { debug("read sensor data " + String(sensor_cnt)); if (sensors[sensor_cnt]) { @@ -375,7 +376,7 @@ void _loop() sensor_cnt++; fsm_state = FSM_STATE_US; // jump to same state again, more sensors to read } else { - update_sensor_cnt = 0; + update_sensor_cnt = millis(); sensor_cnt = 0; fsm_state = FSM_STATE_SC; // next state } @@ -400,15 +401,11 @@ void _loop() { if (currentSensorData[i] != 0) { - validData = true; // at least one value is not zero, the data + // send data only if valid data is available + pushToInfluxDB(DEVICE_NAME, currentSensorData); + validData = true; } } - - if (validData == true) - { - // send data only if valid data is available - pushToInfluxDB(DEVICE_NAME, currentSensorData); - } #endif fsm_state = FSM_STATE_SD; break; diff --git a/firmware/webUpdater.ino b/firmware/webUpdater.ino index 2fc34f6..ef601c2 100644 --- a/firmware/webUpdater.ino +++ b/firmware/webUpdater.ino @@ -92,6 +92,15 @@ void showHTMLMain(void) "" "" "
firmware update

" +#ifdef USE_LOGGER +"
logfile
" +#endif +#ifdef DEBUG_WINDSPEED_MEASUREMENT +"
manual wind measurement
" +#endif +#ifdef HOMEBRIDGE_WEBSTAT +"
homebridge websatt
" +#endif "" TR_TD_START_STR + "temperature" + TD_TD_MID_STR + String(_webUpdater_sensValues[SENSOR_TEMPERATURE]) + TR_TD_END_STR TR_TD_START_STR + "humidity" + TD_TD_MID_STR + String(_webUpdater_sensValues[SENSOR_HUMIDITY]) + TR_TD_END_STR @@ -103,6 +112,7 @@ TR_TD_START_STR + "millis" + TD_TD_MID_STR + String(millis()) TR_TD_START_STR + "valid" + TD_TD_MID_STR + String(wuValidData) + TR_TD_END_STR TR_TD_START_STR + "wifi rssi" + TD_TD_MID_STR + WiFi.RSSI() + TR_TD_END_STR "
" +"




reset WiFi Manager
" ""; httpServer.send(200, "text/html", message); @@ -184,7 +194,7 @@ void measureWindspeed() #ifdef USE_LOGGER -#define LOGFILE_SIZE 10 +#define LOGFILE_SIZE 15 String logfile[LOGFILE_SIZE]; @@ -208,8 +218,9 @@ void logdata(String s) void showLog() { String message = "OKO Weatherstation - " + String(_webUpdater_dev) + " - logfile" +"" "" -"Logfile data: "; +"Logfile data:
"; for (uint16_t lp = 0; lp < logger_pos; lp++) {