From c0b59dd421286acd6f477088399444675cac396f Mon Sep 17 00:00:00 2001 From: Kai Lauterbach Date: Mon, 9 May 2022 09:42:42 +0200 Subject: [PATCH 01/37] Updated InfluxDB connection to get it compatible with InfluxDB 2.2.0 --- esphome_config/esphome_run_weatherstation.sh | 2 - esphome_config/weatherstation.yml | 179 ------------------- esphome_firmware/weatherstation.yml | 146 --------------- firmware/config.h | 8 +- firmware/firmware.ino | 20 ++- firmware/hb_webstat_srv.ino | 30 ++++ firmware/influxdb.ino | 113 +++++++----- firmware/sensor_apds9960.ino | 0 firmware/sensor_wind.ino | 0 firmware/webUpdater.ino | 0 10 files changed, 119 insertions(+), 379 deletions(-) delete mode 100644 esphome_config/esphome_run_weatherstation.sh delete mode 100644 esphome_config/weatherstation.yml delete mode 100644 esphome_firmware/weatherstation.yml create mode 100755 firmware/hb_webstat_srv.ino mode change 100644 => 100755 firmware/influxdb.ino mode change 100644 => 100755 firmware/sensor_apds9960.ino mode change 100644 => 100755 firmware/sensor_wind.ino mode change 100644 => 100755 firmware/webUpdater.ino diff --git a/esphome_config/esphome_run_weatherstation.sh b/esphome_config/esphome_run_weatherstation.sh deleted file mode 100644 index adfd1b8..0000000 --- a/esphome_config/esphome_run_weatherstation.sh +++ /dev/null @@ -1,2 +0,0 @@ - -sudo docker-compose exec esphome esphome run /config/weatherstation.yml diff --git a/esphome_config/weatherstation.yml b/esphome_config/weatherstation.yml deleted file mode 100644 index 6f240c9..0000000 --- a/esphome_config/weatherstation.yml +++ /dev/null @@ -1,179 +0,0 @@ -esphome: - name: weatherstation - platform: ESP8266 - board: d1_mini - -i2c: - - id: bus_a - sda: 4 # D2 - scl: 5 # D1 - scan: true - -apds9960: - address: 0x39 - update_interval: 300s - -sensor: - # - #################### wind sensor - - platform: pulse_counter - pin: - # pin D8 - number: GPIO15 - mode: INPUT # TODO check if required - unit_of_measurement: 'm/s' ##change to m/s if metric - name: 'wind_speed' - icon: 'mdi:weather-windy' - id: my_wind - count_mode: - rising_edge: DISABLE - falling_edge: INCREMENT - internal_filter: 50us - update_interval: 60s - #rotations_per_sec = pulses/2/60 - #circ_m=0.09*2*3.14 = 0.5652 - #mps = 1.18*circ_m*rotations_per_sec - #mps = 1.18*0.5652/2/60 =0,0055578 - filters: - - multiply: 0.0055578 #use for m/s - # - multiply: 2.237 #m/s to mph - # - sliding_window_moving_average: - # window_size: 4 - # send_every: 1 - #- multiply: 0.04973 #1.492mph switch to close 1/sec per spec, pulse/sec (/60/2)*1.492 - #- multiply: 0.0124327986 #m/s * mph conversion - - #################### apds9960 sensor i2c - - platform: apds9960 - type: CLEAR - name: "APDS9960_clear_channel" - - platform: apds9960 - type: RED - name: "APDS9960_red_channel" - id: apds9960_red - - platform: apds9960 - type: GREEN - name: "APDS9960_green_channel" - id: apds9960_green - - platform: apds9960 - type: BLUE - name: "APDS9960_blue_channel" - id: apds9960_blue - # TODO fix calculation to get lux instead of a percentual value - # - # Code from Adafruit_APDS9960 lib: - # illuminance = (-0.32466F * r) + (1.57837F * g) + (-0.73191F * b); - # - # Code from: https://esphome.io/api/apds9960_8cpp_source.html - # 180: float red_perc = (uint_red / float(UINT16_MAX)) * 100.0f; - # 181: float green_perc = (uint_green / float(UINT16_MAX)) * 100.0f; - # 182: float blue_perc = (uint_blue / float(UINT16_MAX)) * 100.0f; - - platform: template - name: "light" - lambda: |- - const float perc_to_raw = 65535.0 / 100.0; - const float red_to_lux = -0.32466; - const float green_to_lux = 1.57837; - const float blue_to_lux = -0.73191; - return ((red_to_lux * id(apds9960_red).raw_state) + (green_to_lux * id(apds9960_green).raw_state) + (blue_to_lux * id(apds9960_blue).raw_state)) * perc_to_raw; - update_interval: 300s - accuracy_decimals: 2 - unit_of_measurement: 'lux' - - #################### bme280 sensor i2c - - platform: bme280 - temperature: - name: "BME280_temperature" - id: bme280_temperature - pressure: - name: "BME280_ressure" - id: bme280_pressure - humidity: - name: "BME280 Relative Humidity" - id: bme280_humidity - address: 0x76 - update_interval: 300s - - - platform: template - name: "altitude" - lambda: |- - const float STANDARD_SEA_LEVEL_PRESSURE = 1013.25; //in hPa, see note - return ((id(bme280_temperature).state + 273.15) / 0.0065) * - (powf((STANDARD_SEA_LEVEL_PRESSURE / id(bme280_pressure).state), 0.190234) - 1); // in meter - update_interval: 300s - icon: 'mdi:signal' - unit_of_measurement: 'm' - - - platform: template - name: "absolute_humidity" - lambda: |- - const float mw = 18.01534; // molar mass of water g/mol - const float r = 8.31447215; // Universal gas constant J/mol/K - return (6.112 * powf(2.718281828, (17.67 * id(bme280_temperature).state) / - (id(bme280_temperature).state + 243.5)) * id(bme280_humidity).state * mw) / - ((273.15 + id(bme280_temperature).state) * r); // in grams/m^3 - accuracy_decimals: 2 - update_interval: 300s - icon: 'mdi:water' - unit_of_measurement: 'g/m³' - - - platform: template - name: "dew_point" - lambda: |- - return (243.5*(log(id(bme280_humidity).state/100)+((17.67*id(bme280_temperature).state)/ - (243.5+id(bme280_temperature).state)))/(17.67-log(id(bme280_humidity).state/100)- - ((17.67*id(bme280_temperature).state)/(243.5+id(bme280_temperature).state)))); - unit_of_measurement: °C - update_interval: 300s - icon: 'mdi:thermometer-alert' - - # battery power adc - - platform: adc - pin: A0 - name: "battery_power" - update_interval: 300s - -binary_sensor: - # battery charged pin - - platform: gpio - pin: - number: 12 # D6 - mode: - input: true - pullup: false - name: "battery_charged" - - # battery charging pin - - platform: gpio - pin: - number: 14 # D5 - mode: - input: true - pullup: false - name: "battery_charging" - -time: - - platform: sntp - id: my_time - -# Enable logging -logger: - -# Enable Home Assistant API -api: - -ota: - password: "xxxxxxxxxxxxxxxxxxxxxxxxxxxx" - -wifi: - ssid: UPC4556888 - password: emmydhvZ4faw - manual_ip: - # Set this to the IP of the ESP - static_ip: 192.168.0.91 - # Set this to the IP address of the router. Often ends with .1 - gateway: 192.168.0.1 - # The subnet of the network. 255.255.255.0 works for most home networks. - subnet: 255.255.255.0 - -captive_portal: diff --git a/esphome_firmware/weatherstation.yml b/esphome_firmware/weatherstation.yml deleted file mode 100644 index 61f0e38..0000000 --- a/esphome_firmware/weatherstation.yml +++ /dev/null @@ -1,146 +0,0 @@ -esphome: - name: weatherstation - platform: ESP8266 - board: d1_mini - -i2c: - - id: bus_a - sda: 4 # D2 - scl: 5 # D1 - scan: true - -apds9960: - address: 0x39 - update_interval: 300s - -sensor: - # - #################### wind sensor - - platform: pulse_counter - pin: - # pin D8 - number: GPIO15 - mode: INPUT_PULLUP # TODO check if required - unit_of_measurement: 'm/s' ##change to m/s if metric - name: 'wind_speed' - icon: 'mdi:weather-windy' - id: my_wind - count_mode: - rising_edge: DISABLE - falling_edge: INCREMENT - internal_filter: 50us - update_interval: 300s - #rotations_per_sec = pulses/2/60 - #circ_m=0.09*2*3.14 = 0.5652 - #mps = 1.18*circ_m*rotations_per_sec - #mps = 1.18*0.5652/2/60 =0,0055578 - filters: - - multiply: 0.0055578 #use for m/s - # - multiply: 2.237 #m/s to mph - # - sliding_window_moving_average: - # window_size: 4 - # send_every: 1 - #- multiply: 0.04973 #1.492mph switch to close 1/sec per spec, pulse/sec (/60/2)*1.492 - #- multiply: 0.0124327986 #m/s * mph conversion - - #################### apds9960 sensor i2c - - platform: apds9960 - type: CLEAR - name: "APDS9960_clear_channel" - - - platform: bme280 - temperature: - name: "BME280_temperature" - id: bme280_temperature - pressure: - name: "BME280_ressure" - id: bme280_pressure - humidity: - name: "BME280 Relative Humidity" - id: bme280_humidity - address: 0x76 - update_interval: 300s - - - platform: template - name: "altitude" - lambda: |- - const float STANDARD_SEA_LEVEL_PRESSURE = 1013.25; //in hPa, see note - return ((id(bme280_temperature).state + 273.15) / 0.0065) * - (powf((STANDARD_SEA_LEVEL_PRESSURE / id(bme280_pressure).state), 0.190234) - 1); // in meter - update_interval: 300s - icon: 'mdi:signal' - unit_of_measurement: 'm' - - - platform: template - name: "absolute_humidity" - lambda: |- - const float mw = 18.01534; // molar mass of water g/mol - const float r = 8.31447215; // Universal gas constant J/mol/K - return (6.112 * powf(2.718281828, (17.67 * id(bme280_temperature).state) / - (id(bme280_temperature).state + 243.5)) * id(bme280_humidity).state * mw) / - ((273.15 + id(bme280_temperature).state) * r); // in grams/m^3 - accuracy_decimals: 2 - update_interval: 300s - icon: 'mdi:water' - unit_of_measurement: 'g/m³' - - - platform: template - name: "dew_point" - lambda: |- - return (243.5*(log(id(bme280_humidity).state/100)+((17.67*id(bme280_temperature).state)/ - (243.5+id(bme280_temperature).state)))/(17.67-log(id(bme280_humidity).state/100)- - ((17.67*id(bme280_temperature).state)/(243.5+id(bme280_temperature).state)))); - unit_of_measurement: °C - update_interval: 300s - icon: 'mdi:thermometer-alert' - - # battery power adc - - platform: adc - pin: A0 - name: "battery_power" - update_interval: 300s - -binary_sensor: - # battery charged pin - - platform: gpio - pin: - number: 12 # D6 - mode: - input: true - pullup: false - name: "battery_charged" - - # battery charging pin - - platform: gpio - pin: - number: 14 # D5 - mode: - input: true - pullup: false - name: "battery_charging" - -time: - - platform: sntp - id: my_time - -# Enable logging -logger: - -# Enable Home Assistant API -api: - -ota: - password: "xxxxxxxxxxxxxxxxxxxxxxxxxxxx" - -wifi: - ssid: UPC4556888 - password: emmydhvZ4faw - manual_ip: - # Set this to the IP of the ESP - static_ip: 192.168.0.91 - # Set this to the IP address of the router. Often ends with .1 - gateway: 192.168.0.1 - # The subnet of the network. 255.255.255.0 works for most home networks. - subnet: 255.255.255.0 - -captive_portal: diff --git a/firmware/config.h b/firmware/config.h index 09ffc77..358fd4e 100755 --- a/firmware/config.h +++ b/firmware/config.h @@ -16,9 +16,9 @@ #define ENERGY_SAVE_MODE_ENABLED 1.0 #define ENERGY_SAVE_MODE_DISABLED 0.0 -#define WIFI_AUTOCONNECT_TIMEOUT_S 120 -#define WIFI_CONFIG_PORTAL_TIMEOUT_S 120 -#define UPDATE_SENSOR_INTERVAL_S 10 +#define WIFI_AUTOCONNECT_TIMEOUT_S 120 +#define WIFI_CONFIG_PORTAL_TIMEOUT_S 120 +#define UPDATE_SENSOR_INTERVAL_S 300 #define UPDATE_WEBSERVER_INTVERVAL_S 1 #define DELAY_LOOP_MS 50 #define POWERSAVING_SLEEP_S 600 @@ -49,4 +49,4 @@ #define INITIAL_WEBSERVER_TIME 20 -#endif +#endif diff --git a/firmware/firmware.ino b/firmware/firmware.ino index 5918152..4146769 100755 --- a/firmware/firmware.ino +++ b/firmware/firmware.ino @@ -1,4 +1,4 @@ -// Standard ESP8266 libs +// Standard ESP8266 libs from project folder #include #include #include @@ -6,7 +6,7 @@ #include #include // WiFiClient -#include // WiFiManager +#include // WiFiManager from bib manager // Project includes #include "config.h" @@ -35,7 +35,7 @@ void debug(String x) { void setup() { - // Erase WiFi Credentials (maybe this will work ...) + // Erase WiFi Credentials, enable, compile, flash, disable and reflash. //WiFi.disconnect(true); //delay(2000); //ESP.reset(); @@ -118,6 +118,12 @@ void setup() { #ifndef BATTERY_POWERED setupWebUpdater(DEVICE_NAME, WiFi.localIP().toString()); #endif +#endif + +#ifdef HOMEBRIDGE_WEBSTAT +#ifndef BATTERY_POWERED + hb_webstat_server_setup(); +#endif #endif //It's magic! leave in @@ -192,7 +198,6 @@ void _loop() { } #endif - #ifdef defined(BATTERY_POWERED) && defined(SENSOR_WIND) if (energySavingMode() == 1) { // Disable expensive tasks @@ -225,6 +230,11 @@ void _loop() { #ifdef WEBUPDATER_FEATURE setSensorData(currentSensorData); #endif + +#ifdef HOMEBRIDGE_WEBSTAT + hb_webstat_loop(currentSensorData); +#endif + } void logToSerial(float sensorValues[]) { @@ -238,4 +248,4 @@ void logToSerial(float sensorValues[]) { Serial.println("Bat Voltage: " + String(sensorValues[SENSOR_BAT_VOLTAGE]) + " V"); Serial.println("Bat charge state: " + String(sensorValues[SENSOR_BATCHARGESTATE])); Serial.println("Energy saving: " + String(sensorValues[SENSOR_ESAVEMODE])); -} +} diff --git a/firmware/hb_webstat_srv.ino b/firmware/hb_webstat_srv.ino new file mode 100755 index 0000000..8a186bc --- /dev/null +++ b/firmware/hb_webstat_srv.ino @@ -0,0 +1,30 @@ + +// Port des Web Servers auf 80 setzen + +#ifdef HOMEBRIDGE_WEBSTAT + +WiFiServer hb_webstat_server(80); + +const char *hb_ws_msg_start = "{\"temperature\": "; +const char *hb_ws_msg_mid = "\"humidity\": "; +const char *hb_ws_msg_end = "}"; + +void hb_webstat_setup() +{ + hb_webstat_server.begin() +} + +void hb_webstat_send(floar currentSensorDfloar currentSensorData[]ata[]) +{ + WiFiClient client = server.available(); + + if (client.available()) { + client.print(hb_ws_msg_start); + client.print(String(currentSensorData[0],2)); + client.print(hb_ws_msg_mid); + client.print(hb_ws_msg_end); + } +} + +#endif + diff --git a/firmware/influxdb.ino b/firmware/influxdb.ino old mode 100644 new mode 100755 index 8d1ebde..9b6a186 --- a/firmware/influxdb.ino +++ b/firmware/influxdb.ino @@ -1,76 +1,103 @@ -#include // https://github.com/hwwong/ESP8266Influxdb +#include // from bib manager -Influxdb _influxdb(INFLUXDB_HOST, INFLUXDB_PORT); +// Data point +Point sensor("weatherstation"); + +// Init variables to influxdb config - doesn't talk to database +InfluxDBClient client(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN); void influxdb_begin() { - // Init variables to influxdb config - doesn't talk to database - _influxdb.opendb(INFLUXDB_DB, INFLUXDB_USER, INFLUXDB_PASS); + + // Check server connection + if (client.validateConnection()) { + // success +#ifdef DEBUG + Serial.print("InfluxDB connect success\n"); +#endif + } else { + // fail +#ifdef DEBUG + Serial.print("InfluxDB connect failed\n"); +#endif + } + } void pushToInfluxDB(String device, float sensorValues[]) { - uint8_t tries = 0; - boolean addComma = false; - String msg = "weather,device=" + device + " "; if (!(isnan(sensorValues[SENSOR_TEMPERATURE]))) { - msg += "temperature=" + String(sensorValues[SENSOR_TEMPERATURE]); - addComma = true; + sensor.clearFields(); + sensor.addField("temperature", sensorValues[SENSOR_TEMPERATURE]); + _writePoint(); } if (!(isnan(sensorValues[SENSOR_HUMIDITY]))) { - if (true == addComma) - msg += ","; - msg += "humidity=" + String(sensorValues[SENSOR_HUMIDITY]); - addComma = true; + sensor.clearFields(); + sensor.addField("humidity", sensorValues[SENSOR_HUMIDITY]); + _writePoint(); } if (!(isnan(sensorValues[SENSOR_LIGHT]))) { - if (true == addComma) - msg += ","; - msg += "light=" + String(sensorValues[SENSOR_LIGHT]); - addComma = true; + sensor.clearFields(); + sensor.addField("light", sensorValues[SENSOR_LIGHT]); + _writePoint(); } if (!(isnan(sensorValues[SENSOR_WINDSPEED]))) { - if (true == addComma) - msg += ","; - msg += "windspeed=" + String(sensorValues[SENSOR_WINDSPEED]); - addComma = true; + sensor.clearFields(); + sensor.addField("windspeed", sensorValues[SENSOR_WINDSPEED]); + _writePoint(); } if (!(isnan(sensorValues[SENSOR_PRESSURE]))) { - if (true == addComma) - msg += ","; - msg += "pressure=" + String(sensorValues[SENSOR_PRESSURE]); - addComma = true; + sensor.clearFields(); + sensor.addField("pressure", sensorValues[SENSOR_PRESSURE]); + _writePoint(); } if (!(isnan(sensorValues[SENSOR_BAT_VOLTAGE]))) { - if (true == addComma) - msg += ","; - msg += "batvoltage=" + String(sensorValues[SENSOR_BAT_VOLTAGE]); - addComma = true; + sensor.clearFields(); + sensor.addField("batvoltage", sensorValues[SENSOR_BAT_VOLTAGE]); + _writePoint(); } if (!(isnan(sensorValues[SENSOR_ESAVEMODE]))) { - if (true == addComma) - msg += ","; - msg += "esavemode=" + String(sensorValues[SENSOR_ESAVEMODE]); - addComma = true; + sensor.clearFields(); + sensor.addField("esavemode", sensorValues[SENSOR_ESAVEMODE]); + _writePoint(); } if (!(isnan(sensorValues[SENSOR_BATCHARGESTATE]))) { - if (true == addComma) - msg += ","; - msg += "batchargestate=" + String(sensorValues[SENSOR_BATCHARGESTATE]); - addComma = true; + sensor.clearFields(); + sensor.addField("batchargestate", sensorValues[SENSOR_BATCHARGESTATE]); + _writePoint(); + } + +} + +void _writePoint() { + + // wait unitl ready + do { +#ifdef DEBUG + Serial.print("InfluxDB: waiting for write ready\n"); +#endif + } while (client.canSendRequest() == false); + + // Write point + if (!client.writePoint(sensor)) { +#ifdef DEBUG + Serial.print("InfluxDB write failed: "); + Serial.println(client.getLastErrorMessage()); + Serial.print("\nErrorcode: "); + Serial.println(client.getLastStatusCode()); + Serial.print("\n"); +#endif + } else { +#ifdef DEBUG + Serial.print("InfluxDB write done\n"); +#endif } - debug(msg); - - do { - tries++; - _influxdb.write(msg); - } while (_influxdb.response() != DB_SUCCESS and tries < 5); } diff --git a/firmware/sensor_apds9960.ino b/firmware/sensor_apds9960.ino old mode 100644 new mode 100755 diff --git a/firmware/sensor_wind.ino b/firmware/sensor_wind.ino old mode 100644 new mode 100755 diff --git a/firmware/webUpdater.ino b/firmware/webUpdater.ino old mode 100644 new mode 100755 From f392fce1cc3c9257d13b9d1d30abdb3f38c1433f Mon Sep 17 00:00:00 2001 From: Kai Lauterbach Date: Sat, 14 May 2022 19:38:58 +0200 Subject: [PATCH 02/37] WifiManager reset feature added to webUpdater. --- firmware/README.md | 0 firmware/firmware.ino | 14 +++++--------- firmware/webUpdater.ino | 23 ++++++++++++++++++++++- 3 files changed, 27 insertions(+), 10 deletions(-) mode change 100644 => 100755 firmware/README.md mode change 100755 => 100644 firmware/firmware.ino mode change 100755 => 100644 firmware/webUpdater.ino diff --git a/firmware/README.md b/firmware/README.md old mode 100644 new mode 100755 diff --git a/firmware/firmware.ino b/firmware/firmware.ino old mode 100755 new mode 100644 index 4146769..b8442a1 --- a/firmware/firmware.ino +++ b/firmware/firmware.ino @@ -35,11 +35,6 @@ void debug(String x) { void setup() { - // Erase WiFi Credentials, enable, compile, flash, disable and reflash. - //WiFi.disconnect(true); - //delay(2000); - //ESP.reset(); - #if defined(DEBUG) || defined(SERIAL_FEATURE) Serial.begin(115200); #endif @@ -69,11 +64,12 @@ void setup() { wifiManager.setConnectTimeout(WIFI_AUTOCONNECT_TIMEOUT_S); // the time in seconds to wait for the known wifi connection wifiManager.setTimeout(WIFI_CONFIG_PORTAL_TIMEOUT_S); // the time in seconds to wait for the user to configure the device - if (!wifiManager.autoConnect(wifiName.c_str(), "DEADBEEF")) { - debug("WiFi connection failed, going into deep sleep ..."); + while (!wifiManager.autoConnect(wifiName.c_str(), "DEADBEEF")) + { + debug("WiFi connection failed, try again in 5 seconds..."); // If autoconnect to WLAN failed and no client connected, go to deep sleep - ESP.deepSleep(POWERSAVING_SLEEP_S * 1000000, WAKE_RF_DEFAULT); - delay(100); + //ESP.deepSleep(POWERSAVING_SLEEP_S * 1000000, WAKE_RF_DEFAULT); + delay(5000); } debug("Connected!"); diff --git a/firmware/webUpdater.ino b/firmware/webUpdater.ino old mode 100755 new mode 100644 index 82b264d..3f440a2 --- a/firmware/webUpdater.ino +++ b/firmware/webUpdater.ino @@ -27,7 +27,8 @@ void setupWebUpdater(String device, String ip) httpUpdater.setup(&httpServer); - httpServer.on("/", showHTMLMain); // oko specific site + httpServer.on("/", showHTMLMain); + httpServer.on("/resetWifiManager", resetWifiManager); httpServer.begin(); @@ -63,3 +64,23 @@ void showHTMLMain(void) { httpServer.send(200, "text/html", message); } + +void resetWifiManager() +{ + + String message = "OKO Weatherstation - " + String(_webUpdater_dev) + "" +"" +"" +"Reset WifiManager config.
" +"Rebooting...
" +""; + + httpServer.send(200, "text/html", message); + + // Erase WiFi Credentials, enable, compile, flash, disable and reflash. + WiFiManager.resetSettings() + + delay(5000); + + ESP.restart(); +} From 4fcb7ba2a7eaa251717587ed22ed917bfd7ef625 Mon Sep 17 00:00:00 2001 From: Kai Lauterbach Date: Sat, 14 May 2022 19:53:31 +0200 Subject: [PATCH 03/37] Added ; --- firmware/webUpdater.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/firmware/webUpdater.ino b/firmware/webUpdater.ino index 3f440a2..6ecf87e 100644 --- a/firmware/webUpdater.ino +++ b/firmware/webUpdater.ino @@ -78,7 +78,7 @@ void resetWifiManager() httpServer.send(200, "text/html", message); // Erase WiFi Credentials, enable, compile, flash, disable and reflash. - WiFiManager.resetSettings() + WiFiManager.resetSettings(); delay(5000); From 116dfa658fd157bc6e26ff11c488d58bd5eec3ce Mon Sep 17 00:00:00 2001 From: Kai Lauterbach Date: Sat, 14 May 2022 19:54:51 +0200 Subject: [PATCH 04/37] Added include of wifiManager to webUpdater --- firmware/webUpdater.ino | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/firmware/webUpdater.ino b/firmware/webUpdater.ino index 6ecf87e..ca8a043 100644 --- a/firmware/webUpdater.ino +++ b/firmware/webUpdater.ino @@ -11,6 +11,7 @@ #include #include +#include // WiFiManager from bib manager ESP8266WebServer httpServer(8080); ESP8266HTTPUpdateServer httpUpdater; @@ -81,6 +82,6 @@ void resetWifiManager() WiFiManager.resetSettings(); delay(5000); - + ESP.restart(); } From 5f0614ce4f966925f40ab2af6d6f3b3bf7561e06 Mon Sep 17 00:00:00 2001 From: Kai Lauterbach Date: Sun, 15 May 2022 22:34:36 +0200 Subject: [PATCH 05/37] Moved Homebridge webstat to webUpdater logic. Fixed Windspeed calculation. Added a interval firmware reset functionality. --- firmware/config.h | 10 +++- firmware/firmware.ino | 47 ++++++++++++++----- firmware/hb_webstat_srv.ino | 30 ------------ firmware/influxdb.ino | 4 +- firmware/sensor_wind.ino | 15 +++--- firmware/webUpdater.ino | 41 ++++++++++++++-- .../influxdb_write/influxdb_write.ino | 0 7 files changed, 92 insertions(+), 55 deletions(-) mode change 100755 => 100644 firmware/config.h delete mode 100755 firmware/hb_webstat_srv.ino mode change 100755 => 100644 firmware/sensor_wind.ino mode change 100644 => 100755 influxdb-lib/examples/influxdb_write/influxdb_write.ino diff --git a/firmware/config.h b/firmware/config.h old mode 100755 new mode 100644 index 358fd4e..beea44a --- a/firmware/config.h +++ b/firmware/config.h @@ -24,6 +24,13 @@ #define POWERSAVING_SLEEP_S 600 #define EMERGENCY_SLEEP_S 172800 // Sleep for 2 days to recover #define ENERGY_SAVING_ITERATIONS 30 +#define WIFI_MINIMUM_SIGNAL_QUALITY 10 // percent +#define RESET_ESP_TIME_INTERVAL_MS 3600000 + +#define ROTOR_LENGTH_KM 0.000105 +#define WIND_SENSOR_MEAS_TIME_S 5.0 +#define SEC_TO_HOUR_FACTOR (60.0 * 60.0) +#define COUNT_TO_KMH ((TWO_PI * ROTOR_LENGTH_KM / WIND_SENSOR_MEAS_TIME_S) * SEC_TO_HOUR_FACTOR) #define BAT_LOW_VOLTAGE 3.6 #define BAT_EMERGENCY_DEEPSLEEP_VOLTAGE 3.5 @@ -47,6 +54,7 @@ #define BME_CS 10 #define BME_ADDRESS 0x76 -#define INITIAL_WEBSERVER_TIME 20 +#define INITIAL_WEBSERVER_TIME 20 +#define WEB_UPDATER_HTTP_PORT 8080 #endif diff --git a/firmware/firmware.ino b/firmware/firmware.ino index b8442a1..d56a7bb 100644 --- a/firmware/firmware.ino +++ b/firmware/firmware.ino @@ -5,8 +5,8 @@ #include #include -#include // WiFiClient -#include // WiFiManager from bib manager +#include // WiFiClient +#include // WiFiManager from bib manager // Project includes #include "config.h" @@ -19,21 +19,22 @@ const uint8_t VALUES = 8; float currentSensorData[VALUES] = {nanf("no value"), nanf("no value"), nanf("no value"), nanf("no value"), nanf("no value"), nanf("no value"), nanf("no value"), nanf("no value")}; float (*sensors[VALUES])() = {}; -uint16_t update_sensor_cnt = 0; -uint16_t update_webserver_cnt = 0; +uint16_t update_sensor_cnt = 0; +uint16_t update_webserver_cnt = 0; WiFiManager wifiManager; //*************************************************************************// -void debug(String x) { +void debug(String x) +{ #ifdef DEBUG Serial.println(x); #endif } - -void setup() { +void setup() +{ #if defined(DEBUG) || defined(SERIAL_FEATURE) Serial.begin(115200); @@ -60,16 +61,29 @@ void setup() { // Establish WiFi connection String wifiName = "oko-weather-" + DEVICE_NAME; - wifiManager.setMinimumSignalQuality(16); - wifiManager.setConnectTimeout(WIFI_AUTOCONNECT_TIMEOUT_S); // the time in seconds to wait for the known wifi connection - wifiManager.setTimeout(WIFI_CONFIG_PORTAL_TIMEOUT_S); // the time in seconds to wait for the user to configure the device + wifiManager.setMinimumSignalQuality(WIFI_MINIMUM_SIGNAL_QUALITY); + // the time in seconds to wait for the known wifi connection + wifiManager.setConnectTimeout(WIFI_AUTOCONNECT_TIMEOUT_S); + // the time in seconds to wait for the user to configure the device + wifiManager.setTimeout(WIFI_CONFIG_PORTAL_TIMEOUT_S); - while (!wifiManager.autoConnect(wifiName.c_str(), "DEADBEEF")) +#ifdef SLEEP_IF_NO_WLAN_CONNECTION + while +#endif +#ifndef SLEEP_IF_NO_WLAN_CONNECTION + if +#endif + (!wifiManager.autoConnect(wifiName.c_str(), "DEADBEEF")) { debug("WiFi connection failed, try again in 5 seconds..."); // If autoconnect to WLAN failed and no client connected, go to deep sleep - //ESP.deepSleep(POWERSAVING_SLEEP_S * 1000000, WAKE_RF_DEFAULT); +#ifdef SLEEP_IF_NO_WLAN_CONNECTION + ESP.deepSleep(POWERSAVING_SLEEP_S * 1000000, WAKE_RF_DEFAULT); + delay(100); +#endif +#ifndef SLEEP_IF_NO_WLAN_CONNECTION delay(5000); +#endif } debug("Connected!"); @@ -125,6 +139,15 @@ void setup() { //It's magic! leave in delay(100); +#ifdef RESET_ESP_TIMEINTERVAL + // if millis() reached interval (1h) restart ESP + if (millis() >= RESET_ESP_TIME_INTERVAL_MS) + { + // TODO test + ESP.restart(); + } +#endif + #ifdef BATTERY_POWERED debug("battery powered"); _loop(); diff --git a/firmware/hb_webstat_srv.ino b/firmware/hb_webstat_srv.ino deleted file mode 100755 index 8a186bc..0000000 --- a/firmware/hb_webstat_srv.ino +++ /dev/null @@ -1,30 +0,0 @@ - -// Port des Web Servers auf 80 setzen - -#ifdef HOMEBRIDGE_WEBSTAT - -WiFiServer hb_webstat_server(80); - -const char *hb_ws_msg_start = "{\"temperature\": "; -const char *hb_ws_msg_mid = "\"humidity\": "; -const char *hb_ws_msg_end = "}"; - -void hb_webstat_setup() -{ - hb_webstat_server.begin() -} - -void hb_webstat_send(floar currentSensorDfloar currentSensorData[]ata[]) -{ - WiFiClient client = server.available(); - - if (client.available()) { - client.print(hb_ws_msg_start); - client.print(String(currentSensorData[0],2)); - client.print(hb_ws_msg_mid); - client.print(hb_ws_msg_end); - } -} - -#endif - diff --git a/firmware/influxdb.ino b/firmware/influxdb.ino index 9b6a186..bf27cfc 100755 --- a/firmware/influxdb.ino +++ b/firmware/influxdb.ino @@ -1,7 +1,9 @@ #include // from bib manager +#include "config_user.h" + // Data point -Point sensor("weatherstation"); +Point sensor(DEVICE_NAME); // Init variables to influxdb config - doesn't talk to database InfluxDBClient client(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN); diff --git a/firmware/sensor_wind.ino b/firmware/sensor_wind.ino old mode 100755 new mode 100644 index 746a5f5..6e61a71 --- a/firmware/sensor_wind.ino +++ b/firmware/sensor_wind.ino @@ -1,9 +1,11 @@ + #include "config_user.h" +#include "config.h" -int anemometerRotations = 0; -unsigned long currentTime = 0; +unsigned int anemometerRotations = 0; -ICACHE_RAM_ATTR void _anemometerInterrupt() { +ICACHE_RAM_ATTR void _anemometerInterrupt() +{ anemometerRotations++; #ifdef DEBUG Serial.print("*"); @@ -12,12 +14,13 @@ ICACHE_RAM_ATTR void _anemometerInterrupt() { float wind_speed() { anemometerRotations = 0; - currentTime = millis(); + int interruptNumber = digitalPinToInterrupt(ANEMOMETER_PIN); attachInterrupt(interruptNumber, _anemometerInterrupt, RISING); - delay(1000 * 5); + delay(1000 * WIND_SENSOR_MEAS_TIME_S); // time to measure detachInterrupt(interruptNumber); - return (float)anemometerRotations / 5.0 * 2.4; + return (float)anemometerRotations * COUNT_TO_KMH; + } diff --git a/firmware/webUpdater.ino b/firmware/webUpdater.ino index ca8a043..4cf7da9 100644 --- a/firmware/webUpdater.ino +++ b/firmware/webUpdater.ino @@ -13,12 +13,22 @@ #include #include // WiFiManager from bib manager -ESP8266WebServer httpServer(8080); +#include "config.h" +#include "config_user.h" + +ESP8266WebServer httpServer(WEB_UPDATER_HTTP_PORT); ESP8266HTTPUpdateServer httpUpdater; String _webUpdater_ip = "127.0.0.1"; String _webUpdater_dev = "unknown"; -float _webUpdater_sensValues[VALUES]; +float _webUpdater_sensValues[VALUES]; + +String hb_ws_msg_start = "{"; +String hb_ws_msg_temp = "\"temperature\": "; +String hb_ws_msg_humi = "\"humidity\": "; +String hb_ws_msg_light = "\"light\": "; +String hb_ws_msg_windspeed = "\"windspeed\": "; +String hb_ws_msg_end = "}"; void setupWebUpdater(String device, String ip) { @@ -30,6 +40,9 @@ void setupWebUpdater(String device, String ip) httpServer.on("/", showHTMLMain); httpServer.on("/resetWifiManager", resetWifiManager); +#ifdef HOMEBRIDGE_WEBSTAT + httpServer.on("/hbWebstat", hb_webstat_send); +#endif httpServer.begin(); @@ -42,13 +55,15 @@ void doWebUpdater(void) httpServer.handleClient(); } -void setSensorData(float sensorValues[]) { +void setSensorData(float sensorValues[]) +{ for (uint8_t i = 0; i < VALUES; i++) { _webUpdater_sensValues[i] = sensorValues[i]; } } -void showHTMLMain(void) { +void showHTMLMain(void) +{ String message = "OKO Weatherstation - " + String(_webUpdater_dev) + "" "" "" @@ -66,6 +81,20 @@ void showHTMLMain(void) { httpServer.send(200, "text/html", message); } +void hb_webstat_send(void) +{ + httpServer.send(200, "text/html", hb_ws_msg_start + + hb_ws_msg_temp + + String(_webUpdater_sensValues[SENSOR_TEMPERATURE], 2) + + hb_ws_msg_humi + + String(_webUpdater_sensValues[SENSOR_HUMIDITY], 2) + + hb_ws_msg_light + + String(_webUpdater_sensValues[SENSOR_LIGHT], 2) + + hb_ws_msg_windspeed + + String(_webUpdater_sensValues[SENSOR_WINDSPEED], 2) + + hb_ws_msg_end); +} + void resetWifiManager() { @@ -79,9 +108,11 @@ void resetWifiManager() httpServer.send(200, "text/html", message); // Erase WiFi Credentials, enable, compile, flash, disable and reflash. - WiFiManager.resetSettings(); + WiFiManager wifiManager; + wifiManager.resetSettings(); delay(5000); + // manual reset after restart is required ESP.restart(); } diff --git a/influxdb-lib/examples/influxdb_write/influxdb_write.ino b/influxdb-lib/examples/influxdb_write/influxdb_write.ino old mode 100644 new mode 100755 From 4250c6c9cc6117a02f8cc76ddce7ac261b10cd8b Mon Sep 17 00:00:00 2001 From: Kai Lauterbach Date: Sun, 15 May 2022 22:47:23 +0200 Subject: [PATCH 06/37] Temperatur const value added to fix temperature issues. Updated user config example. Fixed wifi setup. --- firmware/config_user.h.example | 14 +++++++++++--- firmware/firmware.ino | 4 ++-- firmware/sensor_bme280.ino | 4 ++-- 3 files changed, 15 insertions(+), 7 deletions(-) mode change 100644 => 100755 firmware/config_user.h.example mode change 100755 => 100644 firmware/sensor_bme280.ino diff --git a/firmware/config_user.h.example b/firmware/config_user.h.example old mode 100644 new mode 100755 index 4383af4..fbc2ed8 --- a/firmware/config_user.h.example +++ b/firmware/config_user.h.example @@ -9,18 +9,25 @@ // Enable/Disable features //#define WEBUPDATER_FEATURE -#define INFLUXDB_FEATURE +//#define INFLUXDB_FEATURE #define SERIAL_FEATURE -#define BATTERY_POWERED -#define SENSOR_WIND +//#define BATTERY_POWERED +//#define SENSOR_WIND #define SENSOR_APDS9960 //#define SENSOR_APDS9930 #define SENSOR_BME280 #define SENSOR_BATTERY //#define BAT_PINS_D34 +// only possible if webupdater is also enabled +//#define HOMEBRIDGE_WEBSTAT +// retries to connect after 5 seconds or starts the wifimanager +//#define SLEEP_IF_NO_WLAN_CONNECTION +// Restarts the firmware every n seconds +//#define RESET_ESP_TIMEINTERVAL const float HUMIDITY_FACTOR = 1.0; const float LIGHT_FACTOR = 1.0; +const float TEMP_FACTOR = 1.0; // InfluxDB credentials const char *INFLUXDB_HOST = "hostname"; @@ -35,3 +42,4 @@ const char *INFLUXDB_PASS = "password"; String DEVICE_NAME = "devicename"; #endif + diff --git a/firmware/firmware.ino b/firmware/firmware.ino index d56a7bb..b293347 100644 --- a/firmware/firmware.ino +++ b/firmware/firmware.ino @@ -67,10 +67,10 @@ void setup() // the time in seconds to wait for the user to configure the device wifiManager.setTimeout(WIFI_CONFIG_PORTAL_TIMEOUT_S); -#ifdef SLEEP_IF_NO_WLAN_CONNECTION +#ifndef SLEEP_IF_NO_WLAN_CONNECTION while #endif -#ifndef SLEEP_IF_NO_WLAN_CONNECTION +#if def SLEEP_IF_NO_WLAN_CONNECTION if #endif (!wifiManager.autoConnect(wifiName.c_str(), "DEADBEEF")) diff --git a/firmware/sensor_bme280.ino b/firmware/sensor_bme280.ino old mode 100755 new mode 100644 index 5510f79..ae22d3f --- a/firmware/sensor_bme280.ino +++ b/firmware/sensor_bme280.ino @@ -14,11 +14,11 @@ bool sensor_bme280_begin(uint8_t addr) { } float bme280_temperature() { - return _sensor_bme280.readTemperature(); + return _sensor_bme280.readTemperature() * TEMP_FACTOR; } float bme280_pressure() { return _sensor_bme280.readPressure() / 100.0F; } float bme280_humidity() { return _sensor_bme280.readHumidity() * HUMIDITY_FACTOR; -} +} From bce0c811394833aa611e9c742bf0eb461bce0a3d Mon Sep 17 00:00:00 2001 From: Kai Lauterbach Date: Sun, 15 May 2022 23:06:02 +0200 Subject: [PATCH 07/37] #ifdef fixed --- firmware/firmware.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/firmware/firmware.ino b/firmware/firmware.ino index b293347..c9655e0 100644 --- a/firmware/firmware.ino +++ b/firmware/firmware.ino @@ -70,7 +70,7 @@ void setup() #ifndef SLEEP_IF_NO_WLAN_CONNECTION while #endif -#if def SLEEP_IF_NO_WLAN_CONNECTION +#ifdef def SLEEP_IF_NO_WLAN_CONNECTION if #endif (!wifiManager.autoConnect(wifiName.c_str(), "DEADBEEF")) From fdc9ddbf31ba2b08df8ade63287d1e875cecfa35 Mon Sep 17 00:00:00 2001 From: Kai Lauterbach Date: Mon, 16 May 2022 08:46:05 +0200 Subject: [PATCH 08/37] Removed unused code --- firmware/firmware.ino | 6 ------ firmware/influxdb.ino | 4 ++++ 2 files changed, 4 insertions(+), 6 deletions(-) mode change 100755 => 100644 firmware/influxdb.ino diff --git a/firmware/firmware.ino b/firmware/firmware.ino index c9655e0..554d6aa 100644 --- a/firmware/firmware.ino +++ b/firmware/firmware.ino @@ -128,12 +128,6 @@ void setup() #ifndef BATTERY_POWERED setupWebUpdater(DEVICE_NAME, WiFi.localIP().toString()); #endif -#endif - -#ifdef HOMEBRIDGE_WEBSTAT -#ifndef BATTERY_POWERED - hb_webstat_server_setup(); -#endif #endif //It's magic! leave in diff --git a/firmware/influxdb.ino b/firmware/influxdb.ino old mode 100755 new mode 100644 index bf27cfc..91fa61e --- a/firmware/influxdb.ino +++ b/firmware/influxdb.ino @@ -2,6 +2,8 @@ #include "config_user.h" +#if INFLUXDB_VERSION == 2 + // Data point Point sensor(DEVICE_NAME); @@ -103,3 +105,5 @@ void _writePoint() { } } + +#endif // influxdb version 2 check From dea274117c7b427c716a09e90b42f364bd82087a Mon Sep 17 00:00:00 2001 From: Kai Lauterbach Date: Mon, 16 May 2022 10:12:36 +0200 Subject: [PATCH 09/37] Removed old unused code --- firmware/firmware.ino | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/firmware/firmware.ino b/firmware/firmware.ino index 554d6aa..df69d7f 100644 --- a/firmware/firmware.ino +++ b/firmware/firmware.ino @@ -137,7 +137,8 @@ void setup() // if millis() reached interval (1h) restart ESP if (millis() >= RESET_ESP_TIME_INTERVAL_MS) { - // TODO test + debug("Resetting firmware intentionally"); + // Push reset button after flashing once or do a manual power cycle to get the functionality working. ESP.restart(); } #endif @@ -244,10 +245,6 @@ void _loop() { setSensorData(currentSensorData); #endif -#ifdef HOMEBRIDGE_WEBSTAT - hb_webstat_loop(currentSensorData); -#endif - } void logToSerial(float sensorValues[]) { From e13d72fa2cb310d3b631e27e6a35f3f5376fd565 Mon Sep 17 00:00:00 2001 From: Kai Lauterbach Date: Mon, 16 May 2022 10:25:07 +0200 Subject: [PATCH 10/37] Fixed homebrew stat json data generation. --- firmware/config.h | 2 +- firmware/firmware.ino | 7 ++++--- firmware/webUpdater.ino | 6 +++--- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/firmware/config.h b/firmware/config.h index beea44a..49753ac 100644 --- a/firmware/config.h +++ b/firmware/config.h @@ -20,7 +20,7 @@ #define WIFI_CONFIG_PORTAL_TIMEOUT_S 120 #define UPDATE_SENSOR_INTERVAL_S 300 #define UPDATE_WEBSERVER_INTVERVAL_S 1 -#define DELAY_LOOP_MS 50 +#define DELAY_LOOP_MS 60 #define POWERSAVING_SLEEP_S 600 #define EMERGENCY_SLEEP_S 172800 // Sleep for 2 days to recover #define ENERGY_SAVING_ITERATIONS 30 diff --git a/firmware/firmware.ino b/firmware/firmware.ino index df69d7f..9dc6c7b 100644 --- a/firmware/firmware.ino +++ b/firmware/firmware.ino @@ -165,7 +165,8 @@ void setup() //*************************************************************************// #ifdef BATTERY_POWERED -void criticalBatCheck() { +void criticalBatCheck() +{ float volt = battery_voltage(); if (volt <= BAT_EMERGENCY_DEEPSLEEP_VOLTAGE) { debug("Bat Voltage: " + String(volt) + " V"); @@ -178,7 +179,8 @@ void criticalBatCheck() { #endif -void loop() { +void loop() +{ #ifdef BATTERY_POWERED delay(50); return; @@ -191,7 +193,6 @@ void loop() { } #endif - _loop(); //Needed to give WIFI time to function properly diff --git a/firmware/webUpdater.ino b/firmware/webUpdater.ino index 4cf7da9..1abaf1f 100644 --- a/firmware/webUpdater.ino +++ b/firmware/webUpdater.ino @@ -85,11 +85,11 @@ void hb_webstat_send(void) { httpServer.send(200, "text/html", hb_ws_msg_start + hb_ws_msg_temp + - String(_webUpdater_sensValues[SENSOR_TEMPERATURE], 2) + + String(_webUpdater_sensValues[SENSOR_TEMPERATURE], 2) + ", " + hb_ws_msg_humi + - String(_webUpdater_sensValues[SENSOR_HUMIDITY], 2) + + String(_webUpdater_sensValues[SENSOR_HUMIDITY], 2) + ", " + hb_ws_msg_light + - String(_webUpdater_sensValues[SENSOR_LIGHT], 2) + + String(_webUpdater_sensValues[SENSOR_LIGHT], 2) + ", " + hb_ws_msg_windspeed + String(_webUpdater_sensValues[SENSOR_WINDSPEED], 2) + hb_ws_msg_end); From fe3232cfe3049e73b2fbb88d29a1f17aaf1bb9e8 Mon Sep 17 00:00:00 2001 From: Kai Lauterbach Date: Mon, 16 May 2022 10:45:08 +0200 Subject: [PATCH 11/37] Debug output of windspeed calculation added. --- firmware/config.h | 2 +- firmware/sensor_wind.ino | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/firmware/config.h b/firmware/config.h index 49753ac..cae52d4 100644 --- a/firmware/config.h +++ b/firmware/config.h @@ -27,7 +27,7 @@ #define WIFI_MINIMUM_SIGNAL_QUALITY 10 // percent #define RESET_ESP_TIME_INTERVAL_MS 3600000 -#define ROTOR_LENGTH_KM 0.000105 +#define ROTOR_LENGTH_KM 0.000115 #define WIND_SENSOR_MEAS_TIME_S 5.0 #define SEC_TO_HOUR_FACTOR (60.0 * 60.0) #define COUNT_TO_KMH ((TWO_PI * ROTOR_LENGTH_KM / WIND_SENSOR_MEAS_TIME_S) * SEC_TO_HOUR_FACTOR) diff --git a/firmware/sensor_wind.ino b/firmware/sensor_wind.ino index 6e61a71..cfda894 100644 --- a/firmware/sensor_wind.ino +++ b/firmware/sensor_wind.ino @@ -12,7 +12,8 @@ ICACHE_RAM_ATTR void _anemometerInterrupt() #endif } -float wind_speed() { +float wind_speed() +{ anemometerRotations = 0; int interruptNumber = digitalPinToInterrupt(ANEMOMETER_PIN); @@ -21,6 +22,13 @@ float wind_speed() { delay(1000 * WIND_SENSOR_MEAS_TIME_S); // time to measure detachInterrupt(interruptNumber); - return (float)anemometerRotations * COUNT_TO_KMH; + // calculate the speed as km/h + float tmp_speed = (float)anemometerRotations * COUNT_TO_KMH; + +#ifdef DEBUG + Serial.print("Windspeed: " + String(tmp_speed)); +#endif + + return tmp_speed; } From 8852f13d04715999d17d0c5e3ea171e5f31280d2 Mon Sep 17 00:00:00 2001 From: Kai Lauterbach Date: Mon, 16 May 2022 10:47:20 +0200 Subject: [PATCH 12/37] Comments added --- firmware/firmware.ino | 3 +++ 1 file changed, 3 insertions(+) diff --git a/firmware/firmware.ino b/firmware/firmware.ino index 9dc6c7b..ebb2db9 100644 --- a/firmware/firmware.ino +++ b/firmware/firmware.ino @@ -68,9 +68,11 @@ void setup() wifiManager.setTimeout(WIFI_CONFIG_PORTAL_TIMEOUT_S); #ifndef SLEEP_IF_NO_WLAN_CONNECTION + // do not sleep, repeat connecting while #endif #ifdef def SLEEP_IF_NO_WLAN_CONNECTION + // stop connecting after fail to connect to wifi if #endif (!wifiManager.autoConnect(wifiName.c_str(), "DEADBEEF")) @@ -82,6 +84,7 @@ void setup() delay(100); #endif #ifndef SLEEP_IF_NO_WLAN_CONNECTION + // sleep a few seconds and go on trying to connect delay(5000); #endif } From 747724c2edf9b3369de8a29f204c571da82a57d3 Mon Sep 17 00:00:00 2001 From: Kai Lauterbach Date: Mon, 16 May 2022 11:01:06 +0200 Subject: [PATCH 13/37] Updated homebrew web status code to get it compatible to github.com:metbosch/homebridge-http-temperature and github.com/UnPoilTefal/homebridge-advanced-http-temperature-humidity --- firmware/webUpdater.ino | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/firmware/webUpdater.ino b/firmware/webUpdater.ino index 1abaf1f..f416c19 100644 --- a/firmware/webUpdater.ino +++ b/firmware/webUpdater.ino @@ -26,7 +26,7 @@ float _webUpdater_sensValues[VALUES]; String hb_ws_msg_start = "{"; String hb_ws_msg_temp = "\"temperature\": "; String hb_ws_msg_humi = "\"humidity\": "; -String hb_ws_msg_light = "\"light\": "; +String hb_ws_msg_light = "\"lightlevel\": "; String hb_ws_msg_windspeed = "\"windspeed\": "; String hb_ws_msg_end = "}"; @@ -87,9 +87,9 @@ void hb_webstat_send(void) hb_ws_msg_temp + String(_webUpdater_sensValues[SENSOR_TEMPERATURE], 2) + ", " + hb_ws_msg_humi + - String(_webUpdater_sensValues[SENSOR_HUMIDITY], 2) + ", " + + String(int(_webUpdater_sensValues[SENSOR_HUMIDITY]), 2) + ", " + hb_ws_msg_light + - String(_webUpdater_sensValues[SENSOR_LIGHT], 2) + ", " + + String(int(_webUpdater_sensValues[SENSOR_LIGHT]), 2) + ", " + hb_ws_msg_windspeed + String(_webUpdater_sensValues[SENSOR_WINDSPEED], 2) + hb_ws_msg_end); From 676a67c3c0373978ca5433bee07279922456f2b1 Mon Sep 17 00:00:00 2001 From: Kai Lauterbach Date: Mon, 16 May 2022 13:34:33 +0200 Subject: [PATCH 14/37] Fixed webserver update time and modified webstat website content. --- firmware/config.h | 4 ++-- firmware/webUpdater.ino | 33 +++++++++++++++++++++++---------- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/firmware/config.h b/firmware/config.h index cae52d4..3f592b2 100644 --- a/firmware/config.h +++ b/firmware/config.h @@ -19,8 +19,8 @@ #define WIFI_AUTOCONNECT_TIMEOUT_S 120 #define WIFI_CONFIG_PORTAL_TIMEOUT_S 120 #define UPDATE_SENSOR_INTERVAL_S 300 -#define UPDATE_WEBSERVER_INTVERVAL_S 1 -#define DELAY_LOOP_MS 60 +#define UPDATE_WEBSERVER_INTVERVAL_S 1 // Do not change, bigger values will prevent using the webinterface +#define DELAY_LOOP_MS 50 #define POWERSAVING_SLEEP_S 600 #define EMERGENCY_SLEEP_S 172800 // Sleep for 2 days to recover #define ENERGY_SAVING_ITERATIONS 30 diff --git a/firmware/webUpdater.ino b/firmware/webUpdater.ino index f416c19..15f5cc1 100644 --- a/firmware/webUpdater.ino +++ b/firmware/webUpdater.ino @@ -83,16 +83,29 @@ void showHTMLMain(void) void hb_webstat_send(void) { - httpServer.send(200, "text/html", hb_ws_msg_start + - hb_ws_msg_temp + - String(_webUpdater_sensValues[SENSOR_TEMPERATURE], 2) + ", " + - hb_ws_msg_humi + - String(int(_webUpdater_sensValues[SENSOR_HUMIDITY]), 2) + ", " + - hb_ws_msg_light + - String(int(_webUpdater_sensValues[SENSOR_LIGHT]), 2) + ", " + - hb_ws_msg_windspeed + - String(_webUpdater_sensValues[SENSOR_WINDSPEED], 2) + - hb_ws_msg_end); + unsigned int timestamp = 0; + for (int i = 0; i < VALUES; i++) + { + timestamp += int(_webUpdater_sensValues[i]) + millis(); + } + String msg = hb_ws_msg_start + + hb_ws_msg_temp + + String(_webUpdater_sensValues[SENSOR_TEMPERATURE], 2) + + ", " + + hb_ws_msg_humi + + String(_webUpdater_sensValues[SENSOR_HUMIDITY], 2) + + ", " + + hb_ws_msg_light + + String(_webUpdater_sensValues[SENSOR_LIGHT], 2) + + ", " + + hb_ws_msg_windspeed + + String(_webUpdater_sensValues[SENSOR_WINDSPEED], 2) + + ", " + + "\"timestamp\": " + + String(timestamp) + + hb_ws_msg_end; + + httpServer.send(200, "text/html", msg); } void resetWifiManager() From 236034d890ccaa9520cac00fafd6891bb95b51b6 Mon Sep 17 00:00:00 2001 From: Kai Lauterbach Date: Mon, 16 May 2022 14:00:35 +0200 Subject: [PATCH 15/37] Comment added --- firmware/config.h | 2 +- firmware/webUpdater.ino | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/firmware/config.h b/firmware/config.h index 3f592b2..c24f67e 100644 --- a/firmware/config.h +++ b/firmware/config.h @@ -19,7 +19,7 @@ #define WIFI_AUTOCONNECT_TIMEOUT_S 120 #define WIFI_CONFIG_PORTAL_TIMEOUT_S 120 #define UPDATE_SENSOR_INTERVAL_S 300 -#define UPDATE_WEBSERVER_INTVERVAL_S 1 // Do not change, bigger values will prevent using the webinterface +#define UPDATE_WEBSERVER_INTVERVAL_S 1 // Do not change, bigger values will prevent using webupdater webinterface #define DELAY_LOOP_MS 50 #define POWERSAVING_SLEEP_S 600 #define EMERGENCY_SLEEP_S 172800 // Sleep for 2 days to recover diff --git a/firmware/webUpdater.ino b/firmware/webUpdater.ino index 15f5cc1..d14ae57 100644 --- a/firmware/webUpdater.ino +++ b/firmware/webUpdater.ino @@ -96,7 +96,7 @@ void hb_webstat_send(void) String(_webUpdater_sensValues[SENSOR_HUMIDITY], 2) + ", " + hb_ws_msg_light + - String(_webUpdater_sensValues[SENSOR_LIGHT], 2) + + String(_webUpdater_sensValues[SENSOR_LIGHT], 0) + // The light level for the homebridge-http-lux2 plugin is only able to parse integer values ", " + hb_ws_msg_windspeed + String(_webUpdater_sensValues[SENSOR_WINDSPEED], 2) + From ad2502db2a147fcb461d1ceb173d0f3693e207fd Mon Sep 17 00:00:00 2001 From: Kai Lauterbach Date: Mon, 16 May 2022 14:26:17 +0200 Subject: [PATCH 16/37] Just commiting --- firmware/sensor_apds9930.ino | 2 +- firmware/sensor_apds9960.ino | 0 firmware/sensor_battery.ino | 2 +- 3 files changed, 2 insertions(+), 2 deletions(-) mode change 100755 => 100644 firmware/sensor_apds9930.ino mode change 100755 => 100644 firmware/sensor_apds9960.ino mode change 100755 => 100644 firmware/sensor_battery.ino diff --git a/firmware/sensor_apds9930.ino b/firmware/sensor_apds9930.ino old mode 100755 new mode 100644 index 9a81c36..0917dd5 --- a/firmware/sensor_apds9930.ino +++ b/firmware/sensor_apds9930.ino @@ -20,4 +20,4 @@ float apds9930_light() { } else { return 0; } -} +} diff --git a/firmware/sensor_apds9960.ino b/firmware/sensor_apds9960.ino old mode 100755 new mode 100644 diff --git a/firmware/sensor_battery.ino b/firmware/sensor_battery.ino old mode 100755 new mode 100644 index 4cba750..a2e7745 --- a/firmware/sensor_battery.ino +++ b/firmware/sensor_battery.ino @@ -48,4 +48,4 @@ int energySavingMode() { float isEnergySavingMode() { return _sensor_battery_saveMode; -} +} From cc1e91d679a5affadbd1b2a1438475cc3fc281db Mon Sep 17 00:00:00 2001 From: klaute Date: Mon, 16 May 2022 14:27:39 +0200 Subject: [PATCH 17/37] Comment changed --- firmware/config.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/firmware/config.h b/firmware/config.h index 3f592b2..c24f67e 100644 --- a/firmware/config.h +++ b/firmware/config.h @@ -19,7 +19,7 @@ #define WIFI_AUTOCONNECT_TIMEOUT_S 120 #define WIFI_CONFIG_PORTAL_TIMEOUT_S 120 #define UPDATE_SENSOR_INTERVAL_S 300 -#define UPDATE_WEBSERVER_INTVERVAL_S 1 // Do not change, bigger values will prevent using the webinterface +#define UPDATE_WEBSERVER_INTVERVAL_S 1 // Do not change, bigger values will prevent using webupdater webinterface #define DELAY_LOOP_MS 50 #define POWERSAVING_SLEEP_S 600 #define EMERGENCY_SLEEP_S 172800 // Sleep for 2 days to recover From 6109a2901dbfee0d78dcfbc67ffd128ee8524554 Mon Sep 17 00:00:00 2001 From: Kai Lauterbach Date: Mon, 16 May 2022 22:28:42 +0200 Subject: [PATCH 18/37] Fixed windspeed calculation radius value. --- firmware/config.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/firmware/config.h b/firmware/config.h index c24f67e..5fc9260 100644 --- a/firmware/config.h +++ b/firmware/config.h @@ -27,7 +27,7 @@ #define WIFI_MINIMUM_SIGNAL_QUALITY 10 // percent #define RESET_ESP_TIME_INTERVAL_MS 3600000 -#define ROTOR_LENGTH_KM 0.000115 +#define ROTOR_LENGTH_KM 0.000016 // 1.6cm #define WIND_SENSOR_MEAS_TIME_S 5.0 #define SEC_TO_HOUR_FACTOR (60.0 * 60.0) #define COUNT_TO_KMH ((TWO_PI * ROTOR_LENGTH_KM / WIND_SENSOR_MEAS_TIME_S) * SEC_TO_HOUR_FACTOR) From 1e534bf86a24419f2cb18bef07043f032a9dd98f Mon Sep 17 00:00:00 2001 From: Kai Lauterbach Date: Mon, 16 May 2022 22:29:28 +0200 Subject: [PATCH 19/37] Added missing influxDB v1 implementation --- firmware/influxdb.ino | 80 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 79 insertions(+), 1 deletion(-) diff --git a/firmware/influxdb.ino b/firmware/influxdb.ino index 91fa61e..90cd178 100644 --- a/firmware/influxdb.ino +++ b/firmware/influxdb.ino @@ -2,7 +2,85 @@ #include "config_user.h" -#if INFLUXDB_VERSION == 2 +#if INFLUXDB_VERSION == 1 + + +Influxdb _influxdb(INFLUXDB_HOST, INFLUXDB_PORT); + +void influxdb_begin() { + // Init variables to influxdb config - doesn't talk to database + _influxdb.opendb(INFLUXDB_DB, INFLUXDB_USER, INFLUXDB_PASS); +} + +void pushToInfluxDB(String device, float sensorValues[]) { + uint8_t tries = 0; + boolean addComma = false; + + String msg = "weather,device=" + device + " "; + if (!(isnan(sensorValues[SENSOR_TEMPERATURE]))) + { + msg += "temperature=" + String(sensorValues[SENSOR_TEMPERATURE]); + addComma = true; + } + if (!(isnan(sensorValues[SENSOR_HUMIDITY]))) + { + if (true == addComma) + msg += ","; + msg += "humidity=" + String(sensorValues[SENSOR_HUMIDITY]); + addComma = true; + } + if (!(isnan(sensorValues[SENSOR_LIGHT]))) + { + if (true == addComma) + msg += ","; + msg += "light=" + String(sensorValues[SENSOR_LIGHT]); + addComma = true; + } + if (!(isnan(sensorValues[SENSOR_WINDSPEED]))) + { + if (true == addComma) + msg += ","; + msg += "windspeed=" + String(sensorValues[SENSOR_WINDSPEED]); + addComma = true; + } + if (!(isnan(sensorValues[SENSOR_PRESSURE]))) + { + if (true == addComma) + msg += ","; + msg += "pressure=" + String(sensorValues[SENSOR_PRESSURE]); + addComma = true; + } + if (!(isnan(sensorValues[SENSOR_BAT_VOLTAGE]))) + { + if (true == addComma) + msg += ","; + msg += "batvoltage=" + String(sensorValues[SENSOR_BAT_VOLTAGE]); + addComma = true; + } + if (!(isnan(sensorValues[SENSOR_ESAVEMODE]))) + { + if (true == addComma) + msg += ","; + msg += "esavemode=" + String(sensorValues[SENSOR_ESAVEMODE]); + addComma = true; + } + if (!(isnan(sensorValues[SENSOR_BATCHARGESTATE]))) + { + if (true == addComma) + msg += ","; + msg += "batchargestate=" + String(sensorValues[SENSOR_BATCHARGESTATE]); + addComma = true; + } + + debug(msg); + + do { + tries++; + _influxdb.write(msg); + } while (_influxdb.response() != DB_SUCCESS and tries < 5); +} + +#elif INFLUXDB_VERSION == 2 // Data point Point sensor(DEVICE_NAME); From 5f48c233f49ad64a2f212231c5608daec34cdc6f Mon Sep 17 00:00:00 2001 From: Kai Lauterbach Date: Mon, 16 May 2022 22:30:05 +0200 Subject: [PATCH 20/37] Some more files to ignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) mode change 100644 => 100755 .gitignore diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 index 2521773..bc3383e --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ firmware/config_user.h +firmware/.DS_Store +.DS_Store From da25197cdbfa395baa73ce54ccb41fa691bec430 Mon Sep 17 00:00:00 2001 From: Kai Lauterbach Date: Tue, 17 May 2022 10:00:34 +0200 Subject: [PATCH 21/37] Git ignore file udpate --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index bc3383e..5bf2e9d 100755 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ firmware/config_user.h firmware/.DS_Store .DS_Store +schematics/#auto_saved_files# +schematics/_autosave-oko-weatherstation.sch From fde1c5da2d06a89ab39d76aaac66d182cca49d7c Mon Sep 17 00:00:00 2001 From: Kai Lauterbach Date: Tue, 17 May 2022 10:13:17 +0200 Subject: [PATCH 22/37] Optical code changes --- firmware/firmware.ino | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/firmware/firmware.ino b/firmware/firmware.ino index ebb2db9..dc26f0d 100644 --- a/firmware/firmware.ino +++ b/firmware/firmware.ino @@ -41,11 +41,11 @@ void setup() #endif // Pin settings - pinMode(BAT_CHARGED_PIN, INPUT); - pinMode(BAT_CHARGING_PIN, INPUT); - pinMode(STATUS_LED_PIN, OUTPUT); - pinMode(ANEMOMETER_PIN, INPUT_PULLUP); - pinMode(A0, INPUT); + pinMode(BAT_CHARGED_PIN, INPUT); + pinMode(BAT_CHARGING_PIN, INPUT); + pinMode(STATUS_LED_PIN, OUTPUT); + pinMode(ANEMOMETER_PIN, INPUT_PULLUP); + pinMode(A0, INPUT); digitalWrite(STATUS_LED_PIN, LOW); From 3ee77e178fef3160a3df7b5751d63daa17d64789 Mon Sep 17 00:00:00 2001 From: Kai Lauterbach Date: Tue, 17 May 2022 16:58:45 +0200 Subject: [PATCH 23/37] Changed rotor length again --- firmware/config.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/firmware/config.h b/firmware/config.h index 5fc9260..efb0d5d 100644 --- a/firmware/config.h +++ b/firmware/config.h @@ -27,7 +27,7 @@ #define WIFI_MINIMUM_SIGNAL_QUALITY 10 // percent #define RESET_ESP_TIME_INTERVAL_MS 3600000 -#define ROTOR_LENGTH_KM 0.000016 // 1.6cm +#define ROTOR_LENGTH_KM 0.0000825 #define WIND_SENSOR_MEAS_TIME_S 5.0 #define SEC_TO_HOUR_FACTOR (60.0 * 60.0) #define COUNT_TO_KMH ((TWO_PI * ROTOR_LENGTH_KM / WIND_SENSOR_MEAS_TIME_S) * SEC_TO_HOUR_FACTOR) From 7d561546ede0d920f73d9da98ed2f920c865ff8a Mon Sep 17 00:00:00 2001 From: Kai Lauterbach Date: Wed, 18 May 2022 19:08:55 +0200 Subject: [PATCH 24/37] Changed windspeed tp m/s --- firmware/config.h | 12 ++++++++---- firmware/sensor_wind.ino | 2 +- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/firmware/config.h b/firmware/config.h index efb0d5d..66d6d40 100644 --- a/firmware/config.h +++ b/firmware/config.h @@ -27,10 +27,14 @@ #define WIFI_MINIMUM_SIGNAL_QUALITY 10 // percent #define RESET_ESP_TIME_INTERVAL_MS 3600000 -#define ROTOR_LENGTH_KM 0.0000825 -#define WIND_SENSOR_MEAS_TIME_S 5.0 -#define SEC_TO_HOUR_FACTOR (60.0 * 60.0) -#define COUNT_TO_KMH ((TWO_PI * ROTOR_LENGTH_KM / WIND_SENSOR_MEAS_TIME_S) * SEC_TO_HOUR_FACTOR) +#define WIND_SENSOR_MEAS_TIME_S 5.0 +#define ROTOR_LENGTH_CM 8.25 +#define ROTOR_LENGTH_M (ROTOR_LENGTH_CM / 100.0) +#define ROTOR_LENGTH_KM (ROTOR_LENGTH_M / 1000.0) +#define SEC_TO_HOUR_FACTOR (60.0 * 60.0) +#define COUNT_TO_KMH ((TWO_PI * ROTOR_LENGTH_KM / WIND_SENSOR_MEAS_TIME_S) * SEC_TO_HOUR_FACTOR) +#define COUNT_TO_MPS (TWO_PI * ROTOR_LENGTH_M / WIND_SENSOR_MEAS_TIME_S) +#define WINDSPEED_FACTOR COUNT_TO_MPS #define BAT_LOW_VOLTAGE 3.6 #define BAT_EMERGENCY_DEEPSLEEP_VOLTAGE 3.5 diff --git a/firmware/sensor_wind.ino b/firmware/sensor_wind.ino index cfda894..f7cd6bd 100644 --- a/firmware/sensor_wind.ino +++ b/firmware/sensor_wind.ino @@ -23,7 +23,7 @@ float wind_speed() detachInterrupt(interruptNumber); // calculate the speed as km/h - float tmp_speed = (float)anemometerRotations * COUNT_TO_KMH; + float tmp_speed = (float)anemometerRotations * WINDSPEED_FACTOR; #ifdef DEBUG Serial.print("Windspeed: " + String(tmp_speed)); From 795e00be29b30f42b26a33505740b2fc68cc5808 Mon Sep 17 00:00:00 2001 From: Kai Lauterbach Date: Wed, 18 May 2022 19:24:49 +0200 Subject: [PATCH 25/37] Windspeed measurement time increased --- firmware/config.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/firmware/config.h b/firmware/config.h index 66d6d40..92f012e 100644 --- a/firmware/config.h +++ b/firmware/config.h @@ -27,7 +27,7 @@ #define WIFI_MINIMUM_SIGNAL_QUALITY 10 // percent #define RESET_ESP_TIME_INTERVAL_MS 3600000 -#define WIND_SENSOR_MEAS_TIME_S 5.0 +#define WIND_SENSOR_MEAS_TIME_S 15.0 #define ROTOR_LENGTH_CM 8.25 #define ROTOR_LENGTH_M (ROTOR_LENGTH_CM / 100.0) #define ROTOR_LENGTH_KM (ROTOR_LENGTH_M / 1000.0) From 345639395dfa8945637ff0ff12d66fce2b9d7cb6 Mon Sep 17 00:00:00 2001 From: Kai Lauterbach Date: Thu, 19 May 2022 19:41:36 +0200 Subject: [PATCH 26/37] Added pressure to hbWebstat --- firmware/webUpdater.ino | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/firmware/webUpdater.ino b/firmware/webUpdater.ino index d14ae57..f8e3a7d 100644 --- a/firmware/webUpdater.ino +++ b/firmware/webUpdater.ino @@ -28,6 +28,8 @@ String hb_ws_msg_temp = "\"temperature\": "; String hb_ws_msg_humi = "\"humidity\": "; String hb_ws_msg_light = "\"lightlevel\": "; String hb_ws_msg_windspeed = "\"windspeed\": "; +String hb_ws_msg_pressure = "\"pressure\": "; +String hb_ws_msg_timestamp = "\"timestamp\": "; String hb_ws_msg_end = "}"; void setupWebUpdater(String device, String ip) @@ -101,7 +103,10 @@ void hb_webstat_send(void) hb_ws_msg_windspeed + String(_webUpdater_sensValues[SENSOR_WINDSPEED], 2) + ", " + - "\"timestamp\": " + + hb_ws_msg_pressure + + String(_webUpdater_sensValues[SENSOR_PRESSURE], 2) + + ", " + + hb_ws_msg_timestamp + String(timestamp) + hb_ws_msg_end; From 562d0d294df0abf0400dc9724428c48373ab5b33 Mon Sep 17 00:00:00 2001 From: Kai Lauterbach Date: Fri, 20 May 2022 10:13:02 +0200 Subject: [PATCH 27/37] Updated user config example --- firmware/config_user.h.example | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/firmware/config_user.h.example b/firmware/config_user.h.example index fbc2ed8..47a8d57 100755 --- a/firmware/config_user.h.example +++ b/firmware/config_user.h.example @@ -9,7 +9,8 @@ // Enable/Disable features //#define WEBUPDATER_FEATURE -//#define INFLUXDB_FEATURE +#define INFLUXDB_FEATURE +#define INFLUXDB_VERSION 1 // 1 or 2 #define SERIAL_FEATURE //#define BATTERY_POWERED //#define SENSOR_WIND @@ -21,7 +22,7 @@ // only possible if webupdater is also enabled //#define HOMEBRIDGE_WEBSTAT // retries to connect after 5 seconds or starts the wifimanager -//#define SLEEP_IF_NO_WLAN_CONNECTION +#define SLEEP_IF_NO_WLAN_CONNECTION // Restarts the firmware every n seconds //#define RESET_ESP_TIMEINTERVAL @@ -36,6 +37,12 @@ const char *INFLUXDB_DB = "database"; const char *INFLUXDB_USER = "user"; const char *INFLUXDB_PASS = "password"; +// InfluxDB2 credentials +const char *INFLUXDB_URL = "http://192.168.0.123:3124"; +const char *INFLUXDB_ORG = "home_org"; +const char *INFLUXDB_BUCKET = "mybucket; +const char *INFLUXDB_TOKEN = "your api tiken"; + // Device name // WARNING: Keep the name short! If your access point did not show up, you // maybe have a TOO LONG SSID! From b61564d206ae21823da49b623c24890b8e790036 Mon Sep 17 00:00:00 2001 From: Kai Lauterbach Date: Sat, 21 May 2022 17:46:06 +0200 Subject: [PATCH 28/37] Added a weather station fix factor --- firmware/config.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/firmware/config.h b/firmware/config.h index 92f012e..3a02556 100644 --- a/firmware/config.h +++ b/firmware/config.h @@ -32,9 +32,10 @@ #define ROTOR_LENGTH_M (ROTOR_LENGTH_CM / 100.0) #define ROTOR_LENGTH_KM (ROTOR_LENGTH_M / 1000.0) #define SEC_TO_HOUR_FACTOR (60.0 * 60.0) +#define MPS_CORRECT_FACT 9.28 #define COUNT_TO_KMH ((TWO_PI * ROTOR_LENGTH_KM / WIND_SENSOR_MEAS_TIME_S) * SEC_TO_HOUR_FACTOR) #define COUNT_TO_MPS (TWO_PI * ROTOR_LENGTH_M / WIND_SENSOR_MEAS_TIME_S) -#define WINDSPEED_FACTOR COUNT_TO_MPS +#define WINDSPEED_FACTOR (COUNT_TO_MPS * MPS_CORRECT_FACT) #define BAT_LOW_VOLTAGE 3.6 #define BAT_EMERGENCY_DEEPSLEEP_VOLTAGE 3.5 From 7a689034b44ef69697178bcae8b28855d354d2cb Mon Sep 17 00:00:00 2001 From: Kai Lauterbach Date: Sun, 22 May 2022 08:51:24 +0200 Subject: [PATCH 29/37] Added windspeed exceed functionality, it calls an url if max windspeed is exceeded --- firmware/config.h | 4 ++++ firmware/firmware.ino | 38 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/firmware/config.h b/firmware/config.h index 3a02556..352a019 100644 --- a/firmware/config.h +++ b/firmware/config.h @@ -37,6 +37,10 @@ #define COUNT_TO_MPS (TWO_PI * ROTOR_LENGTH_M / WIND_SENSOR_MEAS_TIME_S) #define WINDSPEED_FACTOR (COUNT_TO_MPS * MPS_CORRECT_FACT) +#define HTTP_CALL_ON_WINDSPEED_EXCEED_MPS 5.0 // 5.0 m/s == 18 km/h +#define HTTP_CALL_ON_WINDSPEED_INTERVAL_S 60 // it's required to be bigger than WIND_SENSOR_MEAS_TIME_S +#define HTTP_CALL_ON_WINDSPEED_URL "http://192.168.0.250:3001/button-windspeedexceed?event=click" + #define BAT_LOW_VOLTAGE 3.6 #define BAT_EMERGENCY_DEEPSLEEP_VOLTAGE 3.5 diff --git a/firmware/firmware.ino b/firmware/firmware.ino index dc26f0d..24ce7dd 100644 --- a/firmware/firmware.ino +++ b/firmware/firmware.ino @@ -4,6 +4,7 @@ #include #include #include +#include #include // WiFiClient #include // WiFiManager from bib manager @@ -11,6 +12,12 @@ // Project includes #include "config.h" +#ifdef HTTP_CALL_ON_WINDSPEED_EXCEED +#if (HTTP_CALL_ON_WINDSPEED_INTERVAL_S < HTTP_CALL_ON_WINDSPEED_EXCEED_MPS) +#error "HTTP_CALL_ON_WINDSPEED_INTERVAL_S < WIND_SENSOR_MEAS_TIME_S" +#endif +#endif + #include "config_user.h" //*************************************************************************// @@ -19,8 +26,9 @@ const uint8_t VALUES = 8; float currentSensorData[VALUES] = {nanf("no value"), nanf("no value"), nanf("no value"), nanf("no value"), nanf("no value"), nanf("no value"), nanf("no value"), nanf("no value")}; float (*sensors[VALUES])() = {}; -uint16_t update_sensor_cnt = 0; -uint16_t update_webserver_cnt = 0; +uint16_t update_sensor_cnt = 0; +uint16_t update_webserver_cnt = 0; +uint16_t update_windspeed_exceed_cnt = 0; WiFiManager wifiManager; @@ -206,10 +214,36 @@ void loop() #ifdef WEBUPDATER_FEATURE update_webserver_cnt++; #endif + +#ifdef HTTP_CALL_ON_WINDSPEED_EXCEED + update_windspeed_exceed_cnt++; +#endif } void _loop() { +#ifdef HTTP_CALL_ON_WINDSPEED_EXCEED + if (HTTP_CALL_ON_WINDSPEED_INTERVAL_S * 1000 / DELAY_LOOP_MS > update_windspeed_exceed_cnt) { + if (sensors[SENSOR_WINDSPEED]) { + currentSensorData[SENSOR_WINDSPEED] = sensors[SENSOR_WINDSPEED](); + + if (currentSensorData[SENSOR_WINDSPEED] >= HTTP_CALL_ON_WINDSPEED_EXCEED_MPS) + { + // call the url HTTP_CALL_ON_WINDSPEED_URL + WiFiClient client; + HTTPClient http; + http.begin(client, String(HTTP_CALL_ON_WINDSPEED_URL).c_str()); + http.end(); + // Send HTTP GET request + int httpResponseCode = http.GET(); + } + } else { + currentSensorData[SENSOR_WINDSPEED] = nan("no value"); + } + update_windspeed_exceed_cnt = 0; + } +#endif + #ifndef BATTERY_POWERED if (UPDATE_SENSOR_INTERVAL_S * 1000 / DELAY_LOOP_MS > update_sensor_cnt) { return; From 23bf57648c92e5597d626e002b4ca400bba243f6 Mon Sep 17 00:00:00 2001 From: klaute Date: Sun, 22 May 2022 09:40:59 +0200 Subject: [PATCH 30/37] Disabled weatherstation notice functionality. --- firmware/config.h | 0 firmware/config_user.h.example | 1 + 2 files changed, 1 insertion(+) mode change 100644 => 100755 firmware/config.h diff --git a/firmware/config.h b/firmware/config.h old mode 100644 new mode 100755 diff --git a/firmware/config_user.h.example b/firmware/config_user.h.example index 47a8d57..d98217e 100755 --- a/firmware/config_user.h.example +++ b/firmware/config_user.h.example @@ -25,6 +25,7 @@ #define SLEEP_IF_NO_WLAN_CONNECTION // Restarts the firmware every n seconds //#define RESET_ESP_TIMEINTERVAL +//#define HTTP_CALL_ON_WINDSPEED_EXCEED const float HUMIDITY_FACTOR = 1.0; const float LIGHT_FACTOR = 1.0; From bf413c9897f310bedf7f7d6fd99145c7cb8105ce Mon Sep 17 00:00:00 2001 From: Kai Lauterbach Date: Sun, 22 May 2022 16:35:10 +0200 Subject: [PATCH 31/37] Fixed review findings --- firmware/config.h | 24 ++++++++++++++++-------- firmware/config_user.h.example | 4 ++-- firmware/firmware.ino | 20 ++++++++------------ firmware/influxdb.ino | 4 +++- 4 files changed, 29 insertions(+), 23 deletions(-) mode change 100755 => 100644 firmware/config.h diff --git a/firmware/config.h b/firmware/config.h old mode 100755 new mode 100644 index 352a019..aeb536e --- a/firmware/config.h +++ b/firmware/config.h @@ -23,23 +23,31 @@ #define DELAY_LOOP_MS 50 #define POWERSAVING_SLEEP_S 600 #define EMERGENCY_SLEEP_S 172800 // Sleep for 2 days to recover +#define RESET_ESP_TIME_INTERVAL_MS 3600000 +#define WIND_SENSOR_MEAS_TIME_S 15.0 + #define ENERGY_SAVING_ITERATIONS 30 #define WIFI_MINIMUM_SIGNAL_QUALITY 10 // percent -#define RESET_ESP_TIME_INTERVAL_MS 3600000 -#define WIND_SENSOR_MEAS_TIME_S 15.0 +// thingiverse anomometer settings: https://www.thingiverse.com/thing:2559929/files #define ROTOR_LENGTH_CM 8.25 #define ROTOR_LENGTH_M (ROTOR_LENGTH_CM / 100.0) -#define ROTOR_LENGTH_KM (ROTOR_LENGTH_M / 1000.0) -#define SEC_TO_HOUR_FACTOR (60.0 * 60.0) +//#define ROTOR_LENGTH_KM (ROTOR_LENGTH_M / 1000.0) // configuration example for generalization +//#define SEC_TO_HOUR_FACTOR (60.0 * 60.0) // configuration example for generalization #define MPS_CORRECT_FACT 9.28 -#define COUNT_TO_KMH ((TWO_PI * ROTOR_LENGTH_KM / WIND_SENSOR_MEAS_TIME_S) * SEC_TO_HOUR_FACTOR) +//#define COUNT_TO_KMH ((TWO_PI * ROTOR_LENGTH_KM / WIND_SENSOR_MEAS_TIME_S) * SEC_TO_HOUR_FACTOR) // configuration exampe for generalization #define COUNT_TO_MPS (TWO_PI * ROTOR_LENGTH_M / WIND_SENSOR_MEAS_TIME_S) +// only this define is used for calculation, all other before are only used to describe the math v_wind = correction_factor * rotations * 2 * pi * radius / time_of_measurement_in_sec +// and if required the result has t be multiplied by another factor to convert it from m/s #define WINDSPEED_FACTOR (COUNT_TO_MPS * MPS_CORRECT_FACT) -#define HTTP_CALL_ON_WINDSPEED_EXCEED_MPS 5.0 // 5.0 m/s == 18 km/h -#define HTTP_CALL_ON_WINDSPEED_INTERVAL_S 60 // it's required to be bigger than WIND_SENSOR_MEAS_TIME_S -#define HTTP_CALL_ON_WINDSPEED_URL "http://192.168.0.250:3001/button-windspeedexceed?event=click" +// china aliexpress anemometer settings (calculation unknown) +//#define WINDSPEED_FACTOR 2.4 + +// enable HTTP_CALL_ON_WINDSPEED_EXCEED in config_user.h to enable this feature +#define HTTP_CALL_ON_WINDSPEED_EXCEED_MPS 5.0 // 5.0 m/s == 18 km/h +#define HTTP_CALL_ON_WINDSPEED_INTERVAL_S 60 // it's required to be bigger than WIND_SENSOR_MEAS_TIME_S +#define HTTP_CALL_ON_WINDSPEED_URL "http://192.168.178.100:3001/button-windspeedexceed?event=click" #define BAT_LOW_VOLTAGE 3.6 #define BAT_EMERGENCY_DEEPSLEEP_VOLTAGE 3.5 diff --git a/firmware/config_user.h.example b/firmware/config_user.h.example index d98217e..b8d4f83 100755 --- a/firmware/config_user.h.example +++ b/firmware/config_user.h.example @@ -41,8 +41,8 @@ const char *INFLUXDB_PASS = "password"; // InfluxDB2 credentials const char *INFLUXDB_URL = "http://192.168.0.123:3124"; const char *INFLUXDB_ORG = "home_org"; -const char *INFLUXDB_BUCKET = "mybucket; -const char *INFLUXDB_TOKEN = "your api tiken"; +const char *INFLUXDB_BUCKET = "mybucket"; +const char *INFLUXDB_TOKEN = "your api token"; // Device name // WARNING: Keep the name short! If your access point did not show up, you diff --git a/firmware/firmware.ino b/firmware/firmware.ino index 24ce7dd..aac9672 100644 --- a/firmware/firmware.ino +++ b/firmware/firmware.ino @@ -75,15 +75,7 @@ void setup() // the time in seconds to wait for the user to configure the device wifiManager.setTimeout(WIFI_CONFIG_PORTAL_TIMEOUT_S); -#ifndef SLEEP_IF_NO_WLAN_CONNECTION - // do not sleep, repeat connecting - while -#endif -#ifdef def SLEEP_IF_NO_WLAN_CONNECTION - // stop connecting after fail to connect to wifi - if -#endif - (!wifiManager.autoConnect(wifiName.c_str(), "DEADBEEF")) + while (!wifiManager.autoConnect(wifiName.c_str(), "DEADBEEF")) { debug("WiFi connection failed, try again in 5 seconds..."); // If autoconnect to WLAN failed and no client connected, go to deep sleep @@ -198,7 +190,8 @@ void loop() #endif #ifdef WEBUPDATER_FEATURE - if (UPDATE_WEBSERVER_INTVERVAL_S * 1000 / DELAY_LOOP_MS <= update_webserver_cnt) { + if (UPDATE_WEBSERVER_INTVERVAL_S * 1000 / DELAY_LOOP_MS <= update_webserver_cnt) + { update_webserver_cnt = 0; doWebUpdater(); } @@ -223,8 +216,11 @@ void loop() void _loop() { #ifdef HTTP_CALL_ON_WINDSPEED_EXCEED - if (HTTP_CALL_ON_WINDSPEED_INTERVAL_S * 1000 / DELAY_LOOP_MS > update_windspeed_exceed_cnt) { - if (sensors[SENSOR_WINDSPEED]) { + if (HTTP_CALL_ON_WINDSPEED_INTERVAL_S * 1000 / DELAY_LOOP_MS > update_windspeed_exceed_cnt) + { + if (sensors[SENSOR_WINDSPEED]) + { + // read from windspeed sensor currentSensorData[SENSOR_WINDSPEED] = sensors[SENSOR_WINDSPEED](); if (currentSensorData[SENSOR_WINDSPEED] >= HTTP_CALL_ON_WINDSPEED_EXCEED_MPS) diff --git a/firmware/influxdb.ino b/firmware/influxdb.ino index 90cd178..8356d43 100644 --- a/firmware/influxdb.ino +++ b/firmware/influxdb.ino @@ -1,9 +1,9 @@ -#include // from bib manager #include "config_user.h" #if INFLUXDB_VERSION == 1 +#include // https://github.com/hwwong/ESP8266Influxdb Influxdb _influxdb(INFLUXDB_HOST, INFLUXDB_PORT); @@ -82,6 +82,8 @@ void pushToInfluxDB(String device, float sensorValues[]) { #elif INFLUXDB_VERSION == 2 +#include // from bib manager + // Data point Point sensor(DEVICE_NAME); From 79b61baf71e8513c3d9abd0792facd1c690e8b6f Mon Sep 17 00:00:00 2001 From: Kai Lauterbach Date: Mon, 23 May 2022 12:09:45 +0200 Subject: [PATCH 32/37] Moved some parts of config.h to the user config file. Also added a constants header. --- .gitignore | 1 + firmware/config.h | 37 ++-------------------------------- firmware/config_user.h.example | 37 +++++++++++++++++++++++++++------- firmware/constants.h | 20 ++++++++++++++++++ firmware/firmware.ino | 30 +++++++++++++++------------ 5 files changed, 70 insertions(+), 55 deletions(-) create mode 100644 firmware/constants.h diff --git a/.gitignore b/.gitignore index 5bf2e9d..3762d44 100755 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ firmware/.DS_Store .DS_Store schematics/#auto_saved_files# schematics/_autosave-oko-weatherstation.sch +firmware.ino.bin diff --git a/firmware/config.h b/firmware/config.h index aeb536e..392fdb7 100644 --- a/firmware/config.h +++ b/firmware/config.h @@ -1,20 +1,7 @@ #ifndef __CONFIG_H__ #define __CONFIG_H__ -#define SENSOR_TEMPERATURE 0 -#define SENSOR_HUMIDITY 1 -#define SENSOR_LIGHT 2 -#define SENSOR_WINDSPEED 3 -#define SENSOR_PRESSURE 4 -#define SENSOR_BAT_VOLTAGE 5 -#define SENSOR_ESAVEMODE 6 -#define SENSOR_BATCHARGESTATE 7 - -#define BAT_CHARGE_STATE_CHARGED 2.0 -#define BAT_CHARGE_STATE_CHARGING 1.0 -#define BAT_CHARGE_STATE_NOTCHARGING 0.0 -#define ENERGY_SAVE_MODE_ENABLED 1.0 -#define ENERGY_SAVE_MODE_DISABLED 0.0 +// config general setting and behavior of the weatherstation #define WIFI_AUTOCONNECT_TIMEOUT_S 120 #define WIFI_CONFIG_PORTAL_TIMEOUT_S 120 @@ -25,30 +12,11 @@ #define EMERGENCY_SLEEP_S 172800 // Sleep for 2 days to recover #define RESET_ESP_TIME_INTERVAL_MS 3600000 #define WIND_SENSOR_MEAS_TIME_S 15.0 +#define INITIAL_WEBSERVER_TIME 20 #define ENERGY_SAVING_ITERATIONS 30 #define WIFI_MINIMUM_SIGNAL_QUALITY 10 // percent -// thingiverse anomometer settings: https://www.thingiverse.com/thing:2559929/files -#define ROTOR_LENGTH_CM 8.25 -#define ROTOR_LENGTH_M (ROTOR_LENGTH_CM / 100.0) -//#define ROTOR_LENGTH_KM (ROTOR_LENGTH_M / 1000.0) // configuration example for generalization -//#define SEC_TO_HOUR_FACTOR (60.0 * 60.0) // configuration example for generalization -#define MPS_CORRECT_FACT 9.28 -//#define COUNT_TO_KMH ((TWO_PI * ROTOR_LENGTH_KM / WIND_SENSOR_MEAS_TIME_S) * SEC_TO_HOUR_FACTOR) // configuration exampe for generalization -#define COUNT_TO_MPS (TWO_PI * ROTOR_LENGTH_M / WIND_SENSOR_MEAS_TIME_S) -// only this define is used for calculation, all other before are only used to describe the math v_wind = correction_factor * rotations * 2 * pi * radius / time_of_measurement_in_sec -// and if required the result has t be multiplied by another factor to convert it from m/s -#define WINDSPEED_FACTOR (COUNT_TO_MPS * MPS_CORRECT_FACT) - -// china aliexpress anemometer settings (calculation unknown) -//#define WINDSPEED_FACTOR 2.4 - -// enable HTTP_CALL_ON_WINDSPEED_EXCEED in config_user.h to enable this feature -#define HTTP_CALL_ON_WINDSPEED_EXCEED_MPS 5.0 // 5.0 m/s == 18 km/h -#define HTTP_CALL_ON_WINDSPEED_INTERVAL_S 60 // it's required to be bigger than WIND_SENSOR_MEAS_TIME_S -#define HTTP_CALL_ON_WINDSPEED_URL "http://192.168.178.100:3001/button-windspeedexceed?event=click" - #define BAT_LOW_VOLTAGE 3.6 #define BAT_EMERGENCY_DEEPSLEEP_VOLTAGE 3.5 @@ -71,7 +39,6 @@ #define BME_CS 10 #define BME_ADDRESS 0x76 -#define INITIAL_WEBSERVER_TIME 20 #define WEB_UPDATER_HTTP_PORT 8080 #endif diff --git a/firmware/config_user.h.example b/firmware/config_user.h.example index b8d4f83..6dbd1b4 100755 --- a/firmware/config_user.h.example +++ b/firmware/config_user.h.example @@ -7,6 +7,11 @@ // Debug output on the serial console #define DEBUG +// Device name +// WARNING: Keep the name short! If your access point did not show up, you +// maybe have a TOO LONG SSID! +String DEVICE_NAME = "weatherstation"; + // Enable/Disable features //#define WEBUPDATER_FEATURE #define INFLUXDB_FEATURE @@ -19,13 +24,13 @@ #define SENSOR_BME280 #define SENSOR_BATTERY //#define BAT_PINS_D34 -// only possible if webupdater is also enabled +// Homebridge Webstat is only possible if webupdater is also enabled //#define HOMEBRIDGE_WEBSTAT // retries to connect after 5 seconds or starts the wifimanager #define SLEEP_IF_NO_WLAN_CONNECTION // Restarts the firmware every n seconds -//#define RESET_ESP_TIMEINTERVAL -//#define HTTP_CALL_ON_WINDSPEED_EXCEED +//#define RESET_ESP_TIMEINTERVAL // BETA STATUS +//#define HTTP_CALL_ON_WINDSPEED_EXCEED // BETA STATUS const float HUMIDITY_FACTOR = 1.0; const float LIGHT_FACTOR = 1.0; @@ -39,15 +44,33 @@ const char *INFLUXDB_USER = "user"; const char *INFLUXDB_PASS = "password"; // InfluxDB2 credentials +/* const char *INFLUXDB_URL = "http://192.168.0.123:3124"; const char *INFLUXDB_ORG = "home_org"; const char *INFLUXDB_BUCKET = "mybucket"; const char *INFLUXDB_TOKEN = "your api token"; +*/ -// Device name -// WARNING: Keep the name short! If your access point did not show up, you -// maybe have a TOO LONG SSID! -String DEVICE_NAME = "devicename"; +// enable HTTP_CALL_ON_WINDSPEED_EXCEED to enable this feature +#define HTTP_CALL_ON_WINDSPEED_EXCEED_MPS 5.0 // 5.0 m/s == 18 km/h +#define HTTP_CALL_ON_WINDSPEED_INTERVAL_S 60 // it's required to be bigger than WIND_SENSOR_MEAS_TIME_S +#define HTTP_CALL_ON_WINDSPEED_URL "http://192.168.178.100:3001/button-windspeedexceed?event=click" + +// anemometer settings, to use enable SENSOR_WIND +// thingiverse anemometer settings: https://www.thingiverse.com/thing:2559929/files +#define ROTOR_LENGTH_CM 8.25 +#define ROTOR_LENGTH_M (ROTOR_LENGTH_CM / 100.0) +//#define ROTOR_LENGTH_KM (ROTOR_LENGTH_M / 1000.0) // configuration example for generalization +//#define SEC_TO_HOUR_FACTOR (60.0 * 60.0) // configuration example for generalization +#define MPS_CORRECT_FACT 9.28 +//#define COUNT_TO_KMH ((TWO_PI * ROTOR_LENGTH_KM / WIND_SENSOR_MEAS_TIME_S) * SEC_TO_HOUR_FACTOR) // configuration exampe for generalization +#define COUNT_TO_MPS (TWO_PI * ROTOR_LENGTH_M / WIND_SENSOR_MEAS_TIME_S) +// only this define is used for calculation, all other before are only used to describe the math v_wind = correction_factor * rotations * 2 * pi * radius / time_of_measurement_in_sec +// and if required the result has t be multiplied by another factor to convert it from m/s +#define WINDSPEED_FACTOR (COUNT_TO_MPS * MPS_CORRECT_FACT) + +// china aliexpress anemometer settings (calculation unknown) +//#define WINDSPEED_FACTOR 2.4 #endif diff --git a/firmware/constants.h b/firmware/constants.h new file mode 100644 index 0000000..6710d2e --- /dev/null +++ b/firmware/constants.h @@ -0,0 +1,20 @@ + +#ifndef __CONSTANTS_H__ +#define __CONSTANTS_H__ + +#define SENSOR_TEMPERATURE 0 +#define SENSOR_HUMIDITY 1 +#define SENSOR_LIGHT 2 +#define SENSOR_WINDSPEED 3 +#define SENSOR_PRESSURE 4 +#define SENSOR_BAT_VOLTAGE 5 +#define SENSOR_ESAVEMODE 6 +#define SENSOR_BATCHARGESTATE 7 + +#define BAT_CHARGE_STATE_CHARGED 2.0 +#define BAT_CHARGE_STATE_CHARGING 1.0 +#define BAT_CHARGE_STATE_NOTCHARGING 0.0 +#define ENERGY_SAVE_MODE_ENABLED 1.0 +#define ENERGY_SAVE_MODE_DISABLED 0.0 + +#endif diff --git a/firmware/firmware.ino b/firmware/firmware.ino index aac9672..f5c05ef 100644 --- a/firmware/firmware.ino +++ b/firmware/firmware.ino @@ -10,17 +10,21 @@ #include // WiFiManager from bib manager // Project includes +#include "constants.h" #include "config.h" +#include "config_user.h" + +//*************************************************************************// +// check if some settings are correct #ifdef HTTP_CALL_ON_WINDSPEED_EXCEED -#if (HTTP_CALL_ON_WINDSPEED_INTERVAL_S < HTTP_CALL_ON_WINDSPEED_EXCEED_MPS) +#if (HTTP_CALL_ON_WINDSPEED_INTERVAL_S < WIND_SENSOR_MEAS_TIME_S) #error "HTTP_CALL_ON_WINDSPEED_INTERVAL_S < WIND_SENSOR_MEAS_TIME_S" #endif #endif -#include "config_user.h" - //*************************************************************************// +// constant variables const uint8_t VALUES = 8; float currentSensorData[VALUES] = {nanf("no value"), nanf("no value"), nanf("no value"), nanf("no value"), nanf("no value"), nanf("no value"), nanf("no value"), nanf("no value")}; @@ -66,6 +70,7 @@ void setup() #ifdef BATTERY_POWERED criticalBatCheck(); #endif + // Establish WiFi connection String wifiName = "oko-weather-" + DEVICE_NAME; @@ -181,7 +186,6 @@ void criticalBatCheck() } #endif - void loop() { #ifdef BATTERY_POWERED @@ -189,14 +193,7 @@ void loop() return; #endif -#ifdef WEBUPDATER_FEATURE - if (UPDATE_WEBSERVER_INTVERVAL_S * 1000 / DELAY_LOOP_MS <= update_webserver_cnt) - { - update_webserver_cnt = 0; - doWebUpdater(); - } -#endif - + // call sub loop function _loop(); //Needed to give WIFI time to function properly @@ -215,6 +212,14 @@ void loop() void _loop() { +#ifdef WEBUPDATER_FEATURE + if (UPDATE_WEBSERVER_INTVERVAL_S * 1000 / DELAY_LOOP_MS <= update_webserver_cnt) + { + update_webserver_cnt = 0; + doWebUpdater(); + } +#endif + #ifdef HTTP_CALL_ON_WINDSPEED_EXCEED if (HTTP_CALL_ON_WINDSPEED_INTERVAL_S * 1000 / DELAY_LOOP_MS > update_windspeed_exceed_cnt) { @@ -256,7 +261,6 @@ void _loop() { #endif update_sensor_cnt = 0; - for (uint8_t i = 0; i < VALUES; i++) { if (sensors[i]) { currentSensorData[i] = sensors[i](); From 174eaa985e6e3c152bf779e2db95ed507eeaf1cf Mon Sep 17 00:00:00 2001 From: Kai Lauterbach Date: Mon, 23 May 2022 12:14:38 +0200 Subject: [PATCH 33/37] Windsensor meas time type is int not float --- firmware/config.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/firmware/config.h b/firmware/config.h index 392fdb7..2b2c99e 100644 --- a/firmware/config.h +++ b/firmware/config.h @@ -11,7 +11,7 @@ #define POWERSAVING_SLEEP_S 600 #define EMERGENCY_SLEEP_S 172800 // Sleep for 2 days to recover #define RESET_ESP_TIME_INTERVAL_MS 3600000 -#define WIND_SENSOR_MEAS_TIME_S 15.0 +#define WIND_SENSOR_MEAS_TIME_S 15 #define INITIAL_WEBSERVER_TIME 20 #define ENERGY_SAVING_ITERATIONS 30 From d8dbc002809d517ee44f51ddebd826569ca0080a Mon Sep 17 00:00:00 2001 From: Kai Lauterbach Date: Mon, 23 May 2022 12:21:11 +0200 Subject: [PATCH 34/37] HTTP_CALL_ON_WINDSPEED_EXCEED execution bug fix --- firmware/firmware.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/firmware/firmware.ino b/firmware/firmware.ino index f5c05ef..ddc1d45 100644 --- a/firmware/firmware.ino +++ b/firmware/firmware.ino @@ -221,7 +221,7 @@ void _loop() { #endif #ifdef HTTP_CALL_ON_WINDSPEED_EXCEED - if (HTTP_CALL_ON_WINDSPEED_INTERVAL_S * 1000 / DELAY_LOOP_MS > update_windspeed_exceed_cnt) + if (HTTP_CALL_ON_WINDSPEED_INTERVAL_S * 1000 / DELAY_LOOP_MS <= update_windspeed_exceed_cnt) { if (sensors[SENSOR_WINDSPEED]) { From 7ef9dd4e53a3f71b13a0a844e1025dfeb6716c52 Mon Sep 17 00:00:00 2001 From: Kai Lauterbach Date: Mon, 23 May 2022 12:52:57 +0200 Subject: [PATCH 35/37] Debug output added --- firmware/firmware.ino | 2 ++ 1 file changed, 2 insertions(+) diff --git a/firmware/firmware.ino b/firmware/firmware.ino index ddc1d45..aaa62ec 100644 --- a/firmware/firmware.ino +++ b/firmware/firmware.ino @@ -223,6 +223,7 @@ void _loop() { #ifdef HTTP_CALL_ON_WINDSPEED_EXCEED if (HTTP_CALL_ON_WINDSPEED_INTERVAL_S * 1000 / DELAY_LOOP_MS <= update_windspeed_exceed_cnt) { + debug("Reading wind sensor because of exceed call functionality"); if (sensors[SENSOR_WINDSPEED]) { // read from windspeed sensor @@ -237,6 +238,7 @@ void _loop() { http.end(); // Send HTTP GET request int httpResponseCode = http.GET(); + debug("Called windspeed exceed callout"); } } else { currentSensorData[SENSOR_WINDSPEED] = nan("no value"); From d670b753dc6ce99ec78d43f4b81c534cc062f268 Mon Sep 17 00:00:00 2001 From: Kai Lauterbach Date: Mon, 23 May 2022 13:10:15 +0200 Subject: [PATCH 36/37] Added staus led toggle code --- firmware/firmware.ino | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/firmware/firmware.ino b/firmware/firmware.ino index aaa62ec..f72329a 100644 --- a/firmware/firmware.ino +++ b/firmware/firmware.ino @@ -226,11 +226,14 @@ void _loop() { debug("Reading wind sensor because of exceed call functionality"); if (sensors[SENSOR_WINDSPEED]) { - // read from windspeed sensor + // 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; @@ -239,6 +242,7 @@ void _loop() { // Send HTTP GET request int httpResponseCode = http.GET(); debug("Called windspeed exceed callout"); + digitalWrite(STATUS_LED_PIN, LOW); } } else { currentSensorData[SENSOR_WINDSPEED] = nan("no value"); From ef6920dbe308d4513520a7b51fc52a7ccaf617b8 Mon Sep 17 00:00:00 2001 From: Kai Lauterbach Date: Mon, 23 May 2022 17:34:59 +0200 Subject: [PATCH 37/37] Added http response handling (debug output print). --- firmware/firmware.ino | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/firmware/firmware.ino b/firmware/firmware.ino index f72329a..3009661 100644 --- a/firmware/firmware.ino +++ b/firmware/firmware.ino @@ -234,13 +234,24 @@ void _loop() { 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()); - http.end(); // Send HTTP GET request int httpResponseCode = http.GET(); + + if (httpResponseCode > 0) { + String response = http.getString(); +#ifdef DEBUG + Serial.println("http response code: " + String(httpResponseCode) + " = " + response); +#endif + // TODO handle response + } + + http.end(); debug("Called windspeed exceed callout"); digitalWrite(STATUS_LED_PIN, LOW); }