LoRa_APRS_iGate/src/lora_utils.cpp

201 lines
6.6 KiB
C++
Raw Normal View History

2023-12-26 20:37:54 +01:00
#include <RadioLib.h>
2023-11-26 18:30:57 +01:00
#include <WiFi.h>
2023-06-06 17:21:59 +02:00
#include "configuration.h"
2023-10-08 14:39:44 +02:00
#include "aprs_is_utils.h"
#include "syslog_utils.h"
2023-10-08 14:39:44 +02:00
#include "pins_config.h"
2023-06-04 16:10:39 +02:00
#include "display.h"
2024-03-17 12:21:11 +01:00
#include "utils.h"
2023-06-04 16:10:39 +02:00
2023-06-08 23:09:05 +02:00
extern Configuration Config;
2023-06-04 16:10:39 +02:00
2023-12-26 20:37:54 +01:00
bool transmissionFlag = true;
bool ignorePacket = false;
bool operationDone = true;
#ifdef HAS_SX1262
SX1262 radio = new Module(RADIO_CS_PIN, RADIO_DIO1_PIN, RADIO_RST_PIN, RADIO_BUSY_PIN);
2023-12-26 20:37:54 +01:00
#endif
#ifdef HAS_SX1268
2024-01-03 02:41:54 +01:00
SX1268 radio = new Module(RADIO_CS_PIN, RADIO_DIO1_PIN, RADIO_RST_PIN, RADIO_BUSY_PIN);
#endif
#ifdef HAS_SX1278
SX1278 radio = new Module(RADIO_CS_PIN, RADIO_BUSY_PIN, RADIO_RST_PIN);
2024-01-03 02:41:54 +01:00
#endif
2023-12-26 20:37:54 +01:00
2023-06-20 01:44:55 +02:00
int rssi, freqError;
float snr;
2023-06-07 23:25:50 +02:00
namespace LoRa_Utils {
2023-06-04 16:10:39 +02:00
2024-02-24 14:09:05 +01:00
void setFlag(void) {
operationDone = true;
2023-12-27 00:47:59 +01:00
}
2023-06-04 16:10:39 +02:00
2024-02-24 14:09:05 +01:00
void setup() {
SPI.begin(RADIO_SCLK_PIN, RADIO_MISO_PIN, RADIO_MOSI_PIN);
2024-03-17 12:21:11 +01:00
float freq = (float)Config.loramodule.rxFreq / 1000000;
2024-02-24 14:09:05 +01:00
int state = radio.begin(freq);
if (state == RADIOLIB_ERR_NONE) {
Utils::println("Initializing LoRa Module");
} else {
Utils::println("Starting LoRa failed!");
2024-03-17 12:21:11 +01:00
while (true);
2024-02-24 14:09:05 +01:00
}
#ifdef HAS_SX127X
radio.setDio0Action(setFlag, RISING);
#endif
#ifdef HAS_SX126X
2024-03-28 18:00:46 +01:00
if (!Config.lowPowerMode) {
radio.setDio1Action(setFlag);
} else {
radio.setDIOMapping(1, RADIOLIB_SX126X_IRQ_RX_DONE);
}
#endif
2024-02-24 14:09:05 +01:00
radio.setSpreadingFactor(Config.loramodule.spreadingFactor);
2024-03-20 02:41:07 +01:00
float signalBandwidth = Config.loramodule.signalBandwidth/1000;
radio.setBandwidth(signalBandwidth);
2024-02-24 14:09:05 +01:00
radio.setCodingRate(Config.loramodule.codingRate4);
2024-02-25 16:55:38 +01:00
radio.setCRC(true);
#if defined(ESP32_DIY_1W_LoRa)
2024-02-24 14:09:05 +01:00
radio.setRfSwitchPins(RADIO_RXEN, RADIO_TXEN);
#endif
#if defined(HAS_SX127X) || ESP32_DIY_1W_LoRa
state = radio.setOutputPower(Config.loramodule.power); // max value 20dB for 400M30S as it has Low Noise Amp
#endif
#if defined(HELTEC_V3) || defined(HELTEC_WS) || defined(TTGO_T_Beam_V1_0_SX1268) || defined(TTGO_T_Beam_V1_2_SX1262)
2024-02-24 14:09:05 +01:00
state = radio.setOutputPower(Config.loramodule.power + 2); // values available: 10, 17, 22 --> if 20 in tracker_conf.json it will be updated to 22.
#endif
2024-02-24 14:09:05 +01:00
if (state == RADIOLIB_ERR_NONE) {
Utils::println("init : LoRa Module ... done!");
2024-03-24 03:36:18 +01:00
} else {
Utils::println("Starting LoRa failed!");
2024-02-24 14:09:05 +01:00
while (true);
}
2023-12-27 00:47:59 +01:00
}
2023-06-06 20:37:47 +02:00
2024-03-07 17:46:38 +01:00
void changeFreqTx() {
delay(500);
2024-03-17 12:21:11 +01:00
float freq = (float)Config.loramodule.txFreq / 1000000;
2024-03-07 17:46:38 +01:00
radio.setFrequency(freq);
}
void changeFreqRx() {
delay(500);
2024-03-17 12:21:11 +01:00
float freq = (float)Config.loramodule.rxFreq / 1000000;
2024-03-07 17:46:38 +01:00
radio.setFrequency(freq);
}
2024-03-17 12:21:11 +01:00
void sendNewPacket(const String& typeOfMessage, const String& newPacket) {
2024-03-07 17:46:38 +01:00
if (!Config.loramodule.txActive) return;
if (Config.loramodule.txFreq != Config.loramodule.rxFreq) {
2024-03-17 12:21:11 +01:00
changeFreqTx();
2024-03-07 17:46:38 +01:00
}
2024-03-17 12:21:11 +01:00
#ifdef HAS_INTERNAL_LED
digitalWrite(internalLedPin, HIGH);
#endif
2024-02-24 14:09:05 +01:00
int state = radio.transmit("\x3c\xff\x01" + newPacket);
if (state == RADIOLIB_ERR_NONE) {
2024-03-23 13:48:12 +01:00
if (Config.syslog.active && WiFi.status() == WL_CONNECTED) {
SYSLOG_Utils::log("Tx", newPacket, 0, 0, 0);
}
Utils::print("---> LoRa Packet Tx : ");
Utils::println(newPacket);
2024-03-24 03:36:18 +01:00
} else if (state == RADIOLIB_ERR_PACKET_TOO_LONG) {
Utils::println(F("too long!"));
2024-03-24 03:36:18 +01:00
} else if (state == RADIOLIB_ERR_TX_TIMEOUT) {
Utils::println(F("timeout!"));
2024-03-24 03:36:18 +01:00
} else {
Utils::print(F("failed, code "));
Utils::println(String(state));
2024-02-24 14:09:05 +01:00
}
2024-03-17 12:21:11 +01:00
#ifdef HAS_INTERNAL_LED
digitalWrite(internalLedPin, LOW);
#endif
2024-03-07 17:46:38 +01:00
if (Config.loramodule.txFreq != Config.loramodule.rxFreq) {
changeFreqRx();
}
ignorePacket = true;
2024-01-26 02:35:15 +01:00
}
2024-02-24 14:09:05 +01:00
String generatePacket(String aprsisPacket) {
String firstPart, messagePart;
aprsisPacket.trim();
firstPart = aprsisPacket.substring(0, aprsisPacket.indexOf(","));
2024-03-17 12:21:11 +01:00
messagePart = aprsisPacket.substring(aprsisPacket.indexOf("::") + 2);
2024-02-24 14:09:05 +01:00
return firstPart + ",TCPIP,WIDE1-1," + Config.callsign + "::" + messagePart;
2023-12-26 20:37:54 +01:00
}
2024-02-24 14:09:05 +01:00
String packetSanitization(String packet) {
2024-03-17 12:21:11 +01:00
if (packet.indexOf("\0") > 0) {
packet.replace("\0", "");
2024-02-24 14:09:05 +01:00
}
2024-03-17 12:21:11 +01:00
if (packet.indexOf("\r") > 0) {
packet.replace("\r", "");
2024-02-24 14:09:05 +01:00
}
2024-03-17 12:21:11 +01:00
if (packet.indexOf("\n") > 0) {
packet.replace("\n", "");
2024-02-24 14:09:05 +01:00
}
return packet;
2024-01-03 03:05:07 +01:00
}
2024-02-24 14:09:05 +01:00
2024-03-28 18:00:46 +01:00
void startReceive() {
radio.startReceive();
}
2024-02-24 14:09:05 +01:00
String receivePacket() {
if(!operationDone && !Config.lowPowerMode) return "";
operationDone = false;
2024-02-24 14:09:05 +01:00
String loraPacket = "";
2024-03-23 13:48:12 +01:00
if (transmissionFlag && !Config.lowPowerMode) {
2024-02-24 14:09:05 +01:00
radio.startReceive();
transmissionFlag = false;
} else {
2024-02-24 14:09:05 +01:00
int state = radio.readData(loraPacket);
if (state == RADIOLIB_ERR_NONE) {
if (loraPacket != "" && !ignorePacket) {
2024-03-23 13:48:12 +01:00
rssi = radio.getRSSI();
snr = radio.getSNR();
freqError = radio.getFrequencyError();
Utils::println("<--- LoRa Packet Rx : " + loraPacket);
Utils::println("(RSSI:" + String(rssi) + " / SNR:" + String(snr) + " / FreqErr:" + String(freqError) + ")");
if (Config.syslog.active && WiFi.status() == WL_CONNECTED && loraPacket != "") {
SYSLOG_Utils::log("Rx", loraPacket, rssi, snr, freqError);
}
return loraPacket;
}
2024-03-24 03:36:18 +01:00
} else if (state == RADIOLIB_ERR_RX_TIMEOUT) {
2024-02-24 14:09:05 +01:00
// timeout occurred while waiting for a packet
2024-03-24 03:36:18 +01:00
} else if (state == RADIOLIB_ERR_CRC_MISMATCH) {
Utils::println(F("CRC error!"));
2024-03-23 13:48:12 +01:00
if (Config.syslog.active && WiFi.status() == WL_CONNECTED) {
SYSLOG_Utils::log("Rx", "RADIOLIB_ERR_CRC_MISMATCH", 0,0,0);
}
2024-03-24 03:36:18 +01:00
} else {
Utils::print(F("failed, code "));
Utils::println(String(state));
}
if (ignorePacket) {
Utils::println("<--- LoRa Packet Rx : " + loraPacket);
Utils::println("Received own packet. Ignoring");
ignorePacket = false;
return "";
2024-02-24 14:09:05 +01:00
}
}
2024-02-24 14:09:05 +01:00
return loraPacket;
2023-12-27 00:47:59 +01:00
}
2023-06-04 16:10:39 +02:00
}