LoRa_APRS_iGate/src/LoRa_APRS_iGate.cpp

242 lines
9.8 KiB
C++
Raw Normal View History

2024-11-06 16:47:42 +01:00
/*___________________________________________________________________
Ricardo Guzman - CA2RXU
https://github.com/richonguzman/LoRa_APRS_Tracker
(donations : http://paypal.me/richonguzman)
___________________________________________________________________*/
2024-05-11 18:02:08 +02:00
2024-04-20 16:06:22 +02:00
#include <ElegantOTA.h>
2024-10-14 22:04:28 +02:00
#include <TinyGPS++.h>
2023-03-02 12:51:38 +01:00
#include <Arduino.h>
#include <WiFi.h>
2023-05-20 00:52:59 +02:00
#include <vector>
2023-06-06 17:21:59 +02:00
#include "configuration.h"
2024-04-20 16:06:22 +02:00
#include "battery_utils.h"
2023-06-06 17:30:32 +02:00
#include "aprs_is_utils.h"
2023-06-06 20:26:17 +02:00
#include "station_utils.h"
2024-07-09 05:02:45 +02:00
#include "battery_utils.h"
2024-11-05 22:20:44 +01:00
#include "board_pinout.h"
2023-06-12 07:31:18 +02:00
#include "syslog_utils.h"
2023-06-06 21:53:06 +02:00
#include "query_utils.h"
#include "power_utils.h"
2023-06-08 06:58:10 +02:00
#include "lora_utils.h"
#include "wifi_utils.h"
2023-06-07 23:25:50 +02:00
#include "digi_utils.h"
2023-06-08 06:58:10 +02: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 16:44:22 +02:00
#include "ntp_utils.h"
2024-10-05 13:48:08 +02:00
#include "wx_utils.h"
2023-06-08 06:58:10 +02:00
#include "display.h"
2023-06-04 20:54:11 +02:00
#include "utils.h"
2024-06-19 01:55:33 +02:00
#ifdef HAS_A7670
2024-05-11 18:59:25 +02:00
#include "A7670_utils.h"
2024-04-23 18:30:57 +02:00
#endif
2024-04-20 16:06:22 +02:00
2025-03-09 14:23:34 +01:00
String versionDate = "2025.03.09";
2024-10-14 22:04:28 +02:00
Configuration Config;
WiFiClient espClient;
#ifdef HAS_GPS
HardwareSerial gpsSerial(1);
TinyGPSPlus gps;
2025-02-24 20:10:02 +01:00
uint32_t gpsSatelliteTime = 0;
bool gpsInfoToggle = false;
2024-10-14 22:04:28 +02:00
#endif
2023-03-26 18:19:01 +02:00
2024-10-14 22:04:28 +02:00
uint8_t myWiFiAPIndex = 0;
int myWiFiAPSize = Config.wifiAPs.size();
WiFi_AP *currentWiFi = &Config.wifiAPs[myWiFiAPIndex];
2024-05-22 21:29:00 +02:00
2024-10-14 22:04:28 +02:00
bool isUpdatingOTA = false;
uint32_t lastBatteryCheck = 0;
2024-05-24 20:42:39 +02:00
2024-10-14 22:04:28 +02:00
bool backUpDigiMode = false;
bool modemLoggedToAPRSIS = false;
2024-04-23 18:30:57 +02:00
2025-02-28 20:48:26 +01:00
#ifdef HAS_EPAPER
uint32_t lastEpaperTime = 0;
extern String lastEpaperText;
#endif
2024-05-22 22:19:45 +02:00
std::vector<ReceivedPacket> receivedPackets;
2024-05-14 05:30:15 +02:00
String firstLine, secondLine, thirdLine, fourthLine, fifthLine, sixthLine, seventhLine;
2024-05-13 14:45:19 +02:00
//#define STARTUP_DELAY 5 //min
2023-03-02 12:51:38 +01: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 22:04:28 +02:00
GPS_Utils::setup();
2025-03-09 14:23:34 +01:00
STATION_Utils::loadBlacklist();
STATION_Utils::loadManagers();
2024-03-07 17:46:38 +01:00
2024-05-13 14:45:19 +02:00
#ifdef STARTUP_DELAY // (TEST) just to wait for WiFi init of Routers
2024-10-14 16:44:22 +02:00
displayShow("", " STARTUP DELAY ...", "", "", 0);
delay(STARTUP_DELAY * 60 * 1000);
2024-05-13 14:45:19 +02:00
#endif
2024-05-11 18:59:25 +02:00
#ifdef HELTEC_HTCT62
if (Config.lowPowerMode) {
gpio_wakeup_enable(GPIO_NUM_3, GPIO_INTR_HIGH_LEVEL);
esp_deep_sleep_enable_gpio_wakeup(GPIO_NUM_3, ESP_GPIO_WAKEUP_GPIO_HIGH);
long lastBeacon = 0;
LoRa_Utils::startReceive();
while (true) {
auto wakeup_reason = esp_sleep_get_wakeup_cause();
if (wakeup_reason == 7) { // packet received
Serial.println("Received packet");
String packet = LoRa_Utils::receivePacket();
Serial.println(packet);
2024-05-14 15:23:22 +02:00
if (Config.digi.mode == 2) DIGI_Utils::processLoRaPacket(packet);
2024-03-28 18:00:46 +01:00
2024-05-11 18:59:25 +02:00
if (packet.indexOf(Config.callsign + ":?APRSELP{") != -1) { // Send `?APRSELP` to exit low power
Serial.println("Got ?APRSELP message, exiting from low power mode");
break;
};
2024-03-28 18:00:46 +01:00
}
2024-05-11 18:59:25 +02:00
long time = esp_timer_get_time() / 1000000;
if (lastBeacon == 0 || time - lastBeacon >= Config.beacon.interval * 60) {
Serial.println("Sending beacon");
String comment = Config.beacon.comment;
2024-05-24 20:27:39 +02:00
if (Config.battery.sendInternalVoltage) comment += " Batt=" + String(BATTERY_Utils::checkInternalVoltage(),2) + "V";
if (Config.battery.sendExternalVoltage) comment += " Ext=" + String(BATTERY_Utils::checkExternalVoltage(),2) + "V";
2024-08-13 18:36:37 +02:00
LoRa_Utils::sendNewPacket(GPS_Utils::getiGateLoRaBeaconPacket() + comment);
2024-05-11 18:59:25 +02:00
lastBeacon = time;
}
2024-05-11 18:59:25 +02:00
LoRa_Utils::startReceive();
Serial.println("Sleeping");
long sleep = (Config.beacon.interval * 60) - (time - lastBeacon);
Serial.flush();
esp_sleep_enable_timer_wakeup(sleep * 1000000);
esp_light_sleep_start();
Serial.println("Waked up");
}
Config.loramodule.rxActive = false;
2024-03-28 18:00:46 +01:00
}
2024-05-11 18:59:25 +02:00
#endif
2024-10-08 06:03:58 +02:00
DIGI_Utils::checkEcoMode();
2024-03-28 18:00:46 +01:00
WIFI_Utils::setup();
2024-10-14 16:44:22 +02:00
NTP_Utils::setup();
2024-02-24 14:09:05 +01:00
SYSLOG_Utils::setup();
2024-10-05 13:48:08 +02: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();
2024-06-19 01:55:33 +02:00
#ifdef HAS_A7670
2024-05-11 18:59:25 +02:00
A7670_Utils::setup();
#endif
2024-05-23 01:19:25 +02:00
Utils::checkRebootMode();
2024-12-06 16:03:37 +01:00
APRS_IS_Utils::firstConnection();
2023-06-07 23:25:50 +02:00
}
2023-03-03 00:40:59 +01:00
2023-06-07 23:25:50 +02:00
void loop() {
2024-10-07 04:23:22 +02:00
WIFI_Utils::checkAutoAPTimeout();
2024-02-24 14:09:05 +01:00
2024-03-07 17:46:38 +01:00
if (isUpdatingOTA) {
ElegantOTA.loop();
return; // Don't process IGate and Digi during OTA update
}
if (Config.lowVoltageCutOff > 0) {
BATTERY_Utils::checkIfShouldSleep();
}
2024-12-06 16:03:37 +01:00
2025-02-24 20:10:02 +01:00
#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
thirdLine = Utils::getLocalIP();
#endif
2024-03-07 17:46:38 +01:00
2024-06-19 01:55:33 +02:00
#ifdef HAS_A7670
2024-05-13 04:02:12 +02:00
if (Config.aprs_is.active && !modemLoggedToAPRSIS) A7670_Utils::APRS_IS_connect();
2024-04-23 18:30:57 +02:00
#else
2024-12-06 16:03:37 +01:00
WIFI_Utils::checkWiFi();
2024-05-22 21:29:00 +02:00
if (Config.aprs_is.active && (WiFi.status() == WL_CONNECTED) && !espClient.connected()) APRS_IS_Utils::connect();
2024-04-23 18:30:57 +02:00
#endif
2024-03-07 17:46:38 +01:00
2024-10-14 16:44:22 +02:00
NTP_Utils::update();
2024-03-09 16:25:23 +01:00
TNC_Utils::loop();
2024-03-07 17:46:38 +01:00
Utils::checkDisplayInterval();
Utils::checkBeaconInterval();
2024-04-09 18:51:51 +02:00
APRS_IS_Utils::checkStatus(); // Need that to update display, maybe split this and send APRSIS status to display func?
String packet = "";
2024-03-07 17:46:38 +01:00
if (Config.loramodule.rxActive) {
packet = LoRa_Utils::receivePacket(); // We need to fetch LoRa packet above APRSIS and Digi
2024-06-03 06:02:43 +02:00
}
2024-03-07 17:46:38 +01:00
2024-03-09 16:25:23 +01:00
if (packet != "") {
if (Config.aprs_is.active) { // If APRSIS enabled
2024-03-27 15:26:02 +01:00
APRS_IS_Utils::processLoRaPacket(packet); // Send received packet to APRSIS
2024-03-09 16:25:23 +01:00
}
2024-08-14 19:37:52 +02:00
if (Config.loramodule.txActive && (Config.digi.mode == 2 || Config.digi.mode == 3 || backUpDigiMode)) { // If Digi enabled
2024-06-18 22:45:01 +02:00
STATION_Utils::clean25SegBuffer();
2024-11-06 16:47:42 +01:00
DIGI_Utils::processLoRaPacket(packet); // Send received packet to Digi
2024-03-09 16:25:23 +01:00
}
2024-03-07 17:46:38 +01:00
2024-03-17 12:21:11 +01:00
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
}
2024-03-07 17:46:38 +01:00
}
2024-12-06 16:03:37 +01:00
if (Config.aprs_is.active) {
2024-03-27 15:26:02 +01:00
APRS_IS_Utils::listenAPRSIS(); // listen received packet from APRSIS
}
2024-12-06 16:03:37 +01:00
2024-06-30 16:36:15 +02:00
STATION_Utils::processOutputPacketBuffer();
2025-02-28 20:48:26 +01: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();
}
}
#else
displayShow(firstLine, secondLine, thirdLine, fourthLine, fifthLine, sixthLine, seventhLine, 0);
#endif
2024-05-23 01:19:25 +02:00
Utils::checkRebootTime();
2024-05-24 20:24:40 +02:00
Utils::checkSleepByLowBatteryVoltage(1);
2023-03-26 14:12:27 +02:00
}