Mostly running main loop and FSM. Call of sensor functions does not work yet.

This commit is contained in:
Kai Lauterbach 2022-09-13 09:59:35 +02:00
parent 0cb4212fc8
commit 0482db2f21
2 changed files with 24 additions and 10 deletions

View file

@ -21,6 +21,7 @@
#define FSM_STATE_WU 0 #define FSM_STATE_WU 0
#define FSM_STATE_WSE 1 #define FSM_STATE_WSE 1
#define FSM_STATE_RS 2 #define FSM_STATE_RS 2
#define FSM_STATE_WC 3
#define FSM_STATE_WS 4 #define FSM_STATE_WS 4
#define FSM_STATE_US 5 #define FSM_STATE_US 5
#define FSM_STATE_SC 6 #define FSM_STATE_SC 6

View file

@ -153,8 +153,8 @@ void initSensors()
//Temperature + pressure //Temperature + pressure
if (sensor_bme280_begin(BME_ADDRESS)) { if (sensor_bme280_begin(BME_ADDRESS)) {
sensors[SENSOR_TEMPERATURE] = &bme280_temperature; sensors[SENSOR_TEMPERATURE] = &bme280_temperature;
sensors[SENSOR_HUMIDITY] = &bme280_humidity; sensors[SENSOR_HUMIDITY] = &bme280_humidity;
sensors[SENSOR_PRESSURE] = &bme280_pressure; sensors[SENSOR_PRESSURE] = &bme280_pressure;
} }
#endif #endif
@ -163,9 +163,9 @@ void initSensors()
#endif #endif
#ifdef SENSOR_BATTERY #ifdef SENSOR_BATTERY
sensors[SENSOR_BAT_VOLTAGE] = &battery_voltage; sensors[SENSOR_BAT_VOLTAGE] = &battery_voltage;
sensors[SENSOR_BATCHARGESTATE] = &battery_charging; sensors[SENSOR_BATCHARGESTATE] = &battery_charging;
sensors[SENSOR_ESAVEMODE] = &isEnergySavingMode; sensors[SENSOR_ESAVEMODE] = &isEnergySavingMode;
#endif #endif
} }
@ -207,16 +207,19 @@ void wifiConnect()
while (!wifiManager.autoConnect(wifiName.c_str(), "DEADBEEF")) while (!wifiManager.autoConnect(wifiName.c_str(), "DEADBEEF"))
{ {
// If autoconnect to WLAN failed and no client connected, go to deep sleep
#ifdef SLEEP_IF_NO_WLAN_CONNECTION #ifdef SLEEP_IF_NO_WLAN_CONNECTION
// If autoconnect to WLAN failed and no client connected, go to deep sleep
ESP.deepSleep(POWERSAVING_SLEEP_S * 1000000, WAKE_RF_DEFAULT); ESP.deepSleep(POWERSAVING_SLEEP_S * 1000000, WAKE_RF_DEFAULT);
delay(100); delay(100);
#endif #endif
#ifndef SLEEP_IF_NO_WLAN_CONNECTION #ifndef SLEEP_IF_NO_WLAN_CONNECTION
// sleep a few seconds and go on trying to connect // sleep a few seconds and go on trying to connect
debug("WiFi connection failed, try again in 5 seconds..."); debug("WiFi connection failed, try again in 5 seconds...");
delay(5000); delay(5000);
#endif #endif
} }
} }
@ -227,7 +230,8 @@ void wifiConnect()
void criticalBatCheck() void criticalBatCheck()
{ {
float volt = battery_voltage(); float volt = battery_voltage();
if (volt <= BAT_EMERGENCY_DEEPSLEEP_VOLTAGE) { if (volt <= BAT_EMERGENCY_DEEPSLEEP_VOLTAGE)
{
debug("Bat Voltage: " + String(volt) + " V"); debug("Bat Voltage: " + String(volt) + " V");
debug("Low battery, going into deep sleep."); debug("Low battery, going into deep sleep.");
// Casting to an unsigned int, so it fits into the integer range // Casting to an unsigned int, so it fits into the integer range
@ -246,8 +250,6 @@ void loop()
ESP.wdtFeed(); ESP.wdtFeed();
#endif #endif
wifiConnectionCheck();
#ifdef BATTERY_POWERED #ifdef BATTERY_POWERED
delay(50); delay(50);
return; return;
@ -308,6 +310,11 @@ void _loop()
ESP.restart(); ESP.restart();
} }
#endif #endif
fsm_state = FSM_STATE_WC;
break;
case FSM_STATE_WC:
wifiConnectionCheck();
fsm_state = FSM_STATE_WS; fsm_state = FSM_STATE_WS;
break; break;
@ -332,9 +339,15 @@ void _loop()
if ((update_sensor_cnt + (UPDATE_SENSOR_INTERVAL_S * 1000)) <= millis()) 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])
currentSensorData[sensor_cnt] = sensors[sensor_cnt](); {
debug("sensors[" + String(sensor_cnt) + "]=" + String((int)sensors[sensor_cnt]));
//currentSensorData[sensor_cnt] = sensors[sensor_cnt]();
currentSensorData[sensor_cnt] = sensor_cnt;
} else { } else {
debug("sensors[" + String(sensor_cnt) + "]=nan");
currentSensorData[sensor_cnt] = nan("no value"); currentSensorData[sensor_cnt] = nan("no value");
} }