new LoRa APRS class -> NOT TESTED!

This commit is contained in:
Peter Buchegger 2020-06-04 22:27:44 +02:00
parent 8a4ec5061b
commit 8128b94619
4 changed files with 142 additions and 48 deletions

82
src/LoRa_APRS.cpp Normal file
View file

@ -0,0 +1,82 @@
#include <LoRa_APRS.h>
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<APRSMessage>(new APRSMessage());
_LastReceivedMsg->decode(str);
return true;
}
std::shared_ptr<APRSMessage> 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<APRSMessage> 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);
}

39
src/LoRa_APRS.h Normal file
View file

@ -0,0 +1,39 @@
#ifndef LORA_H_
#define LORA_H_
#include <memory>
#include <Arduino.h>
#include <LoRa.h>
#include <APRS-Decoder.h>
#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<APRSMessage> getMessage();
int getMessageRssi();
float getMessageSnr();
void sendMessage(const std::shared_ptr<APRSMessage> msg);
// settings:
long rx_frequency;
long tx_frequency;
int spreadingfactor;
long signalbandwidth;
int codingrate4;
private:
std::shared_ptr<APRSMessage> _LastReceivedMsg;
};
#endif

View file

@ -3,9 +3,9 @@
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <LoRa.h>
#include <APRS-IS.h>
#include <APRS-Decoder.h>
#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<APRSMessage> msg = std::shared_ptr<APRSMessage>(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<APRSMessage> 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);
}

View file

@ -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