From 357683cbb9fd32407b4271420599658418a94dee Mon Sep 17 00:00:00 2001 From: Frank <91616163+softhack007@users.noreply.github.com> Date: Wed, 4 Jan 2023 17:30:08 +0100 Subject: [PATCH] UM Battery: more bugfixing, and improvements for esp32 - improved support for esp32 (read calibrated voltage) - corrected config saving (measurement pin, and battery min/max were lost) --- usermods/Battery/readme.md | 6 ++++++ usermods/Battery/usermod_v2_Battery.h | 21 +++++++++++++++------ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/usermods/Battery/readme.md b/usermods/Battery/readme.md index 5a6678704..ea346a747 100644 --- a/usermods/Battery/readme.md +++ b/usermods/Battery/readme.md @@ -79,6 +79,12 @@ Specification from: [Molicel INR18650-M35A, 3500mAh 10A Lithium-ion battery, 3. ## 📝 Change Log +2023-01-04 + +- improved support for esp32 (read calibrated voltage) +- corrected config saving (measurement pin, and battery min/max were lost) +- various bugfixes + 2022-12-25 - added "auto-off" feature diff --git a/usermods/Battery/usermod_v2_Battery.h b/usermods/Battery/usermod_v2_Battery.h index 64e13ec60..7dbc49522 100644 --- a/usermods/Battery/usermod_v2_Battery.h +++ b/usermods/Battery/usermod_v2_Battery.h @@ -176,13 +176,22 @@ class UsermodBattery : public Usermod initializing = false; +#ifdef ARDUINO_ARCH_ESP32 + // use calibrated millivolts analogread on esp32 (150 mV ~ 2450 mV) + rawValue = analogReadMilliVolts(batteryPin); + // calculate the voltage + voltage = (rawValue / 1000.0f) + calibration; + // usually a voltage divider (50%) is used on ESP32, so we need to multiply by 2 + voltage *= 2.0f; +#else // read battery raw input rawValue = analogRead(batteryPin); // calculate the voltage voltage = ((rawValue / getAdcPrecision()) * maxBatteryVoltage) + calibration; +#endif // check if voltage is within specified voltage range - voltage = voltagemaxBatteryVoltage?-1.0f:voltage; + voltage = ((voltagemaxBatteryVoltage)) ? -1.0f : voltage; // translate battery voltage into percentage /* @@ -329,7 +338,7 @@ class UsermodBattery : public Usermod { JsonObject battery = root.createNestedObject(FPSTR(_name)); // usermodname #ifdef ARDUINO_ARCH_ESP32 - battery["pin"] = batteryPin; + battery[F("pin")] = batteryPin; #endif // battery[F("time-left")] = calculateTimeLeftEnabled; @@ -409,8 +418,8 @@ class UsermodBattery : public Usermod newBatteryPin = battery[F("pin")] | newBatteryPin; #endif // calculateTimeLeftEnabled = battery[F("time-left")] | calculateTimeLeftEnabled; - setMinBatteryVoltage(battery[F("min-Voltage")] | minBatteryVoltage); - setMaxBatteryVoltage(battery[F("max-Voltage")] | maxBatteryVoltage); + setMinBatteryVoltage(battery[F("min-voltage")] | minBatteryVoltage); + setMaxBatteryVoltage(battery[F("max-voltage")] | maxBatteryVoltage); setTotalBatteryCapacity(battery[F("capacity")] | totalBatteryCapacity); setCalibration(battery[F("calibration")] | calibration); setReadingInterval(battery[FPSTR(_readInterval)] | readingInterval); @@ -432,14 +441,14 @@ class UsermodBattery : public Usermod if (!initDone) { // first run: reading from cfg.json - newBatteryPin = batteryPin; + batteryPin = newBatteryPin; DEBUG_PRINTLN(F(" config loaded.")); } else { DEBUG_PRINTLN(F(" config (re)loaded.")); - // changing paramters from settings page + // changing parameters from settings page if (newBatteryPin != batteryPin) { // deallocate pin