Compare commits

..

No commits in common. "0cb4212fc8c183ec65565e855c13894f9d6fe4c9" and "0b84725d78736b7960c4df5298d96701d02706c9" have entirely different histories.

3 changed files with 62 additions and 75 deletions

View file

@ -13,7 +13,7 @@
#define RESET_ESP_TIME_INTERVAL_MS (60*60*12*1000) // reset every 12 hours #define RESET_ESP_TIME_INTERVAL_MS (60*60*12*1000) // reset every 12 hours
#define WIND_SENSOR_MEAS_TIME_S 15 #define WIND_SENSOR_MEAS_TIME_S 15
#define WATCHDOG_TIMEOUT_MS 30000 #define WATCHDOG_TIMEOUT_MS 30000
#define WIFI_CHECK_INTERVAL_MS 600000 #define WIFI_CHECK_INTERVAL_MS 60000
#define ENERGY_SAVING_ITERATIONS 30 #define ENERGY_SAVING_ITERATIONS 30

View file

@ -35,18 +35,17 @@ float (*sensors[VALUES])() = {};
uint32_t update_sensor_cnt = 0; uint32_t update_sensor_cnt = 0;
uint32_t update_webserver_cnt = 0; uint32_t update_webserver_cnt = 0;
uint32_t update_windspeed_exceed_cnt = 0; uint32_t update_windspeed_exceed_cnt = 0;
uint32_t wifi_check_interval_counter = 0;
const String wifiName = "oko-weather-" + DEVICE_NAME; const String wifiName = "oko-weather-" + DEVICE_NAME;
uint32_t wifi_check_interval_counter = millis();
WiFiManager wifiManager; WiFiManager wifiManager;
uint8_t fsm_state = FSM_STATE_WU; uint8_t fsm_state = FSM_STATE_WU;
uint8_t sensor_cnt = 0; uint8_t sensor_cnt = 0;
boolean validData = false;
//*************************************************************************// //*************************************************************************//
void debug(String x) void debug(String x)
@ -174,7 +173,7 @@ void initSensors()
void wifiConnectionCheck() void wifiConnectionCheck()
{ {
if ((wifi_check_interval_counter + WIFI_CHECK_INTERVAL_MS) > millis()) if (wifi_check_interval_counter + WIFI_CHECK_INTERVAL_MS > millis() or WiFi.status() == WL_CONNECTED)
{ {
// if check interval is not exceeded abort check // if check interval is not exceeded abort check
return; return;
@ -182,12 +181,6 @@ void wifiConnectionCheck()
wifi_check_interval_counter = millis(); 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)); debug("no connection or time to check " + String(WiFi.status() == WL_CONNECTED));
wifiConnect(); wifiConnect();
@ -259,22 +252,33 @@ void 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);
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() void _loop()
{ {
boolean validData = false;
switch (fsm_state) switch (fsm_state)
{ {
case FSM_STATE_WU: case FSM_STATE_WU:
//debug("web updater call if required"); debug("web updater call if required");
#ifdef WEBUPDATER_FEATURE #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(); update_webserver_cnt = millis();
doWebUpdater(); doWebUpdater();
} }
@ -285,20 +289,49 @@ void _loop()
case FSM_STATE_WSE: 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 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");
}
update_windspeed_exceed_cnt = millis(); update_windspeed_exceed_cnt = millis();
readWindSpeedExceed();
} }
#endif #endif
fsm_state = FSM_STATE_RS; fsm_state = FSM_STATE_RS;
break; break;
case FSM_STATE_RS: case FSM_STATE_RS:
//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
if (RESET_ESP_TIME_INTERVAL_MS <= millis()) if (RESET_ESP_TIME_INTERVAL_MS <= millis())
@ -312,15 +345,15 @@ void _loop()
break; break;
case FSM_STATE_WS: 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) #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");
} else { } else {
sensors[SENSOR_WINDSPEED] = &wind_speed; 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 #endif
sensor_cnt = 0; sensor_cnt = 0;
@ -328,8 +361,8 @@ void _loop()
break; break;
case FSM_STATE_US: case FSM_STATE_US:
//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 (sensors[sensor_cnt]) { if (sensors[sensor_cnt]) {
@ -367,15 +400,11 @@ void _loop()
for (uint8_t i = 0; i < 5 and validData == false; i++) for (uint8_t i = 0; i < 5 and validData == false; i++)
{ {
if (currentSensorData[i] != 0) if (currentSensorData[i] != 0)
{
validData = true;
}
}
if (validData == true)
{ {
// send data only if valid data is available // send data only if valid data is available
pushToInfluxDB(DEVICE_NAME, currentSensorData); pushToInfluxDB(DEVICE_NAME, currentSensorData);
validData = true;
}
} }
#endif #endif
fsm_state = FSM_STATE_SD; fsm_state = FSM_STATE_SD;
@ -398,52 +427,12 @@ void _loop()
//delay(100); // TODO warum hier ein delay? //delay(100); // TODO warum hier ein delay?
//debug("FSM state = " + String(fsm_state)); //debug("FSM state = " + String(fsm_state));
/*if (fsm_state == FSM_STATE_WU) if (fsm_state == FSM_STATE_WU)
{ {
debug("----------"); 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[]) void logToSerial(float sensorValues[])
{ {
Serial.println(""); Serial.println("");
@ -457,5 +446,3 @@ 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]));
} }
//*************************************************************************//

View file

@ -101,7 +101,7 @@ void showHTMLMain(void)
#ifdef HOMEBRIDGE_WEBSTAT #ifdef HOMEBRIDGE_WEBSTAT
"<br><a href=\"http://" + _webUpdater_ip + ":8080/hbWebstat\">homebridge websatt</a><br>" "<br><a href=\"http://" + _webUpdater_ip + ":8080/hbWebstat\">homebridge websatt</a><br>"
#endif #endif
"<br><br><table>" "<table>"
TR_TD_START_STR + "temperature" + TD_TD_MID_STR + String(_webUpdater_sensValues[SENSOR_TEMPERATURE]) + TR_TD_END_STR TR_TD_START_STR + "temperature" + TD_TD_MID_STR + String(_webUpdater_sensValues[SENSOR_TEMPERATURE]) + TR_TD_END_STR
TR_TD_START_STR + "humidity" + TD_TD_MID_STR + String(_webUpdater_sensValues[SENSOR_HUMIDITY]) + TR_TD_END_STR TR_TD_START_STR + "humidity" + TD_TD_MID_STR + String(_webUpdater_sensValues[SENSOR_HUMIDITY]) + TR_TD_END_STR
TR_TD_START_STR + "light" + TD_TD_MID_STR + String(_webUpdater_sensValues[SENSOR_LIGHT]) + TR_TD_END_STR TR_TD_START_STR + "light" + TD_TD_MID_STR + String(_webUpdater_sensValues[SENSOR_LIGHT]) + TR_TD_END_STR