diff --git a/firmware/config.h b/firmware/config.h index fa7e5ec..1712f62 100644 --- a/firmware/config.h +++ b/firmware/config.h @@ -12,11 +12,10 @@ #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 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 WIFI_MINIMUM_SIGNAL_QUALITY 10 // percent - #define BAT_LOW_VOLTAGE 3.6 #define BAT_EMERGENCY_DEEPSLEEP_VOLTAGE 3.5 diff --git a/firmware/constants.h b/firmware/constants.h index 19da869..76f6702 100644 --- a/firmware/constants.h +++ b/firmware/constants.h @@ -18,4 +18,13 @@ #define ENERGY_SAVE_MODE_ENABLED 1.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 diff --git a/firmware/firmware.ino b/firmware/firmware.ino index fafbfd2..b588451 100644 --- a/firmware/firmware.ino +++ b/firmware/firmware.ino @@ -44,19 +44,9 @@ uint32_t wifi_check_interval_counter = millis(); WiFiManager wifiManager; -typedef enum fsm_states_et { - 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; +uint8_t fsm_state = FSM_STATE_WU; -t_fsm_states fsm_state = FSM_STATE_WU; +uint8_t sensor_cnt = 0; //*************************************************************************// @@ -181,7 +171,7 @@ void initSensors() 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 return; @@ -279,9 +269,11 @@ 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) { + debug("web updater call"); update_webserver_cnt = 0; doWebUpdater(); } @@ -290,10 +282,11 @@ void _loop() break; 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) { - debug("Reading wind sensor because of exceed call functionality"); + debug("reading wind sensor because of exceed call functionality"); if (sensors[SENSOR_WINDSPEED]) { // read from windspeed sensorSTATUS_LED_PIN @@ -315,9 +308,7 @@ void _loop() if (httpResponseCode > 0) { String response = http.getString(); -#ifdef DEBUG - Serial.println("http response code: " + String(httpResponseCode) + " = " + response); -#endif + debug("http response code: " + String(httpResponseCode) + " = " + response); // TODO handle response } @@ -335,63 +326,69 @@ void _loop() break; case FSM_STATE_RS: + debug("reset time check if required"); #ifdef RESET_ESP_TIME_INTERVAL // if millis() reached interval restart ESP 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. 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 fsm_state = FSM_STATE_WS; break; case FSM_STATE_WS: + debug("disable measure of wind speed if required"); #ifdef defined(BATTERY_POWERED) && defined(SENSOR_WIND) if (energySavingMode() == 1) { // Disable expensive tasks sensors[SENSOR_WINDSPEED] = 0; + debug("read of wind sensor because of low battery disabled"); } else { sensors[SENSOR_WINDSPEED] = &wind_speed; + debug("read of wind sensor because of high battery enabled"); } #endif fsm_state = FSM_STATE_US; break; case FSM_STATE_US: - update_sensor_cnt = 0; - for (uint8_t i = 0; i < VALUES; i++) + debug("read sensor data check"); + if (UPDATE_SENSOR_INTERVAL_S * 1000 / DELAY_LOOP_MS <= update_sensor_cnt) { - if (sensors[i]) { - currentSensorData[i] = sensors[i](); + debug("read sensor data" + String(sensor_cnt)); + if (sensors[sensor_cnt]) { + currentSensorData[sensor_cnt] = sensors[sensor_cnt](); } 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; + } + } else { + debug("skip read sensor data"); + fsm_state = FSM_STATE_WU; // no new data, reset FSM } - fsm_state = FSM_STATE_SC; break; case FSM_STATE_SC: + debug("log to serial if required"); #ifdef SERIAL_FEATURE logToSerial(currentSensorData); #endif fsm_state = FSM_STATE_ID; break; - //delay(100); // TODO warum? - case FSM_STATE_ID: + debug("Send data to influxdb"); #ifdef INFLUXDB_FEATURE for (uint8_t i = 0; i < 5 and validData == false; i++) { @@ -411,6 +408,7 @@ void _loop() break; case FSM_STATE_SD: + debug("set sensor data in webupdater"); #ifdef WEBUPDATER_FEATURE setSensorData(currentSensorData); #endif @@ -422,7 +420,10 @@ void _loop() break; } // 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[])