LoRa_APRS_iGate/src/LoRa_APRS_iGate.cpp

224 lines
6.6 KiB
C++
Raw Normal View History

2024-04-20 16:06:22 +02:00
#include <ElegantOTA.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"
2023-06-12 07:31:18 +02:00
#include "syslog_utils.h"
2023-06-08 06:58:10 +02:00
#include "pins_config.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"
2023-06-18 00:28:40 +02:00
#include "bme_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"
2023-06-08 06:58:10 +02:00
#include "display.h"
2023-06-04 20:54:11 +02:00
#include "utils.h"
2024-04-20 16:06:22 +02:00
2023-07-16 22:24:09 +02:00
2023-06-08 06:44:13 +02:00
Configuration Config;
2023-03-26 18:19:01 +02:00
WiFiClient espClient;
2024-04-23 16:53:53 +02:00
String versionDate = "2024.04.23";
uint8_t myWiFiAPIndex = 0;
int myWiFiAPSize = Config.wifiAPs.size();
WiFi_AP *currentWiFi = &Config.wifiAPs[myWiFiAPIndex];
2023-03-26 18:19:01 +02:00
bool isUpdatingOTA = false;
bool statusAfterBoot = true;
bool beaconUpdate = true;
uint32_t lastBeaconTx = 0;
uint32_t previousWiFiMillis = 0;
uint32_t lastScreenOn = millis();
2023-06-08 05:55:31 +02:00
uint32_t lastWiFiCheck = 0;
bool WiFiConnect = true;
bool WiFiConnected = false;
bool WiFiAutoAPStarted = false;
long WiFiAutoAPTime = false;
2024-02-24 14:09:05 +01:00
uint32_t lastBatteryCheck = 0;
2024-03-21 17:31:54 +01:00
uint32_t bmeLastReading = -60000;
2024-03-06 13:01:17 +01:00
2023-07-06 06:14:26 +02:00
String batteryVoltage;
2023-05-20 00:52:59 +02:00
std::vector<String> lastHeardStation;
2023-05-25 16:27:46 +02:00
std::vector<String> lastHeardStation_temp;
std::vector<String> outputPacketBuffer;
uint32_t lastTxTime = millis();
2024-04-20 15:37:22 +02:00
uint32_t lastRxTime = millis();
2023-05-10 06:59:00 +02:00
2024-04-13 18:20:10 +02:00
std::vector<ReceivedPacket> receivedPackets;
String firstLine, secondLine, thirdLine, fourthLine, fifthLine, sixthLine, seventhLine, iGateBeaconPacket, iGateLoRaBeaconPacket;
2023-03-02 12:51:38 +01:00
void setup() {
2024-02-24 14:09:05 +01:00
Serial.begin(115200);
2024-04-19 17:06:26 +02:00
#ifdef BATTERY_PIN
pinMode(BATTERY_PIN, INPUT);
2024-02-24 14:09:05 +01:00
#endif
2024-03-09 13:43:51 +01:00
#ifdef HAS_INTERNAL_LED
2024-02-24 14:09:05 +01:00
pinMode(internalLedPin, OUTPUT);
#endif
if (Config.externalVoltageMeasurement) {
pinMode(Config.externalVoltagePin, INPUT);
}
#if defined(TTGO_T_Beam_V1_0) || defined(TTGO_T_Beam_V1_0_SX1268) || defined(TTGO_T_Beam_V1_2) || defined(TTGO_T_Beam_V1_2_SX1262)
POWER_Utils::setup();
#endif
delay(1000);
Utils::setupDisplay();
2024-03-07 17:46:38 +01:00
Config.check();
2024-02-24 14:09:05 +01:00
LoRa_Utils::setup();
2024-03-07 17:46:38 +01:00
Utils::validateFreqs();
2024-02-24 14:09:05 +01:00
iGateBeaconPacket = GPS_Utils::generateBeacon();
iGateLoRaBeaconPacket = GPS_Utils::generateiGateLoRaBeacon();
2024-03-07 17:46:38 +01:00
#ifdef HELTEC_HTCT62
2024-03-28 18:00:46 +01:00
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);
if (Config.digi.mode == 2) { // If Digi enabled
2024-04-20 19:17:25 +02:00
DIGI_Utils::processLoRaPacket(packet); // Send received packet to Digi
2024-03-28 18:00:46 +01:00
}
if (packet.indexOf(Config.callsign + ":?APRSELP{") != -1) { // Send `?APRSELP` to exit low power
Serial.println("Got ?APRSELP message, exiting from low power mode");
2024-03-28 18:00:46 +01:00
break;
};
}
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;
if (Config.sendBatteryVoltage) {
comment += " Batt=" + String(BATTERY_Utils::checkBattery(),2) + "V";
}
if (Config.externalVoltageMeasurement) {
comment += " Ext=" + String(BATTERY_Utils::checkExternalVoltage(),2) + "V";
}
2024-04-20 19:17:25 +02:00
STATION_Utils::addToOutputPacketBuffer(iGateLoRaBeaconPacket + comment);
//LoRa_Utils::sendNewPacket("APRS", iGateLoRaBeaconPacket + comment);
2024-03-28 18:00:46 +01:00
lastBeacon = time;
}
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;
}
#endif
WIFI_Utils::setup();
2024-02-24 14:09:05 +01:00
SYSLOG_Utils::setup();
BME_Utils::setup();
WEB_Utils::setup();
2024-03-09 16:25:23 +01:00
TNC_Utils::setup();
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-02-24 14:09:05 +01:00
WIFI_Utils::checkIfAutoAPShouldPowerOff();
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-03-21 17:31:54 +01:00
2024-03-07 17:46:38 +01:00
thirdLine = Utils::getLocalIP();
WIFI_Utils::checkWiFi(); // Always use WiFi, not related to IGate/Digi mode
// Utils::checkWiFiInterval();
if (Config.aprs_is.active && !espClient.connected()) {
APRS_IS_Utils::connect();
}
2024-03-07 17:46:38 +01:00
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-04-09 18:51:51 +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
}
if (Config.digi.mode == 2) { // If Digi enabled
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-03-27 15:26:02 +01:00
if (Config.aprs_is.active) { // If APRSIS enabled
APRS_IS_Utils::listenAPRSIS(); // listen received packet from APRSIS
}
STATION_Utils::processOutputPacketBuffer();
2024-03-07 17:46:38 +01:00
show_display(firstLine, secondLine, thirdLine, fourthLine, fifthLine, sixthLine, seventhLine, 0);
2023-03-26 14:12:27 +02:00
}