|
|
|
@ -35,17 +35,18 @@ float (*sensors[VALUES])() = {};
|
|
|
|
|
uint32_t update_sensor_cnt = 0;
|
|
|
|
|
uint32_t update_webserver_cnt = 0;
|
|
|
|
|
uint32_t update_windspeed_exceed_cnt = 0;
|
|
|
|
|
uint32_t wifi_check_interval_counter = 0;
|
|
|
|
|
|
|
|
|
|
const String wifiName = "oko-weather-" + DEVICE_NAME;
|
|
|
|
|
|
|
|
|
|
uint32_t wifi_check_interval_counter = millis();
|
|
|
|
|
|
|
|
|
|
WiFiManager wifiManager;
|
|
|
|
|
|
|
|
|
|
uint8_t fsm_state = FSM_STATE_WU;
|
|
|
|
|
|
|
|
|
|
uint8_t sensor_cnt = 0;
|
|
|
|
|
|
|
|
|
|
boolean validData = false;
|
|
|
|
|
|
|
|
|
|
//*************************************************************************//
|
|
|
|
|
|
|
|
|
|
void debug(String x)
|
|
|
|
@ -173,7 +174,7 @@ void initSensors()
|
|
|
|
|
void wifiConnectionCheck()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if (wifi_check_interval_counter + WIFI_CHECK_INTERVAL_MS > millis() or WiFi.status() == WL_CONNECTED)
|
|
|
|
|
if ((wifi_check_interval_counter + WIFI_CHECK_INTERVAL_MS) > millis())
|
|
|
|
|
{
|
|
|
|
|
// if check interval is not exceeded abort check
|
|
|
|
|
return;
|
|
|
|
@ -181,6 +182,12 @@ void wifiConnectionCheck()
|
|
|
|
|
|
|
|
|
|
wifi_check_interval_counter = millis();
|
|
|
|
|
|
|
|
|
|
if (WiFi.status() == WL_CONNECTED)
|
|
|
|
|
{
|
|
|
|
|
// if we are connected
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
debug("no connection or time to check " + String(WiFi.status() == WL_CONNECTED));
|
|
|
|
|
|
|
|
|
|
wifiConnect();
|
|
|
|
@ -252,33 +259,22 @@ void loop()
|
|
|
|
|
// Needed to give WIFI time to function properly
|
|
|
|
|
delay(DELAY_LOOP_MS);
|
|
|
|
|
|
|
|
|
|
update_sensor_cnt == millis();
|
|
|
|
|
|
|
|
|
|
#ifdef WEBUPDATER_FEATURE
|
|
|
|
|
update_webserver_cnt == millis();
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef HTTP_CALL_ON_WINDSPEED_EXCEED
|
|
|
|
|
update_windspeed_exceed_cnt == millis();
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//*************************************************************************//
|
|
|
|
|
|
|
|
|
|
void _loop()
|
|
|
|
|
{
|
|
|
|
|
boolean validData = false;
|
|
|
|
|
|
|
|
|
|
switch (fsm_state)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
case FSM_STATE_WU:
|
|
|
|
|
debug("web updater call if required");
|
|
|
|
|
//debug("web updater call if required");
|
|
|
|
|
#ifdef WEBUPDATER_FEATURE
|
|
|
|
|
if (update_webserver_cnt + (UPDATE_WEBSERVER_INTVERVAL_S * 1000) <= millis())
|
|
|
|
|
if ((update_webserver_cnt + (UPDATE_WEBSERVER_INTVERVAL_S * 1000)) <= millis())
|
|
|
|
|
{
|
|
|
|
|
debug("web updater call");
|
|
|
|
|
//debug("web updater call");
|
|
|
|
|
update_webserver_cnt = millis();
|
|
|
|
|
doWebUpdater();
|
|
|
|
|
}
|
|
|
|
@ -289,49 +285,20 @@ void _loop()
|
|
|
|
|
case FSM_STATE_WSE:
|
|
|
|
|
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())
|
|
|
|
|
if ((update_windspeed_exceed_cnt + (HTTP_CALL_ON_WINDSPEED_INTERVAL_S * 1000)) <= millis())
|
|
|
|
|
{
|
|
|
|
|
debug("reading wind sensor because of exceed call functionality");
|
|
|
|
|
if (sensors[SENSOR_WINDSPEED])
|
|
|
|
|
{
|
|
|
|
|
// read from windspeed sensorSTATUS_LED_PIN
|
|
|
|
|
digitalWrite(STATUS_LED_PIN, HIGH);
|
|
|
|
|
currentSensorData[SENSOR_WINDSPEED] = sensors[SENSOR_WINDSPEED]();
|
|
|
|
|
digitalWrite(STATUS_LED_PIN, LOW);
|
|
|
|
|
|
|
|
|
|
if (currentSensorData[SENSOR_WINDSPEED] >= HTTP_CALL_ON_WINDSPEED_EXCEED_MPS)
|
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
currentSensorData[SENSOR_WINDSPEED] = nan("no value");
|
|
|
|
|
}
|
|
|
|
|
debug("reading wind sensor exceed");
|
|
|
|
|
|
|
|
|
|
update_windspeed_exceed_cnt = millis();
|
|
|
|
|
|
|
|
|
|
readWindSpeedExceed();
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
fsm_state = FSM_STATE_RS;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case FSM_STATE_RS:
|
|
|
|
|
debug("reset time check if required");
|
|
|
|
|
//debug("reset time check if required");
|
|
|
|
|
#ifdef RESET_ESP_TIME_INTERVAL
|
|
|
|
|
// if millis() reached interval restart ESP
|
|
|
|
|
if (RESET_ESP_TIME_INTERVAL_MS <= millis())
|
|
|
|
@ -345,15 +312,15 @@ void _loop()
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case FSM_STATE_WS:
|
|
|
|
|
debug("disable measure of wind speed if required");
|
|
|
|
|
//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");
|
|
|
|
|
//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");
|
|
|
|
|
//debug("read of wind sensor because of high battery enabled");
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
sensor_cnt = 0;
|
|
|
|
@ -361,8 +328,8 @@ void _loop()
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case FSM_STATE_US:
|
|
|
|
|
debug("read sensor data check");
|
|
|
|
|
if (update_sensor_cnt + (UPDATE_SENSOR_INTERVAL_S * 1000) <= millis())
|
|
|
|
|
//debug("read sensor data check");
|
|
|
|
|
if ((update_sensor_cnt + (UPDATE_SENSOR_INTERVAL_S * 1000)) <= millis())
|
|
|
|
|
{
|
|
|
|
|
debug("read sensor data " + String(sensor_cnt));
|
|
|
|
|
if (sensors[sensor_cnt]) {
|
|
|
|
@ -401,11 +368,15 @@ void _loop()
|
|
|
|
|
{
|
|
|
|
|
if (currentSensorData[i] != 0)
|
|
|
|
|
{
|
|
|
|
|
// send data only if valid data is available
|
|
|
|
|
pushToInfluxDB(DEVICE_NAME, currentSensorData);
|
|
|
|
|
validData = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (validData == true)
|
|
|
|
|
{
|
|
|
|
|
// send data only if valid data is available
|
|
|
|
|
pushToInfluxDB(DEVICE_NAME, currentSensorData);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
fsm_state = FSM_STATE_SD;
|
|
|
|
|
break;
|
|
|
|
@ -427,12 +398,52 @@ void _loop()
|
|
|
|
|
//delay(100); // TODO warum hier ein delay?
|
|
|
|
|
|
|
|
|
|
//debug("FSM state = " + String(fsm_state));
|
|
|
|
|
if (fsm_state == FSM_STATE_WU)
|
|
|
|
|
/*if (fsm_state == FSM_STATE_WU)
|
|
|
|
|
{
|
|
|
|
|
debug("----------");
|
|
|
|
|
}*/
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//*************************************************************************//
|
|
|
|
|
|
|
|
|
|
void readWindSpeedExceed()
|
|
|
|
|
{
|
|
|
|
|
if (sensors[SENSOR_WINDSPEED])
|
|
|
|
|
{
|
|
|
|
|
// read from windspeed sensorSTATUS_LED_PIN
|
|
|
|
|
digitalWrite(STATUS_LED_PIN, HIGH);
|
|
|
|
|
currentSensorData[SENSOR_WINDSPEED] = sensors[SENSOR_WINDSPEED]();
|
|
|
|
|
digitalWrite(STATUS_LED_PIN, LOW);
|
|
|
|
|
|
|
|
|
|
if (currentSensorData[SENSOR_WINDSPEED] >= HTTP_CALL_ON_WINDSPEED_EXCEED_MPS)
|
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
currentSensorData[SENSOR_WINDSPEED] = nan("no value");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//*************************************************************************//
|
|
|
|
|
|
|
|
|
|
void logToSerial(float sensorValues[])
|
|
|
|
|
{
|
|
|
|
|
Serial.println("");
|
|
|
|
@ -446,3 +457,5 @@ void logToSerial(float sensorValues[])
|
|
|
|
|
Serial.println("Bat charge state: " + String(sensorValues[SENSOR_BATCHARGESTATE]));
|
|
|
|
|
Serial.println("Energy saving: " + String(sensorValues[SENSOR_ESAVEMODE]));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//*************************************************************************//
|
|
|
|
|