Waiting for wind sensor result code changed to be processed by FSM states. Added new battery mode main function. Renamed FSM state names.

This commit is contained in:
Kai Lauterbach 2022-09-14 11:58:13 +02:00
parent 7225866dd4
commit 5fcfd89652

View file

@ -40,7 +40,7 @@ const String wifiName = "oko-weather-" + DEVICE_NAME;
WiFiManager wifiManager;
uint8_t fsm_state = FSM_STATE_WU;
uint8_t fsm_state = FSM_STATE_1;
uint8_t sensor_cnt = 0;
@ -67,7 +67,7 @@ void setup()
{
#if defined(DEBUG) || defined(SERIAL_FEATURE)
Serial.begin(115200);
Serial.begin(SERIAL_BARD_RATE);
#endif
// Pin settings
@ -109,7 +109,11 @@ void setup()
#ifdef BATTERY_POWERED
debug("battery powered");
_loop();
do {
_battery_mode_main();
} while (fsm_state != FSM_STATE_1);
digitalWrite(STATUS_LED_PIN, LOW);
@ -126,10 +130,12 @@ void setup()
delay(100);
#endif
#ifndef BATTERY_POWERED
#ifdef ENABLE_WATCHDOG
// Enable the internal watchdog
ESP.wdtEnable(WATCHDOG_TIMEOUT_MS);
#endif
#endif
}
@ -238,7 +244,7 @@ void wifiConnectionCheck()
return;
}
debug("no connection or time to check " + String(WiFi.status() == WL_CONNECTED));
debug("no wifi connection, try to reconnect");
wifiConnect();
@ -303,19 +309,23 @@ void loop()
#ifdef BATTERY_POWERED
delay(50);
return;
#endif
// call sub loop function
_loop();
#else
// call fsm loop function
_fsm_loop();
// Needed to give WIFI time to function properly
delay(DELAY_LOOP_MS);
#endif
}
//*************************************************************************//
void _loop()
#ifndef BATTERY_POWERED
void _fsm_loop()
{
#ifdef WEBUPDATER_FEATURE
@ -330,37 +340,27 @@ void _loop()
switch (fsm_state)
{
case FSM_STATE_WU:
/*
//debug("web updater call if required");
#ifdef WEBUPDATER_FEATURE
if ((update_webserver_cnt + (UPDATE_WEBSERVER_INTVERVAL_S * 1000)) <= millis())
{
//debug("web updater call");
update_webserver_cnt = millis();
doWebUpdater();
}
#endif
*/
fsm_state = FSM_STATE_WSE;
break;
case FSM_STATE_WSE:
/* -------------------------------------------------------------------------------- */
case FSM_STATE_1:
//debug("wind speed exceeded check if required");
#ifdef HTTP_CALL_ON_WINDSPEED_EXCEED
if ((update_windspeed_exceed_cnt + (HTTP_CALL_ON_WINDSPEED_INTERVAL_S * 1000)) <= millis())
{
debug("reading wind sensor exceed");
// 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();
readWindSpeedExceed();
start_measure_wind(); // start measurement of wind speed
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_RS;
fsm_state = FSM_STATE_2;
break;
case FSM_STATE_RS:
/* -------------------------------------------------------------------------------- */
case FSM_STATE_2:
//debug("reset time check if required");
#ifdef RESET_ESP_TIME_INTERVAL
// if millis() reached interval restart ESP
@ -371,18 +371,21 @@ void _loop()
ESP.restart();
}
#endif
fsm_state = FSM_STATE_WC;
fsm_state = FSM_STATE_3;
break;
case FSM_STATE_WC:
/* -------------------------------------------------------------------------------- */
case FSM_STATE_3:
wifiConnectionCheck();
fsm_state = FSM_STATE_WS;
fsm_state = FSM_STATE_4;
break;
case FSM_STATE_WS:
/* -------------------------------------------------------------------------------- */
case FSM_STATE_4:
//debug("disable measure of wind speed if required");
#ifdef defined(BATTERY_POWERED) && defined(SENSOR_WIND)
if (energySavingMode() == 1) {
if (energySavingMode() == 1)
{
// Disable expensive tasks
//sensors[SENSOR_WINDSPEED] = 0;
//debug("read of wind sensor because of low battery disabled");
@ -395,45 +398,57 @@ void _loop()
}
#endif
sensor_cnt = 0;
fsm_state = FSM_STATE_US;
fsm_state = FSM_STATE_5;
break;
case FSM_STATE_US:
/* -------------------------------------------------------------------------------- */
case FSM_STATE_5:
//debug("read sensor data check");
if ((update_sensor_cnt + (UPDATE_SENSOR_INTERVAL_S * 1000)) <= millis())
{
debug("read sensor data " + String(sensor_cnt));
currentSensorData[sensor_cnt] = readSensors(sensor_cnt);
if (sensor_cnt != SENSOR_WINDSPEED)
{
// read data from sensor
currentSensorData[sensor_cnt] = readSensors(sensor_cnt);
} else {
start_measure_wind(); // start measurement of wind speed
fsm_state = FSM_STATE_9; // wait untile the wind meas time exceeded
break; // abort case here to prevent read of next sensor in list
}
if (sensor_cnt < VALUES)
{
sensor_cnt++;
fsm_state = FSM_STATE_US; // jump to same state again, more sensors to read
fsm_state = FSM_STATE_5; // jump to same state again, more sensors to read
} else {
update_sensor_cnt = millis();
update_sensor_cnt = millis(); // reset the update interval counter
sensor_cnt = 0;
fsm_state = FSM_STATE_SC; // next state
fsm_state = FSM_STATE_6; // next state
}
} else {
//debug("skip read sensor data");
fsm_state = FSM_STATE_WU; // no new data, reset FSM
fsm_state = FSM_STATE_1; // no new data, reset FSM
}
break;
case FSM_STATE_SC:
/* -------------------------------------------------------------------------------- */
case FSM_STATE_6:
//debug("log to serial if required");
#ifdef SERIAL_FEATURE
logToSerial(currentSensorData);
#endif
fsm_state = FSM_STATE_ID;
fsm_state = FSM_STATE_7;
break;
case FSM_STATE_ID:
/* -------------------------------------------------------------------------------- */
case FSM_STATE_7:
//debug("send data to influxdb if required");
#ifdef INFLUXDB_FEATURE
for (uint8_t i = 0; i < 5 and validData == false; i++)
@ -450,73 +465,157 @@ void _loop()
pushToInfluxDB(DEVICE_NAME, currentSensorData);
}
#endif
fsm_state = FSM_STATE_SD;
fsm_state = FSM_STATE_8;
break;
case FSM_STATE_SD:
/* -------------------------------------------------------------------------------- */
case FSM_STATE_8:
//debug("set sensor data in webupdater if required");
#ifdef WEBUPDATER_FEATURE
setSensorData(currentSensorData);
#endif
fsm_state = FSM_STATE_WU;
fsm_state = FSM_STATE_1;
break;
/* -------------------------------------------------------------------------------- */
case FSM_STATE_9:
#ifdef SENSOR_WIND
if (check_measure_wind_done() == false)
{
//debug("wait for wind sensor finish");
fsm_state = FSM_STATE_9; // stay here until the wind measurement is done
} else {
debug("wind sensor read finish");
fsm_state = FSM_STATE_10;
}
#else
// in case that the wind sensor is not used skip this step
sensor_cnt++;
fsm_state = FSM_STATE_5;
#endif
break;
/* -------------------------------------------------------------------------------- */
case FSM_STATE_10:
#ifdef SENSOR_WIND
currentSensorData[sensor_cnt] = measure_wind_result();
debug("wind sensor " + String(currentSensorData[sensor_cnt]));
#endif
// step into read of next sensor read
sensor_cnt++;
fsm_state = FSM_STATE_5;
break;
/* -------------------------------------------------------------------------------- */
case FSM_STATE_11:
#ifdef SENSOR_WIND
if (check_measure_wind_done() == false)
{
//debug("wait for wind sensor finish");
fsm_state = FSM_STATE_11; // stay here until the wind measurement is done
} else {
debug("wind sensor read finish");
fsm_state = FSM_STATE_12;
}
#else
// in case that the wind sensor is not used skip this step
sensor_cnt++;
fsm_state = FSM_STATE_5;
#endif
break;
/* -------------------------------------------------------------------------------- */
case FSM_STATE_12:
#ifdef HTTP_CALL_ON_WINDSPEED_EXCEED
currentSensorData[sensor_cnt] = measure_wind_result();
debug("wind sensor value " + String(currentSensorData[sensor_cnt]));
if (currentSensorData[SENSOR_WINDSPEED] >= HTTP_CALL_ON_WINDSPEED_EXCEED_MPS)
{
// windspeed exceeded send http call
digitalWrite(STATUS_LED_PIN, HIGH);
// call the url HTTP_CALL_ON_WINDSPEED_URL
WiFiClient client;
HTTPClient http;
http.begin(client, String(HTTP_CALL_ON_WINDSPEED_URL).c_str());
// Send HTTP GET request
int httpResponseCode = http.GET();
if (httpResponseCode > 0)
{
String response = http.getString();
debug("http response code: " + String(httpResponseCode) + " = " + response);
// TODO handle response
}
http.end();
debug("Called windspeed exceed callout");
digitalWrite(STATUS_LED_PIN, LOW);
}
#ifdef WEBUPDATER_FEATURE
setSensorData(currentSensorData);
#endif
#endif
// step into read of next fsm state
fsm_state = FSM_STATE_2;
break;
/* -------------------------------------------------------------------------------- */
default:
fsm_state = FSM_STATE_WU;
fsm_state = FSM_STATE_1;
break;
} // close of switch
//delay(100); // TODO warum hier ein delay?
//debug("FSM state = " + String(fsm_state));
/*if (fsm_state == FSM_STATE_WU)
/*if (fsm_state == FSM_STATE_1)
{
debug("----------");
}*/
}
#endif
//*************************************************************************//
void readWindSpeedExceed()
void _battery_mode_main()
{
// read from windspeed sensorSTATUS_LED_PIN
digitalWrite(STATUS_LED_PIN, HIGH);
currentSensorData[SENSOR_WINDSPEED] = readSensors(SENSOR_WINDSPEED);
digitalWrite(STATUS_LED_PIN, LOW);
if (currentSensorData[SENSOR_WINDSPEED] >= HTTP_CALL_ON_WINDSPEED_EXCEED_MPS)
if (energySavingMode() == 1)
{
digitalWrite(STATUS_LED_PIN, HIGH);
// Disable expensive tasks
//debug("read of wind sensor because of low battery disabled");
do_not_read_windsensor = true;
// call the url HTTP_CALL_ON_WINDSPEED_URL
WiFiClient client;
HTTPClient http;
http.begin(client, String(HTTP_CALL_ON_WINDSPEED_URL).c_str());
// Send HTTP GET request
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
String response = http.getString();
debug("http response code: " + String(httpResponseCode) + " = " + response);
// TODO handle response
}
http.end();
debug("Called windspeed exceed callout");
digitalWrite(STATUS_LED_PIN, LOW);
} else {
//debug("read of wind sensor because of high battery enabled");
do_not_read_windsensor = false;
}
for (uint8_t i = 0; i < VALUES; i++)
{
currentSensorData[i] = readSensors(i);
}
#ifdef SERIAL_FEATURE
logToSerial(currentSensorData);
#endif
#ifdef INFLUXDB_FEATURE
pushToInfluxDB(DEVICE_NAME, currentSensorData);
#endif
#ifdef WEBUPDATER_FEATURE
setSensorData(currentSensorData);
#endif
}
//*************************************************************************//
#ifdef SERIAL_FEATURE
void logToSerial(float sensorValues[])
{
Serial.println("");
@ -530,5 +629,6 @@ void logToSerial(float sensorValues[])
Serial.println("Bat charge state: " + String(sensorValues[SENSOR_BATCHARGESTATE]));
Serial.println("Energy saving: " + String(sensorValues[SENSOR_ESAVEMODE]));
}
#endif
//*************************************************************************//