LoRa_APRS_iGate/src/LoRa_APRS_iGate.cpp

248 wiersze
6.2 KiB
C++
Czysty Zwykły widok Historia

2020-03-18 18:49:59 +00:00
#include <Arduino.h>
#include <WiFiMulti.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
2020-03-19 12:59:51 +00:00
#include <ArduinoOTA.h>
2020-03-24 09:42:46 +00:00
#include <LoRa.h>
2020-03-18 18:49:59 +00:00
#include <APRS-IS.h>
#include <APRS-Decoder.h>
#include "settings.h"
2020-03-18 22:24:23 +00:00
#include "display.h"
2020-06-01 09:23:48 +00:00
#include "power_management.h"
2020-03-18 18:49:59 +00:00
WiFiMulti WiFiMulti;
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, 60*60);
APRS_IS aprs_is(USER, PASS, TOOL, VERS);
2020-05-11 09:57:42 +00:00
#if defined(ARDUINO_T_Beam) && !defined(ARDUINO_T_Beam_V0_7)
2020-06-01 09:23:48 +00:00
PowerManagement powerManagement;
2020-05-08 22:10:34 +00:00
#endif
2020-03-18 18:49:59 +00:00
2020-03-19 12:29:21 +00:00
int next_update = -1;
2020-04-03 21:32:10 +00:00
void setup_wifi();
void setup_ota();
void setup_lora();
void setup_ntp();
2020-05-08 21:09:44 +00:00
String BeaconMsg;
2020-05-29 19:13:11 +00:00
// cppcheck-suppress unusedFunction
2020-03-18 18:49:59 +00:00
void setup()
{
Serial.begin(115200);
2020-06-01 09:23:48 +00:00
#if defined(ARDUINO_T_Beam) && !defined(ARDUINO_T_Beam_V0_7)
Wire.begin(SDA, SCL);
if (!powerManagement.begin(Wire))
{
Serial.println("LoRa-APRS / Init / AXP192 Begin PASS");
} else {
Serial.println("LoRa-APRS / Init / AXP192 Begin FAIL");
}
powerManagement.activateLoRa();
powerManagement.activateOLED();
powerManagement.deactivateGPS();
#endif
2020-03-19 21:02:24 +00:00
setup_display();
2020-03-18 22:24:23 +00:00
2020-03-18 18:49:59 +00:00
delay(500);
Serial.println("[INFO] LoRa APRS iGate by OE5BPA (Peter Buchegger)");
2020-03-19 21:02:24 +00:00
show_display("OE5BPA", "LoRa APRS iGate", "by Peter Buchegger", 2000);
2020-03-18 18:49:59 +00:00
2020-04-03 21:32:10 +00:00
setup_wifi();
setup_ota();
setup_lora();
setup_ntp();
2020-05-08 21:09:44 +00:00
APRSMessage msg;
msg.setSource(USER);
2020-05-17 15:44:20 +00:00
msg.setDestination("APLG0");
msg.getAPRSBody()->setData(String("=") + BEACON_LAT_POS + "I" + BEACON_LONG_POS + "&" + BEACON_MESSAGE);
2020-05-08 21:09:44 +00:00
BeaconMsg = msg.encode();
2020-04-03 21:32:10 +00:00
2020-03-18 18:49:59 +00:00
delay(500);
}
2020-05-29 19:13:11 +00:00
// cppcheck-suppress unusedFunction
2020-03-18 18:49:59 +00:00
void loop()
{
timeClient.update();
2020-03-19 12:59:51 +00:00
ArduinoOTA.handle();
2020-03-18 18:49:59 +00:00
if(WiFiMulti.run() != WL_CONNECTED)
{
Serial.println("[ERROR] WiFi not connected!");
2020-03-19 21:02:24 +00:00
show_display("ERROR", "WiFi not connected!");
2020-03-18 18:49:59 +00:00
delay(1000);
return;
}
if(!aprs_is.connected())
{
Serial.print("[INFO] connecting to server: ");
Serial.print(SERVER);
Serial.print(" on port: ");
Serial.println(PORT);
2020-03-19 21:02:24 +00:00
show_display("INFO", "Connecting to server");
#ifdef FILTER
2020-03-18 18:49:59 +00:00
if(!aprs_is.connect(SERVER, PORT, FILTER))
#else
if(!aprs_is.connect(SERVER, PORT))
#endif
2020-03-18 18:49:59 +00:00
{
Serial.println("[ERROR] Connection failed.");
Serial.println("[INFO] Waiting 5 seconds before retrying...");
2020-03-19 21:02:24 +00:00
show_display("ERROR", "Server connection failed!", "waiting 5 sec");
2020-03-18 18:49:59 +00:00
delay(5000);
return;
}
Serial.println("[INFO] Connected to server!");
}
2020-03-19 12:29:21 +00:00
if(next_update == timeClient.getMinutes() || next_update == -1)
2020-03-18 22:24:23 +00:00
{
show_display(USER, "Beacon to Server...");
2020-03-18 22:24:23 +00:00
Serial.print("[" + timeClient.getFormattedTime() + "] ");
2020-05-08 21:09:44 +00:00
aprs_is.sendMessage(BeaconMsg);
2020-05-08 20:21:07 +00:00
next_update = (timeClient.getMinutes() + BEACON_TIMEOUT) % 60;
2020-03-18 22:24:23 +00:00
}
2020-03-18 18:49:59 +00:00
if(aprs_is.available() > 0)
{
2020-03-18 22:24:23 +00:00
String str = aprs_is.getMessage();
2020-03-18 18:49:59 +00:00
Serial.print("[" + timeClient.getFormattedTime() + "] ");
2020-03-18 22:24:23 +00:00
Serial.println(str);
#ifdef FILTER
show_display(USER, timeClient.getFormattedTime() + " IS-Server", str, 0);
#endif
2020-03-18 18:49:59 +00:00
}
2020-04-03 21:32:10 +00:00
if(LoRa.parsePacket())
2020-03-18 18:49:59 +00:00
{
2020-05-11 11:41:33 +00:00
// read header:
char dummy[4];
LoRa.readBytes(dummy, 3);
if(dummy[0] != '<')
{
// is no APRS message, ignore message
while(LoRa.available())
{
LoRa.read();
}
return;
}
// read APRS data:
2020-03-18 18:49:59 +00:00
String str;
while(LoRa.available())
{
str += (char)LoRa.read();
}
show_display(USER, timeClient.getFormattedTime() + " LoRa", "RSSI: " + String(LoRa.packetRssi()) + ", SNR: " + String(LoRa.packetSnr()), str, 0);
2020-05-11 11:41:33 +00:00
Serial.print("[" + timeClient.getFormattedTime() + "] ");
Serial.print(" Received packet '");
2020-03-18 18:49:59 +00:00
Serial.print(str);
Serial.print("' with RSSI ");
Serial.print(LoRa.packetRssi());
Serial.print(" and SNR ");
Serial.println(LoRa.packetSnr());
/*APRSMessage msg;
msg.decode(str);
Serial.print("[INFO] ");
Serial.println(msg.toString());*/
aprs_is.sendMessage(str);
2020-03-18 22:24:23 +00:00
2020-03-18 18:49:59 +00:00
}
}
2020-04-03 21:32:10 +00:00
void setup_wifi()
{
WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
WiFi.setHostname(USER);
2020-04-03 21:32:10 +00:00
WiFiMulti.addAP(WIFI_NAME, WIFI_KEY);
Serial.print("[INFO] Waiting for WiFi");
show_display("INFO", "Waiting for WiFi");
while(WiFiMulti.run() != WL_CONNECTED)
{
Serial.print(".");
show_display("INFO", "Waiting for WiFi", "....");
delay(500);
}
Serial.println("");
Serial.println("[INFO] WiFi connected");
Serial.print("[INFO] IP address: ");
Serial.println(WiFi.localIP());
show_display("INFO", "WiFi connected", "IP: ", WiFi.localIP().toString(), 2000);
}
void setup_ota()
{
ArduinoOTA
.onStart([]()
{
String type;
if (ArduinoOTA.getCommand() == U_FLASH)
type = "sketch";
else // U_SPIFFS
type = "filesystem";
Serial.println("Start updating " + type);
show_display("OTA UPDATE", "Start update", type);
})
.onEnd([]()
{
Serial.println();
Serial.println("End");
})
.onProgress([](unsigned int progress, unsigned int total)
{
Serial.print("Progress: ");
Serial.print(progress / (total / 100));
Serial.println("%");
show_display("OTA UPDATE", "Progress: ", String(progress / (total / 100)) + "%");
})
.onError([](ota_error_t error) {
Serial.print("Error[");
Serial.print(error);
Serial.print("]: ");
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.setHostname(USER);
2020-04-03 21:32:10 +00:00
ArduinoOTA.begin();
}
void setup_lora()
{
Serial.println("[INFO] Set SPI pins!");
2020-05-08 21:59:19 +00:00
SPI.begin(LORA_SCK, LORA_MISO, LORA_MOSI, LORA_CS);
LoRa.setPins(LORA_CS, LORA_RST, LORA_IRQ);
2020-04-03 21:32:10 +00:00
Serial.println("[INFO] Set LoRa pins!");
Serial.print("[INFO] frequency: ");
Serial.println(LORA_FREQUENCY);
if (!LoRa.begin(LORA_FREQUENCY)) {
2020-04-03 21:32:10 +00:00
Serial.println("[ERROR] Starting LoRa failed!");
show_display("ERROR", "Starting LoRa failed!");
while (1);
}
LoRa.setSpreadingFactor(LORA_SPREADING_FACTOR);
LoRa.setSignalBandwidth(LORA_SIGNAL_BANDWIDTH);
LoRa.setCodingRate4(LORA_CODING_RATE4);
2020-04-03 21:32:10 +00:00
LoRa.enableCrc();
Serial.println("[INFO] LoRa init done!");
show_display("INFO", "LoRa init done!", 2000);
}
void setup_ntp()
{
timeClient.begin();
if(!timeClient.forceUpdate())
{
Serial.println("[WARN] NTP Client force update issue!");
show_display("WARN", "NTP Client force update issue!", 2000);
}
Serial.println("[INFO] NTP Client init done!");
show_display("INFO", "NTP Client init done!", 2000);
}