diff --git a/src/LoRa_APRS_iGate.cpp b/src/LoRa_APRS_iGate.cpp index 7ca0c20..031cb10 100644 --- a/src/LoRa_APRS_iGate.cpp +++ b/src/LoRa_APRS_iGate.cpp @@ -47,10 +47,12 @@ WiFi_AP *currentWiFi = &Config.wifiAPs[myWiFiAPIndex]; bool isUpdatingOTA = false; uint32_t lastBatteryCheck = 0; -String batteryVoltage; +String batteryVoltage; // ???? bool backUpDigiMode = false; bool modemLoggedToAPRSIS = false; +bool shouldSleep = false; + std::vector receivedPackets; String firstLine, secondLine, thirdLine, fourthLine, fifthLine, sixthLine, seventhLine; @@ -94,7 +96,7 @@ void setup() { if (lastBeacon == 0 || time - lastBeacon >= Config.beacon.interval * 60) { Serial.println("Sending beacon"); String comment = Config.beacon.comment; - if (Config.battery.sendBatteryVoltage) comment += " Batt=" + String(BATTERY_Utils::checkBattery(),2) + "V"; + if (Config.battery.sendBatteryVoltage) comment += " Batt=" + String(BATTERY_Utils::checkInternalVoltage(),2) + "V"; if (Config.battery.externalVoltageMeasurement) comment += " Ext=" + String(BATTERY_Utils::checkExternalVoltage(),2) + "V"; STATION_Utils::addToOutputPacketBuffer(GPS_Utils::getiGateLoRaBeaconPacket() + comment); lastBeacon = time; @@ -183,4 +185,5 @@ void loop() { show_display(firstLine, secondLine, thirdLine, fourthLine, fifthLine, sixthLine, seventhLine, 0); Utils::checkRebootTime(); + Utils::checkSleepByLowBatteryVoltage(); } \ No newline at end of file diff --git a/src/battery_utils.cpp b/src/battery_utils.cpp index 6f44c5e..0ceebf1 100644 --- a/src/battery_utils.cpp +++ b/src/battery_utils.cpp @@ -21,7 +21,7 @@ namespace BATTERY_Utils { return (voltage - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; } - float checkBattery() { + float checkInternalVoltage() { int sample; int sampleSum = 0; #ifdef ADC_CTRL @@ -70,7 +70,7 @@ namespace BATTERY_Utils { } 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 @@ -80,7 +80,7 @@ namespace BATTERY_Utils { if (lastBatteryCheck == 0 || millis() - lastBatteryCheck >= 15 * 60 * 1000) { lastBatteryCheck = millis(); - float voltage = checkBattery(); + float voltage = checkInternalVoltage(); if (voltage < Config.lowVoltageCutOff) { ESP.deepSleep(1800000000); // 30 min sleep (60s = 60e6) diff --git a/src/battery_utils.h b/src/battery_utils.h index fd50532..697a471 100644 --- a/src/battery_utils.h +++ b/src/battery_utils.h @@ -6,9 +6,9 @@ namespace BATTERY_Utils { - float checkBattery(); - float checkExternalVoltage(); - void checkIfShouldSleep(); + float checkInternalVoltage(); + float checkExternalVoltage(); + void checkIfShouldSleep(); } diff --git a/src/utils.cpp b/src/utils.cpp index 4dba2ad..a5e3487 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -32,6 +32,7 @@ extern String distance; extern bool WiFiConnected; extern int wxModuleType; extern bool backUpDigiMode; +extern bool shouldSleep; bool statusAfterBoot = true; bool beaconUpdate = true; @@ -123,18 +124,27 @@ namespace Utils { #ifdef BATTERY_PIN if (Config.battery.sendInternalVoltage) { - String batteryInfo = "Batt=" + String(BATTERY_Utils::checkBattery(),2) + "V"; - beaconPacket += " " + batteryInfo; - secondaryBeaconPacket += " " + batteryInfo; - sixthLine = " ( " + batteryInfo + ")"; + float internalVoltage = BATTERY_Utils::checkInternalVoltage(); + String internalVoltageInfo = "Batt=" + String(internalVoltage,2) + "V"; + beaconPacket += " " + internalVoltageInfo; + secondaryBeaconPacket += " " + internalVoltageInfo; + sixthLine = " ( " + internalVoltageInfo + ")"; } #endif - if (Config.battery.sendExternalVoltage) { - String externalVoltage = String(BATTERY_Utils::checkExternalVoltage(),2) + "V"; - beaconPacket += " Ext=" + externalVoltage; - secondaryBeaconPacket += " Ext=" + externalVoltage; - sixthLine = " (Ext V=" + externalVoltage; + if (Config.battery.sendExternalVoltage || Config.battery.monitorExternalVoltage) { + float externalVoltage = BATTERY_Utils::checkExternalVoltage(); + String externalVoltageInfo = String(externalVoltage,2) + "V"; + if (Config.battery.sendExternalVoltage) { + beaconPacket += " Ext=" + externalVoltageInfo; + secondaryBeaconPacket += " Ext=" + externalVoltageInfo; + sixthLine = " (Ext V=" + externalVoltageInfo + ")"; + } + if (Config.battery.monitorExternalVoltage && externalVoltage <= Config.battery.externalSleepVoltage) { + beaconPacket += " **ExtBatWarning:SLEEP**"; + secondaryBeaconPacket += " **ExtBatWarning:SLEEP**"; + shouldSleep = true; + } } if (Config.aprs_is.active && Config.beacon.sendViaAPRSIS && !backUpDigiMode) { @@ -262,4 +272,12 @@ namespace Utils { } } + void checkSleepByLowBatteryVoltage() { + if (shouldSleep) { + // dormir + shouldSleep = false; + Serial.println("Durmiendo"); + } + } + } \ No newline at end of file diff --git a/src/utils.h b/src/utils.h index 17dc7ed..b8491f6 100644 --- a/src/utils.h +++ b/src/utils.h @@ -25,6 +25,7 @@ namespace Utils { void println(const String& text); void checkRebootMode(); void checkRebootTime(); + void checkSleepByLowBatteryVoltage(); }