From 58c2cfd8d0f43ed524f74d88380588b3a02b6783 Mon Sep 17 00:00:00 2001 From: Matthias Prinke <83612361+matthias-bs@users.noreply.github.com> Date: Tue, 3 Dec 2024 19:05:39 +0100 Subject: [PATCH] [FireBeetle] Fix inadverted sleep if battery is low but external power is good (#116) * Fixed getVoltage(): use parameter 'pin' instead of PIN_ADC_IN * Added supply voltage measurement if PIN_SUPPLY_IN is defined * Moved start of sensor reception after battery voltage check --- BresserWeatherSensorLW.ino | 10 ++++++---- src/adc/adc.cpp | 7 ++++--- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/BresserWeatherSensorLW.ino b/BresserWeatherSensorLW.ino index 8bd96de..f77074d 100644 --- a/BresserWeatherSensorLW.ino +++ b/BresserWeatherSensorLW.ino @@ -105,6 +105,8 @@ // 20240912 Bumped to RadioLib v7.0.0 // 20240928 Modified for LoRaWAN v1.0.4 (requires no nwkKey) // 20241123 PowerFeather: Fixed inadverted sleep if battery low & supply o.k. +// 20241203 Added supply voltage measurement if PIN_SUPPLY_IN is defined +// Moved start of sensor reception after battery voltage check // // ToDo: // - @@ -582,9 +584,6 @@ void setup() #endif loadSecrets(requireNwkKey, joinEUI, devEUI, nwkKey, appKey); - // Initialize Application Layer - appLayer.begin(); - preferences.begin("BWS-LW", false); prefs.sleep_interval = preferences.getUShort("sleep_int", SLEEP_INTERVAL); prefs.sleep_interval_long = preferences.getUShort("sleep_int_long", SLEEP_INTERVAL_LONG); @@ -595,7 +594,7 @@ void setup() if (voltage && voltage <= battery_low) { log_i("Battery low!"); - #if defined(ARDUINO_ESP32S3_POWERFEATHER) + #if defined(ARDUINO_ESP32S3_POWERFEATHER) || defined(PIN_SUPPLY_IN) uint16_t supplyVoltage = getSupplyVoltage(); if (supplyVoltage < battery_low) { gotoSleep(sleepDuration(battery_weak)); @@ -604,6 +603,9 @@ void setup() gotoSleep(sleepDuration(battery_weak)); #endif } + + // Initialize Application Layer - starts sensor reception + appLayer.begin(); // build payload byte array (+ reserve to prevent overflow with configuration at run-time) uint8_t uplinkPayload[PAYLOAD_SIZE + 8]; diff --git a/src/adc/adc.cpp b/src/adc/adc.cpp index b880baf..28c6312 100644 --- a/src/adc/adc.cpp +++ b/src/adc/adc.cpp @@ -40,6 +40,7 @@ // 20240430 Modified getBatteryVoltage() // 20240504 Heltec WiFi 32 LoRa V3: Changed ADC input attenuation to get higher accuracy // 20240607 Changed ARDUINO_HELTEC_WIFI_LORA_32_V3 to uppercase +// 20241203 Fixed getVoltage(): use parameter 'pin' instead of PIN_ADC_IN // // ToDo: // - @@ -66,14 +67,14 @@ getVoltage(uint8_t pin, uint8_t samples, float div) for (uint8_t i = 0; i < UBATT_SAMPLES; i++) { #if defined(ESP32) - voltage_raw += float(analogReadMilliVolts(PIN_ADC_IN)); + voltage_raw += float(analogReadMilliVolts(pin)); #else - voltage_raw += float(analogRead(PIN_ADC_IN)) / 4095.0 * 3300; + voltage_raw += float(analogRead(pin)) / 4095.0 * 3300; #endif } uint16_t voltage = int(voltage_raw / UBATT_SAMPLES / UBATT_DIV); - log_d("Voltage = %dmV", voltage); + log_d("Voltage @GPIO%02d = %dmV", pin, voltage); return voltage; }