/**
* Escáner de canales LR1121 (solo RX, sin TX).
* Barre las bandas de 868 MHz y 2.4 GHz cambiando BW y SF dinámicamente.
* Mide el RSSI (ruido de canal) y muestra los resultados en formato CSV.
*/
/* ───────── KODE | docs.kode.diy ───────── */
#include <Arduino.h>
#include <RadioLib.h>
/* Configuración de pines (según tu diseño) */
#define NSS_PIN 3 /* Chip Select */
#define DIO1_PIN 12 /* IRQ */
#define NRST_PIN 2 /* Reset */
#define BUSY_PIN 13 /* Busy */
#define MISO_PIN 41
#define MOSI_PIN 40
#define SCK_PIN 39
/* Mapeo del RF Switch para E80-900M2213S */
static const uint32_t rfswitch_dio_pins[] = {
RADIOLIB_LR11X0_DIO5, RADIOLIB_LR11X0_DIO6,
RADIOLIB_NC, RADIOLIB_NC, RADIOLIB_NC
};
static const Module::RfSwitchMode_t rfswitch_table[] = {
{ LR11x0::MODE_STBY, { LOW, LOW } },
{ LR11x0::MODE_RX, { LOW, LOW } }, /* RX */
{ LR11x0::MODE_TX, { LOW, HIGH } }, /* TX Sub-1GHz LP */
{ LR11x0::MODE_TX_HP, { HIGH, LOW } }, /* TX Sub-1GHz HP */
{ LR11x0::MODE_TX_HF, { HIGH, HIGH } }, /* TX 2.4GHz */
{ LR11x0::MODE_GNSS, { LOW, LOW } },
{ LR11x0::MODE_WIFI, { LOW, LOW } },
END_OF_MODE_TABLE,
};
/* Instancias */
SPIClass spi(HSPI);
LR1121 radio = new Module(NSS_PIN, DIO1_PIN, NRST_PIN, BUSY_PIN, spi);
/* Configuración de barrido de frecuencias */
const float FREQS_868[] = { 863.0, 866.0, 868.0, 869.5 };
const float FREQS_24[] = { 2403.5, 2425.0, 2450.0, 2479.5 };
const size_t N_868 = sizeof(FREQS_868) / sizeof(FREQS_868[0]);
const size_t N_24 = sizeof(FREQS_24) / sizeof(FREQS_24[0]);
/* Parámetros dinámicos */
const float BWS_KHZ[] = { 125.0, 203.125 };
const int SFS[] = { 7, 9, 12 };
const int CR = 5;
const int PWR_DBM = 10;
const uint8_t PREAMBLE = 8;
const float TCXO_V = 1.8;
/* Tiempo de escucha por punto de medida */
const uint16_t DWELL_MS = 200;
/* Funciones auxiliares */
/* Reset físico del LR1121 */
static void hardResetModule() {
pinMode(NRST_PIN, OUTPUT);
digitalWrite(NRST_PIN, LOW);
delay(50);
digitalWrite(NRST_PIN, HIGH);
delay(50);
}
/* Configura el LR1121 con los parámetros indicados */
static bool configRadio(float freqMHz, float bwkHz, int sf) {
int st = radio.begin(freqMHz, bwkHz, sf, CR, 0x12 /*sync*/, PWR_DBM, PREAMBLE, TCXO_V);
if (st != RADIOLIB_ERR_NONE) {
Serial.print("Fallo de configuración f="); Serial.print(freqMHz, 3);
Serial.print(" MHz BW="); Serial.print(bwkHz, 3);
Serial.print(" kHz SF="); Serial.print(sf);
Serial.print(" código="); Serial.println(st);
return false;
}
return true;
}
/* Mide una vez el RSSI con los parámetros indicados y muestra línea CSV */
static void measureOnce(float freqMHz, float bwkHz, int sf, const char* bandTag) {
if (!configRadio(freqMHz, bwkHz, sf)) return;
/* Iniciar recepción */
int st = radio.startReceive();
if (st != RADIOLIB_ERR_NONE) {
Serial.print("Fallo al iniciar RX, código="); Serial.println(st);
return;
}
delay(DWELL_MS);
float rssi = radio.getRSSI();
/* Salida en formato CSV: BANDA,FREQ_MHz,BW_kHz,SF,RSSI_dBm */
Serial.print(bandTag); Serial.print(",");
Serial.print(freqMHz, 3); Serial.print(",");
Serial.print(bwkHz, 3); Serial.print(",");
Serial.print(sf); Serial.print(",");
Serial.println(rssi, 1);
radio.standby();
}
/* Escanea toda una banda de frecuencias variando BW y SF */
static void scanBand(const float* freqs, size_t nFreq, const char* bandTag) {
Serial.println();
Serial.println("BANDA,FREQ_MHz,BW_kHz,SF,RSSI_dBm");
for (size_t i = 0; i < nFreq; i++) {
for (size_t b = 0; b < sizeof(BWS_KHZ)/sizeof(BWS_KHZ[0]); b++) {
for (size_t s = 0; s < sizeof(SFS)/sizeof(SFS[0]); s++) {
measureOnce(freqs[i], BWS_KHZ[b], SFS[s], bandTag);
}
}
}
}
/* Setup */
void setup() {
Serial.begin(115200);
while (!Serial) { delay(10); }
Serial.println("\n=== Escáner de canales LR1121 (solo RSSI) ===");
Serial.println("RSSI = ruido de canal (más cerca de 0 => más ruido)");
pinMode(BUSY_PIN, INPUT);
Serial.println("Reseteando módulo...");
hardResetModule();
Serial.println("Inicializando SPI...");
spi.begin(SCK_PIN, MISO_PIN, MOSI_PIN, NSS_PIN);
radio.setRfSwitchTable(rfswitch_dio_pins, rfswitch_table);
Serial.println("\n--- Escaneo: 868 MHz ---");
scanBand(FREQS_868, N_868, "868MHz");
Serial.println("\n--- Escaneo: 2.4 GHz ---");
scanBand(FREQS_24, N_24, "2400MHz");
Serial.println("\nFin del escaneo. Reinicia para repetir.");
}
/* Loop */
void loop() {
delay(1000);
}