From ed48819ae8b0f697f7a69ab8f561413e15245360 Mon Sep 17 00:00:00 2001 From: sh123 Date: Thu, 25 Apr 2019 09:17:02 +0300 Subject: [PATCH] Added arduino sketch and loraprs modules --- esp32_loraprs.ino | 39 ++++++++++++++ loraprs.cpp | 126 ++++++++++++++++++++++++++++++++++++++++++++++ loraprs.h | 54 ++++++++++++++++++++ 3 files changed, 219 insertions(+) create mode 100644 esp32_loraprs.ino create mode 100644 loraprs.cpp create mode 100644 loraprs.h diff --git a/esp32_loraprs.ino b/esp32_loraprs.ino new file mode 100644 index 0000000..6d52dad --- /dev/null +++ b/esp32_loraprs.ino @@ -0,0 +1,39 @@ +#include "WiFi.h" +#include "loraprs.h" + +#define LORAPRS_CLIENT + +#define LORAPRS_FREQ 432.5E6 + +#ifdef LORAPRS_CLIENT +#define LORAPRS_BT_NAME "loraprs_client" +#define LORAPRS_WIFI_SSID "" +#define LORAPRS_WIFI_KEY "" +#define LORAPRS_LOGIN "NOCALL-0" +#define LORAPRS_PASS "00000" +#else +#define LORAPRS_BT_NAME "loraprs_server" +#define LORAPRS_WIFI_SSID "" +#define LORAPRS_WIFI_KEY "" +#define LORAPRS_LOGIN "NOCALL-0" +#define LORAPRS_PASS "00000" +#endif + +LoraPrs loraPrs( + LORAPRS_FREQ, + LORAPRS_BT_NAME, + LORAPRS_WIFI_SSID, + LORAPRS_WIFI_KEY, + LORAPRS_LOGIN, + LORAPRS_PASS); + +void setup() { + Serial.begin(115200); + while (!Serial); + + loraPrs.setup(); +} + +void loop() { + loraPrs.loop(); +} diff --git a/loraprs.cpp b/loraprs.cpp new file mode 100644 index 0000000..d071726 --- /dev/null +++ b/loraprs.cpp @@ -0,0 +1,126 @@ +#include "loraprs.h" + +LoraPrs::LoraPrs(int loraFreq, String btName, String wifiName, + String wifiKey, String aprsLoginCallsign, String aprsPass) + : serialBt_() + , loraFreq_(loraFreq) + , btName_(btName) + , wifiName_(wifiName) + , wifiKey_(wifiKey) +{ + aprsLogin_ = ""; + aprsLogin_ += "user "; + aprsLogin_ += aprsLoginCallsign; + aprsLogin_ += " pass "; + aprsLogin_ += aprsPass; + aprsLogin_ += " vers "; + aprsLogin_ += CfgLoraprsVersion; + aprsLogin_ += "\n"; +} + +void LoraPrs::setup() +{ + setupWifi(wifiName_, wifiKey_); + setupLora(loraFreq_); + setupBt(btName_); +} + +void LoraPrs::setupWifi(String wifiName, String wifiKey) +{ + if (wifiName.length() != 0) { + Serial.print("WIFI connecting to " + wifiName); + + WiFi.begin(wifiName.c_str(), wifiKey.c_str()); + + // TODO, add timeout + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + Serial.println("ok"); + Serial.println(WiFi.localIP()); + } +} + +void LoraPrs::setupLora(int loraFreq) +{ + Serial.print("LoRa init..."); + + LoRa.setPins(CfgPinSs, CfgPinRst, CfgPinDio0); + + while (!LoRa.begin(loraFreq)) { + Serial.print("."); + delay(500); + } + LoRa.setSyncWord(CfgSync); + LoRa.setSpreadingFactor(CfgSpread); + LoRa.setSignalBandwidth(CfgBw); + LoRa.setTxPower(CfgPower); + + Serial.println("ok"); +} + +void LoraPrs::setupBt(String btName) +{ + Serial.print("BT init " + btName + "..."); + + if (serialBt_.begin(btName)) { + Serial.println("ok"); + } + else + { + Serial.println("failed"); + } +} + +void LoraPrs::loop() +{ + if (serialBt_.available()) { + onBtReceived(); + } + if (LoRa.parsePacket()) { + onLoraReceived(); + } +} + +void LoraPrs::onAprsReceived(String aprsMessage) +{ + Serial.print(aprsMessage); + + if (WiFi.status() == WL_CONNECTED) { + WiFiClient wifiClient; + + if (!wifiClient.connect(CfgAprsHost.c_str(), CfgAprsPort)) { + Serial.println("Failed to connect to " + CfgAprsHost + ":" + CfgAprsPort); + return; + } + wifiClient.print(aprsLogin_); + wifiClient.print(aprsMessage); + wifiClient.stop(); + } +} + +void LoraPrs::onLoraReceived() +{ + String buf; + while (LoRa.available()) { + char c = (char)LoRa.read(); + if (c != '\n') + buf += c; + } + for (int i; i < buf.length(); i++) { + serialBt_.write((uint8_t)buf[i]); + } + onAprsReceived(buf + " " + String(LoRa.packetSnr()) + "dB\n"); + delay(50); +} + +void LoraPrs::onBtReceived() +{ + LoRa.beginPacket(); + while (serialBt_.available()) { + char c = (char)serialBt_.read(); + LoRa.print(c); + } + LoRa.endPacket(); +} diff --git a/loraprs.h b/loraprs.h new file mode 100644 index 0000000..e878c4a --- /dev/null +++ b/loraprs.h @@ -0,0 +1,54 @@ +#ifndef LORAPRS_H +#define LORAPRS_H + +#include +#include +#include +#include + +#include "BluetoothSerial.h" + +class LoraPrs +{ +public: + const String CfgLoraprsVersion = "LoRAPRS 0.1"; + + const byte CfgPinSs = 5; + const byte CfgPinRst = 26; + const byte CfgPinDio0 = 14; + + const int CfgBw = 20e3; + const byte CfgSpread = 9; + const byte CfgSync = 0xf3; + const byte CfgPower = 20; + + const int CfgAprsPort = 14580; + const String CfgAprsHost = "rotate.aprs2.net"; + +public: + LoraPrs(int freq, String btName, String wifiName, + String wifiKey, String aprsLoginCallsign, String aprsPass); + + void setup(); + void loop(); + +private: + void setupWifi(String wifiName, String wifiKey); + void setupLora(int loraFreq); + void setupBt(String btName); + + void onLoraReceived(); + void onBtReceived(); + void onAprsReceived(String aprsMessage); + +private: + BluetoothSerial serialBt_; + + int loraFreq_; + String btName_; + String wifiName_; + String wifiKey_; + String aprsLogin_; +}; + +#endif // LORAPRS_H