diff --git a/data/is-cfg.json b/data/is-cfg.json new file mode 100644 index 0000000..ac4b8da --- /dev/null +++ b/data/is-cfg.json @@ -0,0 +1,24 @@ +{ + "Wifi": + { + "Name":"", + "Password":"" + }, + "IS": + { + "Call":"", + "Password":"", + "Server":"austria.aprs2.net", + "Port":14580 + }, + "Beacon": + { + "Message":"LoRa IGATE (RX only), Info: github.com/peterus/LoRa_APRS_iGate", + "Pos": + { + "Lat":"4819.82N", + "Long":"01418.68E" + }, + "Timeout":15 + } +} diff --git a/platformio.ini b/platformio.ini index d858ac2..f0864ef 100644 --- a/platformio.ini +++ b/platformio.ini @@ -14,6 +14,7 @@ lib_deps = LoRa LoRa-APRS-Lib NTPClient + ArduinoJson AXP202X_Library check_tool = cppcheck check_flags = diff --git a/src/LoRa_APRS_iGate.cpp b/src/LoRa_APRS_iGate.cpp index 9b5c116..32b177a 100644 --- a/src/LoRa_APRS_iGate.cpp +++ b/src/LoRa_APRS_iGate.cpp @@ -10,11 +10,13 @@ #include "settings.h" #include "display.h" #include "power_management.h" +#include "configuration.h" WiFiMulti WiFiMulti; WiFiUDP ntpUDP; NTPClient timeClient(ntpUDP, 60*60); -APRS_IS aprs_is(USER, PASS, TOOL, VERS); +Configuration * Config = 0; +APRS_IS * aprs_is = 0; #if defined(ARDUINO_T_Beam) && !defined(ARDUINO_T_Beam_V0_7) PowerManagement powerManagement; #endif @@ -22,10 +24,12 @@ LoRa_APRS lora_aprs; int next_update = -1; +void load_config(); void setup_wifi(); void setup_ota(); void setup_lora(); void setup_ntp(); +void setup_aprs_is(); String BeaconMsg; @@ -51,19 +55,15 @@ void setup() delay(500); Serial.println("[INFO] LoRa APRS iGate by OE5BPA (Peter Buchegger)"); - show_display("OE5BPA", "LoRa APRS iGate", "by Peter Buchegger", 2000); + show_display("OE5BPA", "LoRa APRS iGate", "by Peter Buchegger", 3000); + load_config(); setup_wifi(); setup_ota(); setup_lora(); setup_ntp(); + setup_aprs_is(); - APRSMessage msg; - msg.setSource(USER); - msg.setDestination("APLG0"); - msg.getAPRSBody()->setData(String("=") + BEACON_LAT_POS + "I" + BEACON_LONG_POS + "&" + BEACON_MESSAGE); - BeaconMsg = msg.encode(); - delay(500); } @@ -79,18 +79,14 @@ void loop() delay(1000); return; } - if(!aprs_is.connected()) + if(!aprs_is->connected()) { Serial.print("[INFO] connecting to server: "); - Serial.print(SERVER); + Serial.print(Config->getIsServer()); Serial.print(" on port: "); - Serial.println(PORT); + Serial.println(Config->getIsPort()); show_display("INFO", "Connecting to server"); -#ifdef FILTER - if(!aprs_is.connect(SERVER, PORT, FILTER)) -#else - if(!aprs_is.connect(SERVER, PORT)) -#endif + if(!aprs_is->connect(Config->getIsServer(), Config->getIsPort())) { Serial.println("[ERROR] Connection failed."); Serial.println("[INFO] Waiting 5 seconds before retrying..."); @@ -102,19 +98,16 @@ void loop() } if(next_update == timeClient.getMinutes() || next_update == -1) { - show_display(USER, "Beacon to Server..."); + show_display(Config->getIsCall(), "Beacon to Server..."); Serial.print("[" + timeClient.getFormattedTime() + "] "); - aprs_is.sendMessage(BeaconMsg); - next_update = (timeClient.getMinutes() + BEACON_TIMEOUT) % 60; + aprs_is->sendMessage(BeaconMsg); + next_update = (timeClient.getMinutes() + Config->getBeaconTimeout()) % 60; } - if(aprs_is.available() > 0) + if(aprs_is->available() > 0) { - String str = aprs_is.getMessage(); + String str = aprs_is->getMessage(); Serial.print("[" + timeClient.getFormattedTime() + "] "); Serial.println(str); -#ifdef FILTER - show_display(USER, timeClient.getFormattedTime() + " IS-Server", str, 0); -#endif #ifdef SEND_MESSAGES_FROM_IS_TO_LORA std::shared_ptr msg = std::shared_ptr(new APRSMessage()); msg->decode(str); @@ -125,7 +118,7 @@ void loop() { std::shared_ptr msg = lora_aprs.getMessage(); - show_display(USER, timeClient.getFormattedTime() + " LoRa", "RSSI: " + String(lora_aprs.getMessageRssi()) + ", SNR: " + String(lora_aprs.getMessageSnr()), msg->toString(), 0); + show_display(Config->getIsCall(), timeClient.getFormattedTime() + " LoRa", "RSSI: " + String(lora_aprs.getMessageRssi()) + ", SNR: " + String(lora_aprs.getMessageSnr()), msg->toString(), 0); Serial.print("[" + timeClient.getFormattedTime() + "] "); Serial.print(" Received packet '"); Serial.print(msg->toString()); @@ -134,15 +127,27 @@ void loop() Serial.print(" and SNR "); Serial.println(lora_aprs.getMessageSnr()); - aprs_is.sendMessage(msg->encode()); + aprs_is->sendMessage(msg->encode()); + } +} + +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) + {} } } void setup_wifi() { WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE); - WiFi.setHostname(USER); - WiFiMulti.addAP(WIFI_NAME, WIFI_KEY); + WiFi.setHostname(Config->getIsCall().c_str()); + WiFiMulti.addAP(Config->getWifiName().c_str(), Config->getWifiPassword().c_str()); Serial.print("[INFO] Waiting for WiFi"); show_display("INFO", "Waiting for WiFi"); while(WiFiMulti.run() != WL_CONNECTED) @@ -193,7 +198,7 @@ void setup_ota() else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed"); else if (error == OTA_END_ERROR) Serial.println("End Failed"); }); - ArduinoOTA.setHostname(USER); + ArduinoOTA.setHostname(Config->getIsCall().c_str()); ArduinoOTA.begin(); } @@ -223,3 +228,14 @@ void setup_ntp() Serial.println("[INFO] NTP Client init done!"); show_display("INFO", "NTP Client init done!", 2000); } + +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(); +} diff --git a/src/configuration.cpp b/src/configuration.cpp new file mode 100644 index 0000000..08999c4 --- /dev/null +++ b/src/configuration.cpp @@ -0,0 +1,165 @@ +#include "SPIFFS.h" + +#include "configuration.h" +#include "settings.h" + + +Configuration::Configuration(String FilePath) + : mFilePath(FilePath), mData(1024) +{ + if(!SPIFFS.begin(true)) + { + Serial.println("[ERROR] Mounting SPIFFS was not possible. Trying to format SPIFFS..."); + SPIFFS.format(); + if(!SPIFFS.begin()) + { + Serial.println("[ERROR] Formating SPIFFS was not okay!"); + return; + } + } + if(SPIFFS.exists(mFilePath)) + { + readFile(); + } + else + { + mData["Wifi"]["Name"] = WIFI_NAME; + mData["Wifi"]["Password"] = WIFI_KEY; + mData["IS"]["Call"] = USER; + mData["IS"]["Password"] = PASS; + mData["IS"]["Server"] = SERVER; + mData["IS"]["Port"] = PORT; + mData["Beacon"]["Message"] = BEACON_MESSAGE; + mData["Beacon"]["Pos"]["Lat"] = BEACON_LAT_POS; + mData["Beacon"]["Pos"]["Long"] = BEACON_LONG_POS; + mData["Beacon"]["Timeout"] = BEACON_TIMEOUT; + writeFile(); + } +} + +String Configuration::getWifiName() const +{ + return mData["Wifi"]["Name"]; +} + +void Configuration::setWifiName(String WifiName) +{ + mData["Wifi"]["Name"] = WifiName; +} + +String Configuration::getWifiPassword() const +{ + return mData["Wifi"]["Password"]; +} + +void Configuration::setWifiPassword(String WifiPassword) +{ + mData["Wifi"]["Password"] = WifiPassword; +} + +String Configuration::getIsCall() const +{ + return mData["IS"]["Call"]; +} + +void Configuration::setIsCall(String IsCall) +{ + mData["IS"]["Call"] = IsCall; +} + +String Configuration::getIsPassword() const +{ + return mData["IS"]["Password"]; +} + +void Configuration::setIsPassword(String IsPassword) +{ + mData["IS"]["Password"] = IsPassword; +} + +String Configuration::getIsServer() const +{ + return mData["IS"]["Server"]; +} + +void Configuration::setIsServer(String IsServer) +{ + mData["IS"]["Server"] = IsServer; +} + +int Configuration::getIsPort() const +{ + return mData["IS"]["Port"]; +} + +void Configuration::setIsPort(int IsPort) +{ + mData["IS"]["Port"] = IsPort; +} + +String Configuration::getBeaconMessage() const +{ + return mData["Beacon"]["Message"]; +} + +void Configuration::setBeaconMessage(String BeaconMessage) +{ + mData["Beacon"]["Message"] = BeaconMessage; +} + +String Configuration::getBeaconPosLat() const +{ + return mData["Beacon"]["Pos"]["Lat"]; +} + +void Configuration::setBeaconPosLat(String BeaconPosLat) +{ + mData["Beacon"]["Pos"]["Lat"] = BeaconPosLat; +} + +String Configuration::getBeaconPosLong() const +{ + return mData["Beacon"]["Pos"]["Long"]; +} + +void Configuration::setBeaconPosLong(String BeaconPosLong) +{ + mData["Beacon"]["Pos"]["Long"] = BeaconPosLong; +} + +int Configuration::getBeaconTimeout() const +{ + return mData["Beacon"]["Timeout"]; +} + +void Configuration::setBeaconTimeout(int BeaconTimeout) +{ + mData["Beacon"]["Timeout"] = BeaconTimeout; +} + + +void Configuration::readFile() +{ + File file = SPIFFS.open(mFilePath); + if(!file) + { + Serial.println("Failed to open file for reading..."); + return; + } + deserializeJson(mData, file); + file.close(); +} + +void Configuration::writeFile() +{ + File file = SPIFFS.open(mFilePath, "w"); + if(!file) + { + Serial.println("Failed to open file for writing..."); + return; + } + serializeJson(mData, file); + serializeJson(mData, Serial); + Serial.println(); + file.close(); +} diff --git a/src/configuration.h b/src/configuration.h new file mode 100644 index 0000000..40e3620 --- /dev/null +++ b/src/configuration.h @@ -0,0 +1,45 @@ +#ifndef CONFIGURATION_H_ +#define CONFIGURATION_H_ + +#include +#include + +class Configuration +{ +public: + Configuration(String FilePath); + + void readFile(); + void writeFile(); + + String getWifiName() const; + void setWifiName(String WifiName); + String getWifiPassword() const; + void setWifiPassword(String WifiPassword); + + String getIsCall() const; + void setIsCall(String IsCall); + String getIsPassword() const; + void setIsPassword(String IsPassword); + String getIsServer() const; + void setIsServer(String IsServer); + int getIsPort() const; + void setIsPort(int IsPort); + + String getBeaconMessage() const; + void setBeaconMessage(String BeaconMessage); + String getBeaconPosLat() const; + void setBeaconPosLat(String BeaconPosLat); + String getBeaconPosLong() const; + void setBeaconPosLong(String BeaconPosLong); + int getBeaconTimeout() const; + void setBeaconTimeout(int BeaconTimeout); + + +private: + const String mFilePath; + + DynamicJsonDocument mData; +}; + +#endif diff --git a/src/settings.h b/src/settings.h index 0d685be..ec974ae 100644 --- a/src/settings.h +++ b/src/settings.h @@ -2,16 +2,24 @@ #ifndef SETTINGS_H_ #define SETTINGS_H_ +/////////////////////////////////////////////////// +/////// DON'T CHANGE THIS FILE ANYMORE! /////// +/////////////////////////////////////////////////// +/////// Use the json file in the folder /////// +/////// 'data' to configure your iGate /////// +/////// and upload it via 'Upload File /////// +/////// System image'! These settings /////// +/////// will be removed soon! /////// +/////////////////////////////////////////////////// + + + + #define WIFI_NAME "" #define WIFI_KEY "" #define USER "NOCALL-10" #define PASS "" -#define TOOL "ESP32-APRS-IS" -#define VERS "0.1" -// if FILTER is NOT active: it will use no filter on the IS server: will display just LoRa received messages as there are no messages coming from the server. -// if FILTER is active: it will be used on login on IS server and will display received messages from server. More Info: http://www.aprs-is.net/javAPRSFilter.aspx -//#define FILTER "r/48.29/14.29/25" #define SERVER "austria.aprs2.net" //#define SERVER "euro.aprs2.net"