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
This commit is contained in:
Frank 2023-01-04 12:32:31 +01:00
parent e84b0c91f8
commit 7cdafa76a5
2 changed files with 15 additions and 2 deletions

View File

@ -36,7 +36,7 @@ define `USERMOD_BATTERY` in `wled00/my_config.h`
| Name | Unit | Description | | Name | Unit | Description |
| ----------------------------------------------- | ----------- |-------------------------------------------------------------------------------------- | | ----------------------------------------------- | ----------- |-------------------------------------------------------------------------------------- |
| `USERMOD_BATTERY` | | define this (in `my_config.h`) to have this usermod included wled00\usermods_list.cpp | | `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_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_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) | | `USERMOD_BATTERY_MAX_VOLTAGE` | v | maximum battery voltage. default is 4.2 (18650 battery standard) |

View File

@ -93,6 +93,7 @@ class UsermodBattery : public Usermod
void lowPowerIndicator() void lowPowerIndicator()
{ {
if (!lowPowerIndicatorEnabled) return; if (!lowPowerIndicatorEnabled) return;
if (batteryPin < 0) return; // no measurement
if (lowPowerIndicationDone && lowPowerIndicatorReactivationThreshold <= batteryLevel) lowPowerIndicationDone = false; if (lowPowerIndicationDone && lowPowerIndicatorReactivationThreshold <= batteryLevel) lowPowerIndicationDone = false;
if (lowPowerIndicatorThreshold <= batteryLevel) return; if (lowPowerIndicatorThreshold <= batteryLevel) return;
if (lowPowerIndicationDone) return; if (lowPowerIndicationDone) return;
@ -130,6 +131,8 @@ class UsermodBattery : public Usermod
if (!success) { if (!success) {
DEBUG_PRINTLN(F("Battery pin allocation failed.")); DEBUG_PRINTLN(F("Battery pin allocation failed."));
batteryPin = -1; // allocation failed batteryPin = -1; // allocation failed
} else {
pinMode(batteryPin, INPUT);
} }
#else //ESP8266 boards have only one analog input pin A0 #else //ESP8266 boards have only one analog input pin A0
@ -168,6 +171,9 @@ class UsermodBattery : public Usermod
nextReadTime = millis() + readingInterval; nextReadTime = millis() + readingInterval;
lastReadTime = millis(); lastReadTime = millis();
if (batteryPin < 0) return; // nothing to read
initializing = false; initializing = false;
// read battery raw input // read battery raw input
@ -214,6 +220,13 @@ class UsermodBattery : public Usermod
JsonObject user = root["u"]; JsonObject user = root["u"];
if (user.isNull()) user = root.createNestedObject("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 // info modal display names
JsonArray infoPercentage = user.createNestedArray(F("Battery level")); JsonArray infoPercentage = user.createNestedArray(F("Battery level"));
JsonArray infoVoltage = user.createNestedArray(F("Battery voltage")); JsonArray infoVoltage = user.createNestedArray(F("Battery voltage"));
@ -561,7 +574,7 @@ class UsermodBattery : public Usermod
{ {
#ifdef ARDUINO_ARCH_ESP32 #ifdef ARDUINO_ARCH_ESP32
// esp32 // esp32
return 4095.0f; return 4096.0f;
#else #else
// esp8266 // esp8266
return 1024.0f; return 1024.0f;