diff --git a/LoRa_APRS_iGate/LoRa_APRS_iGate.ino b/LoRa_APRS_iGate/LoRa_APRS_iGate.ino index 787fc6a..5a92f6b 100644 --- a/LoRa_APRS_iGate/LoRa_APRS_iGate.ino +++ b/LoRa_APRS_iGate/LoRa_APRS_iGate.ino @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -17,6 +18,11 @@ APRS_IS aprs_is(USER, PASS, TOOL, VERS); int next_update = -1; +void setup_wifi(); +void setup_ota(); +void setup_lora(); +void setup_ntp(); + void setup() { Serial.begin(115200); @@ -26,79 +32,11 @@ void setup() Serial.println("[INFO] LoRa APRS iGate by OE5BPA (Peter Buchegger)"); show_display("OE5BPA", "LoRa APRS iGate", "by Peter Buchegger", 2000); - WiFi.setHostname("LoRa_APRS_iGate"); - 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); - - ArduinoOTA - .onStart([]() - { - String type; - if (ArduinoOTA.getCommand() == U_FLASH) - type = "sketch"; - else // U_SPIFFS - type = "filesystem"; - Serial.println("Start updating " + type); - }) - .onEnd([]() - { - Serial.println("\nEnd"); - }) - .onProgress([](unsigned int progress, unsigned int total) - { - Serial.printf("Progress: %u%%\r\n", (progress / (total / 100))); - }) - .onError([](ota_error_t error) { - Serial.printf("Error[%u]: ", error); - 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.begin(); - - Serial.println("[INFO] Set SPI pins!"); - SPI.begin(SCK, MISO, MOSI, SS); - LoRa.setPins(SS, RST, DIO0); - Serial.println("[INFO] Set LoRa pins!"); - - long freq = 433775000; - Serial.print("[INFO] frequency: "); - Serial.println(freq); - if (!LoRa.begin(freq)) { - Serial.println("[ERROR] Starting LoRa failed!"); - show_display("ERROR", "Starting LoRa failed!"); - while (1); - } - LoRa.setSpreadingFactor(12); - LoRa.setSignalBandwidth(125E3); - LoRa.setCodingRate4(5); - LoRa.enableCrc(); - Serial.println("[INFO] LoRa init done!"); - show_display("INFO", "LoRa init done!", 2000); - - 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); - + setup_wifi(); + setup_ota(); + setup_lora(); + setup_ntp(); + delay(500); } @@ -145,8 +83,7 @@ void loop() Serial.println(str); show_display(call, timeClient.getFormattedTime(), str, 0); } - int packetSize = LoRa.parsePacket(); - if(packetSize) + if(LoRa.parsePacket()) { String str; Serial.print("[" + timeClient.getFormattedTime() + "] "); @@ -170,3 +107,99 @@ void loop() show_display(call, timeClient.getFormattedTime(), "RSSI: " + String(LoRa.packetRssi()), "SNR: " + String(LoRa.packetSnr()), str, 0); } } + +void setup_wifi() +{ + char hostname[] = "LoRaAPRSiGate"; + WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE); + WiFi.setHostname(hostname); + 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); + + MDNS.begin(hostname); +} + +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.begin(); +} + +void setup_lora() +{ + Serial.println("[INFO] Set SPI pins!"); + SPI.begin(SCK, MISO, MOSI, SS); + LoRa.setPins(SS, RST, DIO0); + Serial.println("[INFO] Set LoRa pins!"); + + long freq = 433775000; + Serial.print("[INFO] frequency: "); + Serial.println(freq); + if (!LoRa.begin(freq)) { + Serial.println("[ERROR] Starting LoRa failed!"); + show_display("ERROR", "Starting LoRa failed!"); + while (1); + } + LoRa.setSpreadingFactor(12); + LoRa.setSignalBandwidth(125E3); + LoRa.setCodingRate4(5); + 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); +}