Compare commits
No commits in common. "0cb4212fc8c183ec65565e855c13894f9d6fe4c9" and "0b84725d78736b7960c4df5298d96701d02706c9" have entirely different histories.
0cb4212fc8
...
0b84725d78
3 changed files with 62 additions and 75 deletions
|
@ -13,7 +13,7 @@
|
|||
#define RESET_ESP_TIME_INTERVAL_MS (60*60*12*1000) // reset every 12 hours
|
||||
#define WIND_SENSOR_MEAS_TIME_S 15
|
||||
#define WATCHDOG_TIMEOUT_MS 30000
|
||||
#define WIFI_CHECK_INTERVAL_MS 600000
|
||||
#define WIFI_CHECK_INTERVAL_MS 60000
|
||||
|
||||
#define ENERGY_SAVING_ITERATIONS 30
|
||||
|
||||
|
|
|
@ -35,18 +35,17 @@ 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)
|
||||
|
@ -174,7 +173,7 @@ void initSensors()
|
|||
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
|
||||
return;
|
||||
|
@ -182,12 +181,6 @@ 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();
|
||||
|
@ -259,22 +252,33 @@ 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();
|
||||
}
|
||||
|
@ -285,20 +289,49 @@ 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 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();
|
||||
|
||||
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())
|
||||
|
@ -312,15 +345,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;
|
||||
|
@ -328,8 +361,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]) {
|
||||
|
@ -367,15 +400,11 @@ void _loop()
|
|||
for (uint8_t i = 0; i < 5 and validData == false; i++)
|
||||
{
|
||||
if (currentSensorData[i] != 0)
|
||||
{
|
||||
validData = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (validData == true)
|
||||
{
|
||||
// send data only if valid data is available
|
||||
pushToInfluxDB(DEVICE_NAME, currentSensorData);
|
||||
validData = true;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
fsm_state = FSM_STATE_SD;
|
||||
|
@ -398,52 +427,12 @@ 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("");
|
||||
|
@ -457,5 +446,3 @@ void logToSerial(float sensorValues[])
|
|||
Serial.println("Bat charge state: " + String(sensorValues[SENSOR_BATCHARGESTATE]));
|
||||
Serial.println("Energy saving: " + String(sensorValues[SENSOR_ESAVEMODE]));
|
||||
}
|
||||
|
||||
//*************************************************************************//
|
||||
|
|
|
@ -101,7 +101,7 @@ void showHTMLMain(void)
|
|||
#ifdef HOMEBRIDGE_WEBSTAT
|
||||
"<br><a href=\"http://" + _webUpdater_ip + ":8080/hbWebstat\">homebridge websatt</a><br>"
|
||||
#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 + "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
|
||||
|
|
Loading…
Reference in a new issue