Skip to content

Commit

Permalink
[FireBeetle] Fix inadverted sleep if battery is low but external powe…
Browse files Browse the repository at this point in the history
…r 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
  • Loading branch information
matthias-bs authored Dec 3, 2024
1 parent 93bde3b commit 58c2cfd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
10 changes: 6 additions & 4 deletions BresserWeatherSensorLW.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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:
// -
Expand Down Expand Up @@ -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);
Expand All @@ -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));
Expand All @@ -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];
Expand Down
7 changes: 4 additions & 3 deletions src/adc/adc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
// -
Expand All @@ -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;
}
Expand Down

0 comments on commit 58c2cfd

Please sign in to comment.