I have two sets of ESP-32 and Ra-02 LoRa module. I want to send a message from one node to the other and get the reply back. Sending a message from node1 to node2 reaches node2. But sending the reply message from node2 to node1 is not happening. Am I missing something here.
Node1 code:
#include <SPI.h>
#include <LoRa.h>
#define SCK 18
#define MISO 19
#define MOSI 23
#define NSS 5
#define RESET 14
#define DIO0 26
void setup() {
Serial.begin(115200);
while (!Serial);
LoRa.setPins(NSS, RESET, DIO0);
if (!LoRa.begin(433E6)) {
Serial.println("Starting LoRa failed!");
while (1);
}
Serial.println("LoRa Transmitter");
}
void loop() {
String message = "Hello";
Serial.print("Sending: ");
Serial.println(message);
LoRa.beginPacket();
LoRa.print(message);
LoRa.endPacket();
delay(100); // Brief delay to ensure packet is sent
// Wait for acknowledgment
unsigned long startMillis = millis();
unsigned long timeout = 2000; // 2 seconds timeout
String response = "";
LoRa.idle(); // Ensure the module is in idle state before entering the loop
while (millis() - startMillis < timeout) {
int packetSize = LoRa.parsePacket();
if (packetSize) {
while (LoRa.available()) {
response += (char)LoRa.read();
}
break; // Exit the loop once a packet is received
}
}
if (response != "") {
Serial.print("Received response: ");
Serial.println(response);
} else {
Serial.println("No response received");
}
delay(5000); // Wait for 5 seconds before the next loop
}
Node2 code:
#include <SPI.h>
#include <LoRa.h>
#define SCK 18
#define MISO 19
#define MOSI 23
#define NSS 5
#define RESET 14
#define DIO0 26
void setup() {
Serial.begin(115200);
while (!Serial);
LoRa.setPins(NSS, RESET, DIO0);
if (!LoRa.begin(433E6)) {
Serial.println("Starting LoRa failed!");
while (1);
}
Serial.println("LoRa Receiver");
}
void loop() {
int packetSize = LoRa.parsePacket();
if (packetSize) {
String incoming = "";
while (LoRa.available()) {
incoming += (char)LoRa.read();
}
Serial.print("Received: ");
Serial.println(incoming);
// Send acknowledgment
String response = incoming + ":ack";
Serial.print("Sending: ");
Serial.println(response);
LoRa.beginPacket();
LoRa.print(response);
LoRa.endPacket();
}
}