LoRa_APRS_iGate/src/utils.cpp

154 lines
5.2 KiB
C++
Raw Normal View History

2023-06-11 02:17:10 +02:00
#include <ESPAsyncWebServer.h>
#include <AsyncElegantOTA.h>
#include <AsyncTCP.h>
2023-06-12 01:33:16 +02:00
#include <SPIFFS.h>
2023-06-04 20:54:11 +02:00
#include <WiFi.h>
2023-06-06 17:21:59 +02:00
#include "configuration.h"
2023-06-10 02:20:39 +02:00
#include "pins_config.h"
2023-06-07 23:25:50 +02:00
#include "wifi_utils.h"
2023-06-08 05:55:31 +02:00
#include "lora_utils.h"
#include "display.h"
2023-06-04 20:18:30 +02:00
#include "utils.h"
2023-06-11 02:17:10 +02:00
AsyncWebServer server(80);
2023-06-04 20:18:30 +02:00
2023-06-08 05:55:31 +02:00
extern WiFiClient espClient;
extern Configuration Config;
2023-06-09 14:34:34 +02:00
extern String versionDate;
2023-06-08 05:55:31 +02:00
extern bool statusAfterBoot;
extern String firstLine;
extern String secondLine;
extern String thirdLine;
extern String fourthLine;
extern uint32_t lastBeaconTx;
extern uint32_t lastScreenOn;
extern bool beacon_update;
extern int stationMode;
extern String iGateBeaconPacket;
2023-06-04 20:18:30 +02:00
2023-06-04 20:54:11 +02:00
namespace utils {
2023-06-04 20:18:30 +02:00
2023-06-04 20:54:11 +02:00
void processStatus() {
2023-06-08 06:44:13 +02:00
String status = Config.callsign + ">APLRG1";
if (stationMode==1 || stationMode==2) {
delay(1000);
2023-06-09 06:23:11 +02:00
status += ",qAC:>https://github.com/richonguzman/LoRa_APRS_iGate";
2023-06-08 06:44:13 +02:00
espClient.write((status + "\n").c_str());
} else {
delay(5000);
2023-06-09 06:23:11 +02:00
status += ":>https://github.com/richonguzman/LoRa_APRS_iGate";
if (stationMode == 4) {
2023-06-08 23:09:05 +02:00
LoRa_Utils::changeFreqTx();
}
2023-06-08 06:44:13 +02:00
LoRa_Utils::sendNewPacket("APRS", status);
2023-06-08 23:09:05 +02:00
if (stationMode == 4) {
LoRa_Utils::changeFreqRx();
}
2023-06-08 06:44:13 +02:00
}
2023-06-04 20:54:11 +02:00
statusAfterBoot = false;
2023-06-04 20:18:30 +02:00
}
2023-06-08 05:55:31 +02:00
void setupDiplay() {
setup_display();
2023-06-10 02:20:39 +02:00
digitalWrite(greenLed,HIGH);
2023-06-09 14:34:34 +02:00
Serial.println("\nStarting iGate: " + Config.callsign + " Version: " + versionDate);
show_display(" LoRa APRS iGate", " Richonguzman", " -- CD2RXU --", " " + versionDate, 4000);
2023-06-10 02:20:39 +02:00
digitalWrite(greenLed,LOW);
2023-06-08 05:55:31 +02:00
firstLine = "LoRa iGate: " + Config.callsign;
if (stationMode==3 || stationMode==4) {
secondLine = "<DigiRepeater Active>";
} else {
secondLine = "";
}
2023-06-11 17:12:09 +02:00
thirdLine = "";
2023-06-08 05:55:31 +02:00
fourthLine = " listening...";
}
2023-06-11 00:41:40 +02:00
String getLocalIP() {
return "IP : " + String(WiFi.localIP()[0]) + "." + String(WiFi.localIP()[1]) + "." + String(WiFi.localIP()[2]) + "." + String(WiFi.localIP()[3]);
}
2023-06-08 05:55:31 +02:00
void checkBeaconInterval() {
uint32_t lastTx = millis() - lastBeaconTx;
if (lastTx >= Config.beaconInterval*60*1000) {
beacon_update = true;
}
if (beacon_update) {
display_toggle(true);
2023-06-11 17:12:09 +02:00
//thirdLine = getLocalIP();
2023-06-08 05:55:31 +02:00
Serial.println("---- Sending iGate Beacon ----");
2023-06-08 23:09:05 +02:00
if (stationMode==1 || stationMode==2) {
2023-06-08 05:55:31 +02:00
show_display(firstLine, secondLine, thirdLine, "SENDING iGate BEACON", 1000);
2023-06-11 17:12:09 +02:00
thirdLine = getLocalIP();
2023-06-08 05:55:31 +02:00
fourthLine = " listening...";
espClient.write((iGateBeaconPacket + "\n").c_str());
show_display(firstLine, secondLine, thirdLine, fourthLine, 0);
2023-06-08 23:09:05 +02:00
} else if (stationMode==3 || stationMode==4) {
show_display(firstLine, secondLine, thirdLine, "SENDING iGate BEACON", 0);
fourthLine = " listening...";
2023-06-09 14:34:34 +02:00
if (stationMode == 4) {
2023-06-08 23:09:05 +02:00
LoRa_Utils::changeFreqTx();
}
LoRa_Utils::sendNewPacket("APRS",iGateBeaconPacket);
if (stationMode == 4) {
LoRa_Utils::changeFreqRx();
}
2023-06-08 05:55:31 +02:00
}
lastBeaconTx = millis();
lastScreenOn = millis();
beacon_update = false;
}
2023-06-08 23:09:05 +02:00
if (statusAfterBoot) {
processStatus();
}
2023-06-08 05:55:31 +02:00
}
void checkDisplayInterval() {
uint32_t lastDisplayTime = millis() - lastScreenOn;
if (!Config.display.alwaysOn) {
if (lastDisplayTime >= Config.display.timeout*1000) {
display_toggle(false);
}
}
}
2023-06-08 23:09:05 +02:00
void validateDigiFreqs() {
if (stationMode == 4) {
if (abs(Config.loramodule.digirepeaterTxFreq - Config.loramodule.digirepeaterRxFreq) < 125000) {
Serial.println("Tx Freq less than 125kHz from Rx Freq ---> NOT VALID, check 'data/igate_conf.json'");
show_display("Tx Freq is less than ", "125kHz from Rx Freq", "change it on : /data/", "igate_conf.json", 0);
while (1);
}
}
}
2023-06-09 07:12:13 +02:00
void typeOfPacket(String packet) {
2023-06-09 14:34:34 +02:00
if (stationMode==1 || stationMode==2) {
thirdLine = "Callsign = " + packet.substring(0,packet.indexOf(">"));
} else {
thirdLine = "Callsign = " + packet.substring(3,packet.indexOf(">"));
}
2023-06-09 07:12:13 +02:00
if (packet.indexOf("::") >= 10) {
fourthLine = "TYPE ----> MESSAGE";
} else if (packet.indexOf(":>") >= 10) {
fourthLine = "TYPE ----> NEW STATUS";
} else if (packet.indexOf(":!") >= 10 || packet.indexOf(":=") >= 10) {
fourthLine = "TYPE ----> GPS BEACON";
} else {
fourthLine = "TYPE ----> ??????????";
}
}
void startOTAServer() {
if (stationMode==1 || stationMode==2) {
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
2023-06-12 01:55:43 +02:00
request->send(200, "text/plain", "Hi " + Config.callsign + ", this is your (Richonguzman/CD2RXU) LoRa iGate.\n\nTo update your firmware or filesystem go to: http://" + getLocalIP().substring(getLocalIP().indexOf(":")+2) + "/update\n\n\n73!");
});
AsyncElegantOTA.begin(&server);
server.begin();
Serial.println("HTTP server started (OTA Firmware Updates)!\n");
}
}
2023-06-04 20:18:30 +02:00
}