feature/http_call_send_json #3
1 changed files with 175 additions and 75 deletions
|
@ -40,7 +40,7 @@ const String wifiName = "oko-weather-" + DEVICE_NAME;
|
||||||
|
|
||||||
WiFiManager wifiManager;
|
WiFiManager wifiManager;
|
||||||
|
|
||||||
uint8_t fsm_state = FSM_STATE_WU;
|
uint8_t fsm_state = FSM_STATE_1;
|
||||||
|
|
||||||
uint8_t sensor_cnt = 0;
|
uint8_t sensor_cnt = 0;
|
||||||
|
|
||||||
|
@ -67,7 +67,7 @@ void setup()
|
||||||
{
|
{
|
||||||
|
|
||||||
#if defined(DEBUG) || defined(SERIAL_FEATURE)
|
#if defined(DEBUG) || defined(SERIAL_FEATURE)
|
||||||
Serial.begin(115200);
|
Serial.begin(SERIAL_BARD_RATE);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Pin settings
|
// Pin settings
|
||||||
|
@ -109,7 +109,11 @@ void setup()
|
||||||
|
|
||||||
#ifdef BATTERY_POWERED
|
#ifdef BATTERY_POWERED
|
||||||
debug("battery powered");
|
debug("battery powered");
|
||||||
_loop();
|
|
||||||
|
do {
|
||||||
|
_battery_mode_main();
|
||||||
|
} while (fsm_state != FSM_STATE_1);
|
||||||
|
|
||||||
|
|
||||||
digitalWrite(STATUS_LED_PIN, LOW);
|
digitalWrite(STATUS_LED_PIN, LOW);
|
||||||
|
|
||||||
|
@ -126,10 +130,12 @@ void setup()
|
||||||
delay(100);
|
delay(100);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef BATTERY_POWERED
|
||||||
#ifdef ENABLE_WATCHDOG
|
#ifdef ENABLE_WATCHDOG
|
||||||
// Enable the internal watchdog
|
// Enable the internal watchdog
|
||||||
ESP.wdtEnable(WATCHDOG_TIMEOUT_MS);
|
ESP.wdtEnable(WATCHDOG_TIMEOUT_MS);
|
||||||
#endif
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -238,7 +244,7 @@ void wifiConnectionCheck()
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
debug("no connection or time to check " + String(WiFi.status() == WL_CONNECTED));
|
debug("no wifi connection, try to reconnect");
|
||||||
|
|
||||||
wifiConnect();
|
wifiConnect();
|
||||||
|
|
||||||
|
@ -303,19 +309,23 @@ void loop()
|
||||||
#ifdef BATTERY_POWERED
|
#ifdef BATTERY_POWERED
|
||||||
delay(50);
|
delay(50);
|
||||||
return;
|
return;
|
||||||
#endif
|
|
||||||
|
|
||||||
// call sub loop function
|
#else
|
||||||
_loop();
|
|
||||||
|
// call fsm loop function
|
||||||
|
_fsm_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);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//*************************************************************************//
|
//*************************************************************************//
|
||||||
|
|
||||||
void _loop()
|
#ifndef BATTERY_POWERED
|
||||||
|
void _fsm_loop()
|
||||||
{
|
{
|
||||||
|
|
||||||
#ifdef WEBUPDATER_FEATURE
|
#ifdef WEBUPDATER_FEATURE
|
||||||
|
@ -330,37 +340,27 @@ void _loop()
|
||||||
switch (fsm_state)
|
switch (fsm_state)
|
||||||
{
|
{
|
||||||
|
|
||||||
case FSM_STATE_WU:
|
/* -------------------------------------------------------------------------------- */
|
||||||
/*
|
case FSM_STATE_1:
|
||||||
//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:
|
|
||||||
//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 ((update_windspeed_exceed_cnt + (HTTP_CALL_ON_WINDSPEED_INTERVAL_S * 1000)) <= millis())
|
if ((update_windspeed_exceed_cnt + (HTTP_CALL_ON_WINDSPEED_INTERVAL_S * 1000)) <= millis())
|
||||||
{
|
{
|
||||||
debug("reading wind sensor exceed");
|
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();
|
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
|
#endif
|
||||||
fsm_state = FSM_STATE_RS;
|
fsm_state = FSM_STATE_2;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case FSM_STATE_RS:
|
/* -------------------------------------------------------------------------------- */
|
||||||
|
case FSM_STATE_2:
|
||||||
//debug("reset time check if required");
|
//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
|
||||||
|
@ -371,18 +371,21 @@ void _loop()
|
||||||
ESP.restart();
|
ESP.restart();
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
fsm_state = FSM_STATE_WC;
|
fsm_state = FSM_STATE_3;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case FSM_STATE_WC:
|
/* -------------------------------------------------------------------------------- */
|
||||||
|
case FSM_STATE_3:
|
||||||
wifiConnectionCheck();
|
wifiConnectionCheck();
|
||||||
fsm_state = FSM_STATE_WS;
|
fsm_state = FSM_STATE_4;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case FSM_STATE_WS:
|
/* -------------------------------------------------------------------------------- */
|
||||||
|
case FSM_STATE_4:
|
||||||
//debug("disable measure of wind speed if required");
|
//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");
|
//debug("read of wind sensor because of low battery disabled");
|
||||||
|
@ -395,45 +398,57 @@ void _loop()
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
sensor_cnt = 0;
|
sensor_cnt = 0;
|
||||||
fsm_state = FSM_STATE_US;
|
fsm_state = FSM_STATE_5;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case FSM_STATE_US:
|
/* -------------------------------------------------------------------------------- */
|
||||||
|
case FSM_STATE_5:
|
||||||
//debug("read sensor data check");
|
//debug("read sensor data check");
|
||||||
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 (sensor_cnt != SENSOR_WINDSPEED)
|
||||||
|
{
|
||||||
|
// read data from sensor
|
||||||
currentSensorData[sensor_cnt] = readSensors(sensor_cnt);
|
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)
|
if (sensor_cnt < VALUES)
|
||||||
{
|
{
|
||||||
sensor_cnt++;
|
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 {
|
} else {
|
||||||
|
|
||||||
update_sensor_cnt = millis();
|
update_sensor_cnt = millis(); // reset the update interval counter
|
||||||
sensor_cnt = 0;
|
sensor_cnt = 0;
|
||||||
fsm_state = FSM_STATE_SC; // next state
|
fsm_state = FSM_STATE_6; // next state
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
//debug("skip read sensor data");
|
//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;
|
break;
|
||||||
|
|
||||||
case FSM_STATE_SC:
|
/* -------------------------------------------------------------------------------- */
|
||||||
|
case FSM_STATE_6:
|
||||||
//debug("log to serial if required");
|
//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_7;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case FSM_STATE_ID:
|
/* -------------------------------------------------------------------------------- */
|
||||||
|
case FSM_STATE_7:
|
||||||
//debug("send data to influxdb if required");
|
//debug("send data to influxdb if required");
|
||||||
#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++)
|
||||||
|
@ -450,44 +465,74 @@ void _loop()
|
||||||
pushToInfluxDB(DEVICE_NAME, currentSensorData);
|
pushToInfluxDB(DEVICE_NAME, currentSensorData);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
fsm_state = FSM_STATE_SD;
|
fsm_state = FSM_STATE_8;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case FSM_STATE_SD:
|
/* -------------------------------------------------------------------------------- */
|
||||||
|
case FSM_STATE_8:
|
||||||
//debug("set sensor data in webupdater if required");
|
//debug("set sensor data in webupdater if required");
|
||||||
#ifdef WEBUPDATER_FEATURE
|
#ifdef WEBUPDATER_FEATURE
|
||||||
setSensorData(currentSensorData);
|
setSensorData(currentSensorData);
|
||||||
#endif
|
#endif
|
||||||
fsm_state = FSM_STATE_WU;
|
fsm_state = FSM_STATE_1;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
/* -------------------------------------------------------------------------------- */
|
||||||
fsm_state = FSM_STATE_WU;
|
case FSM_STATE_9:
|
||||||
break;
|
#ifdef SENSOR_WIND
|
||||||
|
if (check_measure_wind_done() == false)
|
||||||
} // close of switch
|
|
||||||
|
|
||||||
//delay(100); // TODO warum hier ein delay?
|
|
||||||
|
|
||||||
//debug("FSM state = " + String(fsm_state));
|
|
||||||
/*if (fsm_state == FSM_STATE_WU)
|
|
||||||
{
|
{
|
||||||
debug("----------");
|
//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;
|
||||||
|
|
||||||
void readWindSpeedExceed()
|
/* -------------------------------------------------------------------------------- */
|
||||||
{
|
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;
|
||||||
|
|
||||||
// read from windspeed sensorSTATUS_LED_PIN
|
/* -------------------------------------------------------------------------------- */
|
||||||
digitalWrite(STATUS_LED_PIN, HIGH);
|
case FSM_STATE_12:
|
||||||
currentSensorData[SENSOR_WINDSPEED] = readSensors(SENSOR_WINDSPEED);
|
#ifdef HTTP_CALL_ON_WINDSPEED_EXCEED
|
||||||
digitalWrite(STATUS_LED_PIN, LOW);
|
currentSensorData[sensor_cnt] = measure_wind_result();
|
||||||
|
debug("wind sensor value " + String(currentSensorData[sensor_cnt]));
|
||||||
|
|
||||||
if (currentSensorData[SENSOR_WINDSPEED] >= HTTP_CALL_ON_WINDSPEED_EXCEED_MPS)
|
if (currentSensorData[SENSOR_WINDSPEED] >= HTTP_CALL_ON_WINDSPEED_EXCEED_MPS)
|
||||||
{
|
{
|
||||||
|
// windspeed exceeded send http call
|
||||||
digitalWrite(STATUS_LED_PIN, HIGH);
|
digitalWrite(STATUS_LED_PIN, HIGH);
|
||||||
|
|
||||||
// call the url HTTP_CALL_ON_WINDSPEED_URL
|
// call the url HTTP_CALL_ON_WINDSPEED_URL
|
||||||
|
@ -498,7 +543,8 @@ void readWindSpeedExceed()
|
||||||
// Send HTTP GET request
|
// Send HTTP GET request
|
||||||
int httpResponseCode = http.GET();
|
int httpResponseCode = http.GET();
|
||||||
|
|
||||||
if (httpResponseCode > 0) {
|
if (httpResponseCode > 0)
|
||||||
|
{
|
||||||
String response = http.getString();
|
String response = http.getString();
|
||||||
debug("http response code: " + String(httpResponseCode) + " = " + response);
|
debug("http response code: " + String(httpResponseCode) + " = " + response);
|
||||||
// TODO handle response
|
// TODO handle response
|
||||||
|
@ -511,12 +557,65 @@ void readWindSpeedExceed()
|
||||||
|
|
||||||
#ifdef WEBUPDATER_FEATURE
|
#ifdef WEBUPDATER_FEATURE
|
||||||
setSensorData(currentSensorData);
|
setSensorData(currentSensorData);
|
||||||
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// step into read of next fsm state
|
||||||
|
fsm_state = FSM_STATE_2;
|
||||||
|
break;
|
||||||
|
|
||||||
|
/* -------------------------------------------------------------------------------- */
|
||||||
|
default:
|
||||||
|
fsm_state = FSM_STATE_1;
|
||||||
|
break;
|
||||||
|
|
||||||
|
} // close of switch
|
||||||
|
|
||||||
|
//debug("FSM state = " + String(fsm_state));
|
||||||
|
/*if (fsm_state == FSM_STATE_1)
|
||||||
|
{
|
||||||
|
debug("----------");
|
||||||
|
}*/
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
//*************************************************************************//
|
||||||
|
|
||||||
|
void _battery_mode_main()
|
||||||
|
{
|
||||||
|
|
||||||
|
if (energySavingMode() == 1)
|
||||||
|
{
|
||||||
|
// Disable expensive tasks
|
||||||
|
//debug("read of wind sensor because of low battery disabled");
|
||||||
|
do_not_read_windsensor = true;
|
||||||
|
|
||||||
|
} 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[])
|
void logToSerial(float sensorValues[])
|
||||||
{
|
{
|
||||||
Serial.println("");
|
Serial.println("");
|
||||||
|
@ -530,5 +629,6 @@ void logToSerial(float sensorValues[])
|
||||||
Serial.println("Bat charge state: " + String(sensorValues[SENSOR_BATCHARGESTATE]));
|
Serial.println("Bat charge state: " + String(sensorValues[SENSOR_BATCHARGESTATE]));
|
||||||
Serial.println("Energy saving: " + String(sensorValues[SENSOR_ESAVEMODE]));
|
Serial.println("Energy saving: " + String(sensorValues[SENSOR_ESAVEMODE]));
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
//*************************************************************************//
|
//*************************************************************************//
|
||||||
|
|
Loading…
Reference in a new issue