feature/http_call_send_json #3

Merged
f merged 56 commits from feature/http_call_send_json into master 2022-11-27 11:02:22 +01:00
3 changed files with 69 additions and 62 deletions
Showing only changes of commit 6f7cb3352f - Show all commits

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 60000 #define WIFI_CHECK_INTERVAL_MS 600000
#define ENERGY_SAVING_ITERATIONS 30 #define ENERGY_SAVING_ITERATIONS 30

View file

@ -35,17 +35,18 @@ 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)
@ -173,7 +174,7 @@ void initSensors()
void wifiConnectionCheck() 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() or WiFi.status() == WL_CONNECTED)
{ {
// if check interval is not exceeded abort check // if check interval is not exceeded abort check
return; return;
@ -252,33 +253,22 @@ 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();
} }
@ -289,49 +279,20 @@ 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 because of exceed call functionality"); debug("reading wind sensor exceed");
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())
@ -345,15 +306,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;
@ -361,8 +322,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]) {
@ -401,11 +362,15 @@ void _loop()
{ {
if (currentSensorData[i] != 0) if (currentSensorData[i] != 0)
{ {
// send data only if valid data is available
pushToInfluxDB(DEVICE_NAME, currentSensorData);
validData = true; validData = true;
} }
} }
if (validData == true)
{
// send data only if valid data is available
pushToInfluxDB(DEVICE_NAME, currentSensorData);
}
#endif #endif
fsm_state = FSM_STATE_SD; fsm_state = FSM_STATE_SD;
break; break;
@ -427,12 +392,52 @@ 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("");
@ -446,3 +451,5 @@ 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
"<table>" "<br><br><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