LoRa_APRS_iGate/src/battery_utils.cpp

88 lines
2.7 KiB
C++
Raw Normal View History

2023-07-06 06:14:26 +02:00
#include "battery_utils.h"
2023-09-21 02:28:17 +02:00
#include "configuration.h"
2023-07-06 06:14:26 +02:00
#include "pins_config.h"
//#include "lora_utils.h"
2023-07-06 06:14:26 +02:00
2024-03-24 14:01:54 +01:00
// Uncomment if you want to monitor voltage and sleep if voltage is too low (<3.15V)
//#define LOW_VOLTAGE_CUTOFF
2024-03-21 17:31:54 +01:00
2024-03-24 14:01:54 +01:00
float cutOffVoltage = 3.15;
2024-03-21 17:31:54 +01:00
2023-09-21 02:28:17 +02:00
extern Configuration Config;
2024-03-21 17:31:54 +01:00
extern uint32_t lastBatteryCheck;
2023-09-21 02:28:17 +02:00
2023-07-06 07:01:10 +02:00
float adcReadingTransformation = (3.3/4095);
float voltageDividerCorrection = 0.288;
2023-07-06 06:14:26 +02:00
2023-09-21 02:28:17 +02:00
// for External Voltage Measurment (MAX = 15Volts !!!)
float R1 = 100.000; //in Kilo-Ohms
float R2 = 27.000; //in Kilo-Ohms
float readingCorrection = 0.125;
float multiplyCorrection = 0.035;
2024-02-25 16:00:44 +01:00
2023-07-06 06:14:26 +02:00
namespace BATTERY_Utils {
2024-03-21 17:31:54 +01:00
float mapVoltage(float voltage, float in_min, float in_max, float out_min, float out_max) {
return (voltage - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
2023-09-21 02:28:17 +02:00
float checkBattery() {
int sample;
int sampleSum = 0;
for (int i=0; i<100; i++) {
2024-03-21 17:31:54 +01:00
#if defined(TTGO_T_LORA32_V2_1) || defined(HELTEC_V2) || defined(HELTEC_WSL)
2023-09-21 02:28:17 +02:00
sample = analogRead(batteryPin);
2023-12-26 21:27:36 +01:00
#endif
2024-01-03 02:41:54 +01:00
#if defined(HELTEC_V3) || defined(ESP32_DIY_LoRa) || defined(ESP32_DIY_1W_LoRa)
2023-12-26 21:27:36 +01:00
sample = 0;
#endif
2023-09-21 02:28:17 +02:00
sampleSum += sample;
delayMicroseconds(50);
}
2024-03-21 17:31:54 +01:00
float voltage = (2 * (sampleSum/100) * adcReadingTransformation) + voltageDividerCorrection;
return voltage; // raw voltage without mapping
// return mapVoltage(voltage, 3.34, 4.71, 3.0, 4.2); // mapped voltage
2023-09-21 02:28:17 +02:00
}
float checkExternalVoltage() {
int sample;
int sampleSum = 0;
for (int i=0; i<100; i++) {
sample = analogRead(Config.externalVoltagePin);
sampleSum += sample;
delayMicroseconds(50);
}
2024-03-21 17:31:54 +01:00
float voltage = ((((sampleSum/100)* adcReadingTransformation) + readingCorrection) * ((R1+R2)/R2)) - multiplyCorrection;
return voltage; // raw voltage without mapping
// return mapVoltage(voltage, 5.05, 6.32, 4.5, 5.5); // mapped voltage
}
void checkIfShouldSleep() {
2024-03-21 17:31:54 +01:00
#ifdef LOW_VOLTAGE_CUTOFF
if (lastBatteryCheck == 0 || millis() - lastBatteryCheck >= 15 * 60 * 1000) {
lastBatteryCheck = millis();
float voltage = checkBattery();
if (voltage < cutOffVoltage) {
/* Send message over APRS-IS or over LoRa???
APRS_IS_Utils::upload("battery is low = sleeping")
LoRa_Utils::sendNewMessage("battery is low = sleeping");
*/
ESP.deepSleep(1800000000); // 30 min sleep (60s = 60e6)
2024-03-21 17:31:54 +01:00
}
}
#endif
2023-07-06 06:14:26 +02:00
}
}