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>
|
|
|
|
|
|
|
|
|
|
#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
|
|
|
#include "iGate_config_OLD.h"
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
//static bool displayEcoMode = true;
|
2023-03-26 18:19:01 +02:00
|
|
|
|
2023-03-26 22:50:32 +02:00
|
|
|
static int myWiFiAPIndex = 1;
|
|
|
|
|
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-26 22:50:32 +02:00
|
|
|
Serial.print("\nConnecting to '"); Serial.print(currentWiFi->ssid); Serial.println("' WiFi ...");
|
2023-03-03 00:40:59 +01:00
|
|
|
WiFi.mode(WIFI_STA);
|
|
|
|
|
WiFi.disconnect();
|
|
|
|
|
delay(100);
|
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);
|
|
|
|
|
}
|
|
|
|
|
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-26 23:29:43 +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;
|
|
|
|
|
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) {
|
2023-03-26 22:50:32 +02:00
|
|
|
show_display("LoRa iGate: " + Config.callsign, secondLine, String(aprsPacket.substring(0,aprsPacket.indexOf(">"))) + " MESSAGE", 1000);
|
2023-03-26 14:12:27 +02:00
|
|
|
} else if (aprsPacket.indexOf(":>") >= 10) {
|
2023-03-26 22:50:32 +02:00
|
|
|
show_display("LoRa iGate: " + Config.callsign, secondLine, String(aprsPacket.substring(0,aprsPacket.indexOf(">"))) + " NEW STATUS", 1000);
|
2023-03-26 14:12:27 +02:00
|
|
|
} else {
|
2023-03-26 22:50:32 +02:00
|
|
|
show_display("LoRa iGate: " + Config.callsign, secondLine, String(aprsPacket.substring(0,aprsPacket.indexOf(">"))) + " 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-26 22:50:32 +02:00
|
|
|
/*void showConfig() {
|
2023-03-26 18:19:01 +02:00
|
|
|
Serial.println(myWiFiAPSize);
|
|
|
|
|
Serial.println(myWiFiAPIndex);
|
|
|
|
|
if(myWiFiAPIndex >= (myWiFiAPSize-1)) {
|
|
|
|
|
myWiFiAPIndex = 0;
|
|
|
|
|
} else {
|
|
|
|
|
myWiFiAPIndex++;
|
|
|
|
|
}
|
|
|
|
|
Serial.println(myWiFiAPIndex);
|
|
|
|
|
currentWiFi = &Config.wifiAPs[myWiFiAPIndex];
|
|
|
|
|
Serial.println(currentWiFi->ssid);
|
|
|
|
|
Serial.println(currentWiFi->password);
|
|
|
|
|
Serial.println(currentWiFi->latitude);
|
|
|
|
|
Serial.println(currentWiFi->longitude);
|
|
|
|
|
Serial.println(Config.callsign);
|
2023-03-26 22:50:32 +02:00
|
|
|
Serial.println(Config.comment);
|
|
|
|
|
Serial.println(Config.aprs_is.active);
|
|
|
|
|
Serial.println(Config.aprs_is.passcode);
|
|
|
|
|
Serial.println(Config.aprs_is.server);
|
|
|
|
|
Serial.println(Config.aprs_is.port);
|
|
|
|
|
Serial.println(Config.loramodule.frequency);
|
|
|
|
|
Serial.println(Config.loramodule.spreading_factor);
|
|
|
|
|
Serial.println(Config.loramodule.signal_bandwidth);
|
|
|
|
|
Serial.println(Config.loramodule.coding_rate4);
|
|
|
|
|
Serial.println(Config.loramodule.power);
|
|
|
|
|
Serial.println(Config.display.always_on);
|
|
|
|
|
Serial.println(Config.display.timeout);
|
|
|
|
|
Serial.println(" ");
|
|
|
|
|
}*/
|
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-26 22:50:32 +02:00
|
|
|
//showConfig();
|
|
|
|
|
//delay(2000);
|
|
|
|
|
String wifiState, aprsisState;
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
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;
|
2023-03-26 22:50:32 +02:00
|
|
|
if (Config.display.always_on) {
|
|
|
|
|
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-26 22:50:32 +02:00
|
|
|
//String iGateBeaconPacket = Config.callsign + ">APRS,TCPIP*,qAC,CHILE:=" + currentWiFi->latitude + "L" + currentWiFi->longitude + "&" + Config.comment;
|
|
|
|
|
String iGateBeaconPacket = Config.callsign + ">APRS,TCPIP*,qAC,CHILE:=" + Latitude + "L" + Longitude + "&" + 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
|
|
|
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
|
|
|
}
|