LoRa Two Way Communication Failed

I Have a problem with the concept of LoRa Two Way Communication. I tried to apply this concept in my project. So the project is like this:

when there is an object that passes from point A to point B, the ultrasonic sensor will be triggered and the arduino will send a message from point A to point B. The problem is when point a sends a message to point b, sometimes point b can't send a message back to point a, and vice versa.

The sensor will be attached on left, right, and bellow the pillars.

What i used :

  1. Arduiono Uno R3
  2. LoRa Ra-02
  3. Sensor Ultrasonic HC-SR04

4

here's my messy code.

Transmitter

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

#define TRIGGER1 8
#define TRIGGER2 6
#define TRIGGER3 4

#define ECHO1 7
#define ECHO2 5
#define ECHO3 3

#define LED A0

const int ss = 10;
const int rst = 9;
const int dio0 = 2;

int count = 0;

byte localAddress = 0xBB;     // address of this device
byte destination = 0xFF;      // destination to send to
long lastSendTime = 0;        // last send time
int interval = 50;          // interval between sends

String outgoing;
String LoRaData;

long duration, distance, FIRSTSensor,SECONDSensor,THIRDSensor;


void setup() {
  Serial.begin (115200);
  Serial.println("Starting the system");
  
  Serial.println("LoRa Transmiter"); 

  pinMode(TRIGGER1, OUTPUT);
  pinMode(TRIGGER2, OUTPUT);
  pinMode(TRIGGER3, OUTPUT);
  
  pinMode(ECHO1, INPUT);
  pinMode(ECHO2, INPUT);
  pinMode(ECHO3, INPUT);
  
  pinMode(LED, OUTPUT);

  LoRa.setPins(ss, rst, dio0);
  
  while (!Serial);
  if (!LoRa.begin(433E6)){
    Serial.println("Starting LoRa failed!");
    while(1);
  }
}

void loop() {
  if(millis() - lastSendTime > interval){
    SonarSensor(TRIGGER1, ECHO1);
    FIRSTSensor = distance;

    SonarSensor(TRIGGER2, ECHO2);
    SECONDSensor = distance;

    SonarSensor(TRIGGER3, ECHO3);
    THIRDSensor = distance;
    
    if((FIRSTSensor >=5) & (FIRSTSensor <=8)){
      if((SECONDSensor >=8) & (SECONDSensor <=10)){
        if((THIRDSensor >=5) & (THIRDSensor <=8)){
      
          count = count-1;
          digitalWrite(LED,HIGH); 
          Serial.print("MOBIL TERDETEKSI\n");
          String message = "ONNN";
          sendMessage(message);
          delay(3000);
          //digitalWrite(LED, LOW);
        }
      }
    }
    if(count==0){
      digitalWrite(LED, LOW);
    }
    if(count<=-3){
      //Serial.println("GANTIAN");
      digitalWrite(LED, HIGH);
    }
    //lastSendTime = millis();        // timestamp the message
    //interval = random(50) + 150;    // 2-3 seconds
    //onReceive(LoRa.parsePacket());
  }
     // parse for a packet, and call onReceive with the result:
     onReceive(LoRa.parsePacket());
}

void sendMessage(String outgoing){
  LoRa.beginPacket();                   // start packet
  LoRa.write(destination);              // add destination address
  LoRa.write(localAddress);             // add sender address
  LoRa.print(outgoing);                 // add payload
  //LoRa.endPacket();                     // finish packet and send it
  LoRa.endPacket(true);                 // lora non-blocking mode
}

void onReceive(int packetSize){
  if (packetSize == 0) return;          // if there's no packet, return

  int recipient = LoRa.read();          // recipient address
  byte sender = LoRa.read();            // sender address

  String incoming = "";                 // payload of packet

  Serial.print("Packet incoming : " + incoming);

  while (LoRa.available()) {            // can't use readString() in callback, so
    incoming += (char)LoRa.read();      // add bytes one by one
  }

  if(incoming == "ONN"){
    //count++;
    digitalWrite(LED, HIGH);
    Serial.println("Message: " + incoming);
    Serial.println("MOBIL TERDTEKSI");
    Serial.println("RSSI: " + String(LoRa.packetRssi()));
    Serial.println("Snr: " + String(LoRa.packetSnr()));
    Serial.println(count = count+1);

  }
}

void SonarSensor(int TRIGGER,int ECHO) {
  
  digitalWrite(TRIGGER, LOW);
  delayMicroseconds(2);
  
  digitalWrite(TRIGGER, HIGH);
  delayMicroseconds(10);
  
  digitalWrite(TRIGGER, LOW);
  duration = pulseIn(ECHO, HIGH);
  distance = (duration/2) / 29.1;

}

Receiver

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

#define TRIGGER1 8
#define TRIGGER2 6
#define TRIGGER3 4

#define ECHO1 7
#define ECHO2 5
#define ECHO3 3

#define LED A0

const int ss = 10;
const int rst = 9;
const int dio0 = 2;

int count = 0;

byte localAddress = 0xBB;     // address of this device
byte destination = 0xFF;      // destination to send to
long lastSendTime = 0;        // last send time
int interval = 50;          // interval between sends

String outgoing;
String LoRaData;

long duration, distance, FIRSTSensor,SECONDSensor,THIRDSensor;


void setup() {
  Serial.begin (115200);
  Serial.println("Starting the system");
  
  Serial.println("LoRa Transmiter"); 

  pinMode(TRIGGER1, OUTPUT);
  pinMode(TRIGGER2, OUTPUT);
  pinMode(TRIGGER3, OUTPUT);
  
  pinMode(ECHO1, INPUT);
  pinMode(ECHO2, INPUT);
  pinMode(ECHO3, INPUT);
  
  pinMode(LED, OUTPUT);

  LoRa.setPins(ss, rst, dio0);
  
  while (!Serial);
  if (!LoRa.begin(433E6)){
    Serial.println("Starting LoRa failed!");
    while(1);
  }
}

void loop() {
  if(millis() - lastSendTime > interval){
    SonarSensor(TRIGGER1, ECHO1);
    FIRSTSensor = distance;

    SonarSensor(TRIGGER2, ECHO2);
    SECONDSensor = distance;

    SonarSensor(TRIGGER3, ECHO3);
    THIRDSensor = distance;

    
    if((FIRSTSensor >=5) & (FIRSTSensor <=8)){
      if((SECONDSensor >=8) & (SECONDSensor <=10)){
        if((THIRDSensor >=5) & (THIRDSensor <=8)){
      
          count = count-1;
          digitalWrite(LED,HIGH); 
          Serial.print("MOBIL TERDETEKSI\n");
          String message = "ONNN";
          sendMessage(message);
          delay(3000);
          //digitalWrite(LED, LOW);
        }
      }
    }
    if(count==0){
      digitalWrite(LED, LOW);
    }
    if(count<=-3){
      //Serial.println("GANTIAN");
      digitalWrite(LED, HIGH);
    }
    //lastSendTime = millis();        // timestamp the message
    //interval = random(50) + 150;    // 2-3 seconds
    //onReceive(LoRa.parsePacket());
  }
     // parse for a packet, and call onReceive with the result:
     onReceive(LoRa.parsePacket());
}

void sendMessage(String outgoing){
  LoRa.beginPacket();                   // start packet
  LoRa.write(destination);              // add destination address
  LoRa.write(localAddress);             // add sender address
  LoRa.print(outgoing);                 // add payload
  //LoRa.endPacket();                     // finish packet and send it
  LoRa.endPacket(true);                 // lora non-blocking mode
}

void onReceive(int packetSize){
  if (packetSize == 0) return;          // if there's no packet, return

  int recipient = LoRa.read();          // recipient address
  byte sender = LoRa.read();            // sender address

  String incoming = "";                 // payload of packet

  Serial.print("Packet incoming : " + incoming);

  while (LoRa.available()) {            // can't use readString() in callback, so
    incoming += (char)LoRa.read();      // add bytes one by one
  }

  if(incoming == "ONN"){
    //count++;
    digitalWrite(LED, HIGH);
    Serial.println("Message: " + incoming);
    Serial.println("MOBIL TERDTEKSI");
    Serial.println("RSSI: " + String(LoRa.packetRssi()));
    Serial.println("Snr: " + String(LoRa.packetSnr()));
    Serial.println(count = count+1);

  }
}

void SonarSensor(int TRIGGER,int ECHO) {
  
  digitalWrite(TRIGGER, LOW);
  delayMicroseconds(2);
  
  digitalWrite(TRIGGER, HIGH);
  delayMicroseconds(10);
  
  digitalWrite(TRIGGER, LOW);
  duration = pulseIn(ECHO, HIGH);
  distance = (duration/2) / 29.1;

}

The Transmitter and Receiver are bassically the same, but why is it a problem. Is there any way to do LoRa two way communication like the concept i was expecting. Thank you

Its probably a problem because after you transmit a packet you then delay for 3 seconds before attempting to recieve a packet;

sendMessage(message);
delay(3000);

So if the packet is transmitted in that 3 seconds, it will be missed.

Also you need to appreciate that the LoRa devices are transceivers only, they are either in transmit or receive mode, so they cannot receive whilst transmitting.

I can point you to a LoRa library that has several worked examples of two way communications with LoRa devices.

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