2023-03-02 12:51:38 +01:00
|
|
|
#include <Arduino.h>
|
|
|
|
|
#include <SPI.h>
|
|
|
|
|
#include <LoRa.h>
|
|
|
|
|
#include <WiFi.h>
|
2023-03-26 14:12:27 +02:00
|
|
|
#include <Adafruit_GFX.h>
|
|
|
|
|
#include <Adafruit_SSD1306.h>
|
2023-03-26 18:19:01 +02:00
|
|
|
#include <Wire.h>
|
2023-04-27 17:53:46 +02:00
|
|
|
#include <SPIFFS.h>
|
2023-05-20 00:52:59 +02:00
|
|
|
#include <vector>
|
2023-03-26 18:19:01 +02:00
|
|
|
|
|
|
|
|
#include "pins_config.h"
|
|
|
|
|
#include "igate_config.h"
|
2023-03-26 14:12:27 +02:00
|
|
|
#include "display.h"
|
2023-03-26 18:19:01 +02:00
|
|
|
|
2023-05-23 06:46:47 +02:00
|
|
|
#define VERSION "2023.05.23"
|
2023-05-17 21:51:47 +02:00
|
|
|
|
2023-03-26 18:19:01 +02:00
|
|
|
WiFiClient espClient;
|
2023-03-26 22:50:32 +02:00
|
|
|
String ConfigurationFilePath = "/igate_conf.json";
|
2023-03-26 18:19:01 +02:00
|
|
|
Configuration Config(ConfigurationFilePath);
|
|
|
|
|
|
2023-03-26 22:50:32 +02:00
|
|
|
uint32_t lastTxTime = 0;
|
|
|
|
|
static bool beacon_update = true;
|
|
|
|
|
unsigned long previousWiFiMillis = 0;
|
|
|
|
|
static uint32_t lastRxTxTime = millis();
|
2023-03-26 18:19:01 +02:00
|
|
|
|
2023-03-27 19:25:15 +02:00
|
|
|
static int myWiFiAPIndex = 0;
|
2023-03-26 22:50:32 +02:00
|
|
|
int myWiFiAPSize = Config.wifiAPs.size();
|
|
|
|
|
WiFi_AP *currentWiFi = &Config.wifiAPs[myWiFiAPIndex];
|
2023-03-26 18:19:01 +02:00
|
|
|
|
2023-05-20 00:52:59 +02:00
|
|
|
std::vector<String> lastHeardStation;
|
|
|
|
|
std::vector<String> lastHeardStation2;
|
|
|
|
|
static uint32_t startUpTime = millis();
|
2023-05-10 06:59:00 +02:00
|
|
|
|
2023-05-23 06:25:47 +02:00
|
|
|
String firstLine, secondLine, thirdLine, fourthLine, iGateLatitude, iGateLongitude;
|
2023-03-26 14:12:27 +02:00
|
|
|
|
2023-03-03 00:40:59 +01:00
|
|
|
void setup_wifi() {
|
|
|
|
|
int status = WL_IDLE_STATUS;
|
2023-05-23 06:46:47 +02:00
|
|
|
Serial.print("\nConnecting to WiFi '"); Serial.print(currentWiFi->ssid); Serial.print("' ");
|
|
|
|
|
show_display(" ", "Connecting to Wifi:", currentWiFi->ssid + " ...", 0);
|
2023-03-03 00:40:59 +01:00
|
|
|
WiFi.mode(WIFI_STA);
|
|
|
|
|
WiFi.disconnect();
|
|
|
|
|
delay(100);
|
2023-03-27 05:32:27 +02:00
|
|
|
unsigned long start = millis();
|
2023-03-26 22:50:32 +02:00
|
|
|
WiFi.begin(currentWiFi->ssid.c_str(), currentWiFi->password.c_str());
|
2023-03-03 00:40:59 +01:00
|
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
|
|
|
Serial.print('.');
|
|
|
|
|
delay(1000);
|
2023-03-27 05:32:27 +02:00
|
|
|
if ((millis() - start) > 15000){
|
|
|
|
|
if(myWiFiAPIndex >= (myWiFiAPSize-1)) {
|
|
|
|
|
myWiFiAPIndex = 0;
|
|
|
|
|
} else {
|
|
|
|
|
myWiFiAPIndex++;
|
|
|
|
|
}
|
|
|
|
|
currentWiFi = &Config.wifiAPs[myWiFiAPIndex];
|
|
|
|
|
start = millis();
|
|
|
|
|
Serial.print("\nConnect to WiFi '"); Serial.print(currentWiFi->ssid); Serial.println("' ...");
|
|
|
|
|
show_display(" ", "Connect to Wifi:", currentWiFi->ssid + " ...", 0);
|
|
|
|
|
WiFi.begin(currentWiFi->ssid.c_str(), currentWiFi->password.c_str());
|
|
|
|
|
}
|
2023-03-03 00:40:59 +01:00
|
|
|
}
|
|
|
|
|
Serial.print("Connected as ");
|
|
|
|
|
Serial.println(WiFi.localIP());
|
|
|
|
|
}
|
2023-03-02 12:51:38 +01:00
|
|
|
|
|
|
|
|
void setup_lora() {
|
|
|
|
|
SPI.begin(LORA_SCK, LORA_MISO, LORA_MOSI, LORA_CS);
|
|
|
|
|
LoRa.setPins(LORA_CS, LORA_RST, LORA_IRQ);
|
2023-03-26 22:50:32 +02:00
|
|
|
if (!LoRa.begin(Config.loramodule.frequency)) {
|
2023-03-02 12:51:38 +01:00
|
|
|
Serial.println("Starting LoRa failed!");
|
|
|
|
|
while (true) {
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-26 22:50:32 +02:00
|
|
|
LoRa.setSpreadingFactor(Config.loramodule.spreading_factor);
|
|
|
|
|
LoRa.setSignalBandwidth(Config.loramodule.signal_bandwidth);
|
|
|
|
|
LoRa.setCodingRate4(Config.loramodule.coding_rate4);
|
2023-03-02 12:51:38 +01:00
|
|
|
LoRa.enableCrc();
|
2023-03-26 22:50:32 +02:00
|
|
|
LoRa.setTxPower(Config.loramodule.power);
|
2023-03-02 12:51:38 +01:00
|
|
|
Serial.println("LoRa init done!\n");
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-03 00:40:59 +01:00
|
|
|
void APRS_IS_connect(){
|
2023-03-02 12:51:38 +01:00
|
|
|
int count = 0;
|
|
|
|
|
String aprsauth;
|
2023-03-03 00:40:59 +01:00
|
|
|
Serial.println("Connecting to APRS-IS ...");
|
2023-03-26 22:50:32 +02:00
|
|
|
while (!espClient.connect(Config.aprs_is.server.c_str(), Config.aprs_is.port) && count < 20) {
|
2023-03-03 00:40:59 +01:00
|
|
|
Serial.println("Didn't connect with server...");
|
2023-03-02 12:51:38 +01:00
|
|
|
delay(1000);
|
|
|
|
|
espClient.stop();
|
|
|
|
|
espClient.flush();
|
|
|
|
|
Serial.println("Run client.stop");
|
2023-03-26 22:50:32 +02:00
|
|
|
Serial.println("Trying to connect with Server: " + String(Config.aprs_is.server) + " AprsServerPort: " + String(Config.aprs_is.port));
|
2023-03-02 12:51:38 +01:00
|
|
|
count++;
|
|
|
|
|
Serial.println("Try: " + String(count));
|
|
|
|
|
}
|
|
|
|
|
if (count == 20) {
|
2023-03-03 00:40:59 +01:00
|
|
|
Serial.println("Tried: " + String(count) + " FAILED!");
|
2023-03-02 12:51:38 +01:00
|
|
|
} else {
|
2023-03-26 22:50:32 +02:00
|
|
|
Serial.println("Connected with Server: '" + String(Config.aprs_is.server) + "' (port: " + String(Config.aprs_is.port)+ ")");
|
2023-03-26 23:29:43 +02:00
|
|
|
aprsauth = "user " + Config.callsign + " pass " + Config.aprs_is.passcode + " vers " + Config.aprs_is.software_name + " " + Config.aprs_is.software_version + " filter t/m/" + Config.callsign + "/" + (String)Config.aprs_is.reporting_distance + "\n\r";
|
2023-03-03 00:40:59 +01:00
|
|
|
espClient.write(aprsauth.c_str());
|
|
|
|
|
delay(200);
|
2023-03-02 12:51:38 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-26 14:12:27 +02:00
|
|
|
String createAPRSPacket(String unprocessedPacket) {
|
2023-03-03 00:40:59 +01:00
|
|
|
String callsign_and_path_tracker, payload_tracker, processedPacket;
|
2023-05-22 23:17:56 +02:00
|
|
|
int dotsPosition = unprocessedPacket.indexOf(':');
|
|
|
|
|
callsign_and_path_tracker = unprocessedPacket.substring(3, dotsPosition);
|
|
|
|
|
payload_tracker = unprocessedPacket.substring(dotsPosition);
|
2023-03-28 15:52:26 +02:00
|
|
|
processedPacket = callsign_and_path_tracker + ",qAO," + Config.callsign + payload_tracker + "\n";
|
2023-03-03 00:40:59 +01:00
|
|
|
return processedPacket;
|
2023-03-02 12:51:38 +01:00
|
|
|
}
|
|
|
|
|
|
2023-05-20 07:03:14 +02:00
|
|
|
bool checkValidHeardStation(String station) {
|
2023-05-23 18:04:22 +02:00
|
|
|
bool validStation = false;
|
2023-05-20 07:03:14 +02:00
|
|
|
for (int i=0; i<lastHeardStation.size(); i++) {
|
|
|
|
|
if (lastHeardStation[i].substring(0,lastHeardStation[i].indexOf(",")) == station) {
|
2023-05-23 18:04:22 +02:00
|
|
|
validStation = true;
|
|
|
|
|
Serial.println(" ---> Listened Station");
|
2023-05-20 07:03:14 +02:00
|
|
|
}
|
|
|
|
|
}
|
2023-05-23 18:04:22 +02:00
|
|
|
if (!validStation) {
|
|
|
|
|
Serial.println(" ---> Station not Heard for last 30 min (Not Tx)\n");
|
|
|
|
|
}
|
|
|
|
|
return validStation;
|
2023-05-20 07:03:14 +02:00
|
|
|
}
|
|
|
|
|
|
2023-05-20 03:42:59 +02:00
|
|
|
void deleteNotHeardStation() {
|
2023-05-20 07:12:24 +02:00
|
|
|
uint32_t minReportingTime = 30*60*1000; // 30 minutes // from .json and CONFIGURATION?????
|
2023-05-20 00:52:59 +02:00
|
|
|
for (int i=0; i<lastHeardStation.size(); i++) {
|
2023-05-20 03:16:47 +02:00
|
|
|
String deltaTimeString = lastHeardStation[i].substring(lastHeardStation[i].indexOf(",")+1);
|
|
|
|
|
uint32_t deltaTime = deltaTimeString.toInt();
|
|
|
|
|
if ((millis() - deltaTime) < minReportingTime) {
|
|
|
|
|
lastHeardStation2.push_back(lastHeardStation[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
lastHeardStation.clear();
|
2023-05-20 07:03:14 +02:00
|
|
|
for (int j=0; j<lastHeardStation2.size(); j++) {
|
2023-05-20 03:16:47 +02:00
|
|
|
lastHeardStation.push_back(lastHeardStation2[j]);
|
|
|
|
|
}
|
|
|
|
|
lastHeardStation2.clear();
|
2023-05-20 03:42:59 +02:00
|
|
|
}
|
2023-05-20 03:16:47 +02:00
|
|
|
|
2023-05-20 03:42:59 +02:00
|
|
|
void updateLastHeardStation(String station) {
|
|
|
|
|
bool stationHeard = false;
|
|
|
|
|
for (int i=0; i<lastHeardStation.size(); i++) {
|
|
|
|
|
if (lastHeardStation[i].substring(0,lastHeardStation[i].indexOf(",")) == station) {
|
|
|
|
|
lastHeardStation[i] = station + "," + String(millis());
|
|
|
|
|
stationHeard = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!stationHeard) {
|
|
|
|
|
lastHeardStation.push_back(station + "," + String(millis()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//////
|
|
|
|
|
Serial.println("Stations Near (last 30 minutes):");
|
2023-05-20 03:16:47 +02:00
|
|
|
for (int k=0; k<lastHeardStation.size(); k++) {
|
|
|
|
|
Serial.println(lastHeardStation[k]);
|
2023-05-20 00:52:59 +02:00
|
|
|
}
|
2023-05-20 03:16:47 +02:00
|
|
|
Serial.println(" ");
|
2023-05-20 00:52:59 +02:00
|
|
|
}
|
|
|
|
|
|
2023-05-23 06:25:47 +02:00
|
|
|
void sendNewLoraPacket(String typeOfMessage, String newPacket) {
|
|
|
|
|
LoRa.beginPacket();
|
|
|
|
|
LoRa.write('<');
|
|
|
|
|
if (typeOfMessage == "APRS") {
|
|
|
|
|
LoRa.write(0xFF);
|
|
|
|
|
} else if (typeOfMessage == "LoRa") {
|
|
|
|
|
LoRa.write(0xF8);
|
|
|
|
|
}
|
|
|
|
|
LoRa.write(0x01);
|
|
|
|
|
LoRa.write((const uint8_t *)newPacket.c_str(), newPacket.length());
|
|
|
|
|
LoRa.endPacket();
|
2023-05-23 18:04:22 +02:00
|
|
|
Serial.print("---> LoRa Packet Tx : ");
|
|
|
|
|
Serial.println(newPacket);
|
2023-05-23 06:25:47 +02:00
|
|
|
}
|
|
|
|
|
|
2023-05-23 18:04:22 +02:00
|
|
|
String processQueryAnswer(String query, String station, String queryOrigin) {
|
2023-05-23 06:25:47 +02:00
|
|
|
String processedQuery, queryAnswer;
|
|
|
|
|
if (query.indexOf("?APRS?") == 0 || query.indexOf("?aprs?") == 0 || query.indexOf("?Aprs?") == 0) {
|
|
|
|
|
processedQuery = "?APRSV ?APRSP ?APRSL ?APRSH ?WHERE callsign";
|
|
|
|
|
} else if (query.indexOf("?APRSV") == 0 || query.indexOf("?aprsv") == 0 || query.indexOf("?Aprsv") == 0) {
|
|
|
|
|
processedQuery = Config.aprs_is.software_name + " " + Config.aprs_is.software_version;
|
|
|
|
|
} else if (query.indexOf("?APRSP") == 0 || query.indexOf("?aprsp") == 0 || query.indexOf("?Aprsp") == 0) {
|
2023-05-23 06:56:39 +02:00
|
|
|
processedQuery = "iGate QTH: " + String(currentWiFi->latitude) + " " + String(currentWiFi->longitude);
|
2023-05-23 06:25:47 +02:00
|
|
|
} else if (query.indexOf("?APRSL") == 0 || query.indexOf("?aprsl") == 0 || query.indexOf("?Aprsl") == 0) {
|
|
|
|
|
for (int i=0; i<lastHeardStation.size(); i++) {
|
|
|
|
|
processedQuery += lastHeardStation[i].substring(0,lastHeardStation[i].indexOf(",")) + " ";
|
|
|
|
|
}
|
|
|
|
|
processedQuery.trim();
|
|
|
|
|
} /*else if (query.indexOf("?APRSH") == 0 || query.indexOf("?aprsv") == 0 || query.indexOf("?Aprsv") == 0) {
|
|
|
|
|
// sacar callsign despues de ?APRSH
|
|
|
|
|
Serial.println("escuchaste a X estacion? en las ultimas 24 o 8 horas?");
|
|
|
|
|
processedQuery = "APRSH";
|
|
|
|
|
} else if (query.indexOf("?WHERE") == 0) {
|
|
|
|
|
// agregar callsign para completar donde esta X callsign
|
|
|
|
|
Serial.println("estaciones escuchadas directo (ultimos 30 min)");
|
|
|
|
|
processedQuery = "WHERE";
|
|
|
|
|
}*/
|
|
|
|
|
for(int i = station.length(); i < 9; i++) {
|
|
|
|
|
station += ' ';
|
|
|
|
|
}
|
2023-05-23 18:04:22 +02:00
|
|
|
if (queryOrigin == "APRSIS") {
|
|
|
|
|
queryAnswer = Config.callsign + ">APLG01,TCPIP,qAC::" + station + ":" + processedQuery + "\n";
|
|
|
|
|
} else if (queryOrigin == "LoRa") {
|
|
|
|
|
queryAnswer = Config.callsign + ">APLG01,RFONLY::" + station + ":" + processedQuery;
|
|
|
|
|
}
|
2023-05-23 06:25:47 +02:00
|
|
|
return queryAnswer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void checkReceivedPacket(String packet) {
|
|
|
|
|
bool queryMessage = false;
|
|
|
|
|
String aprsPacket, Sender, AddresseeAndMessage, Addressee, ackMessage, receivedMessage, queryAnswer;
|
2023-05-23 18:04:22 +02:00
|
|
|
Serial.print("Received Lora Packet : " + String(packet));
|
2023-05-10 06:59:00 +02:00
|
|
|
if ((packet.substring(0, 3) == "\x3c\xff\x01") && (packet.indexOf("TCPIP") == -1) && (packet.indexOf("NOGATE") == -1) && (packet.indexOf("RFONLY") == -1)) {
|
2023-05-23 18:04:22 +02:00
|
|
|
Serial.print(" ---> APRS LoRa Packet!");
|
2023-05-23 06:25:47 +02:00
|
|
|
Sender = packet.substring(3,packet.indexOf(">"));
|
|
|
|
|
if (Sender != Config.callsign) { // avoid listening yourself by digirepeating
|
|
|
|
|
if (packet.indexOf("::") > 10) { // its a Message!
|
|
|
|
|
AddresseeAndMessage = packet.substring(packet.indexOf("::")+2);
|
|
|
|
|
Addressee = AddresseeAndMessage.substring(0,AddresseeAndMessage.indexOf(":"));
|
|
|
|
|
Addressee.trim();
|
|
|
|
|
if (Addressee == Config.callsign) { // its for me!
|
|
|
|
|
if (AddresseeAndMessage.indexOf("{")>0) { // ack?
|
|
|
|
|
ackMessage = "ack" + AddresseeAndMessage.substring(AddresseeAndMessage.indexOf("{")+1);
|
|
|
|
|
ackMessage.trim();
|
|
|
|
|
delay(4000);
|
|
|
|
|
Serial.println(ackMessage);
|
2023-05-23 18:04:22 +02:00
|
|
|
for(int i = Sender.length(); i < 9; i++) {
|
|
|
|
|
Sender += ' ';
|
|
|
|
|
}
|
|
|
|
|
sendNewLoraPacket("APRS", Config.callsign + ">APLG01,RFONLY::" + Sender + ":" + ackMessage);
|
2023-05-23 06:25:47 +02:00
|
|
|
receivedMessage = AddresseeAndMessage.substring(AddresseeAndMessage.indexOf(":")+1, AddresseeAndMessage.indexOf("{"));
|
|
|
|
|
} else {
|
|
|
|
|
receivedMessage = AddresseeAndMessage.substring(AddresseeAndMessage.indexOf(":")+1);
|
|
|
|
|
}
|
|
|
|
|
if (receivedMessage.indexOf("?") == 0) {
|
|
|
|
|
queryMessage = true;
|
2023-05-23 18:04:22 +02:00
|
|
|
queryAnswer = processQueryAnswer(receivedMessage, Sender, "LoRa");
|
2023-05-23 06:25:47 +02:00
|
|
|
delay(2000);
|
2023-05-23 06:46:47 +02:00
|
|
|
if (!Config.display.always_on) {
|
|
|
|
|
display_toggle(true);
|
|
|
|
|
}
|
|
|
|
|
lastRxTxTime = millis();
|
2023-05-23 06:25:47 +02:00
|
|
|
sendNewLoraPacket("APRS", queryAnswer);
|
2023-05-24 15:58:10 +02:00
|
|
|
show_display(firstLine, secondLine, "Callsign = " + Sender, "Type --> QUERY", 1000);
|
2023-05-23 06:25:47 +02:00
|
|
|
Serial.println(queryAnswer);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!queryMessage) {
|
|
|
|
|
aprsPacket = createAPRSPacket(packet);
|
|
|
|
|
if (!Config.display.always_on) {
|
2023-05-23 06:46:47 +02:00
|
|
|
display_toggle(true);
|
2023-05-23 06:25:47 +02:00
|
|
|
}
|
|
|
|
|
lastRxTxTime = millis();
|
|
|
|
|
espClient.write(aprsPacket.c_str());
|
2023-05-23 18:04:22 +02:00
|
|
|
Serial.println(" ---> Uploaded to APRS-IS");
|
2023-05-23 06:25:47 +02:00
|
|
|
deleteNotHeardStation();
|
|
|
|
|
updateLastHeardStation(Sender);
|
|
|
|
|
if (aprsPacket.indexOf("::") >= 10) {
|
2023-05-24 15:54:48 +02:00
|
|
|
show_display(firstLine, secondLine, "Callsign = " + Sender, "Type --> MESSAGE", 1000);
|
2023-05-23 06:25:47 +02:00
|
|
|
} else if (aprsPacket.indexOf(":>") >= 10) {
|
2023-05-24 15:54:48 +02:00
|
|
|
show_display(firstLine, secondLine, "Callsign = " + Sender, "Type --> NEW STATUS", 1000);
|
|
|
|
|
} else if (aprsPacket.indexOf(":!") >= 10) {
|
|
|
|
|
show_display(firstLine, secondLine, "Callsign = " + Sender, "Type --> GPS BEACON", 1000);
|
2023-05-23 06:25:47 +02:00
|
|
|
} else {
|
2023-05-24 15:54:48 +02:00
|
|
|
show_display(firstLine, secondLine, "Callsign = " + Sender, "Type --> ??????????", 1000);
|
2023-05-23 06:25:47 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-02 12:51:38 +01:00
|
|
|
} else {
|
2023-05-23 18:04:22 +02:00
|
|
|
Serial.println(" ---> Not APRS Packet (Ignore)\n");
|
2023-03-02 12:51:38 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-23 14:57:17 +02:00
|
|
|
String processAPRSISPacket(String aprsisMessage) {
|
2023-03-03 03:56:32 +01:00
|
|
|
String firstPart, messagePart, newLoraPacket;
|
2023-05-10 06:59:00 +02:00
|
|
|
aprsisMessage.trim();
|
|
|
|
|
firstPart = aprsisMessage.substring(0, aprsisMessage.indexOf(","));
|
2023-03-03 03:56:32 +01:00
|
|
|
messagePart = aprsisMessage.substring(aprsisMessage.indexOf("::")+2);
|
2023-05-23 06:25:47 +02:00
|
|
|
newLoraPacket = firstPart + ",TCPIP," + Config.callsign + "::" + messagePart;
|
2023-05-23 18:04:22 +02:00
|
|
|
Serial.print("Received from APRS-IS : " + aprsisMessage);
|
2023-03-03 03:56:32 +01:00
|
|
|
return newLoraPacket;
|
2023-03-03 02:31:22 +01:00
|
|
|
}
|
|
|
|
|
|
2023-03-27 05:32:27 +02:00
|
|
|
String double2string(double n, int ndec) {
|
|
|
|
|
String r = "";
|
|
|
|
|
int v = n;
|
|
|
|
|
r += v;
|
|
|
|
|
r += '.';
|
|
|
|
|
int i;
|
|
|
|
|
for (i=0;i<ndec;i++) {
|
|
|
|
|
n -= v;
|
|
|
|
|
n = 10 * abs(n);
|
|
|
|
|
v = n;
|
|
|
|
|
r += v;
|
|
|
|
|
}
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String create_lat_aprs(double lat) {
|
|
|
|
|
String degrees = double2string(lat,6);
|
|
|
|
|
String north_south, latitude, convDeg3;
|
|
|
|
|
float convDeg, convDeg2;
|
|
|
|
|
|
2023-04-27 19:00:20 +02:00
|
|
|
if (abs(degrees.toFloat()) < 10) {
|
|
|
|
|
latitude += "0";
|
|
|
|
|
}
|
|
|
|
|
Serial.println(latitude);
|
2023-03-27 05:32:27 +02:00
|
|
|
if (degrees.indexOf("-") == 0) {
|
|
|
|
|
north_south = "S";
|
2023-04-27 19:00:20 +02:00
|
|
|
latitude += degrees.substring(1,degrees.indexOf("."));
|
2023-03-27 05:32:27 +02:00
|
|
|
} else {
|
|
|
|
|
north_south = "N";
|
2023-04-27 19:00:20 +02:00
|
|
|
latitude += degrees.substring(0,degrees.indexOf("."));
|
2023-04-27 17:53:46 +02:00
|
|
|
}
|
2023-03-27 05:32:27 +02:00
|
|
|
convDeg = abs(degrees.toFloat()) - abs(int(degrees.toFloat()));
|
|
|
|
|
convDeg2 = (convDeg * 60)/100;
|
|
|
|
|
convDeg3 = String(convDeg2,6);
|
|
|
|
|
latitude += convDeg3.substring(convDeg3.indexOf(".")+1,convDeg3.indexOf(".")+3) + "." + convDeg3.substring(convDeg3.indexOf(".")+3,convDeg3.indexOf(".")+5);
|
|
|
|
|
latitude += north_south;
|
|
|
|
|
return latitude;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String create_lng_aprs(double lng) {
|
|
|
|
|
String degrees = double2string(lng,6);
|
|
|
|
|
String east_west, longitude, convDeg3;
|
|
|
|
|
float convDeg, convDeg2;
|
|
|
|
|
|
|
|
|
|
if (abs(degrees.toFloat()) < 100) {
|
|
|
|
|
longitude += "0";
|
|
|
|
|
}
|
2023-04-27 19:00:20 +02:00
|
|
|
if (abs(degrees.toFloat()) < 10) {
|
|
|
|
|
longitude += "0";
|
|
|
|
|
}
|
2023-03-27 05:32:27 +02:00
|
|
|
if (degrees.indexOf("-") == 0) {
|
|
|
|
|
east_west = "W";
|
|
|
|
|
longitude += degrees.substring(1,degrees.indexOf("."));
|
2023-03-26 18:19:01 +02:00
|
|
|
} else {
|
2023-03-27 05:32:27 +02:00
|
|
|
east_west = "E";
|
|
|
|
|
longitude += degrees.substring(0,degrees.indexOf("."));
|
2023-03-26 18:19:01 +02:00
|
|
|
}
|
2023-03-27 05:32:27 +02:00
|
|
|
convDeg = abs(degrees.toFloat()) - abs(int(degrees.toFloat()));
|
|
|
|
|
convDeg2 = (convDeg * 60)/100;
|
|
|
|
|
convDeg3 = String(convDeg2,6);
|
|
|
|
|
longitude += convDeg3.substring(convDeg3.indexOf(".")+1,convDeg3.indexOf(".")+3) + "." + convDeg3.substring(convDeg3.indexOf(".")+3,convDeg3.indexOf(".")+5);
|
|
|
|
|
longitude += east_west;
|
|
|
|
|
return longitude;
|
|
|
|
|
}
|
2023-03-26 18:19:01 +02:00
|
|
|
|
2023-03-02 12:51:38 +01:00
|
|
|
void setup() {
|
|
|
|
|
Serial.begin(115200);
|
2023-05-23 06:46:47 +02:00
|
|
|
delay(3000);
|
|
|
|
|
Serial.println("\nStarting iGate: " + Config.callsign + " Version: " + String(VERSION));
|
2023-03-26 14:12:27 +02:00
|
|
|
setup_display();
|
2023-03-02 12:51:38 +01:00
|
|
|
setup_wifi();
|
|
|
|
|
btStop();
|
|
|
|
|
setup_lora();
|
2023-05-23 06:25:47 +02:00
|
|
|
iGateLatitude = create_lat_aprs(currentWiFi->latitude);
|
|
|
|
|
iGateLongitude = create_lng_aprs(currentWiFi->longitude);
|
2023-03-02 12:51:38 +01:00
|
|
|
}
|
|
|
|
|
|
2023-03-26 14:12:27 +02:00
|
|
|
void loop() {
|
2023-05-23 06:25:47 +02:00
|
|
|
String wifiState, aprsisState;
|
2023-03-26 22:50:32 +02:00
|
|
|
firstLine = "LoRa iGate: " + Config.callsign;
|
2023-03-26 14:12:27 +02:00
|
|
|
secondLine = " ";
|
|
|
|
|
thirdLine = " ";
|
|
|
|
|
fourthLine = " ";
|
2023-03-03 00:40:59 +01:00
|
|
|
unsigned long currentWiFiMillis = millis();
|
|
|
|
|
|
2023-03-27 05:32:27 +02:00
|
|
|
if ((WiFi.status() != WL_CONNECTED) && (currentWiFiMillis - previousWiFiMillis >= 30000)) {
|
2023-03-03 00:40:59 +01:00
|
|
|
Serial.print(millis());
|
|
|
|
|
Serial.println("Reconnecting to WiFi...");
|
|
|
|
|
WiFi.disconnect();
|
|
|
|
|
WiFi.reconnect();
|
|
|
|
|
previousWiFiMillis = currentWiFiMillis;
|
|
|
|
|
}
|
2023-03-27 05:32:27 +02:00
|
|
|
|
2023-03-03 00:40:59 +01:00
|
|
|
if (!espClient.connected()) {
|
|
|
|
|
APRS_IS_connect();
|
2023-03-02 12:51:38 +01:00
|
|
|
}
|
2023-03-27 05:32:27 +02:00
|
|
|
|
2023-03-27 19:20:15 +02:00
|
|
|
if (WiFi.status() == WL_CONNECTED) {
|
|
|
|
|
wifiState = "OK";
|
|
|
|
|
} else {
|
|
|
|
|
wifiState = "--";
|
|
|
|
|
if (!Config.display.always_on) {
|
|
|
|
|
display_toggle(true);
|
|
|
|
|
}
|
|
|
|
|
lastRxTxTime = millis();
|
|
|
|
|
}
|
|
|
|
|
if (espClient.connected()) {
|
|
|
|
|
aprsisState = "OK";
|
|
|
|
|
} else {
|
|
|
|
|
aprsisState = "--";
|
|
|
|
|
if (!Config.display.always_on) {
|
|
|
|
|
display_toggle(true);
|
|
|
|
|
}
|
|
|
|
|
lastRxTxTime = millis();
|
|
|
|
|
}
|
2023-03-26 14:12:27 +02:00
|
|
|
secondLine = "WiFi: " + wifiState + "/ APRS-IS: " + aprsisState;
|
|
|
|
|
|
|
|
|
|
show_display(firstLine, secondLine, thirdLine, fourthLine, 0);
|
2023-03-03 00:40:59 +01:00
|
|
|
|
|
|
|
|
while (espClient.connected()) {
|
2023-03-26 14:12:27 +02:00
|
|
|
uint32_t lastRxTx = millis() - lastRxTxTime;
|
2023-03-27 05:32:27 +02:00
|
|
|
if (!Config.display.always_on) {
|
2023-03-26 22:50:32 +02:00
|
|
|
if (lastRxTx >= Config.display.timeout*1000) {
|
2023-03-26 14:12:27 +02:00
|
|
|
display_toggle(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
thirdLine = " ";
|
|
|
|
|
fourthLine = " ";
|
2023-03-03 00:40:59 +01:00
|
|
|
|
2023-03-26 14:12:27 +02:00
|
|
|
show_display(firstLine, secondLine, thirdLine, fourthLine, 0);
|
2023-03-03 01:40:39 +01:00
|
|
|
uint32_t lastTx = millis() - lastTxTime;
|
2023-03-26 23:29:43 +02:00
|
|
|
if (lastTx >= Config.beacon_interval*60*1000) {
|
2023-03-03 01:40:39 +01:00
|
|
|
beacon_update = true;
|
|
|
|
|
}
|
2023-03-26 14:12:27 +02:00
|
|
|
if (beacon_update) {
|
|
|
|
|
display_toggle(true);
|
2023-03-03 02:31:22 +01:00
|
|
|
Serial.println("---- Sending iGate Beacon ----");
|
2023-05-23 19:00:58 +02:00
|
|
|
String iGateBeaconPacket = Config.callsign + ">APLG01,qAC:=" + iGateLatitude + "L" + iGateLongitude + "&" + Config.comment + "\n";
|
2023-03-26 23:37:42 +02:00
|
|
|
//Serial.println(iGateBeaconPacket);
|
2023-03-03 01:40:39 +01:00
|
|
|
espClient.write(iGateBeaconPacket.c_str());
|
|
|
|
|
lastTxTime = millis();
|
2023-03-26 14:12:27 +02:00
|
|
|
lastRxTxTime = millis();
|
2023-05-23 18:10:49 +02:00
|
|
|
show_display(firstLine, secondLine, thirdLine, "SENDING iGate BEACON", 1000);
|
2023-03-03 01:40:39 +01:00
|
|
|
beacon_update = false;
|
|
|
|
|
}
|
2023-03-03 00:40:59 +01:00
|
|
|
|
2023-03-03 01:40:39 +01:00
|
|
|
String loraPacket = "";
|
|
|
|
|
int packetSize = LoRa.parsePacket();
|
|
|
|
|
if (packetSize) {
|
|
|
|
|
while (LoRa.available()) {
|
|
|
|
|
int inChar = LoRa.read();
|
|
|
|
|
loraPacket += (char)inChar;
|
2023-03-03 00:40:59 +01:00
|
|
|
}
|
2023-05-23 06:25:47 +02:00
|
|
|
checkReceivedPacket(loraPacket);
|
2023-03-02 12:51:38 +01:00
|
|
|
}
|
2023-03-03 01:40:39 +01:00
|
|
|
|
|
|
|
|
if (espClient.available()) {
|
2023-05-23 18:04:22 +02:00
|
|
|
String aprsisData, aprsisPacket, newLoraPacket, Sender, AddresseeAndMessage, Addressee, ackMessage, ackPacket, receivedMessage, queryAnswer;
|
2023-05-20 07:03:14 +02:00
|
|
|
bool validHeardStation = false;
|
2023-05-10 06:59:00 +02:00
|
|
|
aprsisData = espClient.readStringUntil('\r'); // or '\n'
|
2023-03-03 01:40:39 +01:00
|
|
|
aprsisPacket.concat(aprsisData);
|
|
|
|
|
if (!aprsisPacket.startsWith("#")){
|
2023-03-03 21:07:52 +01:00
|
|
|
if (aprsisPacket.indexOf("::")>0) {
|
2023-05-23 18:04:22 +02:00
|
|
|
Sender = aprsisPacket.substring(0,aprsisPacket.indexOf(">"));
|
|
|
|
|
AddresseeAndMessage = aprsisPacket.substring(aprsisPacket.indexOf("::")+2);
|
|
|
|
|
Addressee = AddresseeAndMessage.substring(0, AddresseeAndMessage.indexOf(":"));
|
2023-05-20 07:03:14 +02:00
|
|
|
Addressee.trim();
|
2023-05-23 18:04:22 +02:00
|
|
|
if (Addressee == Config.callsign) { // its for me!
|
|
|
|
|
if (AddresseeAndMessage.indexOf("{")>0) { // ack?
|
|
|
|
|
ackMessage = "ack" + AddresseeAndMessage.substring(AddresseeAndMessage.indexOf("{")+1);
|
|
|
|
|
ackMessage.trim();
|
|
|
|
|
delay(4000);
|
|
|
|
|
Serial.println(ackMessage);
|
|
|
|
|
for(int i = Sender.length(); i < 9; i++) {
|
|
|
|
|
Sender += ' ';
|
|
|
|
|
}
|
|
|
|
|
ackPacket = Config.callsign + ">APLG01,TCPIP,qAC::" + Sender + ":" + ackMessage + "\n";
|
|
|
|
|
espClient.write(ackPacket.c_str());
|
|
|
|
|
receivedMessage = AddresseeAndMessage.substring(AddresseeAndMessage.indexOf(":")+1, AddresseeAndMessage.indexOf("{"));
|
|
|
|
|
} else {
|
|
|
|
|
receivedMessage = AddresseeAndMessage.substring(AddresseeAndMessage.indexOf(":")+1);
|
|
|
|
|
}
|
|
|
|
|
if (receivedMessage.indexOf("?") == 0) {
|
|
|
|
|
Serial.println("Received Query APRS-IS : " + aprsisPacket);
|
|
|
|
|
queryAnswer = processQueryAnswer(receivedMessage, Sender, "APRSIS");
|
|
|
|
|
Serial.println("---> QUERY Answer : " + queryAnswer.substring(0,queryAnswer.indexOf("\n")));
|
|
|
|
|
if (!Config.display.always_on) {
|
|
|
|
|
display_toggle(true);
|
|
|
|
|
}
|
|
|
|
|
lastRxTxTime = millis();
|
|
|
|
|
delay(500);
|
|
|
|
|
espClient.write(queryAnswer.c_str());
|
2023-05-24 15:58:10 +02:00
|
|
|
show_display(firstLine, secondLine, "Callsign = " + Sender, "Type --> QUERY", 1000);
|
2023-05-23 18:04:22 +02:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
newLoraPacket = processAPRSISPacket(aprsisPacket);
|
|
|
|
|
deleteNotHeardStation();
|
|
|
|
|
validHeardStation = checkValidHeardStation(Addressee);
|
|
|
|
|
if (validHeardStation) {
|
|
|
|
|
sendNewLoraPacket("APRS", newLoraPacket);
|
|
|
|
|
display_toggle(true);
|
|
|
|
|
lastRxTxTime = millis();
|
|
|
|
|
show_display(firstLine, secondLine, Sender + " -> " + Addressee, receivedMessage, 2000);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-03 21:07:52 +01:00
|
|
|
}
|
2023-03-03 01:40:39 +01:00
|
|
|
}
|
2023-03-03 02:31:22 +01:00
|
|
|
}
|
2023-03-03 00:40:59 +01:00
|
|
|
}
|
2023-03-26 14:12:27 +02:00
|
|
|
}
|