Code to Detect Signal Strength or Simulated Phase Difference #15

Open
opened 2026-10-05 05:58:52 +02:00 by adam · 2 comments
Owner

Hardware:
ESP32
Two SX12XX/CR1121-based LoRa modules connected VIA SPI
Two antennas

This code uses RSSI readings to approximate signal direction. For phase comparison, you can measure arrival delays or manually simulate the process.

Accuracy can vary: 10–30°.

#include <SPI.h>
#include <LoRa.h>

// Pin definitions for the two LoRa modules
#define LORA1_CS 5
#define LORA1_RST 14
#define LORA1_IRQ 26

#define LORA2_CS 4
#define LORA2_RST 27
#define LORA2_IRQ 25

// Variables for RSSI measurement
int rssi1 = 0;
int rssi2 = 0;

void setup() {
  Serial.begin(115200);
  
  // Setup LoRa module 1
  pinMode(LORA1_CS, OUTPUT);
  LoRa.setPins(LORA1_CS, LORA1_RST, LORA1_IRQ);
  if (!LoRa.begin(433E6)) {
    Serial.println("LoRa1 init failed. Check connections.");
    while (true);
  }
  Serial.println("LoRa Module 1 Initialized!");

  // Setup LoRa module 2
  pinMode(LORA2_CS, OUTPUT);
  LoRa.setPins(LORA2_CS, LORA2_RST, LORA2_IRQ);
  if (!LoRa.begin(433E6)) {
    Serial.println("LoRa2 init failed. Check connections.");
    while (true);
  }
  Serial.println("LoRa Module 2 Initialized!");
}

void loop() {
  // Switch to LoRa Module 1
  digitalWrite(LORA1_CS, LOW);
  digitalWrite(LORA2_CS, HIGH);
  rssi1 = measureSignalStrength();
  Serial.print("LoRa1 RSSI: ");
  Serial.println(rssi1);

  // Switch to LoRa Module 2
  digitalWrite(LORA1_CS, HIGH);
  digitalWrite(LORA2_CS, LOW);
  rssi2 = measureSignalStrength();
  Serial.print("LoRa2 RSSI: ");
  Serial.println(rssi2);

  // Estimate signal direction based on RSSI
  if (rssi1 > rssi2) {
    Serial.println("Signal is closer to Antenna 1 (LoRa1).");
  } else if (rssi2 > rssi1) {
    Serial.println("Signal is closer to Antenna 2 (LoRa2).");
  } else {
    Serial.println("Signal strength is equal, direction unclear.");
  }

  delay(1000); // Wait 1 second before next check
}

int measureSignalStrength() {
  int rssiValue = 0;

  // Attempt to receive a packet and measure RSSI
  int packetSize = LoRa.parsePacket();
  if (packetSize) {
    rssiValue = LoRa.packetRssi();
  } else {
    rssiValue = -100; // Default low signal if no packet is received
  }
  return rssiValue;
}

Hardware: ESP32 Two SX12XX/CR1121-based LoRa modules connected VIA SPI Two antennas This code uses RSSI readings to approximate signal direction. For phase comparison, you can measure arrival delays or manually simulate the process. Accuracy can vary: 10–30°. ``` #include <SPI.h> #include <LoRa.h> // Pin definitions for the two LoRa modules #define LORA1_CS 5 #define LORA1_RST 14 #define LORA1_IRQ 26 #define LORA2_CS 4 #define LORA2_RST 27 #define LORA2_IRQ 25 // Variables for RSSI measurement int rssi1 = 0; int rssi2 = 0; void setup() { Serial.begin(115200); // Setup LoRa module 1 pinMode(LORA1_CS, OUTPUT); LoRa.setPins(LORA1_CS, LORA1_RST, LORA1_IRQ); if (!LoRa.begin(433E6)) { Serial.println("LoRa1 init failed. Check connections."); while (true); } Serial.println("LoRa Module 1 Initialized!"); // Setup LoRa module 2 pinMode(LORA2_CS, OUTPUT); LoRa.setPins(LORA2_CS, LORA2_RST, LORA2_IRQ); if (!LoRa.begin(433E6)) { Serial.println("LoRa2 init failed. Check connections."); while (true); } Serial.println("LoRa Module 2 Initialized!"); } void loop() { // Switch to LoRa Module 1 digitalWrite(LORA1_CS, LOW); digitalWrite(LORA2_CS, HIGH); rssi1 = measureSignalStrength(); Serial.print("LoRa1 RSSI: "); Serial.println(rssi1); // Switch to LoRa Module 2 digitalWrite(LORA1_CS, HIGH); digitalWrite(LORA2_CS, LOW); rssi2 = measureSignalStrength(); Serial.print("LoRa2 RSSI: "); Serial.println(rssi2); // Estimate signal direction based on RSSI if (rssi1 > rssi2) { Serial.println("Signal is closer to Antenna 1 (LoRa1)."); } else if (rssi2 > rssi1) { Serial.println("Signal is closer to Antenna 2 (LoRa2)."); } else { Serial.println("Signal strength is equal, direction unclear."); } delay(1000); // Wait 1 second before next check } int measureSignalStrength() { int rssiValue = 0; // Attempt to receive a packet and measure RSSI int packetSize = LoRa.parsePacket(); if (packetSize) { rssiValue = LoRa.packetRssi(); } else { rssiValue = -100; // Default low signal if no packet is received } return rssiValue; } ``` <!-- github-sync-id:2743764957 -->
Author
Owner

hmmmm... .how is this supposed to work? If both antennae are used sequentially, then they are processing different packets, right?..

hmmmm... .how is this supposed to work? If both antennae are used sequentially, then they are processing different packets, right?.. <!-- github-comment-id:2548494692 -->
Author
Owner

Also, can we instead synchronize two lora devices running in parallel? Like, over UART1. They could confirm that they are looking at the same packet.

Also, can we instead synchronize two lora devices running in parallel? Like, over UART1. They could confirm that they are looking at the same packet. <!-- github-comment-id:2548499559 -->
Sign in to join this conversation.
No labels
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: adam/LoraSA#15