Battery Monitor for T-Beams

This commit is contained in:
richonguzman 2024-05-27 11:46:14 -04:00
parent e9bb0391f2
commit c5217842ff
6 changed files with 64 additions and 36 deletions

View file

@ -50,6 +50,7 @@ ____________________________________________________
____________________________________________________
## Timeline (Versions):
- 2023.05.27 Battery Monitor for internal and External Voltages (to make board sleep and avoid low discharge of batterys) T-Beam boards now with Battery readings as well.
- 2024.05.23 Forced Reboot Mode added.
- 2024.05.22 Experimental backup-Digirepeater-Mode when "only" iGate mode loses WiFi connection added.
- 2024.05.20 WebConfig update to control whether Messages and Objects should be Tx to RF.

View file

@ -37,7 +37,7 @@ ________________________________________________________________________________
#include "A7670_utils.h"
#endif
String versionDate = "2024.05.24";
String versionDate = "2024.05.27";
Configuration Config;
WiFiClient espClient;

View file

@ -1,6 +1,7 @@
#include "battery_utils.h"
#include "configuration.h"
#include "boards_pinout.h"
#include "power_utils.h"
#include "utils.h"
extern Configuration Config;
@ -25,42 +26,50 @@ namespace BATTERY_Utils {
}
float checkInternalVoltage() {
int sample;
int sampleSum = 0;
#ifdef ADC_CTRL
#if defined(HELTEC_WSL_V3) || defined(HELTEC_WIRELESS_TRACKER)
digitalWrite(ADC_CTRL, HIGH);
#endif
#if defined(HELTEC_V3) || defined(HELTEC_V2)
digitalWrite(ADC_CTRL, LOW);
#endif
#endif
for (int i = 0; i < 100; i++) {
#ifdef BATTERY_PIN
sample = analogRead(BATTERY_PIN);
#endif
#if defined(ESP32_DIY_LoRa) || defined(ESP32_DIY_1W_LoRa)
sample = 0;
#endif
sampleSum += sample;
delayMicroseconds(50);
}
#ifdef ADC_CTRL
#if defined(HELTEC_WSL_V3) || defined(HELTEC_WIRELESS_TRACKER)
digitalWrite(ADC_CTRL, LOW);
#endif
#if defined(HELTEC_V3) || defined(HELTEC_V2)
digitalWrite(ADC_CTRL, HIGH);
#endif
double inputDivider = (1.0 / (390.0 + 100.0)) * 100.0; // The voltage divider is a 390k + 100k resistor in series, 100k on the low side.
return (((sampleSum/100) * adcReadingTransformation) / inputDivider) + 0.285; // Yes, this offset is excessive, but the ADC on the ESP32s3 is quite inaccurate and noisy. Adjust to own measurements.
#if defined(HAS_AXP192) || defined(HAS_AXP2101)
if(POWER_Utils::isBatteryConnected()) {
return POWER_Utils::getBatteryVoltage();
} else {
return 0.0;
}
#else
return (2 * (sampleSum/100) * adcReadingTransformation) + voltageDividerCorrection; // raw voltage without mapping
#endif
int sample;
int sampleSum = 0;
#ifdef ADC_CTRL
#if defined(HELTEC_WSL_V3) || defined(HELTEC_WIRELESS_TRACKER)
digitalWrite(ADC_CTRL, HIGH);
#endif
#if defined(HELTEC_V3) || defined(HELTEC_V2)
digitalWrite(ADC_CTRL, LOW);
#endif
#endif
// return mapVoltage(voltage, 3.34, 4.71, 3.0, 4.2); // mapped voltage
for (int i = 0; i < 100; i++) {
#ifdef BATTERY_PIN
sample = analogRead(BATTERY_PIN);
#endif
#if defined(ESP32_DIY_LoRa) || defined(ESP32_DIY_1W_LoRa)
sample = 0;
#endif
sampleSum += sample;
delayMicroseconds(50);
}
#ifdef ADC_CTRL
#if defined(HELTEC_WSL_V3) || defined(HELTEC_WIRELESS_TRACKER)
digitalWrite(ADC_CTRL, LOW);
#endif
#if defined(HELTEC_V3) || defined(HELTEC_V2)
digitalWrite(ADC_CTRL, HIGH);
#endif
double inputDivider = (1.0 / (390.0 + 100.0)) * 100.0; // The voltage divider is a 390k + 100k resistor in series, 100k on the low side.
return (((sampleSum/100) * adcReadingTransformation) / inputDivider) + 0.285; // Yes, this offset is excessive, but the ADC on the ESP32s3 is quite inaccurate and noisy. Adjust to own measurements.
#else
return (2 * (sampleSum/100) * adcReadingTransformation) + voltageDividerCorrection; // raw voltage without mapping
#endif
// return mapVoltage(voltage, 3.34, 4.71, 3.0, 4.2); // mapped voltage
#endif
}
float checkExternalVoltage() {

View file

@ -21,6 +21,22 @@ extern Configuration Config;
namespace POWER_Utils {
double getBatteryVoltage() {
#if defined(HAS_AXP192) || defined(HAS_AXP2101)
return (PMU.getBattVoltage() / 1000.0);
#else
return 0.0;
#endif
}
bool isBatteryConnected() {
#if defined(HAS_AXP192) || defined(HAS_AXP2101)
return PMU.isBatteryConnect();
#else
return false;
#endif
}
void activateMeasurement() {
#if defined(HAS_AXP192) || defined(HAS_AXP2101)
PMU.disableTSPinMeasure();

View file

@ -11,6 +11,8 @@
namespace POWER_Utils {
double getBatteryVoltage();
bool isBatteryConnected();
void activateMeasurement();
void activateLoRa();
void deactivateLoRa();

View file

@ -122,7 +122,7 @@ namespace Utils {
beaconPacket += Config.beacon.comment;
secondaryBeaconPacket += Config.beacon.comment;
#ifdef BATTERY_PIN
#if defined(BATTERY_PIN) || defined(HAS_AXP192) || defined(HAS_AXP2101)
if (Config.battery.sendInternalVoltage || Config.battery.monitorInternalVoltage) {
float internalVoltage = BATTERY_Utils::checkInternalVoltage();
String internalVoltageInfo = String(internalVoltage,2) + "V";