LoRa_APRS_iGate/src/LoRa_APRS_iGate.cpp

226 lines
9.2 KiB
C++
Raw Normal View History

2025-07-15 16:28:23 -04:00
/* Copyright (C) 2025 Ricardo Guzman - CA2RXU
*
* This file is part of LoRa APRS iGate.
*
* LoRa APRS iGate is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LoRa APRS iGate is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LoRa APRS iGate. If not, see <https://www.gnu.org/licenses/>.
*/
2024-11-06 12:47:42 -03:00
/*___________________________________________________________________
Ricardo Guzman - CA2RXU
2025-07-15 16:32:44 -04:00
https://github.com/richonguzman/LoRa_APRS_iGate
2024-11-06 12:47:42 -03:00
(donations : http://paypal.me/richonguzman)
___________________________________________________________________*/
2024-05-11 12:02:08 -04:00
2024-04-20 10:06:22 -04:00
#include <ElegantOTA.h>
2024-10-14 17:04:28 -03:00
#include <TinyGPS++.h>
2023-03-02 08:51:38 -03:00
#include <Arduino.h>
#include <WiFi.h>
2023-05-19 18:52:59 -04:00
#include <vector>
2023-06-06 11:21:59 -04:00
#include "configuration.h"
2023-06-06 11:30:32 -04:00
#include "aprs_is_utils.h"
2023-06-06 14:26:17 -04:00
#include "station_utils.h"
2024-07-08 23:02:45 -04:00
#include "battery_utils.h"
2024-11-05 18:20:44 -03:00
#include "board_pinout.h"
2023-06-12 01:31:18 -04:00
#include "syslog_utils.h"
#include "power_utils.h"
2025-04-24 09:44:57 -04:00
#include "sleep_utils.h"
2025-08-27 13:25:11 -04:00
#include "mqtt_utils.h"
2023-06-08 00:58:10 -04:00
#include "lora_utils.h"
#include "wifi_utils.h"
2023-06-07 17:25:50 -04:00
#include "digi_utils.h"
2023-06-08 00:58:10 -04:00
#include "gps_utils.h"
2024-02-24 14:09:05 +01:00
#include "web_utils.h"
2024-03-09 16:25:23 +01:00
#include "tnc_utils.h"
2024-10-14 11:44:22 -03:00
#include "ntp_utils.h"
2024-10-05 08:48:08 -03:00
#include "wx_utils.h"
2023-06-08 00:58:10 -04:00
#include "display.h"
2023-06-04 14:54:11 -04:00
#include "utils.h"
2024-06-18 19:55:33 -04:00
#ifdef HAS_A7670
2024-05-11 12:59:25 -04:00
#include "A7670_utils.h"
2024-04-23 12:30:57 -04:00
#endif
2024-04-20 10:06:22 -04:00
2025-03-10 03:02:48 -03:00
2025-09-09 09:42:25 -03:00
String versionDate = "2025-09-09";
2025-08-29 14:51:07 -04:00
String versionNumber = "3.1";
2024-10-14 17:04:28 -03:00
Configuration Config;
2025-08-27 13:25:11 -04:00
WiFiClient aprsIsClient;
WiFiClient mqttClient;
2024-10-14 17:04:28 -03:00
#ifdef HAS_GPS
HardwareSerial gpsSerial(1);
TinyGPSPlus gps;
2025-02-24 16:10:02 -03:00
uint32_t gpsSatelliteTime = 0;
bool gpsInfoToggle = false;
2024-10-14 17:04:28 -03:00
#endif
2023-03-26 13:19:01 -03:00
2024-10-14 17:04:28 -03:00
uint8_t myWiFiAPIndex = 0;
int myWiFiAPSize = Config.wifiAPs.size();
WiFi_AP *currentWiFi = &Config.wifiAPs[myWiFiAPIndex];
2024-05-22 15:29:00 -04:00
2024-10-14 17:04:28 -03:00
bool isUpdatingOTA = false;
uint32_t lastBatteryCheck = 0;
2024-05-24 14:42:39 -04:00
2024-10-14 17:04:28 -03:00
bool backUpDigiMode = false;
bool modemLoggedToAPRSIS = false;
2024-04-23 12:30:57 -04:00
2025-02-28 16:48:26 -03:00
#ifdef HAS_EPAPER
uint32_t lastEpaperTime = 0;
extern String lastEpaperText;
#endif
2024-05-22 16:19:45 -04:00
std::vector<ReceivedPacket> receivedPackets;
2024-05-13 23:30:15 -04:00
String firstLine, secondLine, thirdLine, fourthLine, fifthLine, sixthLine, seventhLine;
2024-05-13 08:45:19 -04:00
//#define STARTUP_DELAY 5 //min
2025-03-10 03:02:48 -03:00
2023-03-02 08:51:38 -03:00
void setup() {
2024-02-24 14:09:05 +01:00
Serial.begin(115200);
POWER_Utils::setup();
Utils::setupDisplay();
LoRa_Utils::setup();
2024-03-07 17:46:38 +01:00
Utils::validateFreqs();
2024-10-14 17:04:28 -03:00
GPS_Utils::setup();
2025-04-24 09:50:58 -04:00
STATION_Utils::loadBlacklistAndManagers();
2024-03-07 17:46:38 +01:00
2024-05-13 08:45:19 -04:00
#ifdef STARTUP_DELAY // (TEST) just to wait for WiFi init of Routers
2024-10-14 11:44:22 -03:00
displayShow("", " STARTUP DELAY ...", "", "", 0);
delay(STARTUP_DELAY * 60 * 1000);
2024-05-13 08:45:19 -04:00
#endif
2025-04-24 09:44:57 -04:00
SLEEP_Utils::setup();
2024-03-28 18:00:46 +01:00
WIFI_Utils::setup();
2024-10-14 11:44:22 -03:00
NTP_Utils::setup();
2024-02-24 14:09:05 +01:00
SYSLOG_Utils::setup();
2024-10-05 08:48:08 -03:00
WX_Utils::setup();
2024-02-24 14:09:05 +01:00
WEB_Utils::setup();
2024-03-09 16:25:23 +01:00
TNC_Utils::setup();
2025-08-27 13:25:11 -04:00
MQTT_Utils::setup();
2024-06-18 19:55:33 -04:00
#ifdef HAS_A7670
2024-05-11 12:59:25 -04:00
A7670_Utils::setup();
#endif
2024-05-22 19:19:25 -04:00
Utils::checkRebootMode();
2024-12-06 12:03:37 -03:00
APRS_IS_Utils::firstConnection();
2025-05-08 12:53:55 -04:00
SLEEP_Utils::checkSerial();
2023-06-07 17:25:50 -04:00
}
2023-03-02 20:40:59 -03:00
2023-06-07 17:25:50 -04:00
void loop() {
2025-04-24 10:36:08 -04:00
if (Config.digi.ecoMode == 1) {
SLEEP_Utils::checkWakeUpFlag();
Utils::checkBeaconInterval();
STATION_Utils::processOutputPacketBufferUltraEcoMode();
2025-04-24 11:09:39 -04:00
Utils::checkSleepByLowBatteryVoltage(1);
2025-04-24 10:36:08 -04:00
SLEEP_Utils::startSleeping();
} else {
WIFI_Utils::checkAutoAPTimeout();
if (isUpdatingOTA) {
ElegantOTA.loop();
return; // Don't process IGate and Digi during OTA update
}
#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();
}
2025-02-24 16:10:02 -03:00
} else {
thirdLine = Utils::getLocalIP();
}
2025-04-24 10:36:08 -04:00
#else
2025-02-24 16:10:02 -03:00
thirdLine = Utils::getLocalIP();
2025-04-24 10:36:08 -04:00
#endif
#ifdef HAS_A7670
if (Config.aprs_is.active && !modemLoggedToAPRSIS) A7670_Utils::APRS_IS_connect();
#else
WIFI_Utils::checkWiFi();
2025-08-27 13:25:11 -04:00
if (Config.aprs_is.active && (WiFi.status() == WL_CONNECTED) && !aprsIsClient.connected()) APRS_IS_Utils::connect();
if (Config.mqtt.active && (WiFi.status() == WL_CONNECTED) && !mqttClient.connected()) MQTT_Utils::connect();
2025-04-24 10:36:08 -04:00
#endif
NTP_Utils::update();
TNC_Utils::loop();
2025-08-27 13:25:11 -04:00
MQTT_Utils::loop();
2025-04-24 10:36:08 -04:00
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
2025-02-24 16:10:02 -03:00
}
2024-04-09 12:51:51 -04:00
2025-04-24 10:36:08 -04:00
if (packet != "") {
2025-08-27 13:25:11 -04:00
if (Config.aprs_is.active) { // If APRSIS enabled
2025-04-24 10:36:08 -04:00
APRS_IS_Utils::processLoRaPacket(packet); // Send received packet to APRSIS
}
2024-03-07 17:46:38 +01:00
2025-04-24 10:36:08 -04:00
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
}
2024-03-09 16:25:23 +01:00
2025-08-27 13:25:11 -04:00
if (Config.tnc.enableServer) TNC_Utils::sendToClients(packet); // Send received packet to TNC KISS
if (Config.tnc.enableSerial) TNC_Utils::sendToSerial(packet); // Send received packet to Serial KISS
if (Config.mqtt.active) MQTT_Utils::sendToMqtt(packet); // Send received packet to MQTT
2024-03-09 16:25:23 +01:00
}
2024-03-07 17:46:38 +01:00
2025-08-27 13:25:11 -04:00
if (Config.aprs_is.active) APRS_IS_Utils::listenAPRSIS(); // listen received packet from APRSIS
2024-12-06 12:03:37 -03:00
2025-04-24 10:36:08 -04:00
STATION_Utils::processOutputPacketBuffer();
2025-04-24 10:36:08 -04:00
#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();
}
2025-02-28 16:48:26 -03:00
}
2025-04-24 10:36:08 -04:00
#else
displayShow(firstLine, secondLine, thirdLine, fourthLine, fifthLine, sixthLine, seventhLine, 0);
#endif
2025-02-28 16:48:26 -03:00
2025-04-24 10:36:08 -04:00
Utils::checkRebootTime();
Utils::checkSleepByLowBatteryVoltage(1);
}
2025-07-15 16:33:28 -04:00
}