Updated timing checks to use millis() function, added some links to web updater main page.

This commit is contained in:
Kai Lauterbach 2022-09-12 20:27:42 +02:00
parent 0a605c7300
commit 0b84725d78
3 changed files with 35 additions and 24 deletions

View file

@ -14,8 +14,11 @@
#define WIND_SENSOR_MEAS_TIME_S 15 #define WIND_SENSOR_MEAS_TIME_S 15
#define WATCHDOG_TIMEOUT_MS 30000 #define WATCHDOG_TIMEOUT_MS 30000
#define WIFI_CHECK_INTERVAL_MS 60000 #define WIFI_CHECK_INTERVAL_MS 60000
#define ENERGY_SAVING_ITERATIONS 30 #define ENERGY_SAVING_ITERATIONS 30
#define WIFI_MINIMUM_SIGNAL_QUALITY 10 // percent #define WIFI_MINIMUM_SIGNAL_QUALITY 10 // percent
#define BAT_LOW_VOLTAGE 3.6 #define BAT_LOW_VOLTAGE 3.6
#define BAT_EMERGENCY_DEEPSLEEP_VOLTAGE 3.5 #define BAT_EMERGENCY_DEEPSLEEP_VOLTAGE 3.5

View file

@ -32,11 +32,9 @@ float currentSensorData[VALUES] = { nanf("no value"), nanf("no value"), nanf("no
nanf("no value"), nanf("no value") }; nanf("no value"), nanf("no value") };
float (*sensors[VALUES])() = {}; float (*sensors[VALUES])() = {};
uint16_t update_sensor_cnt = 0; uint32_t update_sensor_cnt = 0;
uint16_t update_webserver_cnt = 0; uint32_t update_webserver_cnt = 0;
uint16_t update_windspeed_exceed_cnt = 0; uint32_t update_windspeed_exceed_cnt = 0;
boolean validData = false;
const String wifiName = "oko-weather-" + DEVICE_NAME; const String wifiName = "oko-weather-" + DEVICE_NAME;
@ -175,7 +173,7 @@ void initSensors()
void wifiConnectionCheck() 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 // if check interval is not exceeded abort check
return; return;
@ -183,6 +181,8 @@ void wifiConnectionCheck()
wifi_check_interval_counter = millis(); wifi_check_interval_counter = millis();
debug("no connection or time to check " + String(WiFi.status() == WL_CONNECTED));
wifiConnect(); wifiConnect();
} }
@ -252,14 +252,14 @@ void loop()
// Needed to give WIFI time to function properly // Needed to give WIFI time to function properly
delay(DELAY_LOOP_MS); delay(DELAY_LOOP_MS);
update_sensor_cnt++; update_sensor_cnt == millis();
#ifdef WEBUPDATER_FEATURE #ifdef WEBUPDATER_FEATURE
update_webserver_cnt++; update_webserver_cnt == millis();
#endif #endif
#ifdef HTTP_CALL_ON_WINDSPEED_EXCEED #ifdef HTTP_CALL_ON_WINDSPEED_EXCEED
update_windspeed_exceed_cnt++; update_windspeed_exceed_cnt == millis();
#endif #endif
} }
@ -268,6 +268,7 @@ void loop()
void _loop() void _loop()
{ {
boolean validData = false;
switch (fsm_state) switch (fsm_state)
{ {
@ -275,10 +276,10 @@ void _loop()
case FSM_STATE_WU: case FSM_STATE_WU:
debug("web updater call if required"); debug("web updater call if required");
#ifdef WEBUPDATER_FEATURE #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"); debug("web updater call");
update_webserver_cnt = 0; update_webserver_cnt = millis();
doWebUpdater(); doWebUpdater();
} }
#endif #endif
@ -288,7 +289,7 @@ void _loop()
case FSM_STATE_WSE: case FSM_STATE_WSE:
debug("wind speed exceeded check if required"); debug("wind speed exceeded check if required");
#ifdef HTTP_CALL_ON_WINDSPEED_EXCEED #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"); debug("reading wind sensor because of exceed call functionality");
if (sensors[SENSOR_WINDSPEED]) if (sensors[SENSOR_WINDSPEED])
@ -323,7 +324,7 @@ void _loop()
} else { } else {
currentSensorData[SENSOR_WINDSPEED] = nan("no value"); currentSensorData[SENSOR_WINDSPEED] = nan("no value");
} }
update_windspeed_exceed_cnt = 0; update_windspeed_exceed_cnt = millis();
} }
#endif #endif
fsm_state = FSM_STATE_RS; fsm_state = FSM_STATE_RS;
@ -361,7 +362,7 @@ void _loop()
case FSM_STATE_US: case FSM_STATE_US:
debug("read sensor data check"); 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)); debug("read sensor data " + String(sensor_cnt));
if (sensors[sensor_cnt]) { if (sensors[sensor_cnt]) {
@ -375,7 +376,7 @@ void _loop()
sensor_cnt++; sensor_cnt++;
fsm_state = FSM_STATE_US; // jump to same state again, more sensors to read fsm_state = FSM_STATE_US; // jump to same state again, more sensors to read
} else { } else {
update_sensor_cnt = 0; update_sensor_cnt = millis();
sensor_cnt = 0; sensor_cnt = 0;
fsm_state = FSM_STATE_SC; // next state fsm_state = FSM_STATE_SC; // next state
} }
@ -399,15 +400,11 @@ void _loop()
for (uint8_t i = 0; i < 5 and validData == false; i++) for (uint8_t i = 0; i < 5 and validData == false; i++)
{ {
if (currentSensorData[i] != 0) if (currentSensorData[i] != 0)
{
validData = true; // at least one value is not zero, the data
}
}
if (validData == true)
{ {
// send data only if valid data is available // send data only if valid data is available
pushToInfluxDB(DEVICE_NAME, currentSensorData); pushToInfluxDB(DEVICE_NAME, currentSensorData);
validData = true;
}
} }
#endif #endif
fsm_state = FSM_STATE_SD; fsm_state = FSM_STATE_SD;

View file

@ -92,6 +92,15 @@ void showHTMLMain(void)
"<meta http-equiv=\"refresh\" content=\"20\">" "<meta http-equiv=\"refresh\" content=\"20\">"
"</head><body>" "</head><body>"
"<br><a href=\"http://" + _webUpdater_ip + ":8080/update\">firmware update</a><br><br>" "<br><a href=\"http://" + _webUpdater_ip + ":8080/update\">firmware update</a><br><br>"
#ifdef USE_LOGGER
"<br><a href=\"http://" + _webUpdater_ip + ":8080/showlog\">logfile</a><br>"
#endif
#ifdef DEBUG_WINDSPEED_MEASUREMENT
"<br><a href=\"http://" + _webUpdater_ip + ":8080/measWind\">manual wind measurement</a><br>"
#endif
#ifdef HOMEBRIDGE_WEBSTAT
"<br><a href=\"http://" + _webUpdater_ip + ":8080/hbWebstat\">homebridge websatt</a><br>"
#endif
"<table>" "<table>"
TR_TD_START_STR + "temperature" + TD_TD_MID_STR + String(_webUpdater_sensValues[SENSOR_TEMPERATURE]) + TR_TD_END_STR 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 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 + "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 TR_TD_START_STR + "wifi rssi" + TD_TD_MID_STR + WiFi.RSSI() + TR_TD_END_STR
"</table>" "</table>"
"<br><br><br><br><br><a href=\"http://" + _webUpdater_ip + ":8080/resetWifiManager\">reset WiFi Manager</a><br>"
"</body></html>"; "</body></html>";
httpServer.send(200, "text/html", message); httpServer.send(200, "text/html", message);
@ -184,7 +194,7 @@ void measureWindspeed()
#ifdef USE_LOGGER #ifdef USE_LOGGER
#define LOGFILE_SIZE 10 #define LOGFILE_SIZE 15
String logfile[LOGFILE_SIZE]; String logfile[LOGFILE_SIZE];
@ -208,8 +218,9 @@ void logdata(String s)
void showLog() void showLog()
{ {
String message = "<html><head><title>OKO Weatherstation - " + String(_webUpdater_dev) + " - logfile</title>" String message = "<html><head><title>OKO Weatherstation - " + String(_webUpdater_dev) + " - logfile</title>"
"<meta http-equiv=\"refresh\" content=\"5\">"
"</head><body>" "</head><body>"
"Logfile data: "; "Logfile data:<br>";
for (uint16_t lp = 0; lp < logger_pos; lp++) for (uint16_t lp = 0; lp < logger_pos; lp++)
{ {