LoRa_APRS_iGate/src/utils.cpp

265 lines
10 KiB
C++
Raw Normal View History

2023-06-04 20:54:11 +02:00
#include <WiFi.h>
2023-06-06 17:21:59 +02:00
#include "configuration.h"
2023-06-18 00:28:40 +02:00
#include "station_utils.h"
2023-07-06 07:01:10 +02:00
#include "battery_utils.h"
2023-07-30 23:12:50 +02:00
#include "aprs_is_utils.h"
2024-05-18 17:27:14 +02:00
#include "boards_pinout.h"
2023-06-13 05:36:39 +02:00
#include "syslog_utils.h"
2024-04-23 19:57:21 +02:00
#include "A7670_utils.h"
2023-06-07 23:25:50 +02:00
#include "wifi_utils.h"
2023-06-20 01:44:55 +02:00
#include "gps_utils.h"
2023-06-18 00:28:40 +02:00
#include "bme_utils.h"
2023-06-08 05:55:31 +02:00
#include "display.h"
2023-06-04 20:18:30 +02:00
#include "utils.h"
2023-06-11 02:17:10 +02:00
2023-06-17 17:10:44 +02:00
extern Configuration Config;
2024-05-14 05:30:15 +02:00
extern WiFiClient espClient;
2023-06-17 17:10:44 +02:00
extern String versionDate;
extern String firstLine;
extern String secondLine;
extern String thirdLine;
extern String fourthLine;
extern String fifthLine;
extern String sixthLine;
extern String seventhLine;
extern String iGateBeaconPacket;
extern String iGateLoRaBeaconPacket;
2023-06-17 17:10:44 +02:00
extern std::vector<String> lastHeardStation;
2023-06-20 01:44:55 +02:00
extern int rssi;
extern float snr;
extern int freqError;
extern String distance;
2023-07-31 00:21:06 +02:00
extern uint32_t lastWiFiCheck;
extern bool WiFiConnect;
2024-02-24 14:09:05 +01:00
extern bool WiFiConnected;
2024-05-13 20:00:07 +02:00
extern int wxModuleType;
2024-05-22 21:29:00 +02:00
extern bool backUpDigiMode;
2023-06-04 20:18:30 +02:00
2024-05-14 05:30:15 +02:00
bool statusAfterBoot = true;
bool beaconUpdate = true;
uint32_t lastBeaconTx = 0;
2024-05-22 22:19:45 +02:00
uint32_t lastScreenOn = millis();
2024-05-14 05:30:15 +02:00
2023-12-20 04:53:04 +01:00
2023-06-12 07:31:18 +02:00
namespace Utils {
2023-06-04 20:18:30 +02:00
2023-10-08 15:16:12 +02:00
void processStatus() {
2024-03-07 17:46:38 +01:00
String status = Config.callsign + ">APLRG1," + Config.beacon.path;
2024-04-09 18:20:31 +02:00
if (WiFi.status() == WL_CONNECTED && Config.aprs_is.active && Config.beacon.sendViaAPRSIS) {
2023-10-08 15:16:12 +02:00
delay(1000);
status += ",qAC:>https://github.com/richonguzman/LoRa_APRS_iGate " + versionDate;
2024-01-24 15:13:34 +01:00
APRS_IS_Utils::upload(status);
2024-05-15 23:47:29 +02:00
SYSLOG_Utils::log(2, status,0,0,0); // APRSIS TX
2024-04-09 18:20:31 +02:00
statusAfterBoot = false;
}
if (statusAfterBoot && !Config.beacon.sendViaAPRSIS && Config.beacon.sendViaRF) {
delay(2000);
status += ":>https://github.com/richonguzman/LoRa_APRS_iGate " + versionDate;
2024-04-20 16:06:22 +02:00
STATION_Utils::addToOutputPacketBuffer(status);
2024-04-09 18:20:31 +02:00
statusAfterBoot = false;
2023-06-08 23:09:05 +02:00
}
2023-06-08 06:44:13 +02:00
}
2023-06-17 01:08:25 +02:00
2023-10-08 15:16:12 +02:00
String getLocalIP() {
2024-02-24 14:09:05 +01:00
if (!WiFiConnected) {
2024-02-25 16:00:44 +01:00
return "IP : 192.168.4.1";
2024-05-22 21:29:00 +02:00
} else if (backUpDigiMode) {
2024-05-22 21:41:25 +02:00
return "- BACKUP DIGI MODE -";
2024-02-24 14:09:05 +01:00
} else {
return "IP : " + String(WiFi.localIP()[0]) + "." + String(WiFi.localIP()[1]) + "." + String(WiFi.localIP()[2]) + "." + String(WiFi.localIP()[3]);
}
2023-06-19 06:52:40 +02:00
}
2023-10-08 15:16:12 +02:00
void setupDisplay() {
setup_display();
2024-04-24 04:25:20 +02:00
#ifdef INTERNAL_LED_PIN
2024-05-11 18:59:07 +02:00
digitalWrite(INTERNAL_LED_PIN,HIGH);
#endif
2024-02-24 14:09:05 +01:00
Serial.println("\nStarting Station: " + Config.callsign + " Version: " + versionDate);
2024-02-25 16:00:44 +01:00
show_display(" LoRa APRS", "", " ( iGATE & DIGI )", "", "", "Richonguzman / CA2RXU", " " + versionDate, 4000);
2024-04-24 04:25:20 +02:00
#ifdef INTERNAL_LED_PIN
2024-05-11 18:59:07 +02:00
digitalWrite(INTERNAL_LED_PIN,LOW);
#endif
2023-10-08 15:16:12 +02:00
firstLine = Config.callsign;
seventhLine = " listening...";
2023-06-08 05:55:31 +02:00
}
2023-10-08 15:16:12 +02:00
void activeStations() {
fourthLine = "Stations (" + String(Config.rememberStationTime) + "min) = ";
if (lastHeardStation.size() < 10) {
fourthLine += " ";
2023-09-21 02:28:17 +02:00
}
2023-10-08 15:16:12 +02:00
fourthLine += String(lastHeardStation.size());
}
void checkBeaconInterval() {
uint32_t lastTx = millis() - lastBeaconTx;
2024-04-09 18:20:31 +02:00
String beaconPacket = iGateBeaconPacket;
String secondaryBeaconPacket = iGateLoRaBeaconPacket;
2024-03-07 17:46:38 +01:00
2024-04-09 18:20:31 +02:00
if (lastBeaconTx == 0 || lastTx >= Config.beacon.interval * 60 * 1000) {
2023-10-08 15:16:12 +02:00
beaconUpdate = true;
2023-07-06 07:08:01 +02:00
}
2024-03-07 17:46:38 +01:00
2023-10-08 15:16:12 +02:00
if (beaconUpdate) {
2024-04-29 15:19:25 +02:00
if (!Config.display.alwaysOn && Config.display.timeout != 0) {
display_toggle(true);
}
2024-03-17 12:21:11 +01:00
Utils::println("-- Sending Beacon to APRSIS --");
2023-10-08 15:16:12 +02:00
STATION_Utils::deleteNotHeard();
2024-03-07 17:46:38 +01:00
2023-10-08 15:16:12 +02:00
activeStations();
2024-03-07 17:46:38 +01:00
2024-05-13 20:00:07 +02:00
if (Config.bme.active && wxModuleType != 0) {
String sensorData = BME_Utils::readDataSensor();
2024-04-11 17:07:28 +02:00
beaconPacket += sensorData;
secondaryBeaconPacket += sensorData;
2024-05-13 20:00:07 +02:00
} else if (Config.bme.active && wxModuleType == 0) {
2024-05-12 17:42:28 +02:00
beaconPacket += ".../...g...t...r...p...P...h..b.....";
secondaryBeaconPacket += ".../...g...t...r...p...P...h..b.....";
2023-06-19 06:52:40 +02:00
}
2024-04-09 18:20:31 +02:00
beaconPacket += Config.beacon.comment;
secondaryBeaconPacket += Config.beacon.comment;
2024-03-07 17:46:38 +01:00
2024-05-02 18:57:18 +02:00
#ifdef BATTERY_PIN
2024-05-11 18:59:07 +02:00
if (Config.sendBatteryVoltage) {
String batteryInfo = "Batt=" + String(BATTERY_Utils::checkBattery(),2) + "V";
2024-05-22 14:36:54 +02:00
beaconPacket += " " + batteryInfo;
secondaryBeaconPacket += " " + batteryInfo;
2024-05-11 18:59:07 +02:00
sixthLine = " ( " + batteryInfo + ")";
}
2023-12-26 21:27:36 +01:00
#endif
2024-03-07 17:46:38 +01:00
2024-05-22 14:36:54 +02:00
if (Config.externalVoltageMeasurement) {
String externalVoltage = String(BATTERY_Utils::checkExternalVoltage(),2) + "V";
beaconPacket += " Ext=" + externalVoltage;
secondaryBeaconPacket += " Ext=" + externalVoltage;
sixthLine = " (Ext V=" + externalVoltage;
2023-07-31 00:21:06 +02:00
}
2024-03-07 17:46:38 +01:00
2024-05-22 21:29:00 +02:00
if (Config.aprs_is.active && Config.beacon.sendViaAPRSIS && !backUpDigiMode) {
2024-03-07 17:46:38 +01:00
show_display(firstLine, secondLine, thirdLine, fourthLine, fifthLine, sixthLine, "SENDING IGATE BEACON", 0);
2023-07-30 23:12:50 +02:00
seventhLine = " listening...";
2024-04-23 19:57:21 +02:00
#ifdef ESP32_DIY_LoRa_A7670
2024-05-11 18:59:07 +02:00
A7670_Utils::uploadToAPRSIS(beaconPacket);
2024-04-23 19:57:21 +02:00
#else
2024-05-11 18:59:07 +02:00
APRS_IS_Utils::upload(beaconPacket);
2024-04-23 19:57:21 +02:00
#endif
2024-03-07 17:46:38 +01:00
}
2024-05-22 21:29:00 +02:00
if (Config.beacon.sendViaRF || backUpDigiMode) {
2024-03-07 17:46:38 +01:00
show_display(firstLine, secondLine, thirdLine, fourthLine, fifthLine, sixthLine, "SENDING DIGI BEACON", 0);
seventhLine = " listening...";
2024-04-20 16:06:22 +02:00
STATION_Utils::addToOutputPacketBuffer(secondaryBeaconPacket);
2023-09-21 07:08:10 +02:00
}
2024-03-07 17:46:38 +01:00
2023-10-08 15:16:12 +02:00
lastBeaconTx = millis();
lastScreenOn = millis();
beaconUpdate = false;
2023-06-08 05:55:31 +02:00
}
2024-03-07 17:46:38 +01:00
2023-10-08 15:16:12 +02:00
if (statusAfterBoot) {
processStatus();
2023-06-08 05:55:31 +02:00
}
}
2023-10-08 15:16:12 +02:00
void checkDisplayInterval() {
uint32_t lastDisplayTime = millis() - lastScreenOn;
2024-04-28 15:30:56 +02:00
if (!Config.display.alwaysOn && lastDisplayTime >= Config.display.timeout * 1000) {
display_toggle(false);
2023-10-08 15:16:12 +02:00
}
2023-07-31 00:21:06 +02:00
}
2023-10-08 15:16:12 +02:00
void checkWiFiInterval() {
uint32_t WiFiCheck = millis() - lastWiFiCheck;
2024-05-14 05:30:15 +02:00
if (WiFi.status() != WL_CONNECTED && WiFiCheck >= 15 * 60 * 1000) {
WiFiConnect = true;
2023-10-08 15:16:12 +02:00
}
if (WiFiConnect) {
2024-05-14 05:30:15 +02:00
Serial.println("\nConnecting to WiFi ...");
WIFI_Utils::startWiFi();
lastWiFiCheck = millis();
WiFiConnect = false;
2023-06-08 23:09:05 +02:00
}
}
2024-03-07 17:46:38 +01:00
void validateFreqs() {
if (Config.loramodule.txFreq != Config.loramodule.rxFreq && abs(Config.loramodule.txFreq - Config.loramodule.rxFreq) < 125000) {
Serial.println("Tx Freq less than 125kHz from Rx Freq ---> NOT VALID");
show_display("Tx Freq is less than ", "125kHz from Rx Freq", "device will autofix", "and then reboot", 1000);
Config.loramodule.txFreq = Config.loramodule.rxFreq; // Inform about that but then change the TX QRG to RX QRG and reset the device
Config.writeFile();
ESP.restart();
2023-06-17 17:10:44 +02:00
}
}
2023-10-08 15:16:12 +02:00
2024-05-15 23:47:29 +02:00
void typeOfPacket(const String& packet, uint8_t packetType) {
2023-10-08 15:16:12 +02:00
String sender;
2024-05-15 23:47:29 +02:00
switch (packetType) {
case 0: // LoRa-APRS
fifthLine = "LoRa Rx ----> APRS-IS";
sender = packet.substring(0,packet.indexOf(">"));
break;
case 1: // APRS-LoRa
fifthLine = "APRS-IS ----> LoRa Tx";
sender = packet.substring(0,packet.indexOf(">"));
break;
case 2: // Digi
fifthLine = "LoRa Rx ----> LoRa Tx";
sender = packet.substring(0,packet.indexOf(">"));
break;
2023-06-17 17:10:44 +02:00
}
2024-03-27 19:28:27 +01:00
for (int i = sender.length(); i < 9; i++) {
2023-10-08 15:16:12 +02:00
sender += " ";
2023-06-20 01:44:55 +02:00
}
2023-10-08 15:16:12 +02:00
if (packet.indexOf("::") >= 10) {
2024-03-18 18:48:56 +01:00
sixthLine = sender + "> MESSAGE";
2023-10-08 15:16:12 +02:00
seventhLine = "RSSI:" + String(rssi) + "dBm SNR: " + String(snr) + "dBm";
} else if (packet.indexOf(":>") >= 10) {
sixthLine = sender + "> NEW STATUS";
seventhLine = "RSSI:" + String(rssi) + "dBm SNR: " + String(snr) + "dBm";
} else if (packet.indexOf(":!") >= 10 || packet.indexOf(":=") >= 10) {
sixthLine = sender + "> GPS BEACON";
2024-05-20 05:34:29 +02:00
if (!Config.syslog.active) {
GPS_Utils::getDistanceAndComment(packet); // to be checked!!!
}
2023-10-08 15:16:12 +02:00
seventhLine = "RSSI:" + String(rssi) + "dBm";
if (rssi <= -100) {
seventhLine += " ";
} else {
seventhLine += " ";
}
if (distance.indexOf(".") == 1) {
seventhLine += " ";
}
seventhLine += "D:" + distance + "km";
} else if (packet.indexOf(":T#") >= 10 && packet.indexOf(":=/") == -1) {
sixthLine = sender + "> TELEMETRY";
seventhLine = "RSSI:" + String(rssi) + "dBm SNR: " + String(snr) + "dBm";
2023-12-08 21:04:05 +01:00
} else if (packet.indexOf(":`") >= 10) {
2023-12-07 05:49:49 +01:00
sixthLine = sender + "> MIC-E";
seventhLine = "RSSI:" + String(rssi) + "dBm SNR: " + String(snr) + "dBm";
} else if (packet.indexOf(":;") >= 10) {
sixthLine = sender + "> OBJECT";
seventhLine = "RSSI:" + String(rssi) + "dBm SNR: " + String(snr) + "dBm";
2023-10-08 15:16:12 +02:00
} else {
sixthLine = sender + "> ??????????";
seventhLine = "RSSI:" + String(rssi) + "dBm SNR: " + String(snr) + "dBm";
2023-06-20 05:06:41 +02:00
}
2023-06-09 07:12:13 +02:00
}
2024-05-15 23:47:29 +02:00
void print(const String& text) {
2024-03-17 12:21:11 +01:00
if (!Config.tnc.enableSerial) {
Serial.print(text);
}
}
2024-05-15 23:47:29 +02:00
void println(const String& text) {
2024-03-17 12:21:11 +01:00
if (!Config.tnc.enableSerial) {
Serial.println(text);
}
}
2023-06-04 20:18:30 +02:00
}