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)
This commit is contained in:
Frank 2023-01-04 17:30:08 +01:00
parent 15bc6159f9
commit 357683cbb9
2 changed files with 21 additions and 6 deletions

View File

@ -79,6 +79,12 @@ Specification from: [Molicel INR18650-M35A, 3500mAh 10A Lithium-ion battery, 3.
## 📝 Change Log ## 📝 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 2022-12-25
- added "auto-off" feature - added "auto-off" feature

View File

@ -176,13 +176,22 @@ class UsermodBattery : public Usermod
initializing = false; 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 // read battery raw input
rawValue = analogRead(batteryPin); rawValue = analogRead(batteryPin);
// calculate the voltage // calculate the voltage
voltage = ((rawValue / getAdcPrecision()) * maxBatteryVoltage) + calibration; voltage = ((rawValue / getAdcPrecision()) * maxBatteryVoltage) + calibration;
#endif
// check if voltage is within specified voltage range // check if voltage is within specified voltage range
voltage = voltage<minBatteryVoltage||voltage>maxBatteryVoltage?-1.0f:voltage; voltage = ((voltage<minBatteryVoltage) || (voltage>maxBatteryVoltage)) ? -1.0f : voltage;
// translate battery voltage into percentage // translate battery voltage into percentage
/* /*
@ -329,7 +338,7 @@ class UsermodBattery : public Usermod
{ {
JsonObject battery = root.createNestedObject(FPSTR(_name)); // usermodname JsonObject battery = root.createNestedObject(FPSTR(_name)); // usermodname
#ifdef ARDUINO_ARCH_ESP32 #ifdef ARDUINO_ARCH_ESP32
battery["pin"] = batteryPin; battery[F("pin")] = batteryPin;
#endif #endif
// battery[F("time-left")] = calculateTimeLeftEnabled; // battery[F("time-left")] = calculateTimeLeftEnabled;
@ -409,8 +418,8 @@ class UsermodBattery : public Usermod
newBatteryPin = battery[F("pin")] | newBatteryPin; newBatteryPin = battery[F("pin")] | newBatteryPin;
#endif #endif
// calculateTimeLeftEnabled = battery[F("time-left")] | calculateTimeLeftEnabled; // calculateTimeLeftEnabled = battery[F("time-left")] | calculateTimeLeftEnabled;
setMinBatteryVoltage(battery[F("min-Voltage")] | minBatteryVoltage); setMinBatteryVoltage(battery[F("min-voltage")] | minBatteryVoltage);
setMaxBatteryVoltage(battery[F("max-Voltage")] | maxBatteryVoltage); setMaxBatteryVoltage(battery[F("max-voltage")] | maxBatteryVoltage);
setTotalBatteryCapacity(battery[F("capacity")] | totalBatteryCapacity); setTotalBatteryCapacity(battery[F("capacity")] | totalBatteryCapacity);
setCalibration(battery[F("calibration")] | calibration); setCalibration(battery[F("calibration")] | calibration);
setReadingInterval(battery[FPSTR(_readInterval)] | readingInterval); setReadingInterval(battery[FPSTR(_readInterval)] | readingInterval);
@ -432,14 +441,14 @@ class UsermodBattery : public Usermod
if (!initDone) if (!initDone)
{ {
// first run: reading from cfg.json // first run: reading from cfg.json
newBatteryPin = batteryPin; batteryPin = newBatteryPin;
DEBUG_PRINTLN(F(" config loaded.")); DEBUG_PRINTLN(F(" config loaded."));
} }
else else
{ {
DEBUG_PRINTLN(F(" config (re)loaded.")); DEBUG_PRINTLN(F(" config (re)loaded."));
// changing paramters from settings page // changing parameters from settings page
if (newBatteryPin != batteryPin) if (newBatteryPin != batteryPin)
{ {
// deallocate pin // deallocate pin