LoRa node isn't able to transmit and receive data

Hello everyone, I am trying to design a home automation system using LoRa. However, from my setup it's either I am able to get the sensor data or control the relay. I can't do both. Any help will be appreciated.

SENDER CIRCUIT:

~Microcontroller: ESP8266 (Wi-Fi module)

~Wireless Communication: LoRa RA-02 module for long-range communication

Integration: Blynk App

RECEIVER CIRCUIT:

~Microcontroller: Arduino Uno

~Wireless Communication: LoRa RA-02 module for long-range communication

~DHT11 (Temperature and Humidity Sensor)

~PIR Motion Sensor

~2-Channel Relay (to control a bulb and a fan)

#include <ESP8266WiFi.h>
#include <LoRa.h>

// WiFi network details
const char* ssid = " ";
const char* password = " ";

// Blynk authentication token
#define BLYNK_TEMPLATE_ID " "
#define BLYNK_TEMPLATE_NAME " "
#define BLYNK_AUTH_TOKEN " "

#include <BlynkSimpleEsp8266.h>

const int csPin = D8;
const int resetPin = D0;
const int irqPin = D2;

String temperature = "N/A";
String humidity = "N/A";
String motionState = "N/A";

void setup() { 
  Serial.begin(9600);
  // Initialize LoRa
  LoRa.setPins(csPin, resetPin, irqPin);
  while(!LoRa.begin(433E6)) {
    Serial.println("Starting LoRa failed!");
    delay(500);
  }
  Serial.println("LoRa Receiver Ready");

  Serial.println("LoRa Initializing OK!");

  // Initialize Blynk
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, password);
}

void loop() {
  Blynk.run(); // Run Blynk

  int packetSize = LoRa.parsePacket();
  if (packetSize) {
    String received = "";
    while (LoRa.available()) {
      received += (char)LoRa.read();
    }
    Serial.print("Received from LoRa: "+received);
    parseLoRaMessage(received);
  }
}

void parseLoRaMessage(String received) {
  Serial.println("Received data from LoRa: " + received);

  if (received.startsWith("Temp:") && received.indexOf(",Humidity:")!= -1 && received.indexOf(",Motion:")!= -1) {
    int tempIndex = received.indexOf("Temp:") + 5;
    int humIndex = received.indexOf(",Humidity:");
    int motionIndex = received.indexOf(",Motion:");

    if (tempIndex!= -1 && humIndex!= -1 && motionIndex!= -1) {
      temperature = received.substring(tempIndex, humIndex);
      humidity = received.substring(humIndex + 10, motionIndex);
      int motionStateNum = received.substring(motionIndex + 8).toInt();
      motionState = (motionStateNum == 1)? "Yes" : "No";

      Serial.println("Temperature: " + temperature);
      Serial.println("Humidity: " + humidity);
      Serial.println("Motion State: " + motionState);
    } else {
      Serial.println("Error: Invalid format");
    }
  } else {
    Serial.println("Error: Invalid data format");
  }

  // Send sensor data to Blynk
  Blynk.virtualWrite(V0, temperature.toFloat()); // Virtual pin V0 for temperature
  Blynk.virtualWrite(V4, humidity.toFloat());    // Virtual pin V4 for humidity
  Blynk.virtualWrite(V3, motionState == "Yes"? 1 : 0); // Virtual pin V3 for motion state
}

BLYNK_WRITE(V1) {
  if (param.asInt() == 1) {
    Serial.println("Button 1 pressed");
    sendLoRaCommand("R1_ON");
  }
  else{
    sendLoRaCommand("R1_OFF");
  }
}

BLYNK_WRITE(V2) {
  if (param.asInt() == 1) {
    Serial.println("Button 2 pressed");
    sendLoRaCommand("R2_ON");
  }
  else{
     sendLoRaCommand("R2_OFF");
  }
}

void sendLoRaCommand(String command) {
  LoRa.beginPacket();
  LoRa.print(command);
  LoRa.endPacket();
  Serial.println("Sent LoRa command: " + command);
}
Receiver code:
#include <SPI.h>
#include <LoRa.h>
#include <DHT.h>

// Define LoRa module pins
const int csPin = 10;    // LoRa radio chip select (NSS)
const int resetPin = 9;  // LoRa radio reset
const int irqPin = 2;    // Interrupt request pin (DIO0)

// Define relay pins
#define relayPin1 5  // Relay for fan connected to GPIO 5
#define relayPin2 6  // Relay for bulb connected to GPIO 6

// Define DHT sensor setup
#define DHTPIN 7       // DHT11 data pin
#define DHTTYPE DHT11  // DHT11 sensor type
DHT dht(DHTPIN, DHTTYPE);

// Define PIR Motion Sensor pin
const int pirPin = 8;  // PIR motion sensor pin

//Define the parameters
float humidity = 0.0;
float temperature = 0.0;
int motionDetected = 0.0;
unsigned long int lastSend = 0;
int interval = 5000;

void setup() {
  Serial.begin(9600);
  pinMode(relayPin1, OUTPUT);
  pinMode(relayPin2, OUTPUT);
  // Ensure relays are OFF by default if using Normally Open
  digitalWrite(relayPin1, HIGH);
  digitalWrite(relayPin2, HIGH);

  // Initialize LoRa
  LoRa.setPins(csPin, resetPin, irqPin);
  while(!LoRa.begin(433E6)) {
    Serial.println("Starting LoRa failed!");
    delay(500);
  }
  Serial.println("LoRa Receiver Ready");

  // Initialize DHT sensor
  dht.begin();

  // Initialize PIR motion sensor
  pinMode(pirPin, INPUT);
}

void loop() {
  // Read sensor data
  humidity = dht.readHumidity();
  temperature = dht.readTemperature();
  motionDetected = digitalRead(pirPin);


  // Print sensor readings
  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.print("°C, Humidity: ");
  Serial.print(humidity);
  Serial.print("%, Motion Detected: ");
  Serial.println(motionDetected ? "Yes" : "No");

  // Send sensor data via LoRa to transmitter
  sendSensorData(temperature, humidity, motionDetected);


  // Check for LoRa commands
   receiveLoRaCommand();
}

void sendSensorData(float temperature, float humidity, int motionDetected) {
  LoRa.beginPacket();
  LoRa.print("Temp:");
  LoRa.print(temperature);
  LoRa.print(",Humidity:");
  LoRa.print(humidity);
  LoRa.print(",Motion:");
  LoRa.println(motionDetected ? "1" : "0");
  LoRa.endPacket();
  Serial.println("Sent sensor data via LoRa");
}

void receiveLoRaCommand() {
  int packetSize = LoRa.parsePacket();
  if (packetSize) {
    String receivedCommand = LoRa.readString();
    Serial.println("Received command: " + receivedCommand);

    // Process the received command
    if (receivedCommand == "R1_ON") {
      digitalWrite(relayPin1, LOW);  // Turn on relay 1
    } else if (receivedCommand == "R1_OFF") {
      digitalWrite(relayPin1, HIGH);  // Turn off relay 1
    } else if (receivedCommand == "R2_ON") {
      digitalWrite(relayPin2, LOW);  // Turn on relay 2
    } else if (receivedCommand == "R2_OFF") {
      digitalWrite(relayPin2, HIGH);  // Turn off relay 2
    }
  }
}

can you upload (as text not a screen image) the serial monitor outputs of the transmitter and receiver
in particular what does
receiver

Serial.println("Received command: " + receivedCommand);

display?

sender:
20:53:15.439 -> Temperature: 25.30
20:53:15.470 -> Humidity: 73.00
20:53:15.470 -> Motion State: Yes
20:53:21.785 -> Button 1 pressed
20:53:21.863 -> Sent LoRa command: R1_ON
20:53:24.799 -> Sent LoRa command: R1_OFF
receiver:
20:53:15.245 -> Temperature: 25.30°C, Humidity: 73.00%, Motion Detected: Yes
20:53:15.308 -> Sent sensor data via LoRa
20:53:15.342 -> Temperature: 25.30°C, Humidity: 73.00%, Motion Detected: Yes
20:53:15.405 -> Sent sensor data via LoRa
20:53:15.442 -> Temperature: 25.30°C, Humidity: 73.00%, Motion Detected: Yes
20:53:15.506 -> Sent sensor data via LoRa
20:53:15.506 -> Temperature: 25.30°C, Humidity: 73.00%, Motion Detected: No

The receiver isn't receiving the control commands

I want to achieve the following functionalities:

  • The sender can receive sensor data and send control commands.
  • The receiver can send sensor data and receive control commands.

maybe worth putting a delay after sending the command, e.g.

void sendLoRaCommand(String command) {
  LoRa.beginPacket();
  LoRa.print(command);
  LoRa.endPacket();
  Serial.println("Sent LoRa command: " + command);
  delay(1000);
}

see what happens

The relay is still only activated when I comment out the sendSensorData function.
Will implementing callback help? If so, how will I go about it?

Did you first verify that the two LoRa modules communicate as expected, using one of the standard code examples and nothing else connected to the Arduinos?

If not, that is the place to start. Add one new bit at a time, testing as you go.

Yes, I used the standard code examples.
I first implemented the relay control which worked perfectly. But when I added the sendSensor function, I couldn't control the relay anymore.

Please post a complete wiring diagram (hand drawn is preferred) with all pins, parts and connections clearly labeled.

Links to the product pages of the relay module and relay power supply, if applicable, would be very helpful.

Sender:

Receiver:

downloaded your code and did some experiments -
think you were transmitting data (temperature, humidity, etc) too fast
try the following where it transmits data once per second

void loop() {
  static long timer = millis();
  if (millis() - timer > 1000) {
    timer = millis();
    // Read sensor data
    humidity = dht.readHumidity();
    temperature = dht.readTemperature();
    motionDetected = digitalRead(pirPin);
    // Print sensor readings
    Serial.print("Temperature: ");
    Serial.print(temperature);
    Serial.print("°C, Humidity: ");
    Serial.print(humidity);
    Serial.print("%, Motion Detected: ");
    Serial.println(motionDetected ? "Yes" : "No");
    // Send sensor data via LoRa to transmitter
    sendSensorData(temperature, humidity, motionDetected);
  }
  // Check for LoRa commands
  receiveLoRaCommand();
}

void sendSensorData(float temperature, float humidity, int motionDetected) {
  LoRa.beginPacket();
  LoRa.print("Temp:");
  LoRa.print(temperature);
  LoRa.print(",Humidity:");
  LoRa.print(humidity);
  LoRa.print(",Motion:");
  LoRa.println(motionDetected ? "1" : "0");
  LoRa.endPacket();
  Serial.println("Sent sensor data via LoRa");
  // delay(5000);
}

void receiveLoRaCommand() {
  //Serial.print("receive testr");
  int packetSize = LoRa.parsePacket();
  // Serial.println(packetSize);
  if (packetSize) {
    String receivedCommand = LoRa.readString();
    Serial.println("Received command: " + receivedCommand);

    // Process the received command
    if (receivedCommand == "R1_ON") {
      digitalWrite(relayPin1, LOW);  // Turn on relay 1
    } else if (receivedCommand == "R1_OFF") {
      digitalWrite(relayPin1, HIGH);  // Turn off relay 1
    } else if (receivedCommand == "R2_ON") {
      digitalWrite(relayPin2, LOW);  // Turn on relay 2
    } else if (receivedCommand == "R2_OFF") {
      digitalWrite(relayPin2, HIGH);  // Turn off relay 2
    }
  }
}

the transmitter displays R1_OFF etc OK

Temperature: 0.00°C, Humidity: 0.00%, Motion Detected: No
Sent sensor data via LoRa
Received command: R1_OFF
Temperature: 0.00°C, Humidity: 0.00%, Motion Detected: No
Sent sensor data via LoRa
Received command: R1_OFF
Temperature: 0.00°C, Humidity: 0.00%, Motion Detected: No
Sent sensor data via LoRa
Received command: R1_ON
Temperature: 0.00°C, Humidity: 0.00%, Motion Detected: No
Sent sensor data via LoRa
Received command: R1_OFF
Temperature: 0.00°C, Humidity: 0.00%, Motion Detected: No
Sent sensor data via LoRa
Received command: R1_OFF
Temperature: 0.00°C, Humidity: 0.00%, Motion Detected: No
Sent sensor data via LoRa
Received command: R1_ON
Temperature: 0.00°C, Humidity: 0.00%, Motion Detected: No
Sent sensor data via LoRa
Received command: R1_OFF
Temperature: 0.00°C, Humidity: 0.00%, Motion Detected: No
Sent sensor data via LoRa
Received command: R1_OFF

I don't have a DHT11 so data is all 0