LoRa_APRS_iGate/src/lora_utils.cpp

282 lines
11 KiB
C++
Raw Normal View History

2025-07-15 16:28:23 -04:00
/* Copyright (C) 2025 Ricardo Guzman - CA2RXU
*
* This file is part of LoRa APRS iGate.
*
* LoRa APRS iGate is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LoRa APRS iGate is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LoRa APRS iGate. If not, see <https://www.gnu.org/licenses/>.
*/
2023-12-26 16:37:54 -03:00
#include <RadioLib.h>
2023-11-26 14:30:57 -03:00
#include <WiFi.h>
2023-06-06 11:21:59 -04:00
#include "configuration.h"
2023-10-08 09:39:44 -03:00
#include "aprs_is_utils.h"
2025-03-03 11:19:25 -03:00
#include "station_utils.h"
2024-11-05 14:41:41 -03:00
#include "board_pinout.h"
#include "syslog_utils.h"
2024-10-14 11:44:22 -03:00
#include "ntp_utils.h"
2023-06-04 10:10:39 -04:00
#include "display.h"
2024-03-17 12:21:11 +01:00
#include "utils.h"
2023-06-04 10:10:39 -04:00
2025-03-10 03:02:48 -03:00
2024-04-20 09:37:22 -04:00
extern Configuration Config;
extern uint32_t lastRxTime;
2025-10-11 18:27:06 -03:00
extern bool packetIsBeacon;
2024-04-20 09:37:22 -04:00
2024-04-13 18:20:10 +02:00
extern std::vector<ReceivedPacket> receivedPackets;
2024-05-17 15:11:59 -04:00
bool operationDone = true;
bool transmitFlag = true;
#ifdef HAS_SX1262
2024-05-11 12:59:07 -04:00
SX1262 radio = new Module(RADIO_CS_PIN, RADIO_DIO1_PIN, RADIO_RST_PIN, RADIO_BUSY_PIN);
2023-12-26 16:37:54 -03:00
#endif
#ifdef HAS_SX1268
2025-05-17 10:00:53 -04:00
#if defined(LIGHTGATEWAY_1_0) || defined(LIGHTGATEWAY_PLUS_1_0)
2024-10-25 10:42:15 -03:00
SPIClass loraSPI(FSPI);
SX1268 radio = new Module(RADIO_CS_PIN, RADIO_DIO1_PIN, RADIO_RST_PIN, RADIO_BUSY_PIN, loraSPI);
#else
SX1268 radio = new Module(RADIO_CS_PIN, RADIO_DIO1_PIN, RADIO_RST_PIN, RADIO_BUSY_PIN);
#endif
#endif
#ifdef HAS_SX1278
2024-05-11 12:59:07 -04:00
SX1278 radio = new Module(RADIO_CS_PIN, RADIO_BUSY_PIN, RADIO_RST_PIN);
2024-01-02 22:41:54 -03:00
#endif
2024-04-23 22:25:20 -04:00
#ifdef HAS_SX1276
2024-05-11 12:59:07 -04:00
SX1276 radio = new Module(RADIO_CS_PIN, RADIO_BUSY_PIN, RADIO_RST_PIN);
2024-04-23 22:25:20 -04:00
#endif
2024-09-23 12:37:35 -03:00
#if defined(HAS_LLCC68) //LLCC68 supports spreading factor only in range of 5-11!
LLCC68 radio = new Module(RADIO_CS_PIN, RADIO_DIO1_PIN, RADIO_RST_PIN, RADIO_BUSY_PIN);
#endif
2024-04-23 22:25:20 -04:00
2023-06-19 19:44:55 -04:00
int rssi, freqError;
float snr;
2024-11-06 12:47:42 -03:00
2023-06-07 17:25:50 -04:00
namespace LoRa_Utils {
2023-06-04 10:10:39 -04:00
2024-02-24 14:09:05 +01:00
void setFlag(void) {
2024-05-16 23:22:27 -04:00
operationDone = true;
2023-12-26 20:47:59 -03:00
}
2023-06-04 10:10:39 -04:00
2024-02-24 14:09:05 +01:00
void setup() {
2025-05-17 10:00:53 -04:00
#if defined (LIGHTGATEWAY_1_0) || defined(LIGHTGATEWAY_PLUS_1_0)
2024-10-25 10:42:15 -03:00
pinMode(RADIO_VCC_PIN,OUTPUT);
digitalWrite(RADIO_VCC_PIN,HIGH);
loraSPI.begin(RADIO_SCLK_PIN, RADIO_MISO_PIN, RADIO_MOSI_PIN, RADIO_CS_PIN);
#else
SPI.begin(RADIO_SCLK_PIN, RADIO_MISO_PIN, RADIO_MOSI_PIN);
#endif
2024-03-17 12:21:11 +01:00
float freq = (float)Config.loramodule.rxFreq / 1000000;
2024-07-18 14:41:09 -04:00
#if defined(RADIO_HAS_XTAL)
radio.XTAL = true;
#endif
2024-02-24 14:09:05 +01:00
int state = radio.begin(freq);
if (state == RADIOLIB_ERR_NONE) {
Utils::println("Initializing LoRa Module");
} else {
2024-07-18 14:41:09 -04:00
Utils::println("Starting LoRa failed! State: " + String(state));
2024-03-17 12:21:11 +01:00
while (true);
2024-02-24 14:09:05 +01:00
}
2024-09-23 12:37:35 -03:00
#if defined(HAS_SX1262) || defined(HAS_SX1268) || defined(HAS_LLCC68)
2025-04-24 11:30:03 -04:00
radio.setDio1Action(setFlag);
#endif
2024-05-16 17:52:25 -04:00
#if defined(HAS_SX1278) || defined(HAS_SX1276)
radio.setDio0Action(setFlag, RISING);
#endif
2025-12-01 10:45:14 -03:00
/*#ifdef SX126X_DIO3_TCXO_VOLTAGE
if (radio.setTCXO(float(SX126X_DIO3_TCXO_VOLTAGE)) == RADIOLIB_ERR_NONE) {
Utils::println("Set LoRa Module TCXO Voltage to:" + String(SX126X_DIO3_TCXO_VOLTAGE));
} else {
Utils::println("Set LoRa Module TCXO Voltage failed! State: " + String(state));
while (true);
}
#endif*/
2025-10-13 01:33:41 -03:00
radio.setSpreadingFactor(Config.loramodule.rxSpreadingFactor);
radio.setCodingRate(Config.loramodule.rxCodingRate4);
float signalBandwidth = Config.loramodule.rxSignalBandwidth/1000;
radio.setBandwidth(signalBandwidth);
2024-02-25 12:55:38 -03:00
radio.setCRC(true);
2025-05-17 10:00:53 -04:00
#if (defined(RADIO_RXEN) && defined(RADIO_TXEN)) // QRP Labs LightGateway has 400M22S (SX1268)
2024-05-11 12:59:07 -04:00
radio.setRfSwitchPins(RADIO_RXEN, RADIO_TXEN);
2024-06-10 14:09:30 -04:00
#endif
2025-12-01 10:45:14 -03:00
/*#ifdef SX126X_DIO2_AS_RF_SWITCH
radio.setRfSwitchPins(RADIO_RXEN, RADIOLIB_NC);
radio.setDio2AsRfSwitch(true);
#endif*/
2024-09-23 12:37:35 -03:00
#ifdef HAS_1W_LORA // Ebyte E22 400M30S (SX1268) / 900M30S (SX1262) / Ebyte E220 400M30S (LLCC68)
2024-06-18 19:30:34 -04:00
state = radio.setOutputPower(Config.loramodule.power); // max value 20dB for 1W modules as they have Low Noise Amp
2024-05-17 15:11:59 -04:00
radio.setCurrentLimit(140); // to be validated (100 , 120, 140)?
#endif
2024-05-17 15:11:59 -04:00
#if defined(HAS_SX1278) || defined(HAS_SX1276)
2024-05-11 12:59:07 -04:00
state = radio.setOutputPower(Config.loramodule.power); // max value 20dB for 400M30S as it has Low Noise Amp
2024-05-17 15:11:59 -04:00
radio.setCurrentLimit(100); // to be validated (80 , 100)?
#endif
2024-06-18 19:30:34 -04:00
#if (defined(HAS_SX1268) || defined(HAS_SX1262)) && !defined(HAS_1W_LORA)
2024-05-11 12:59:07 -04: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.
2024-05-17 15:11:59 -04:00
radio.setCurrentLimit(140);
#endif
2024-05-16 17:52:25 -04:00
2024-09-23 12:37:35 -03:00
#if defined(HAS_SX1262) || defined(HAS_SX1268) || defined(HAS_LLCC68)
2024-05-11 12:59:07 -04:00
radio.setRxBoostedGainMode(true);
2024-05-10 23:18:27 -04:00
#endif
2024-05-16 17:52:25 -04:00
2025-12-12 12:24:53 -03:00
#if defined(HAS_TCXO) && !defined(HAS_1W_LORA)
radio.setDio2AsRfSwitch();
#endif
2025-12-12 12:25:41 -03:00
#ifdef HAS_TCXO
2025-12-12 12:24:53 -03:00
radio.setTCXO(1.8);
#endif
2024-02-24 14:09:05 +01:00
if (state == RADIOLIB_ERR_NONE) {
Utils::println("init : LoRa Module ... done!");
2024-03-23 23:36:18 -03:00
} else {
2024-08-06 09:12:01 -04:00
Utils::println("Starting LoRa failed! State: " + String(state));
2024-02-24 14:09:05 +01:00
while (true);
}
2023-12-26 20:47:59 -03:00
}
2023-06-06 14:37:47 -04:00
2024-03-07 17:46:38 +01:00
void changeFreqTx() {
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);
2025-10-13 01:33:41 -03:00
radio.setSpreadingFactor(Config.loramodule.txSpreadingFactor);
radio.setCodingRate(Config.loramodule.txCodingRate4);
radio.setBandwidth(Config.loramodule.txSignalBandwidth);
2024-03-07 17:46:38 +01:00
}
void changeFreqRx() {
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);
2025-10-13 01:33:41 -03:00
radio.setSpreadingFactor(Config.loramodule.rxSpreadingFactor);
radio.setCodingRate(Config.loramodule.rxCodingRate4);
radio.setBandwidth(Config.loramodule.rxSignalBandwidth);
2024-03-07 17:46:38 +01:00
}
2024-03-17 12:21:11 +01:00
void sendNewPacket(const String& newPacket) {
2024-05-17 09:01:15 -04:00
if (!Config.loramodule.txActive) return;
2024-03-07 17:46:38 +01:00
2024-05-17 09:01:15 -04:00
if (Config.loramodule.txFreq != Config.loramodule.rxFreq) {
2025-10-15 14:18:43 -03:00
if (!packetIsBeacon || (packetIsBeacon && Config.beacon.beaconFreq == 1)) {
2025-10-11 18:27:06 -03:00
changeFreqTx();
}
2024-05-17 09:01:15 -04:00
}
2024-10-08 01:03:58 -03:00
2024-05-17 09:01:15 -04:00
#ifdef INTERNAL_LED_PIN
2025-04-24 16:32:58 -04:00
if (Config.digi.ecoMode != 1) digitalWrite(INTERNAL_LED_PIN, HIGH); // disabled in Ultra Eco Mode
2024-05-17 09:01:15 -04:00
#endif
int state = radio.transmit("\x3c\xff\x01" + newPacket);
transmitFlag = true;
if (state == RADIOLIB_ERR_NONE) {
if (Config.syslog.active && WiFi.status() == WL_CONNECTED) {
2024-05-30 15:12:34 -04:00
SYSLOG_Utils::log(3, newPacket, 0, 0.0, 0); // TX
2024-05-16 23:22:27 -04:00
}
2025-02-25 13:54:36 -03:00
Utils::print("---> LoRa Packet Tx : ");
2024-05-17 09:01:15 -04:00
Utils::println(newPacket);
} else {
Utils::print(F("failed, code "));
Utils::println(String(state));
}
#ifdef INTERNAL_LED_PIN
2025-04-24 16:32:58 -04:00
if (Config.digi.ecoMode != 1) digitalWrite(INTERNAL_LED_PIN, LOW); // disabled in Ultra Eco Mode
2024-05-17 09:01:15 -04:00
#endif
if (Config.loramodule.txFreq != Config.loramodule.rxFreq) {
2025-10-15 14:18:43 -03:00
if (!packetIsBeacon || (packetIsBeacon && Config.beacon.beaconFreq == 1)) {
2025-10-11 18:27:06 -03:00
changeFreqRx();
}
2024-05-17 09:01:15 -04:00
}
2024-01-25 22:35:15 -03:00
}
2025-04-24 10:36:08 -04:00
String receivePacketFromSleep() {
2025-04-24 09:44:57 -04:00
String packet = "";
int state = radio.readData(packet);
if (state == RADIOLIB_ERR_NONE) {
2025-04-24 13:26:34 -04:00
Utils::println("<--- LoRa Packet Rx : " + packet.substring(3));
2025-04-24 09:44:57 -04:00
} else {
packet = "";
}
return packet;
2025-04-24 10:36:08 -04:00
}
2024-03-28 18:00:46 +01:00
2024-02-24 14:09:05 +01:00
String receivePacket() {
2024-05-16 17:52:25 -04:00
String packet = "";
2025-04-24 10:36:08 -04:00
if (operationDone) {
2024-05-16 23:22:27 -04:00
operationDone = false;
2025-04-24 10:36:08 -04:00
if (transmitFlag) {
2024-05-16 23:22:27 -04:00
radio.startReceive();
transmitFlag = false;
} else {
int state = radio.readData(packet);
if (state == RADIOLIB_ERR_NONE) {
if (packet != "") {
2025-03-03 11:19:25 -03:00
String sender = packet.substring(3, packet.indexOf(">"));
2025-11-29 14:11:43 -03:00
if (packet.substring(0,3) == "\x3c\xff\x01" && !STATION_Utils::isBlacklisted(sender)) { // avoid processing BlackListed stations
2025-03-03 11:19:25 -03:00
rssi = radio.getRSSI();
snr = radio.getSNR();
freqError = radio.getFrequencyError();
Utils::println("<--- LoRa Packet Rx : " + packet.substring(3));
Utils::println("(RSSI:" + String(rssi) + " / SNR:" + String(snr) + " / FreqErr:" + String(freqError) + ")");
2025-04-24 10:36:08 -04:00
if (Config.digi.ecoMode == 0) {
2025-03-03 11:19:25 -03:00
if (receivedPackets.size() >= 10) {
receivedPackets.erase(receivedPackets.begin());
}
ReceivedPacket receivedPacket;
receivedPacket.rxTime = NTP_Utils::getFormatedTime();
receivedPacket.packet = packet.substring(3);
receivedPacket.RSSI = rssi;
receivedPacket.SNR = snr;
receivedPackets.push_back(receivedPacket);
}
if (Config.syslog.active && WiFi.status() == WL_CONNECTED) {
SYSLOG_Utils::log(1, packet, rssi, snr, freqError); // RX
2024-10-14 12:49:01 -03:00
}
2025-03-03 11:19:25 -03:00
} else {
packet = "";
2025-05-18 08:44:46 -04:00
}
2024-05-16 23:22:27 -04:00
lastRxTime = millis();
return packet;
2024-11-06 12:47:42 -03:00
}
2024-05-16 23:22:27 -04:00
} else if (state == RADIOLIB_ERR_CRC_MISMATCH) {
2024-05-30 16:27:07 -04:00
rssi = radio.getRSSI();
snr = radio.getSNR();
freqError = radio.getFrequencyError();
2024-05-16 23:22:27 -04:00
Utils::println(F("CRC error!"));
2024-04-09 12:51:51 -04:00
if (Config.syslog.active && WiFi.status() == WL_CONNECTED) {
2024-05-16 23:22:27 -04:00
SYSLOG_Utils::log(0, packet, rssi, snr, freqError); // CRC
2024-03-23 09:48:12 -03:00
}
2024-05-16 23:22:27 -04:00
packet = "";
} else {
Utils::print(F("failed, code "));
Utils::println(String(state));
packet = "";
2024-03-23 09:48:12 -03:00
}
2024-02-24 14:09:05 +01:00
}
}
2024-05-16 17:52:25 -04:00
return packet;
2023-12-26 20:47:59 -03:00
}
2023-06-04 10:10:39 -04:00
2025-04-24 09:44:57 -04:00
void wakeRadio() {
radio.startReceive();
}
2024-05-29 15:43:23 -04:00
void sleepRadio() {
radio.sleep();
}
2023-06-04 10:10:39 -04:00
}