sandeepmistry / arduino-LoRa

An Arduino library for sending and receiving data using LoRa radios.
MIT License
1.67k stars 634 forks source link

LoRa Duplex : problem with 0x*number* communication #114

Closed BlackxRyan closed 6 years ago

BlackxRyan commented 6 years ago

Hello.

I'm using a little modified code of LoRa Duplex with 2 Arduino cards + 2 Dragino LoRa Shields. The code works correctly.

But when I use addresses with numbers like 0x11 and 0x55, both of them use the same address and I don't know why. However, I'm compiling it on two different Arduino cards.

Edit : That was because I was compiling the two programs at the same time, it works fine now.

kadirozdinc commented 6 years ago

Hello ryan I am having the same difficulty with address and cant understand completely the main idea of duplex communication. I cant make the codes work yet. I need the help. When I read this issue I think I understand my problem.

I upload the same sketch into the 2 arduino and then doesn't work. Does it need to be like the following ?

For first sketch : ... byte localAddress = 0xab; // address of this device byte destination = 0xbc; // destination to send to ...

For second sketch : .. byte localAddress = 0xbc; // address of this device byte destination = 0xab; // destination to send to ..

BlackxRyan commented 6 years ago

Hi Willing.

Yes, this is the idea, but I then chose to use simple numbers to identify my devices, here is the program I used.

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

int msgCount = 0;
int local = 1; //ID of this device
int dest = 2; //destination to send to
long lastSendTime = 0;
int interval = 2000; 
String incoming; 
String outgoing; 

void setup() 
{
Serial.begin(115200);
while (!Serial);
LoRa.begin(868E6, true);
}

void loop() 
{
if (millis() - lastSendTime > interval)
{
String message = "Hello";
sendMessage(message);
Serial.println("Sending " + message);
Serial.println("");
lastSendTime = millis();
interval = 2000; 
}

int packetSize = LoRa.parsePacket();

if (packetSize) 
{
int localAddress = LoRa.read();
int dest = LoRa.read();
int id = LoRa.read();

if (localAddress == 2) 
{
Serial.print("LoRa ");
Serial.println(localAddress);
Serial.print("For LoRa ");
Serial.println(dest);
Serial.print("ID = ");
Serial.println(id);
Serial.print("Message : ");
while (LoRa.available()) {
incoming = (char)LoRa.read();
Serial.print(incoming);
}

Serial.println("");
Serial.print("RSSI : ");
Serial.println(LoRa.packetRssi());
Serial.println("");
}
}
}

void sendMessage(String outgoing)
{
LoRa.beginPacket();
LoRa.write(local);
LoRa.write(dest);
LoRa.write(msgCount);
LoRa.print(outgoing);
LoRa.endPacket();
msgCount++;
}

Also, I finally worked with LoRa Heltec modules but it should be the same for all LoRa modules.

Tell me if you have a question on the program.

kadirozdinc commented 6 years ago

Here is my codes for first and second device :

First :

`#include

include

int msgCount = 0; int local = 2; //ID of this device int dest = 1; //destination to send to long lastSendTime = 0; int interval = 2000; String incoming; String outgoing;

void setup() { Serial.begin(115200); while (!Serial); LoRa.begin(433E6); }

void loop() { if (millis() - lastSendTime > interval) { String message = "Hello"; sendMessage(message); Serial.println("Sending " + message); Serial.println(""); lastSendTime = millis(); interval = 2000; }

int packetSize = LoRa.parsePacket();

if (packetSize) { int localAddress = LoRa.read(); int dest = LoRa.read(); int id = LoRa.read();

if (localAddress == 1) { Serial.print("LoRa "); Serial.println(localAddress); Serial.print("For LoRa "); Serial.println(dest); Serial.print("ID = "); Serial.println(id); Serial.print("Message : "); while (LoRa.available()) { incoming = (char)LoRa.read(); Serial.print(incoming); }

Serial.println(""); Serial.print("RSSI : "); Serial.println(LoRa.packetRssi()); Serial.println(""); } } }

void sendMessage(String outgoing) { LoRa.beginPacket(); LoRa.write(local); LoRa.write(dest); LoRa.write(msgCount); LoRa.print(outgoing); LoRa.endPacket(); msgCount++; }`

Second :

`#include

include

int msgCount = 0; int local = 1; //ID of this device int dest = 2; //destination to send to long lastSendTime = 0; int interval = 2000; String incoming; String outgoing;

void setup() { Serial.begin(115200); while (!Serial); LoRa.begin(433E6); }

void loop() { if (millis() - lastSendTime > interval) { String message = "Hello"; sendMessage(message); Serial.println("Sending " + message); Serial.println(""); lastSendTime = millis(); interval = 2000; }

int packetSize = LoRa.parsePacket();

if (packetSize) { int localAddress = LoRa.read(); int dest = LoRa.read(); int id = LoRa.read();

if (localAddress == 2) { Serial.print("LoRa "); Serial.println(localAddress); Serial.print("For LoRa "); Serial.println(dest); Serial.print("ID = "); Serial.println(id); Serial.print("Message : "); while (LoRa.available()) { incoming = (char)LoRa.read(); Serial.print(incoming); }

Serial.println(""); Serial.print("RSSI : "); Serial.println(LoRa.packetRssi()); Serial.println(""); } } }

void sendMessage(String outgoing) { LoRa.beginPacket(); LoRa.write(local); LoRa.write(dest); LoRa.write(msgCount); LoRa.print(outgoing); LoRa.endPacket(); msgCount++; }`

Uploaded the code into arduinos. System works randomly. No receiving, but sending is changeable. Data sometimes is sending for once or more sometimes continuosly but never receiving.

My wire connections seems to be true because LoraSender and LoraReceiver codes work properly.

I dont understand why :(

BlackxRyan commented 6 years ago

Try to remove the verification function on each code just to see if your devices receive messages and if yes, what they receive.

if (localAddress == X) { }

(Just these lines, not what's inside.)

kadirozdinc commented 6 years ago

I tried what you suggested literally but still it doesn't work. This is what happened to my system :

I reset first arduino and approximately 0.5 sn later second arduino that connected to serial monitor. For this scenario, I can receive message truly but for once . If I firstly reset second arduino that connected to serial monitor, message cannot be taken but just sending. What I understand from this behavior of system is that There is some kind of trouble in shifting into either sender mode or receiver mode. It seems One arduino cannot have both ability somehow.

What are the possible mistake I can make ?

My wire connections seem true because I can perform LoraReceiver and LoraSender example successfully it works perfectly I got very good result like communicating from 7km far away for now.

Edit: I figure out the trouble. I change Arduinos and system start to work properly fortunately. Thank you so much for your interest :)

BlackxRyan commented 6 years ago

No problem, glad I could help you :)

halukmy commented 6 years ago

@BlackxRyan what if device node total is over 255?

i guess request page is only byte value 0 to 255

can i send 1055 and more devices?

what shoul di change?

BlackxRyan commented 6 years ago

I don't understand your question.

thannara123 commented 4 years ago

How clear it , iam facing same issue ?

thannara123 commented 4 years ago

I figure out the trouble. I change Arduinos and system start to work properly fortunately. i didnt understand

huaminghuangtw commented 3 years ago

@thannara123 Can you elaborate more how you solve the issue? I did sequentially/one-by-one compile the 2 sketch for 2 LoRa boards separately, but both of them still do the sending task, no receiving at all....