From 446c7a5807cd5440f7c40ec4e6d2b7eb1cf0b2bb Mon Sep 17 00:00:00 2001 From: klaute Date: Tue, 13 Sep 2022 23:18:05 +0200 Subject: [PATCH] Read sensor data code replaced by separate function. --- firmware/firmware.ino | 149 ++++++++++++++++++++++++++---------------- 1 file changed, 94 insertions(+), 55 deletions(-) diff --git a/firmware/firmware.ino b/firmware/firmware.ino index f6759ff..ea4ccf2 100644 --- a/firmware/firmware.ino +++ b/firmware/firmware.ino @@ -30,7 +30,6 @@ const uint8_t VALUES = 8; // see constants.h file - count of number of SENSOR_ d 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])() = {}; uint32_t update_sensor_cnt = 0; uint32_t update_webserver_cnt = 0; @@ -47,6 +46,8 @@ uint8_t sensor_cnt = 0; boolean validData = false; +boolean do_not_read_windsensor = false; + //*************************************************************************// void debug(String x) @@ -138,35 +139,84 @@ void initSensors() { // Initialize and configure the sensors #ifdef SENSOR_APDS9930 - if (sensor_apds9930_begin()) { - sensors[SENSOR_LIGHT] = &apds9930_light; - } + if (sensor_apds9930_begin(); #endif #ifdef SENSOR_APDS9960 - if (sensor_apds9960_begin()) { - sensors[SENSOR_LIGHT] = &apds9960_light; - } + sensor_apds9960_begin(); #endif #ifdef SENSOR_BME280 //Temperature + pressure - if (sensor_bme280_begin(BME_ADDRESS)) { - sensors[SENSOR_TEMPERATURE] = &bme280_temperature; - sensors[SENSOR_HUMIDITY] = &bme280_humidity; - sensors[SENSOR_PRESSURE] = &bme280_pressure; - } + sensor_bme280_begin(BME_ADDRESS); #endif +} +float readSensors(uint8_t s) +{ + float ret = nan("no value"); + + switch(s) + { + case SENSOR_LIGHT: // Initialize and configure the sensors +#ifdef SENSOR_APDS9930 + ret = apds9930_light(); +#endif +#ifdef SENSOR_APDS9960 + ret = apds9960_light(); +#endif + break; + + case SENSOR_TEMPERATURE: +#ifdef SENSOR_BME280 + ret = bme280_temperature(); +#endif + break; + + case SENSOR_HUMIDITY: +#ifdef SENSOR_BME280 + ret = bme280_humidity(); +#endif + break; + + case SENSOR_PRESSURE: +#ifdef SENSOR_BME280 + ret = bme280_pressure(); +#endif + break; + + case SENSOR_WINDSPEED: #ifdef SENSOR_WIND - sensors[SENSOR_WINDSPEED] = &wind_speed; + if (do_not_read_windsensor == false) + { + ret = wind_speed(); + } #endif + break; + case SENSOR_BAT_VOLTAGE: #ifdef SENSOR_BATTERY - sensors[SENSOR_BAT_VOLTAGE] = &battery_voltage; - sensors[SENSOR_BATCHARGESTATE] = &battery_charging; - sensors[SENSOR_ESAVEMODE] = &isEnergySavingMode; + ret = battery_voltage(); #endif + break; + + case SENSOR_ESAVEMODE: +#ifdef SENSOR_BATTERY + ret = battery_charging(); +#endif + break; + + case SENSOR_BATCHARGESTATE: +#ifdef SENSOR_BATTERY + ret = isEnergySavingMode(); +#endif + break; + + default: + break; + } + + return ret; } //*************************************************************************// @@ -323,12 +373,14 @@ void _loop() #ifdef defined(BATTERY_POWERED) && defined(SENSOR_WIND) if (energySavingMode() == 1) { // Disable expensive tasks - sensors[SENSOR_WINDSPEED] = 0; + //sensors[SENSOR_WINDSPEED] = 0; //debug("read of wind sensor because of low battery disabled"); + do_not_read_windsensor = true; } else { - sensors[SENSOR_WINDSPEED] = &wind_speed; + //sensors[SENSOR_WINDSPEED] = &wind_speed; //debug("read of wind sensor because of high battery enabled"); + do_not_read_windsensor = false; } #endif sensor_cnt = 0; @@ -340,17 +392,8 @@ void _loop() if ((update_sensor_cnt + (UPDATE_SENSOR_INTERVAL_S * 1000)) <= millis()) { debug("read sensor data " + String(sensor_cnt)); - if (sensors[sensor_cnt]) - { - debug("sensors[sensor_ids[" + String(sensor_cnt) + "]]=" + String((int)sensors[sensor_cnt])); - //currentSensorData[sensor_cnt] = sensors[sensor_cnt](); - currentSensorData[sensor_cnt] = sensor_cnt; - } else { - - debug("sensors[" + String(sensor_cnt) + "]=nan"); - currentSensorData[sensor_cnt] = nan("no value"); - } + currentSensorData[sensor_cnt] = readSensors(sensor_cnt); if (sensor_cnt < VALUES) { @@ -426,43 +469,39 @@ void _loop() void readWindSpeedExceed() { - if (sensors[SENSOR_WINDSPEED]) + + // read from windspeed sensorSTATUS_LED_PIN + digitalWrite(STATUS_LED_PIN, HIGH); + currentSensorData[SENSOR_WINDSPEED] = readSensors(SENSOR_WINDSPEED); + digitalWrite(STATUS_LED_PIN, LOW); + + if (currentSensorData[SENSOR_WINDSPEED] >= HTTP_CALL_ON_WINDSPEED_EXCEED_MPS) { - // 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; - // 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(); - 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); + 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); + } + #ifdef WEBUPDATER_FEATURE - setSensorData(currentSensorData); + setSensorData(currentSensorData); #endif - } else { - currentSensorData[SENSOR_WINDSPEED] = nan("no value"); - } } //*************************************************************************//