LoRa_APRS_iGate/src/LoRa_APRS_iGate.cpp

219 lines
7.6 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>
#include "iGate_config.h"
#include "pins_config.h"
2023-03-26 14:12:27 +02:00
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "display.h"
2023-03-02 12:51:38 +01:00
2023-03-03 00:40:59 +01:00
WiFiClient espClient;
uint32_t lastTxTime = 0;
static bool beacon_update = true;
unsigned long previousWiFiMillis = 0;
2023-03-26 14:12:27 +02:00
static uint32_t lastRxTxTime = millis();
static bool displayEcoMode = true;
String firstLine, secondLine, thirdLine, fourthLine;
void load_config() {
Serial.println("cargando configuracion desde SPIFFS");
}
2023-03-02 12:51:38 +01:00
2023-03-03 00:40:59 +01:00
void setup_wifi() {
int status = WL_IDLE_STATUS;
2023-03-26 14:12:27 +02:00
Serial.print("\nConnecting to '"); Serial.print(WIFI_SSID); Serial.println("' WiFi ...");
2023-03-03 00:40:59 +01:00
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
Serial.print('.');
delay(1000);
}
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);
long freq = 433775000;
if (!LoRa.begin(freq)) {
Serial.println("Starting LoRa failed!");
while (true) {
}
}
LoRa.setSpreadingFactor(12);
LoRa.setSignalBandwidth(125000);
LoRa.setCodingRate4(5);
LoRa.enableCrc();
LoRa.setTxPower(20);
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 ...");
while (!espClient.connect(AprsServer.c_str(), AprsServerPort) && count < 20) {
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-03 00:40:59 +01:00
Serial.println("Trying to connect with Server: " + String(AprsServer) + " AprsServerPort: " + String(AprsServerPort));
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 14:12:27 +02:00
Serial.println("Connected with Server: '" + String(AprsServer) + "' (port: " + String(AprsServerPort)+ ")");
2023-03-03 00:40:59 +01:00
aprsauth = "user " + iGateCallsign + " pass " + iGatePasscode + " vers " + AprsSoftwareName + " " + AprsSoftwareVersion + " filter " + AprsFilter + "\n\r";
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);
processedPacket = callsign_and_path_tracker + ",qAO," + iGateCallsign + payload_tracker + "\n";
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;
if ((packet.substring(0, 3) == "\x3c\xff\x01") && (packet.substring(4, 5) != "}")) {
2023-03-03 01:40:39 +01:00
Serial.println(" ---> Valid LoRa Packet!");
2023-03-26 14:12:27 +02:00
aprsPacket = createAPRSPacket(packet);
display_toggle(true);
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) {
show_display("LoRa iGate: " + iGateCallsign, secondLine, String(aprsPacket.substring(0,aprsPacket.indexOf(">"))) + " MESSAGE", 1000);
} else if (aprsPacket.indexOf(":>") >= 10) {
show_display("LoRa iGate: " + iGateCallsign, secondLine, String(aprsPacket.substring(0,aprsPacket.indexOf(">"))) + " NEW STATUS", 1000);
} else {
show_display("LoRa iGate: " + iGateCallsign, secondLine, String(aprsPacket.substring(0,aprsPacket.indexOf(">"))) + " GPS BEACON", 1000);
}
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);
newLoraPacket = "}" + firstPart + "," + iGateCallsign + "*::" + messagePart + "\n";
Serial.print(newLoraPacket);
return newLoraPacket;
2023-03-03 02:31:22 +01:00
}
2023-03-02 12:51:38 +01:00
void setup() {
Serial.begin(115200);
2023-03-26 14:12:27 +02:00
Serial.println("Starting iGate: " + iGateCallsign + "\n");
load_config();
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() {
String wifiState, aprsisState;
firstLine = "LoRa iGate: " + iGateCallsign;
secondLine = " ";
thirdLine = " ";
fourthLine = " ";
2023-03-03 00:40:59 +01:00
unsigned long currentWiFiMillis = millis();
if ((WiFi.status() != WL_CONNECTED) && (currentWiFiMillis - previousWiFiMillis >= WifiCheckInterval)) { // if WiFi is down, try reconnecting
Serial.print(millis());
Serial.println("Reconnecting to WiFi...");
WiFi.disconnect();
WiFi.reconnect();
previousWiFiMillis = currentWiFiMillis;
}
if (!espClient.connected()) {
APRS_IS_connect();
2023-03-02 12:51:38 +01:00
}
2023-03-26 14:12:27 +02:00
if (WiFi.status() == WL_CONNECTED) wifiState = "OK"; else wifiState = "--"; display_toggle(true); lastRxTxTime = millis();
if (espClient.connected()) aprsisState = "OK"; else aprsisState = "--"; display_toggle(true); lastRxTxTime = millis();
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;
if (displayEcoMode) {
if (lastRxTx >= EcoModeDisplayTime) {
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;
if (lastTx >= BeaconInterval) {
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-03 01:40:39 +01:00
espClient.write(iGateBeaconPacket.c_str());
lastTxTime = millis();
2023-03-26 14:12:27 +02:00
display_toggle(true);
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);
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
}