LoRa_APRS_iGate/src/LoRa_APRS_iGate.cpp

277 wiersze
7.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-18 18:49:59 +00:00
#include <APRS-IS.h>
2020-06-04 20:27:44 +00:00
#include "LoRa_APRS.h"
2020-03-18 18:49:59 +00:00
2020-07-24 13:29:14 +00:00
#include "pins.h"
2020-03-18 18:49:59 +00:00
#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-07-22 20:21:54 +00:00
#include "configuration.h"
2020-03-18 18:49:59 +00:00
WiFiMulti WiFiMulti;
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, 60*60);
2020-07-22 20:21:54 +00:00
Configuration * Config = 0;
APRS_IS * aprs_is = 0;
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-06-04 20:27:44 +00:00
LoRa_APRS lora_aprs;
2020-03-18 18:49:59 +00:00
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
hw_timer_t * timer = NULL;
volatile bool timerTick = false;
2020-03-19 12:29:21 +00:00
int next_update = -1;
2020-07-22 20:21:54 +00:00
void load_config();
2020-04-03 21:32:10 +00:00
void setup_wifi();
void setup_ota();
void setup_lora();
void setup_ntp();
2020-07-22 20:21:54 +00:00
void setup_aprs_is();
void setup_timer();
2020-04-03 21:32:10 +00:00
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-07-22 20:21:54 +00:00
show_display("OE5BPA", "LoRa APRS iGate", "by Peter Buchegger", 3000);
2020-03-18 18:49:59 +00:00
2020-07-22 20:21:54 +00:00
load_config();
2020-04-03 21:32:10 +00:00
setup_wifi();
setup_ota();
setup_lora();
setup_ntp();
2020-07-22 20:21:54 +00:00
setup_aprs_is();
setup_timer();
2020-05-08 21:09:44 +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()
{
if(timerTick)
{
portENTER_CRITICAL(&timerMux);
timerTick = false;
portEXIT_CRITICAL(&timerMux);
next_update--;
}
2020-03-18 18:49:59 +00:00
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;
}
2020-07-22 20:21:54 +00:00
if(!aprs_is->connected())
2020-03-18 18:49:59 +00:00
{
Serial.print("[INFO] connecting to server: ");
2020-07-22 20:21:54 +00:00
Serial.print(Config->getIsServer());
2020-03-18 18:49:59 +00:00
Serial.print(" on port: ");
2020-07-22 20:21:54 +00:00
Serial.println(Config->getIsPort());
2020-03-19 21:02:24 +00:00
show_display("INFO", "Connecting to server");
2020-07-22 20:21:54 +00:00
if(!aprs_is->connect(Config->getIsServer(), Config->getIsPort()))
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!");
}
if(next_update < 0)
2020-03-18 22:24:23 +00:00
{
2020-07-22 20:21:54 +00:00
show_display(Config->getIsCall(), "Beacon to Server...");
2020-03-18 22:24:23 +00:00
Serial.print("[" + timeClient.getFormattedTime() + "] ");
2020-07-24 13:24:36 +00:00
Serial.print(BeaconMsg);
2020-07-22 20:21:54 +00:00
aprs_is->sendMessage(BeaconMsg);
next_update = Config->getBeaconTimeout() * 60;
2020-03-18 22:24:23 +00:00
}
2020-07-22 20:21:54 +00:00
if(aprs_is->available() > 0)
2020-03-18 18:49:59 +00:00
{
2020-07-22 20:21:54 +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);
2020-06-04 20:27:44 +00:00
#ifdef SEND_MESSAGES_FROM_IS_TO_LORA
std::shared_ptr<APRSMessage> msg = std::shared_ptr<APRSMessage>(new APRSMessage());
msg->decode(str);
lora_aprs.sendMessage(msg);
#endif
2020-03-18 18:49:59 +00:00
}
2020-06-04 20:27:44 +00:00
if(lora_aprs.hasMessage())
2020-03-18 18:49:59 +00:00
{
2020-06-04 20:27:44 +00:00
std::shared_ptr<APRSMessage> msg = lora_aprs.getMessage();
show_display(Config->getIsCall(), timeClient.getFormattedTime() + " LoRa", "RSSI: " + String(lora_aprs.getMessageRssi()) + ", SNR: " + String(lora_aprs.getMessageSnr()), msg->toString());
2020-05-11 11:41:33 +00:00
Serial.print("[" + timeClient.getFormattedTime() + "] ");
Serial.print(" Received packet '");
2020-06-04 20:27:44 +00:00
Serial.print(msg->toString());
2020-03-18 18:49:59 +00:00
Serial.print("' with RSSI ");
2020-06-04 20:27:44 +00:00
Serial.print(lora_aprs.getMessageRssi());
2020-03-18 18:49:59 +00:00
Serial.print(" and SNR ");
2020-06-04 20:27:44 +00:00
Serial.println(lora_aprs.getMessageSnr());
2020-03-18 22:24:23 +00:00
2020-07-22 20:21:54 +00:00
aprs_is->sendMessage(msg->encode());
}
static int _next_update = 0;
if(next_update != _next_update)
{
show_display(Config->getIsCall(), "Time to next beaconing: " + String(next_update));
}
2020-07-22 20:21:54 +00:00
}
void load_config()
{
Config = new Configuration("/is-cfg.json");
if(Config->getIsCall() == "NOCALL-10" || Config->getWifiName() == "")
{
Serial.println("[ERROR] You have to change your settings in 'data/is-cfg.json' and upload it via \"Upload File System image\"!");
show_display("ERROR", "You have to change your settings in 'data/is-cfg.json' and upload it via \"Upload File System image\"!");
while (true)
{}
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);
2020-07-22 20:21:54 +00:00
WiFi.setHostname(Config->getIsCall().c_str());
WiFiMulti.addAP(Config->getWifiName().c_str(), Config->getWifiPassword().c_str());
2020-04-03 21:32:10 +00:00
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");
});
2020-07-22 20:21:54 +00:00
ArduinoOTA.setHostname(Config->getIsCall().c_str());
2020-04-03 21:32:10 +00:00
ArduinoOTA.begin();
}
void setup_lora()
{
Serial.print("[INFO] frequency: ");
2020-06-04 20:27:44 +00:00
Serial.println(LORA_RX_FREQUENCY);
if (!lora_aprs.begin())
{
2020-04-03 21:32:10 +00:00
Serial.println("[ERROR] Starting LoRa failed!");
show_display("ERROR", "Starting LoRa failed!");
while (1);
}
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);
}
2020-07-22 20:21:54 +00:00
void setup_aprs_is()
{
aprs_is = new APRS_IS(Config->getIsCall(), Config->getIsPassword() , "ESP32-APRS-IS", "0.1");
APRSMessage msg;
msg.setSource(Config->getIsCall());
msg.setDestination("APLG0");
msg.getAPRSBody()->setData(String("=") + Config->getBeaconPosLat() + "I" + Config->getBeaconPosLong() + "&" + Config->getBeaconMessage());
BeaconMsg = msg.encode();
}
void IRAM_ATTR onTimer()
{
portENTER_CRITICAL_ISR(&timerMux);
timerTick = true;
portEXIT_CRITICAL_ISR(&timerMux);
}
void setup_timer()
{
timer = timerBegin(0, 80, true);
timerAlarmWrite(timer, 1000000, true);
timerAttachInterrupt(timer, &onTimer, true);
timerAlarmEnable(timer);
}