LoRa_APRS_iGate/src/LoRa_APRS_iGate.cpp

323 lines
11 KiB
C++
Raw Normal View History

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-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
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-02 12:51:38 +01:00
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-03-26 14:12:27 +02:00
String firstLine, secondLine, thirdLine, fourthLine;
2023-03-03 00:40:59 +01:00
void setup_wifi() {
int status = WL_IDLE_STATUS;
2023-03-27 05:32:27 +02:00
Serial.print("\nConnect to WiFi '"); Serial.print(currentWiFi->ssid); Serial.println("' ...");
show_display(" ", "Connect 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;
int two_dots_position = unprocessedPacket.indexOf(':');
callsign_and_path_tracker = unprocessedPacket.substring(3, two_dots_position);
payload_tracker = unprocessedPacket.substring(two_dots_position);
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-03-26 14:12:27 +02:00
void validate_and_upload(String packet) {
String aprsPacket;
2023-05-05 05:04:12 +02:00
if ((packet.substring(0, 3) == "\x3c\xff\x01") && (packet.substring(3, 4) != "}")) {
2023-03-03 01:40:39 +01:00
Serial.println(" ---> Valid LoRa Packet!");
2023-03-26 14:12:27 +02:00
aprsPacket = createAPRSPacket(packet);
2023-03-27 19:20:15 +02:00
if (!Config.display.always_on) {
display_toggle(true);
}
2023-03-26 14:12:27 +02:00
lastRxTxTime = millis();
2023-03-03 01:40:39 +01:00
espClient.write(aprsPacket.c_str());
2023-03-26 14:12:27 +02:00
Serial.print("Message uploaded : "); Serial.println(aprsPacket);
if (aprsPacket.indexOf("::") >= 10) {
2023-03-27 05:32:27 +02:00
show_display("LoRa iGate: " + Config.callsign, secondLine, "Callsign = " + String(aprsPacket.substring(0,aprsPacket.indexOf(">"))), "Type --> MESSAGE", 1000);
2023-03-26 14:12:27 +02:00
} else if (aprsPacket.indexOf(":>") >= 10) {
2023-03-27 05:32:27 +02:00
show_display("LoRa iGate: " + Config.callsign, secondLine, "Callsign = " + String(aprsPacket.substring(0,aprsPacket.indexOf(">"))), "Type --> NEW STATUS", 1000);
2023-03-26 14:12:27 +02:00
} else {
2023-03-27 05:32:27 +02:00
show_display("LoRa iGate: " + Config.callsign, secondLine, "Callsign = " + String(aprsPacket.substring(0,aprsPacket.indexOf(">"))), "Type --> GPS BEACON", 1000);
2023-03-26 14:12:27 +02:00
}
2023-03-02 12:51:38 +01:00
} else {
2023-03-03 01:40:39 +01:00
Serial.println(" ---> Not Valid LoRa Packet (Ignore)");
2023-03-02 12:51:38 +01:00
}
}
2023-03-03 03:56:32 +01:00
String process_aprsisPacket(String aprsisMessage) {
String firstPart, messagePart, newLoraPacket;
firstPart = aprsisMessage.substring(0, aprsisMessage.indexOf("*"));
messagePart = aprsisMessage.substring(aprsisMessage.indexOf("::")+2);
2023-03-26 22:50:32 +02:00
newLoraPacket = "}" + firstPart + "," + Config.callsign + "*::" + messagePart + "\n";
2023-03-03 03:56:32 +01:00
Serial.print(newLoraPacket);
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-03-26 22:50:32 +02:00
Serial.println("Starting iGate: " + Config.callsign + "\n");
2023-03-26 14:12:27 +02:00
setup_display();
2023-03-02 12:51:38 +01:00
setup_wifi();
btStop();
setup_lora();
}
2023-03-26 14:12:27 +02:00
void loop() {
2023-03-27 05:32:27 +02:00
String wifiState, aprsisState, iGateLatitude, iGateLongitude;
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-03-27 05:32:27 +02:00
iGateLatitude = create_lat_aprs(currentWiFi->latitude);
iGateLongitude = create_lng_aprs(currentWiFi->longitude);
2023-03-29 04:47:16 +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();
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-03-03 01:40:39 +01:00
Serial.print("\nReceived Lora Message : " + String(loraPacket));
2023-03-26 14:12:27 +02:00
validate_and_upload(loraPacket);
2023-03-02 12:51:38 +01:00
}
2023-03-03 01:40:39 +01:00
if (espClient.available()) {
2023-03-26 14:12:27 +02:00
String aprsisData, aprsisPacket, newLoraMessage, Sender, AddresseAndMessage, Addressee, Message;
2023-03-03 01:40:39 +01:00
aprsisData = espClient.readStringUntil('\n');
aprsisPacket.concat(aprsisData);
if (!aprsisPacket.startsWith("#")){
2023-03-03 21:07:52 +01:00
if (aprsisPacket.indexOf("::")>0) {
Serial.println("APRS-IS to Tracker : " + aprsisPacket);
newLoraMessage = process_aprsisPacket(aprsisPacket);
LoRa.beginPacket();
LoRa.write('<');
LoRa.write(0xFF);
LoRa.write(0x01);
LoRa.write((const uint8_t *)newLoraMessage.c_str(), newLoraMessage.length());
LoRa.endPacket();
Serial.println("packet LoRa enviado!");
2023-03-26 14:12:27 +02:00
display_toggle(true);
lastRxTxTime = millis();
Sender = newLoraMessage.substring(1,newLoraMessage.indexOf(">"));
AddresseAndMessage = newLoraMessage.substring(newLoraMessage.indexOf("::")+2);
Addressee = AddresseAndMessage.substring(0, AddresseAndMessage.indexOf(":"));
Message = AddresseAndMessage.substring(AddresseAndMessage.indexOf(":")+1);
2023-04-14 17:48:40 +02:00
show_display(firstLine, secondLine, Sender + " -> " + Addressee, Message, 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
}