mirror of
https://github.com/Genaker/LoraSA.git
synced 2026-11-03 12:12:21 +01:00
Detect Lora Signals. #12
Owner
```cpp
#include <SPI.h>
#include <LoRa.h>
// Define LoRa pins
#define SCK 18
#define MISO 19
#define MOSI 23
#define SS 5
#define RST 14
#define DIO0 26
// Frequency of LoRa (adjust based on your region)
#define LORA_FREQUENCY 915E6 // 915 MHz for US, 868 MHz for EU, 433 MHz for some regions
// LoRa parameter ranges
int spreadingFactors[] = {7, 8, 9, 10, 11, 12}; // Spreading factors
long bandwidths[] = {62500, 125000, 250000}; // Bandwidths in Hz
int codingRates[] = {5, 6, 7, 8}; // Coding rates (4/5 to 4/8)
// Index trackers
int sfIndex = 0;
int bwIndex = 0;
int crIndex = 0;
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.println("Starting LoRa receiver...");
// Initialize LoRa with custom pins
LoRa.setPins(SS, RST, DIO0);
if (!LoRa.begin(LORA_FREQUENCY)) {
Serial.println("LoRa initialization failed!");
while (1);
}
Serial.println("LoRa initialized.");
setLoRaParameters(); // Set initial parameters
}
void loop() {
// Check if a packet is received
int packetSize = LoRa.parsePacket();
if (packetSize) {
Serial.println("Received packet:");
// Read packet data
while (LoRa.available()) {
String receivedData = LoRa.readString();
Serial.println("Data: " + receivedData);
}
// Print RSSI and SNR
Serial.print("RSSI: ");
Serial.println(LoRa.packetRssi());
Serial.print("SNR: ");
Serial.println(LoRa.packetSnr());
}
// Loop through all parameter combinations
loopParameters();
}
void setLoRaParameters() {
int sf = spreadingFactors[sfIndex];
long bw = bandwidths[bwIndex];
int cr = codingRates[crIndex];
LoRa.setSpreadingFactor(sf);
LoRa.setSignalBandwidth(bw);
LoRa.setCodingRate4(cr);
Serial.println("LoRa Parameters Set:");
Serial.print("Spreading Factor: ");
Serial.println(sf);
Serial.print("Bandwidth: ");
Serial.print(bw / 1000); // Convert Hz to kHz
Serial.println(" kHz");
Serial.print("Coding Rate: 4/");
Serial.println(cr);
}
void loopParameters() {
// Change to the next set of parameters
delay(5000); // Wait 5 seconds to test each configuration
crIndex++;
if (crIndex >= sizeof(codingRates) / sizeof(codingRates[0])) {
crIndex = 0;
bwIndex++;
}
if (bwIndex >= sizeof(bandwidths) / sizeof(bandwidths[0])) {
bwIndex = 0;
sfIndex++;
}
if (sfIndex >= sizeof(spreadingFactors) / sizeof(spreadingFactors[0])) {
sfIndex = 0;
Serial.println("Completed all parameter combinations.");
return;
}
// Apply the new parameters
setLoRaParameters();
}
Radio lib
#include <RadioLib.h>
// Define LoRa module pins
#define LORA_SCK 18
#define LORA_MISO 19
#define LORA_MOSI 23
#define LORA_CS 5
#define LORA_RST 14
#define LORA_DIO0 26
// Frequency (adjust for your region)
#define LORA_FREQUENCY 915.0 // 915 MHz for US, 868 MHz for EU, 433 MHz for some regions
// Instantiate the module
SX1276 radio = new Module(LORA_CS, LORA_DIO0, LORA_RST, LORA_SCK, LORA_MISO, LORA_MOSI);
// LoRa parameter ranges
int spreadingFactors[] = {7, 8, 9, 10, 11, 12};
float bandwidths[] = {62.5, 125.0, 250.0}; // in kHz
int codingRates[] = {5, 6, 7, 8}; // Coding rates (4/5 to 4/8)
// Index trackers
int sfIndex = 0;
int bwIndex = 0;
int crIndex = 0;
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.println("Initializing LoRa module...");
// Initialize the radio
int state = radio.begin(LORA_FREQUENCY);
if (state != RADIOLIB_ERR_NONE) {
Serial.print("Initialization failed, code: ");
Serial.println(state);
while (true);
}
Serial.println("LoRa module initialized!");
setLoRaParameters(); // Set initial parameters
}
void loop() {
Serial.println("Listening for packets...");
// Start receiving
int state = radio.receive();
if (state == RADIOLIB_ERR_NONE) {
Serial.println("Packet received!");
// Read data
String receivedData;
int length = radio.getPacketLength();
for (int i = 0; i < length; i++) {
receivedData += (char)radio.readDataByte();
}
Serial.print("Data: ");
Serial.println(receivedData);
// Print RSSI and SNR
Serial.print("RSSI: ");
Serial.println(radio.getRSSI());
Serial.print("SNR: ");
Serial.println(radio.getSNR());
} else if (state == RADIOLIB_ERR_RX_TIMEOUT) {
Serial.println("Timeout, no packet received.");
} else {
Serial.print("Receive failed, code: ");
Serial.println(state);
}
// Change parameters after every 5 seconds
delay(5000);
loopParameters();
}
void setLoRaParameters() {
int sf = spreadingFactors[sfIndex];
float bw = bandwidths[bwIndex];
int cr = codingRates[crIndex];
int state;
// Set spreading factor
state = radio.setSpreadingFactor(sf);
if (state != RADIOLIB_ERR_NONE) {
Serial.print("Failed to set spreading factor, code: ");
Serial.println(state);
return;
}
// Set bandwidth
state = radio.setBandwidth(bw);
if (state != RADIOLIB_ERR_NONE) {
Serial.print("Failed to set bandwidth, code: ");
Serial.println(state);
return;
}
// Set coding rate
state = radio.setCodingRate(cr);
if (state != RADIOLIB_ERR_NONE) {
Serial.print("Failed to set coding rate, code: ");
Serial.println(state);
return;
}
Serial.println("LoRa Parameters Set:");
Serial.print("Spreading Factor: ");
Serial.println(sf);
Serial.print("Bandwidth: ");
Serial.print(bw);
Serial.println(" kHz");
Serial.print("Coding Rate: 4/");
Serial.println(cr);
}
void loopParameters() {
crIndex++;
if (crIndex >= sizeof(codingRates) / sizeof(codingRates[0])) {
crIndex = 0;
bwIndex++;
}
if (bwIndex >= sizeof(bandwidths) / sizeof(bandwidths[0])) {
bwIndex = 0;
sfIndex++;
}
if (sfIndex >= sizeof(spreadingFactors) / sizeof(spreadingFactors[0])) {
sfIndex = 0;
Serial.println("Completed all parameter combinations.");
return;
}
setLoRaParameters();
}
```
<!-- github-sync-id:2709689576 -->
Reference in new issue
Block a user