First verison with running FSM. Sensor data read not verified. Web updater is fine.

This commit is contained in:
klaute 2022-09-12 19:09:43 +02:00
parent 277e525314
commit c8626bb5cd
3 changed files with 49 additions and 40 deletions

View file

@ -12,11 +12,10 @@
#define EMERGENCY_SLEEP_S 172800 // Sleep for 2 days to recover #define EMERGENCY_SLEEP_S 172800 // Sleep for 2 days to recover
#define RESET_ESP_TIME_INTERVAL_MS (60*60*12*1000) // reset every 12 hours #define RESET_ESP_TIME_INTERVAL_MS (60*60*12*1000) // reset every 12 hours
#define WIND_SENSOR_MEAS_TIME_S 15 #define WIND_SENSOR_MEAS_TIME_S 15
#define INITIAL_WEBSERVER_TIME 20 #define WATCHDOG_TIMEOUT_MS 30000
#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

@ -18,4 +18,13 @@
#define ENERGY_SAVE_MODE_ENABLED 1.0 #define ENERGY_SAVE_MODE_ENABLED 1.0
#define ENERGY_SAVE_MODE_DISABLED 0.0 #define ENERGY_SAVE_MODE_DISABLED 0.0
#define FSM_STATE_WU 0
#define FSM_STATE_WSE 1
#define FSM_STATE_RS 2
#define FSM_STATE_WS 4
#define FSM_STATE_US 5
#define FSM_STATE_SC 6
#define FSM_STATE_ID 7
#define FSM_STATE_SD 8
#endif #endif

View file

@ -44,19 +44,9 @@ uint32_t wifi_check_interval_counter = millis();
WiFiManager wifiManager; WiFiManager wifiManager;
typedef enum fsm_states_et { uint8_t fsm_state = FSM_STATE_WU;
FSM_STATE_WU,
FSM_STATE_WSE,
FSM_STATE_RS,
FSM_STATE_BP,
FSM_STATE_WS,
FSM_STATE_US,
FSM_STATE_SC,
FSM_STATE_ID,
FSM_STATE_SD
} t_fsm_states;
t_fsm_states fsm_state = FSM_STATE_WU; uint8_t sensor_cnt = 0;
//*************************************************************************// //*************************************************************************//
@ -181,7 +171,7 @@ void initSensors()
void wifiConnectionCheck() void wifiConnectionCheck()
{ {
if (millis() - wifi_check_interval_counter < WIFI_CHECK_INVERVAL_MS) if (millis() - wifi_check_interval_counter < WIFI_CHECK_INTERVAL_MS)
{ {
// if check interval is not exceeded abort check // if check interval is not exceeded abort check
return; return;
@ -279,9 +269,11 @@ void _loop()
{ {
case FSM_STATE_WU: case FSM_STATE_WU:
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_INTVERVAL_S * 1000 / DELAY_LOOP_MS <= update_webserver_cnt)
{ {
debug("web updater call");
update_webserver_cnt = 0; update_webserver_cnt = 0;
doWebUpdater(); doWebUpdater();
} }
@ -290,10 +282,11 @@ void _loop()
break; break;
case FSM_STATE_WSE: case FSM_STATE_WSE:
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 (HTTP_CALL_ON_WINDSPEED_INTERVAL_S * 1000 / DELAY_LOOP_MS <= update_windspeed_exceed_cnt)
{ {
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])
{ {
// read from windspeed sensorSTATUS_LED_PIN // read from windspeed sensorSTATUS_LED_PIN
@ -315,9 +308,7 @@ void _loop()
if (httpResponseCode > 0) { if (httpResponseCode > 0) {
String response = http.getString(); String response = http.getString();
#ifdef DEBUG debug("http response code: " + String(httpResponseCode) + " = " + response);
Serial.println("http response code: " + String(httpResponseCode) + " = " + response);
#endif
// TODO handle response // TODO handle response
} }
@ -335,63 +326,69 @@ void _loop()
break; break;
case FSM_STATE_RS: case FSM_STATE_RS:
debug("reset time check if required");
#ifdef RESET_ESP_TIME_INTERVAL #ifdef RESET_ESP_TIME_INTERVAL
// if millis() reached interval restart ESP // if millis() reached interval restart ESP
if (RESET_ESP_TIME_INTERVAL_MS <= millis()) if (RESET_ESP_TIME_INTERVAL_MS <= millis())
{ {
debug("Resetting firmware intentionally"); debug("resetting firmware intentionally");
// Push reset button after flashing once or do a manual power cycle to get the functionality working. // Push reset button after flashing once or do a manual power cycle to get the functionality working.
ESP.restart(); ESP.restart();
} }
#endif
fsm_state = FSM_STATE_BP;
break;
case FSM_STATE_BP:
#ifndef BATTERY_POWERED
if (UPDATE_SENSOR_INTERVAL_S * 1000 / DELAY_LOOP_MS > update_sensor_cnt)
{
return;
}
#endif #endif
fsm_state = FSM_STATE_WS; fsm_state = FSM_STATE_WS;
break; break;
case FSM_STATE_WS: case FSM_STATE_WS:
debug("disable measure of wind speed if required");
#ifdef defined(BATTERY_POWERED) && defined(SENSOR_WIND) #ifdef defined(BATTERY_POWERED) && defined(SENSOR_WIND)
if (energySavingMode() == 1) { if (energySavingMode() == 1) {
// Disable expensive tasks // Disable expensive tasks
sensors[SENSOR_WINDSPEED] = 0; sensors[SENSOR_WINDSPEED] = 0;
debug("read of wind sensor because of low battery disabled");
} else { } else {
sensors[SENSOR_WINDSPEED] = &wind_speed; sensors[SENSOR_WINDSPEED] = &wind_speed;
debug("read of wind sensor because of high battery enabled");
} }
#endif #endif
fsm_state = FSM_STATE_US; fsm_state = FSM_STATE_US;
break; break;
case FSM_STATE_US: case FSM_STATE_US:
update_sensor_cnt = 0; debug("read sensor data check");
for (uint8_t i = 0; i < VALUES; i++) if (UPDATE_SENSOR_INTERVAL_S * 1000 / DELAY_LOOP_MS <= update_sensor_cnt)
{ {
if (sensors[i]) { debug("read sensor data" + String(sensor_cnt));
currentSensorData[i] = sensors[i](); if (sensors[sensor_cnt]) {
currentSensorData[sensor_cnt] = sensors[sensor_cnt]();
} else { } else {
currentSensorData[i] = nan("no value"); currentSensorData[sensor_cnt] = nan("no value");
}
} }
if (sensor_cnt < VALUES)
{
sensor_cnt++;
} else {
update_sensor_cnt = 0;
sensor_cnt = 0;
fsm_state = FSM_STATE_SC; fsm_state = FSM_STATE_SC;
}
} else {
debug("skip read sensor data");
fsm_state = FSM_STATE_WU; // no new data, reset FSM
}
break; break;
case FSM_STATE_SC: case FSM_STATE_SC:
debug("log to serial if required");
#ifdef SERIAL_FEATURE #ifdef SERIAL_FEATURE
logToSerial(currentSensorData); logToSerial(currentSensorData);
#endif #endif
fsm_state = FSM_STATE_ID; fsm_state = FSM_STATE_ID;
break; break;
//delay(100); // TODO warum?
case FSM_STATE_ID: case FSM_STATE_ID:
debug("Send data to influxdb");
#ifdef INFLUXDB_FEATURE #ifdef INFLUXDB_FEATURE
for (uint8_t i = 0; i < 5 and validData == false; i++) for (uint8_t i = 0; i < 5 and validData == false; i++)
{ {
@ -411,6 +408,7 @@ void _loop()
break; break;
case FSM_STATE_SD: case FSM_STATE_SD:
debug("set sensor data in webupdater");
#ifdef WEBUPDATER_FEATURE #ifdef WEBUPDATER_FEATURE
setSensorData(currentSensorData); setSensorData(currentSensorData);
#endif #endif
@ -422,7 +420,10 @@ void _loop()
break; break;
} // close of switch } // close of switch
debug("FSM state = "+fsm_state);
//delay(100); // TODO warum hier ein delay?
debug("FSM state = " + String(fsm_state));
} }
void logToSerial(float sensorValues[]) void logToSerial(float sensorValues[])