diff --git a/include/lora_utils.h b/include/lora_utils.h index ac62eea..8ce71e6 100644 --- a/include/lora_utils.h +++ b/include/lora_utils.h @@ -8,6 +8,7 @@ namespace LoRa_Utils { void setup(); void sendNewPacket(const String& newPacket); + String receivePacketFromSleep(); String receivePacket(); void changeFreqTx(); void changeFreqRx(); diff --git a/include/sleep_utils.h b/include/sleep_utils.h index 21024c3..52e8ba5 100644 --- a/include/sleep_utils.h +++ b/include/sleep_utils.h @@ -4,8 +4,10 @@ #include namespace SLEEP_Utils { - - void setup(); + + void setup(); + void checkWakeUpFlag(); + void startSleeping(); } diff --git a/include/station_utils.h b/include/station_utils.h index 2230216..ed11eb9 100644 --- a/include/station_utils.h +++ b/include/station_utils.h @@ -27,6 +27,7 @@ namespace STATION_Utils { bool wasHeard(const String& station); void clean25SegBuffer(); bool check25SegBuffer(const String& station, const String& textMessage); + void processOutputPacketBufferUltraEcoMode(); void processOutputPacketBuffer(); void addToOutputPacketBuffer(const String& packet); diff --git a/src/LoRa_APRS_iGate.cpp b/src/LoRa_APRS_iGate.cpp index 1a0182e..fa6cb00 100644 --- a/src/LoRa_APRS_iGate.cpp +++ b/src/LoRa_APRS_iGate.cpp @@ -148,95 +148,102 @@ void setup() { } void loop() { - WIFI_Utils::checkAutoAPTimeout(); + if (Config.digi.ecoMode == 1) { + SLEEP_Utils::checkWakeUpFlag(); + Utils::checkBeaconInterval(); + STATION_Utils::processOutputPacketBufferUltraEcoMode(); + SLEEP_Utils::startSleeping(); + } else { + WIFI_Utils::checkAutoAPTimeout(); - if (isUpdatingOTA) { - ElegantOTA.loop(); - return; // Don't process IGate and Digi during OTA update - } + if (isUpdatingOTA) { + ElegantOTA.loop(); + return; // Don't process IGate and Digi during OTA update + } - if (Config.lowVoltageCutOff > 0) { - BATTERY_Utils::checkIfShouldSleep(); - } - - #ifdef HAS_GPS - if (Config.beacon.gpsActive) { - if (millis() - gpsSatelliteTime > 5000) { - gpsInfoToggle = !gpsInfoToggle; - gpsSatelliteTime = millis(); - } - if (gpsInfoToggle) { - thirdLine = "Satellite(s): "; - String gpsData = String(gps.satellites.value()); - if (gpsData.length() < 2) gpsData = "0" + gpsData; // Ensure two-digit formatting - thirdLine += gpsData; + if (Config.lowVoltageCutOff > 0) { + BATTERY_Utils::checkIfShouldSleep(); + } + + #ifdef HAS_GPS + if (Config.beacon.gpsActive) { + if (millis() - gpsSatelliteTime > 5000) { + gpsInfoToggle = !gpsInfoToggle; + gpsSatelliteTime = millis(); + } + if (gpsInfoToggle) { + thirdLine = "Satellite(s): "; + String gpsData = String(gps.satellites.value()); + if (gpsData.length() < 2) gpsData = "0" + gpsData; // Ensure two-digit formatting + thirdLine += gpsData; + } else { + thirdLine = Utils::getLocalIP(); + } } else { thirdLine = Utils::getLocalIP(); } - } else { + #else thirdLine = Utils::getLocalIP(); - } - #else - thirdLine = Utils::getLocalIP(); - #endif + #endif - #ifdef HAS_A7670 - if (Config.aprs_is.active && !modemLoggedToAPRSIS) A7670_Utils::APRS_IS_connect(); - #else - WIFI_Utils::checkWiFi(); - if (Config.aprs_is.active && (WiFi.status() == WL_CONNECTED) && !espClient.connected()) APRS_IS_Utils::connect(); - #endif + #ifdef HAS_A7670 + if (Config.aprs_is.active && !modemLoggedToAPRSIS) A7670_Utils::APRS_IS_connect(); + #else + WIFI_Utils::checkWiFi(); + if (Config.aprs_is.active && (WiFi.status() == WL_CONNECTED) && !espClient.connected()) APRS_IS_Utils::connect(); + #endif - NTP_Utils::update(); - TNC_Utils::loop(); + NTP_Utils::update(); + TNC_Utils::loop(); - Utils::checkDisplayInterval(); - Utils::checkBeaconInterval(); - - APRS_IS_Utils::checkStatus(); // Need that to update display, maybe split this and send APRSIS status to display func? + Utils::checkDisplayInterval(); + Utils::checkBeaconInterval(); + + APRS_IS_Utils::checkStatus(); // Need that to update display, maybe split this and send APRSIS status to display func? - String packet = ""; - if (Config.loramodule.rxActive) { - packet = LoRa_Utils::receivePacket(); // We need to fetch LoRa packet above APRSIS and Digi - } - - if (packet != "") { - if (Config.aprs_is.active) { // If APRSIS enabled - APRS_IS_Utils::processLoRaPacket(packet); // Send received packet to APRSIS + String packet = ""; + if (Config.loramodule.rxActive) { + packet = LoRa_Utils::receivePacket(); // We need to fetch LoRa packet above APRSIS and Digi } - if (Config.loramodule.txActive && (Config.digi.mode == 2 || Config.digi.mode == 3 || backUpDigiMode)) { // If Digi enabled - STATION_Utils::clean25SegBuffer(); - DIGI_Utils::processLoRaPacket(packet); // Send received packet to Digi - } + if (packet != "") { + if (Config.aprs_is.active) { // If APRSIS enabled + APRS_IS_Utils::processLoRaPacket(packet); // Send received packet to APRSIS + } - if (Config.tnc.enableServer) { // If TNC server enabled - TNC_Utils::sendToClients(packet); // Send received packet to TNC KISS - } - if (Config.tnc.enableSerial) { // If Serial KISS enabled - TNC_Utils::sendToSerial(packet); // Send received packet to Serial KISS - } - } + if (Config.loramodule.txActive && (Config.digi.mode == 2 || Config.digi.mode == 3 || backUpDigiMode)) { // If Digi enabled + STATION_Utils::clean25SegBuffer(); + DIGI_Utils::processLoRaPacket(packet); // Send received packet to Digi + } - if (Config.aprs_is.active) { - APRS_IS_Utils::listenAPRSIS(); // listen received packet from APRSIS - } - - STATION_Utils::processOutputPacketBuffer(); - - #ifdef HAS_EPAPER // Only consider updating every 10 seconds (when data to show is different from before) - if(lastEpaperTime == 0 || millis() - lastEpaperTime > 10000) { - String posibleEpaperText = firstLine + secondLine + thirdLine + fourthLine + fifthLine + sixthLine + seventhLine; - if (lastEpaperText != posibleEpaperText) { - displayShow(firstLine, secondLine, thirdLine, fourthLine, fifthLine, sixthLine, seventhLine, 0); - lastEpaperText = posibleEpaperText; - lastEpaperTime = millis(); + if (Config.tnc.enableServer) { // If TNC server enabled + TNC_Utils::sendToClients(packet); // Send received packet to TNC KISS + } + if (Config.tnc.enableSerial) { // If Serial KISS enabled + TNC_Utils::sendToSerial(packet); // Send received packet to Serial KISS } } - #else - displayShow(firstLine, secondLine, thirdLine, fourthLine, fifthLine, sixthLine, seventhLine, 0); - #endif - Utils::checkRebootTime(); - Utils::checkSleepByLowBatteryVoltage(1); + if (Config.aprs_is.active) { + APRS_IS_Utils::listenAPRSIS(); // listen received packet from APRSIS + } + + STATION_Utils::processOutputPacketBuffer(); + + #ifdef HAS_EPAPER // Only consider updating every 10 seconds (when data to show is different from before) + if(lastEpaperTime == 0 || millis() - lastEpaperTime > 10000) { + String posibleEpaperText = firstLine + secondLine + thirdLine + fourthLine + fifthLine + sixthLine + seventhLine; + if (lastEpaperText != posibleEpaperText) { + displayShow(firstLine, secondLine, thirdLine, fourthLine, fifthLine, sixthLine, seventhLine, 0); + lastEpaperText = posibleEpaperText; + lastEpaperTime = millis(); + } + } + #else + displayShow(firstLine, secondLine, thirdLine, fourthLine, fifthLine, sixthLine, seventhLine, 0); + #endif + + Utils::checkRebootTime(); + Utils::checkSleepByLowBatteryVoltage(1); + } } \ No newline at end of file diff --git a/src/digi_utils.cpp b/src/digi_utils.cpp index f5dee6e..f42ab8f 100644 --- a/src/digi_utils.cpp +++ b/src/digi_utils.cpp @@ -148,11 +148,7 @@ namespace DIGI_Utils { if (!queryMessage) { String loraPacket = generateDigipeatedPacket(packet.substring(3), thirdPartyPacket); if (loraPacket != "") { - if (Config.lowPowerMode) { - LoRa_Utils::sendNewPacket(loraPacket); - } else { - STATION_Utils::addToOutputPacketBuffer(loraPacket); - } + STATION_Utils::addToOutputPacketBuffer(loraPacket); displayToggle(true); lastScreenOn = millis(); } diff --git a/src/lora_utils.cpp b/src/lora_utils.cpp index ccb25b1..c33e823 100644 --- a/src/lora_utils.cpp +++ b/src/lora_utils.cpp @@ -159,7 +159,7 @@ namespace LoRa_Utils { radio.startReceive(); }*/ - /*String receiveFromSleep() { + String receivePacketFromSleep() { String packet = ""; int state = radio.readData(packet); if (state == RADIOLIB_ERR_NONE) { @@ -168,13 +168,13 @@ namespace LoRa_Utils { packet = ""; } return packet; - }*/ + } String receivePacket() { String packet = ""; - if (operationDone || Config.lowPowerMode) { + if (operationDone) { operationDone = false; - if (transmitFlag && !Config.lowPowerMode) { + if (transmitFlag) { radio.startReceive(); transmitFlag = false; } else { @@ -190,7 +190,7 @@ namespace LoRa_Utils { Utils::println("<--- LoRa Packet Rx : " + packet.substring(3)); Utils::println("(RSSI:" + String(rssi) + " / SNR:" + String(snr) + " / FreqErr:" + String(freqError) + ")"); - if (!Config.lowPowerMode && Config.digi.ecoMode == 0) { + if (Config.digi.ecoMode == 0) { if (receivedPackets.size() >= 10) { receivedPackets.erase(receivedPackets.begin()); } diff --git a/src/sleep_utils.cpp b/src/sleep_utils.cpp index 527c2bf..3ea5791 100644 --- a/src/sleep_utils.cpp +++ b/src/sleep_utils.cpp @@ -1,10 +1,12 @@ #include "configuration.h" #include "board_pinout.h" #include "sleep_utils.h" +#include "digi_utils.h" #include "lora_utils.h" extern Configuration Config; +extern uint32_t lastBeaconTx; bool wakeUpFlag = false; @@ -15,6 +17,16 @@ namespace SLEEP_Utils { wakeUpFlag = true; } + void checkWakeUpFlag() { + if (wakeUpFlag) { + String packet = LoRa_Utils::receivePacketFromSleep(); + if (packet != "") { + DIGI_Utils::processLoRaPacket(packet); + } + wakeUpFlag = false; + } + } + void setup() { if (Config.digi.ecoMode == 1) { pinMode(RADIO_WAKEUP_PIN, INPUT); @@ -29,4 +41,17 @@ namespace SLEEP_Utils { } } + uint32_t getSecondsToSleep() { + uint32_t elapsedTime = (millis() - lastBeaconTx) / 1000; // in secs + uint32_t intervalTime = Config.beacon.interval * 60; // in secs + return (elapsedTime < intervalTime) ? (intervalTime - elapsedTime) : 0; + } + + void startSleeping() { + esp_sleep_enable_timer_wakeup(getSecondsToSleep() * 1000000); // 1 min = 60sec + delay(100); + LoRa_Utils::wakeRadio(); + esp_light_sleep_start(); + } + } \ No newline at end of file diff --git a/src/station_utils.cpp b/src/station_utils.cpp index 88cb570..4ff1ddb 100644 --- a/src/station_utils.cpp +++ b/src/station_utils.cpp @@ -149,6 +149,24 @@ namespace STATION_Utils { return true; } + void processOutputPacketBufferUltraEcoMode() { + size_t currentIndex = 0; + while (currentIndex < outputPacketBuffer.size()) { // this sends all packets from output buffer + delay(3000); // and cleans buffer to avoid sending packets with time offset + LoRa_Utils::sendNewPacket(outputPacketBuffer[currentIndex]); // next time it wakes up + currentIndex++; + } + outputPacketBuffer.clear(); + // + if (saveNewDigiEcoModeConfig) { + Config.writeFile(); + delay(1000); + displayToggle(false); + ESP.restart(); + } + // + } + void processOutputPacketBuffer() { int timeToWait = 3 * 1000; // 3 segs between packet Tx and also Rx ??? uint32_t lastRx = millis() - lastRxTime; @@ -166,7 +184,7 @@ namespace STATION_Utils { } } if (saveNewDigiEcoModeConfig) { - setCpuFrequencyMhz(80); + //setCpuFrequencyMhz(80); Config.writeFile(); delay(1000); displayToggle(false);