2023-12-26 20:37:54 +01:00
|
|
|
#include <RadioLib.h>
|
2023-06-04 16:10:39 +02:00
|
|
|
#include <LoRa.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"
|
2023-06-12 05:21:52 +02:00
|
|
|
#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"
|
|
|
|
|
|
2023-06-08 23:09:05 +02:00
|
|
|
extern Configuration Config;
|
|
|
|
|
extern int stationMode;
|
2023-06-04 16:10:39 +02:00
|
|
|
|
2023-12-26 20:37:54 +01:00
|
|
|
#ifdef HELTEC_V3
|
|
|
|
|
SX1262 radio = new Module(RADIO_CS_PIN, RADIO_DIO1_PIN, RADIO_RST_PIN, RADIO_BUSY_PIN);
|
|
|
|
|
bool transmissionFlag = true;
|
|
|
|
|
bool enableInterrupt = true;
|
|
|
|
|
#endif
|
2024-01-03 02:41:54 +01:00
|
|
|
#ifdef ESP32_DIY_1W_LoRa
|
|
|
|
|
SX1268 radio = new Module(RADIO_CS_PIN, RADIO_DIO1_PIN, RADIO_RST_PIN, RADIO_BUSY_PIN);
|
|
|
|
|
bool transmissionFlag = true;
|
|
|
|
|
bool enableInterrupt = true;
|
|
|
|
|
#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
|
|
|
|
2023-12-26 20:37:54 +01:00
|
|
|
void setFlag(void) {
|
2024-01-03 02:41:54 +01:00
|
|
|
#if defined(HELTEC_V3) || defined(ESP32_DIY_1W_LoRa)
|
2023-12-26 20:37:54 +01:00
|
|
|
transmissionFlag = true;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-08 14:39:44 +02:00
|
|
|
void setup() {
|
2024-01-03 02:18:46 +01:00
|
|
|
#if defined(TTGO_T_LORA_V2_1) || defined(HELTEC_V2) || defined(ESP32_DIY_LoRa)
|
2023-10-08 14:39:44 +02:00
|
|
|
SPI.begin(LORA_SCK, LORA_MISO, LORA_MOSI, LORA_CS);
|
|
|
|
|
LoRa.setPins(LORA_CS, LORA_RST, LORA_IRQ);
|
|
|
|
|
long freq;
|
2023-11-28 04:57:15 +01:00
|
|
|
if (stationMode==1 || stationMode==2) {
|
2023-10-08 14:39:44 +02:00
|
|
|
freq = Config.loramodule.iGateFreq;
|
|
|
|
|
} else {
|
|
|
|
|
freq = Config.loramodule.digirepeaterTxFreq;
|
2023-06-04 16:10:39 +02:00
|
|
|
}
|
2023-10-08 14:39:44 +02:00
|
|
|
if (!LoRa.begin(freq)) {
|
|
|
|
|
Serial.println("Starting LoRa failed!");
|
|
|
|
|
show_display("ERROR", "Starting LoRa failed!");
|
|
|
|
|
while (true) {
|
|
|
|
|
delay(1000);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
LoRa.setSpreadingFactor(Config.loramodule.spreadingFactor);
|
|
|
|
|
LoRa.setSignalBandwidth(Config.loramodule.signalBandwidth);
|
|
|
|
|
LoRa.setCodingRate4(Config.loramodule.codingRate4);
|
|
|
|
|
LoRa.enableCrc();
|
|
|
|
|
LoRa.setTxPower(Config.loramodule.power);
|
|
|
|
|
Serial.print("init : LoRa Module ... done!");
|
2023-12-26 20:37:54 +01:00
|
|
|
#endif
|
2024-01-03 02:41:54 +01:00
|
|
|
#if defined(HELTEC_V3) || defined(ESP32_DIY_1W_LoRa)
|
2023-12-27 00:47:59 +01:00
|
|
|
SPI.begin(RADIO_SCLK_PIN, RADIO_MISO_PIN, RADIO_MOSI_PIN);
|
|
|
|
|
float freq = (float)Config.loramodule.iGateFreq/1000000;
|
|
|
|
|
int state = radio.begin(freq);
|
|
|
|
|
if (state == RADIOLIB_ERR_NONE) {
|
|
|
|
|
Serial.print("Initializing SX126X LoRa Module");
|
|
|
|
|
} else {
|
|
|
|
|
Serial.println("Starting LoRa failed!");
|
|
|
|
|
while (true);
|
|
|
|
|
}
|
|
|
|
|
radio.setDio1Action(setFlag);
|
|
|
|
|
radio.setSpreadingFactor(Config.loramodule.spreadingFactor);
|
|
|
|
|
radio.setBandwidth(Config.loramodule.signalBandwidth);
|
|
|
|
|
radio.setCodingRate(Config.loramodule.codingRate4);
|
2024-01-03 02:41:54 +01:00
|
|
|
#ifdef HELTEC_V3
|
2023-12-27 00:47:59 +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.
|
2024-01-03 02:41:54 +01:00
|
|
|
#endif
|
|
|
|
|
#ifdef ESP32_DIY_1W_LoRa_GPS
|
|
|
|
|
state = radio.setOutputPower(Config.loramodule.power); // max value 20 (when 20dB in setup 30dB in output as 400M30S has Low Noise Amp)
|
|
|
|
|
#endif
|
2023-12-27 00:47:59 +01:00
|
|
|
if (state == RADIOLIB_ERR_NONE) {
|
|
|
|
|
Serial.println("init : LoRa Module ... done!");
|
|
|
|
|
} else {
|
|
|
|
|
Serial.println("Starting LoRa failed!");
|
|
|
|
|
while (true);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2023-06-04 16:10:39 +02:00
|
|
|
}
|
|
|
|
|
|
2023-10-08 14:39:44 +02:00
|
|
|
void sendNewPacket(const String &typeOfMessage, const String &newPacket) {
|
2024-01-03 02:12:10 +01:00
|
|
|
digitalWrite(internalLedPin,HIGH);
|
2024-01-03 02:18:46 +01:00
|
|
|
#if defined(TTGO_T_LORA_V2_1) || defined(HELTEC_V2) || defined(ESP32_DIY_LoRa)
|
2023-12-27 00:47:59 +01:00
|
|
|
LoRa.beginPacket();
|
|
|
|
|
LoRa.write('<');
|
|
|
|
|
if (typeOfMessage == "APRS") {
|
|
|
|
|
LoRa.write(0xFF);
|
|
|
|
|
} else if (typeOfMessage == "LoRa") {
|
|
|
|
|
LoRa.write(0xF8);
|
|
|
|
|
}
|
|
|
|
|
LoRa.write(0x01);
|
|
|
|
|
LoRa.write((const uint8_t *)newPacket.c_str(), newPacket.length());
|
|
|
|
|
LoRa.endPacket();
|
|
|
|
|
#endif
|
2024-01-03 02:41:54 +01:00
|
|
|
#if defined(HELTEC_V3) || defined(ESP32_DIY_1W_LoRa)
|
2023-12-26 20:37:54 +01:00
|
|
|
int state = radio.transmit("\x3c\xff\x01" + newPacket);
|
|
|
|
|
if (state == RADIOLIB_ERR_NONE) {
|
|
|
|
|
//Serial.println(F("success!"));
|
|
|
|
|
} else if (state == RADIOLIB_ERR_PACKET_TOO_LONG) {
|
|
|
|
|
Serial.println(F("too long!"));
|
|
|
|
|
} else if (state == RADIOLIB_ERR_TX_TIMEOUT) {
|
|
|
|
|
Serial.println(F("timeout!"));
|
|
|
|
|
} else {
|
|
|
|
|
Serial.print(F("failed, code "));
|
|
|
|
|
Serial.println(state);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2024-01-03 02:12:10 +01:00
|
|
|
digitalWrite(internalLedPin,LOW);
|
2023-10-08 14:39:44 +02:00
|
|
|
SYSLOG_Utils::log("LoRa Tx", newPacket,0,0,0);
|
|
|
|
|
Serial.print("---> LoRa Packet Tx : ");
|
|
|
|
|
Serial.println(newPacket);
|
2023-06-04 16:10:39 +02:00
|
|
|
}
|
|
|
|
|
|
2023-10-08 14:39:44 +02:00
|
|
|
String generatePacket(String aprsisPacket) {
|
|
|
|
|
String firstPart, messagePart;
|
|
|
|
|
aprsisPacket.trim();
|
|
|
|
|
firstPart = aprsisPacket.substring(0, aprsisPacket.indexOf(","));
|
|
|
|
|
messagePart = aprsisPacket.substring(aprsisPacket.indexOf("::")+2);
|
2023-10-10 03:12:29 +02:00
|
|
|
return firstPart + ",TCPIP,WIDE1-1," + Config.callsign + "::" + messagePart;
|
2023-10-08 14:39:44 +02:00
|
|
|
}
|
2023-06-06 20:37:47 +02:00
|
|
|
|
2023-10-08 14:39:44 +02:00
|
|
|
String receivePacket() {
|
|
|
|
|
String loraPacket = "";
|
2024-01-03 02:18:46 +01:00
|
|
|
#if defined(TTGO_T_LORA_V2_1) || defined(HELTEC_V2) || defined(ESP32_DIY_LoRa)
|
2023-10-08 14:39:44 +02:00
|
|
|
int packetSize = LoRa.parsePacket();
|
|
|
|
|
if (packetSize) {
|
|
|
|
|
while (LoRa.available()) {
|
|
|
|
|
int inChar = LoRa.read();
|
|
|
|
|
loraPacket += (char)inChar;
|
|
|
|
|
}
|
|
|
|
|
rssi = LoRa.packetRssi();
|
|
|
|
|
snr = LoRa.packetSnr();
|
|
|
|
|
freqError = LoRa.packetFrequencyError();
|
2023-06-12 05:21:52 +02:00
|
|
|
}
|
2023-12-26 20:37:54 +01:00
|
|
|
#endif
|
2024-01-03 02:41:54 +01:00
|
|
|
#if defined(HELTEC_V3) || defined(ESP32_DIY_1W_LoRa)
|
2023-12-26 20:37:54 +01:00
|
|
|
if (transmissionFlag) {
|
|
|
|
|
transmissionFlag = false;
|
|
|
|
|
radio.startReceive();
|
|
|
|
|
int state = radio.readData(loraPacket);
|
|
|
|
|
if (state == RADIOLIB_ERR_NONE) {
|
|
|
|
|
Serial.println("LoRa Rx ---> " + loraPacket);
|
2023-12-27 00:47:59 +01:00
|
|
|
rssi = radio.getRSSI();
|
|
|
|
|
snr = radio.getSNR();
|
|
|
|
|
freqError = radio.getFrequencyError();
|
2023-12-26 20:37:54 +01:00
|
|
|
} else if (state == RADIOLIB_ERR_RX_TIMEOUT) {
|
|
|
|
|
// timeout occurred while waiting for a packet
|
|
|
|
|
} else if (state == RADIOLIB_ERR_CRC_MISMATCH) {
|
|
|
|
|
Serial.println(F("CRC error!"));
|
|
|
|
|
} else {
|
|
|
|
|
Serial.print(F("failed, code "));
|
|
|
|
|
Serial.println(state);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2023-12-27 00:47:59 +01:00
|
|
|
#ifndef PinPointApp
|
|
|
|
|
Serial.println("(RSSI:" +String(rssi) + " / SNR:" + String(snr) + " / FreqErr:" + String(freqError) + ")");
|
|
|
|
|
#endif
|
2024-01-03 01:48:42 +01:00
|
|
|
if (Config.syslog.active && (stationMode==1 || stationMode==2 || (stationMode==5 && WiFi.status()==WL_CONNECTED)) && loraPacket!="") {
|
2023-12-27 00:47:59 +01:00
|
|
|
SYSLOG_Utils::log("LoRa Rx", loraPacket, rssi, snr, freqError);
|
|
|
|
|
}
|
2023-10-08 14:39:44 +02:00
|
|
|
return loraPacket;
|
2023-06-04 16:10:39 +02:00
|
|
|
}
|
|
|
|
|
|
2023-10-08 14:39:44 +02:00
|
|
|
void changeFreqTx() {
|
|
|
|
|
delay(500);
|
2024-01-03 02:18:46 +01:00
|
|
|
#if defined(TTGO_T_LORA_V2_1) || defined(HELTEC_V2) || defined(ESP32_DIY_LoRa)
|
2023-10-08 14:39:44 +02:00
|
|
|
LoRa.setFrequency(Config.loramodule.digirepeaterTxFreq);
|
2023-12-27 00:47:59 +01:00
|
|
|
#endif
|
2024-01-03 02:41:54 +01:00
|
|
|
#if defined(HELTEC_V3) || defined(ESP32_DIY_1W_LoRa)
|
2023-12-27 00:47:59 +01:00
|
|
|
float freq = (float)Config.loramodule.digirepeaterTxFreq/1000000;
|
|
|
|
|
radio.setFrequency(freq);
|
|
|
|
|
#endif
|
2023-10-08 14:39:44 +02:00
|
|
|
}
|
2023-06-08 01:34:18 +02:00
|
|
|
|
2023-10-08 14:39:44 +02:00
|
|
|
void changeFreqRx() {
|
|
|
|
|
delay(500);
|
2024-01-03 02:18:46 +01:00
|
|
|
#if defined(TTGO_T_LORA_V2_1) || defined(HELTEC_V2) || defined(ESP32_DIY_LoRa)
|
2023-10-08 14:39:44 +02:00
|
|
|
LoRa.setFrequency(Config.loramodule.digirepeaterRxFreq);
|
2023-12-27 00:47:59 +01:00
|
|
|
#endif
|
2024-01-03 02:41:54 +01:00
|
|
|
#if defined(HELTEC_V3) || defined(ESP32_DIY_1W_LoRa)
|
2023-12-27 00:47:59 +01:00
|
|
|
float freq = (float)Config.loramodule.digirepeaterRxFreq/1000000;
|
|
|
|
|
radio.setFrequency(freq);
|
|
|
|
|
#endif
|
2023-10-08 14:39:44 +02:00
|
|
|
}
|
2023-06-08 01:34:18 +02:00
|
|
|
|
2023-06-04 16:10:39 +02:00
|
|
|
}
|