У меня есть два комплекта модулей ESP-32 и Ra-02 LoRa. Я хочу отправить сообщение с одного узла на другой и получить обратно подтверждение ответа.
Отправка сообщения с узла 1 достигает узла 2. Но ответа от узла 2 на узел 1 не происходит. Я что-то упустил?
Код Node1:
#include
#include
#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);
// 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);
}
Код Node2:
#include
#include
#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();
}
}
Подробнее здесь: https://stackoverflow.com/questions/788 ... ot-working