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 :
- Arduiono Uno R3
- LoRa Ra-02
- Sensor Ultrasonic HC-SR04

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