From 7cdafa76a5c99678632af763a87a56e328ae3e6b Mon Sep 17 00:00:00 2001 From: Frank <91616163+softhack007@users.noreply.github.com> Date: Wed, 4 Jan 2023 12:32:31 +0100 Subject: [PATCH] UM Battery - improvements for esp32 * added missing pinMode(.., INPUT) on esp32 * do not try reading from pin = -1 (ESP32-S2 shows very allergic reactions to this) * Info page - show "n/a" when pin = -1 * readme: esp32 default pin = 35 --- usermods/Battery/readme.md | 2 +- usermods/Battery/usermod_v2_Battery.h | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/usermods/Battery/readme.md b/usermods/Battery/readme.md index d77eeccd..5a667870 100644 --- a/usermods/Battery/readme.md +++ b/usermods/Battery/readme.md @@ -36,7 +36,7 @@ define `USERMOD_BATTERY` in `wled00/my_config.h` | Name | Unit | Description | | ----------------------------------------------- | ----------- |-------------------------------------------------------------------------------------- | | `USERMOD_BATTERY` | | define this (in `my_config.h`) to have this usermod included wled00\usermods_list.cpp | -| `USERMOD_BATTERY_MEASUREMENT_PIN` | | defaults to A0 on ESP8266 and GPIO32 on ESP32 | +| `USERMOD_BATTERY_MEASUREMENT_PIN` | | defaults to A0 on ESP8266 and GPIO35 on ESP32 | | `USERMOD_BATTERY_MEASUREMENT_INTERVAL` | ms | battery check interval. defaults to 30 seconds | | `USERMOD_BATTERY_MIN_VOLTAGE` | v | minimum battery voltage. default is 2.6 (18650 battery standard) | | `USERMOD_BATTERY_MAX_VOLTAGE` | v | maximum battery voltage. default is 4.2 (18650 battery standard) | diff --git a/usermods/Battery/usermod_v2_Battery.h b/usermods/Battery/usermod_v2_Battery.h index ba61edd2..461613a4 100644 --- a/usermods/Battery/usermod_v2_Battery.h +++ b/usermods/Battery/usermod_v2_Battery.h @@ -93,6 +93,7 @@ class UsermodBattery : public Usermod void lowPowerIndicator() { if (!lowPowerIndicatorEnabled) return; + if (batteryPin < 0) return; // no measurement if (lowPowerIndicationDone && lowPowerIndicatorReactivationThreshold <= batteryLevel) lowPowerIndicationDone = false; if (lowPowerIndicatorThreshold <= batteryLevel) return; if (lowPowerIndicationDone) return; @@ -130,6 +131,8 @@ class UsermodBattery : public Usermod if (!success) { DEBUG_PRINTLN(F("Battery pin allocation failed.")); batteryPin = -1; // allocation failed + } else { + pinMode(batteryPin, INPUT); } #else //ESP8266 boards have only one analog input pin A0 @@ -168,6 +171,9 @@ class UsermodBattery : public Usermod nextReadTime = millis() + readingInterval; lastReadTime = millis(); + + if (batteryPin < 0) return; // nothing to read + initializing = false; // read battery raw input @@ -214,6 +220,13 @@ class UsermodBattery : public Usermod JsonObject user = root["u"]; if (user.isNull()) user = root.createNestedObject("u"); + if (batteryPin < 0) { + JsonArray infoVoltage = user.createNestedArray(F("Battery voltage")); + infoVoltage.add(F("n/a")); + infoVoltage.add(F(" invalid GPIO")); + return; // no GPIO - nothing to report + } + // info modal display names JsonArray infoPercentage = user.createNestedArray(F("Battery level")); JsonArray infoVoltage = user.createNestedArray(F("Battery voltage")); @@ -561,7 +574,7 @@ class UsermodBattery : public Usermod { #ifdef ARDUINO_ARCH_ESP32 // esp32 - return 4095.0f; + return 4096.0f; #else // esp8266 return 1024.0f;