How to make a loRa program for 2-way communication?

I have a problem when programming on LoRa sx1278 which is not being able to program 2-way communication. can anyone help me?

This my LoRa sender program :
#include <SPI.h>
#include <LoRa.h>

unsigned long prevmil = 0;
unsigned interval=5000;
unsigned long waktu;
int node;
int master = 0;
String addmaster = String(master);

void setup() {
Serial.begin(9600);
while (!Serial);

Serial.println("LoRa Sender");
if (!LoRa.begin(433E6)) {
Serial.println("Starting LoRa failed!");
while (1);
}
}

void loop() {
// broadcast paket menuju node
Serial.println("Sending packet: ");
for(node = 1; node<=4;){
waktu = millis();
if(waktu - prevmil <= 2500) {
Serial.print("node = ");
String data = String(node++);
sendMessage(data);
}else if (waktu - prevmil >= 2500){
receiveData();
prevmil = millis();
}
}

}

void sendMessage(String terima){
Serial.print(terima);
Serial.print(",");
Serial.println(addmaster);

LoRa.beginPacket();
LoRa.print(terima);
LoRa.print(addmaster);
LoRa.endPacket();
}

void receiveData (){
//menerima balasan dari node
Serial.println("Receive Data");
int paket = LoRa.parsePacket();
if(paket){
//baca paket
Serial.print("Menerima Balasan Paket");
while(LoRa.available()){
Serial.println((char)LoRa.read());
}
}
}

This my receiver LoRa program :
#include <SPI.h>
#include <LoRa.h>

unsigned long prevmil = 0;
unsigned interval=5000;
unsigned long waktu;

void setup() {
Serial.begin(9600);
while (!Serial);

Serial.println("LoRa Receiver");

if (!LoRa.begin(433E6)) {
Serial.println("Starting LoRa failed!");
while (1);
}
}

void loop() {
//Terima Data
Serial.println("Received packet '");
int packetSize = LoRa.parsePacket();
if (packetSize) {
// read packet
while (LoRa.available()) {
Serial.print((char)LoRa.read());

  //menjawab balasan dari master

// waktu = millis();
// if(waktu - prevmil >= interval){
// Serial.println("Berhasil");
// LoRa.beginPacket();
// LoRa.print("Node 1 ");
// LoRa.print("Berhasil");
// LoRa.endPacket();
// prevmil = millis();
// }

}

// print RSSI of pLoacket
Serial.print("' with RSSI ");
Serial.println(LoRa.packetRssi());

}
}

You somehow know that you received a packet, without even having to check? How can that work?

I would start with the send and receive example that comes with that library.

If you make changes to that example (LoRaDuplex) and the send and receive stops working you should have a good idea where you went wrong.............

what do you mean 'without even having to check'? I've checked it on the Arduino serial monitor. can you explain the meaning more clearly, please?

I don't understand, what's wrong with that?

Okay, I think this must be for reading the LoRa input buffer. IDK but you might be missing a similar check to see if the packet is available.

Are there no example sketches with the LoRa library that you can try? Oh, wait... you've had some time to do that already... see reply #3.

The example code does it like:

 // try to parse packet
  int packetSize = LoRa.parsePacket();
  if (packetSize) {
    // received a packet
    Serial.print("Received packet '");

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.