Battery Monitor for T-Beams
This commit is contained in:
parent
e9bb0391f2
commit
c5217842ff
|
|
@ -50,6 +50,7 @@ ____________________________________________________
|
||||||
____________________________________________________
|
____________________________________________________
|
||||||
## Timeline (Versions):
|
## 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.23 Forced Reboot Mode added.
|
||||||
- 2024.05.22 Experimental backup-Digirepeater-Mode when "only" iGate mode loses WiFi connection 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.
|
- 2024.05.20 WebConfig update to control whether Messages and Objects should be Tx to RF.
|
||||||
|
|
|
||||||
|
|
@ -37,7 +37,7 @@ ________________________________________________________________________________
|
||||||
#include "A7670_utils.h"
|
#include "A7670_utils.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
String versionDate = "2024.05.24";
|
String versionDate = "2024.05.27";
|
||||||
Configuration Config;
|
Configuration Config;
|
||||||
WiFiClient espClient;
|
WiFiClient espClient;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
#include "battery_utils.h"
|
#include "battery_utils.h"
|
||||||
#include "configuration.h"
|
#include "configuration.h"
|
||||||
#include "boards_pinout.h"
|
#include "boards_pinout.h"
|
||||||
|
#include "power_utils.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
extern Configuration Config;
|
extern Configuration Config;
|
||||||
|
|
@ -25,42 +26,50 @@ namespace BATTERY_Utils {
|
||||||
}
|
}
|
||||||
|
|
||||||
float checkInternalVoltage() {
|
float checkInternalVoltage() {
|
||||||
int sample;
|
#if defined(HAS_AXP192) || defined(HAS_AXP2101)
|
||||||
int sampleSum = 0;
|
if(POWER_Utils::isBatteryConnected()) {
|
||||||
#ifdef ADC_CTRL
|
return POWER_Utils::getBatteryVoltage();
|
||||||
#if defined(HELTEC_WSL_V3) || defined(HELTEC_WIRELESS_TRACKER)
|
} else {
|
||||||
digitalWrite(ADC_CTRL, HIGH);
|
return 0.0;
|
||||||
#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.
|
|
||||||
#else
|
#else
|
||||||
return (2 * (sampleSum/100) * adcReadingTransformation) + voltageDividerCorrection; // raw voltage without mapping
|
int sample;
|
||||||
#endif
|
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() {
|
float checkExternalVoltage() {
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,22 @@ extern Configuration Config;
|
||||||
|
|
||||||
namespace POWER_Utils {
|
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() {
|
void activateMeasurement() {
|
||||||
#if defined(HAS_AXP192) || defined(HAS_AXP2101)
|
#if defined(HAS_AXP192) || defined(HAS_AXP2101)
|
||||||
PMU.disableTSPinMeasure();
|
PMU.disableTSPinMeasure();
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,8 @@
|
||||||
|
|
||||||
namespace POWER_Utils {
|
namespace POWER_Utils {
|
||||||
|
|
||||||
|
double getBatteryVoltage();
|
||||||
|
bool isBatteryConnected();
|
||||||
void activateMeasurement();
|
void activateMeasurement();
|
||||||
void activateLoRa();
|
void activateLoRa();
|
||||||
void deactivateLoRa();
|
void deactivateLoRa();
|
||||||
|
|
|
||||||
|
|
@ -122,7 +122,7 @@ namespace Utils {
|
||||||
beaconPacket += Config.beacon.comment;
|
beaconPacket += Config.beacon.comment;
|
||||||
secondaryBeaconPacket += 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) {
|
if (Config.battery.sendInternalVoltage || Config.battery.monitorInternalVoltage) {
|
||||||
float internalVoltage = BATTERY_Utils::checkInternalVoltage();
|
float internalVoltage = BATTERY_Utils::checkInternalVoltage();
|
||||||
String internalVoltageInfo = String(internalVoltage,2) + "V";
|
String internalVoltageInfo = String(internalVoltage,2) + "V";
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue