From 6ad9dddb998cfd5461e11e893d5e468ecd51cf81 Mon Sep 17 00:00:00 2001 From: Kai Lauterbach Date: Sun, 2 Apr 2023 13:24:32 +0200 Subject: [PATCH] Added ping host config and code, also added ISR independent windspeed measurement. --- firmware/config.h | 3 ++- firmware/config_user.h.example | 5 ++++ firmware/firmware.ino | 9 +++---- firmware/sensor_wind.ino | 46 ++++++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 6 deletions(-) diff --git a/firmware/config.h b/firmware/config.h index f02b78e..cd73300 100644 --- a/firmware/config.h +++ b/firmware/config.h @@ -7,11 +7,12 @@ #define WIFI_CONFIG_PORTAL_TIMEOUT_S 120 #define UPDATE_SENSOR_INTERVAL_S 300 #define UPDATE_WEBSERVER_INTVERVAL_MS 500 // Values greater than 1000 will negative affect availability of the webinterface -#define DELAY_LOOP_MS 100 +#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 (60*60*12*1000) // reset every 12 hours #define WIND_SENSOR_MEAS_TIME_S 15 +#define WIND_SPEED_MEAS_NO_ISR_TIME_MS 50 #define WATCHDOG_TIMEOUT_MS WDTO_8S // Look at Esp.h for further possible time declarations #define WIFI_CHECK_INTERVAL_MS 120000 #define INFLUXDB_TIMEOUT_MS 1000 diff --git a/firmware/config_user.h.example b/firmware/config_user.h.example index d39911a..480fdd5 100755 --- a/firmware/config_user.h.example +++ b/firmware/config_user.h.example @@ -26,6 +26,8 @@ String DEVICE_NAME = "weatherstation"; //#define SERIAL_FEATURE #define SENSOR_WIND +//#define SENSOR_WIND_NO_ISR // enable in case that the ISR shoud not be used to measure wind speed + #define SENSOR_APDS9960 //#define SENSOR_APDS9930 #define SENSOR_BME280 @@ -55,6 +57,7 @@ String DEVICE_NAME = "weatherstation"; //#define SHOW_SENSOR_DATA_ON_WEBUPDATER_MAIN_PAGE //#define DEBUG_RESET_REASON #define WEB_RESET +//#define ENABLE_PING_HOST_TEST /********************************************************************************/ @@ -108,6 +111,8 @@ const char *INFLUXDB_TOKEN = "your api token"; /********************************************************************************/ +#define PING_HOST_IP "192.168.178.1" + #ifdef DISABLE_WIFIMANAGER // Set your Static IP address IPAddress local_IP(192, 168, 178, 123); diff --git a/firmware/firmware.ino b/firmware/firmware.ino index 768c5b4..3ece8a7 100644 --- a/firmware/firmware.ino +++ b/firmware/firmware.ino @@ -807,11 +807,10 @@ void http_call_send_json_data() WDT_FEED(); #endif - if (httpResponseCode > 0) { - String response = http.getString(); - debug("http response code: " + String(httpResponseCode) + " = " + response); - // TODO handle response - } + String response = http.getString(); + debug("http response code: " + String(httpResponseCode) + " = " + response); + + http.end(); digitalWrite(STATUS_LED_PIN, HIGH); diff --git a/firmware/sensor_wind.ino b/firmware/sensor_wind.ino index 4b366e0..1656fc3 100644 --- a/firmware/sensor_wind.ino +++ b/firmware/sensor_wind.ino @@ -6,6 +6,7 @@ volatile unsigned int anemometerRotations = 0; uint32_t start_meas_wind_time = 0; int interruptNumber; +#ifndef SENSOR_WIND_NO_ISR void ICACHE_RAM_ATTR _anemometerInterrupt() { anemometerRotations++; @@ -14,6 +15,7 @@ void ICACHE_RAM_ATTR _anemometerInterrupt() debug("*"); #endif } +#endif float wind_speed() { @@ -28,16 +30,60 @@ void start_measure_wind() { start_meas_wind_time = millis(); anemometerRotations = 0; +#ifndef SENSOR_WIND_NO_ISR interruptNumber = digitalPinToInterrupt(ANEMOMETER_PIN); attachInterrupt(interruptNumber, _anemometerInterrupt, FALLING); +#endif } boolean check_measure_wind_done() { + static uint8_t previous_pin_state = 255; + static uint8_t tmp_pin_state = 255; if ((start_meas_wind_time + (WIND_SENSOR_MEAS_TIME_S * 1000)) <= millis()) { +#ifndef SENSOR_WIND_NO_ISR detachInterrupt(interruptNumber); +#endif return true; +#ifdef SENSOR_WIND_NO_ISR + } else { + // measure wind without ISR + uint32_t start = millis(); + +/*#ifdef DEBUG + Serial.print("."); + debug("."); +#endif*/ + + do { + // read pin state + previous_pin_state = tmp_pin_state; + tmp_pin_state = digitalRead(ANEMOMETER_PIN); + +/*#ifdef DEBUG + Serial.print("+"); + debug("+"); +#endif*/ + + if (previous_pin_state != tmp_pin_state && previous_pin_state != 255) + { + anemometerRotations++; +#ifdef DEBUG + Serial.print("*"); + debug("*"); +#endif + } + + // wait 5 ms + delay(5); + +#ifdef ENABLE_WATCHDOG + // feed the watchdog to prevent reset + WDT_FEED(); +#endif + } while ((start + WIND_SPEED_MEAS_NO_ISR_TIME_MS) >= millis()); +#endif // SENSOR_WIND_NO_ISR } return false; }