diff --git a/src/LoRa_APRS.cpp b/src/LoRa_APRS.cpp new file mode 100644 index 0000000..12918bb --- /dev/null +++ b/src/LoRa_APRS.cpp @@ -0,0 +1,82 @@ +#include + +LoRa_APRS::LoRa_APRS() + : rx_frequency(LORA_RX_FREQUENCY), tx_frequency(LORA_TX_FREQUENCY), spreadingfactor(LORA_SPREADING_FACTOR), + signalbandwidth(LORA_SIGNAL_BANDWIDTH), codingrate4(LORA_CODING_RATE4), _LastReceivedMsg(0) +{ + SPI.begin(LORA_SCK, LORA_MISO, LORA_MOSI, LORA_CS); + LoRa.setPins(LORA_CS, LORA_RST, LORA_IRQ); +} + +bool LoRa_APRS::begin() +{ + if (!LoRa.begin(rx_frequency)) + { + return false; + } + LoRa.setSpreadingFactor(spreadingfactor); + LoRa.setSignalBandwidth(signalbandwidth); + LoRa.setCodingRate4(codingrate4); + LoRa.enableCrc(); + return true; +} + +bool LoRa_APRS::hasMessage() +{ + if(!LoRa.parsePacket()) + { + return false; + } + // read header: + char dummy[4]; + LoRa.readBytes(dummy, 3); + if(dummy[0] != '<') + { + // is no APRS message, ignore message + while(LoRa.available()) + { + LoRa.read(); + } + return false; + } + // read APRS data: + String str; + while(LoRa.available()) + { + str += (char)LoRa.read(); + } + _LastReceivedMsg = std::shared_ptr(new APRSMessage()); + _LastReceivedMsg->decode(str); + return true; +} + +std::shared_ptr LoRa_APRS::getMessage() +{ + return _LastReceivedMsg; +} + +int LoRa_APRS::getMessageRssi() +{ + return LoRa.packetRssi(); +} + +float LoRa_APRS::getMessageSnr() +{ + return LoRa.packetSnr(); +} + +// cppcheck-suppress unusedFunction +void LoRa_APRS::sendMessage(const std::shared_ptr msg) +{ + LoRa.setFrequency(tx_frequency); + String data = msg->encode(); + LoRa.beginPacket(); + // Header: + LoRa.write('<'); + LoRa.write(0xFF); + LoRa.write(0x01); + // APRS Data: + LoRa.write((const uint8_t *)data.c_str(), data.length()); + LoRa.endPacket(); + LoRa.setFrequency(rx_frequency); +} diff --git a/src/LoRa_APRS.h b/src/LoRa_APRS.h new file mode 100644 index 0000000..b8ef880 --- /dev/null +++ b/src/LoRa_APRS.h @@ -0,0 +1,39 @@ +#ifndef LORA_H_ +#define LORA_H_ + +#include +#include +#include +#include + +#define LORA_RX_FREQUENCY (433775000) +#define LORA_TX_FREQUENCY (433900000) +#define LORA_SPREADING_FACTOR (12) +#define LORA_SIGNAL_BANDWIDTH (125E3) +#define LORA_CODING_RATE4 (5) + +class LoRa_APRS +{ +public: + LoRa_APRS(); + bool begin(); + + bool hasMessage(); + std::shared_ptr getMessage(); + int getMessageRssi(); + float getMessageSnr(); + + void sendMessage(const std::shared_ptr msg); + + // settings: + long rx_frequency; + long tx_frequency; + int spreadingfactor; + long signalbandwidth; + int codingrate4; + +private: + std::shared_ptr _LastReceivedMsg; +}; + +#endif diff --git a/src/LoRa_APRS_iGate.cpp b/src/LoRa_APRS_iGate.cpp index e77976c..9b5c116 100644 --- a/src/LoRa_APRS_iGate.cpp +++ b/src/LoRa_APRS_iGate.cpp @@ -3,9 +3,9 @@ #include #include #include -#include #include -#include + +#include "LoRa_APRS.h" #include "settings.h" #include "display.h" @@ -18,6 +18,7 @@ APRS_IS aprs_is(USER, PASS, TOOL, VERS); #if defined(ARDUINO_T_Beam) && !defined(ARDUINO_T_Beam_V0_7) PowerManagement powerManagement; #endif +LoRa_APRS lora_aprs; int next_update = -1; @@ -113,43 +114,27 @@ void loop() 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); + lora_aprs.sendMessage(msg); #endif } - if(LoRa.parsePacket()) + if(lora_aprs.hasMessage()) { - // 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: - 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); + 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); Serial.print("[" + timeClient.getFormattedTime() + "] "); Serial.print(" Received packet '"); - Serial.print(str); + Serial.print(msg->toString()); Serial.print("' with RSSI "); - Serial.print(LoRa.packetRssi()); + Serial.print(lora_aprs.getMessageRssi()); Serial.print(" and SNR "); - Serial.println(LoRa.packetSnr()); - - /*APRSMessage msg; - msg.decode(str); - Serial.print("[INFO] "); - Serial.println(msg.toString());*/ - aprs_is.sendMessage(str); + Serial.println(lora_aprs.getMessageSnr()); + aprs_is.sendMessage(msg->encode()); } } @@ -214,22 +199,15 @@ void setup_ota() void setup_lora() { - Serial.println("[INFO] Set SPI pins!"); - SPI.begin(LORA_SCK, LORA_MISO, LORA_MOSI, LORA_CS); - LoRa.setPins(LORA_CS, LORA_RST, LORA_IRQ); - Serial.println("[INFO] Set LoRa pins!"); - Serial.print("[INFO] frequency: "); - Serial.println(LORA_FREQUENCY); - if (!LoRa.begin(LORA_FREQUENCY)) { + Serial.println(LORA_RX_FREQUENCY); + + if (!lora_aprs.begin()) + { 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); - LoRa.enableCrc(); Serial.println("[INFO] LoRa init done!"); show_display("INFO", "LoRa init done!", 2000); } diff --git a/src/settings.h b/src/settings.h index 83d3fe5..0d685be 100644 --- a/src/settings.h +++ b/src/settings.h @@ -22,11 +22,6 @@ #define BEACON_LONG_POS "01418.68E" #define BEACON_MESSAGE "LoRa IGATE (RX only), Info: github.com/peterus/LoRa_APRS_iGate" -#define LORA_FREQUENCY (433775000) -#define LORA_SPREADING_FACTOR (12) -#define LORA_SIGNAL_BANDWIDTH (125E3) -#define LORA_CODING_RATE4 (5) - -//#define ARDUINO_T_Beam_V0_7 +//#define SEND_MESSAGES_FROM_IS_TO_LORA #endif