hi guys. I have confused these days by Lora Library. what I want to do is send and receive data with SX1278 Module(https://cutt.ly/zbDX8K4) and Arduino Uno… I send message to node2 and node2 send his answer ( with Answer(); ) to node1. but after 3 or 4 times it corrupt.
sender:
#include <SPI.h>
#include <LoRa.h>
unsigned char Hiall[] = "Gateway";
String Byte; // outgoing message
long lastSendTime = 0; // last send time
void setup(){
Serial.begin(9600);
while (!Serial);
Serial.println("LoRa gateway");
if (!LoRa.begin(433E6)) {
Serial.println("Starting LoRa failed!");
while (1);
}
LoRa.onReceive(onReceive);
LoRa.receive();
}
void loop() {
if(runEvery(3000)){
HiAll(Hiall);
}
LoRa.receive();
}
void onReceive(int packetSize){
if (packetSize == 0) return;
String incoming = "";
while (LoRa.available()){
incoming += (char)LoRa.read();
}
Serial.println(incoming);
}
void HiAll(String Byte){
LoRa.beginPacket(); // start packet
LoRa.print(Byte); // add payload
LoRa.endPacket(); // finish packet and send it
Serial.println("Sending " + Byte);
}
boolean runEvery(unsigned long interval)
{
static unsigned long previousMillis = 0;
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
return true;
}
return false;
}
receiver:
#include <SPI.h>
#include <LoRa.h>
unsigned long Time = 10000;
String outgoing; // outgoing message
long lastSendTime = 0; // last send time
int interval = 3000; // interval between sends
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.println("LoRa node");
if (!LoRa.begin(433E6)){
Serial.println("Starting LoRa failed!");
while (1);
}
LoRa.onReceive(onReceive);
LoRa.receive();
}
void loop() {
if(runEvery(3000+random(100))){
LoRa.receive();
}
}
void onReceive(int packetSize) {
if (packetSize == 0) return;
String incoming = "";
while (LoRa.available()) {
incoming += (char)LoRa.read();
}
Serial.print("Received: " + incoming);
Check();
}
void Send(String Byte){
LoRa.beginPacket(); // start packet
LoRa.print(Byte); // add payload
LoRa.endPacket(); // finish packet and send it
Serial.println("Sending "+Byte);
}
void Check(){
String ANSWER = "Receive: Im Node";
Send(ANSWER);
}
boolean runEvery(unsigned long interval){
static unsigned long previousMillis = 0;
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
return true;
}
return false;
}
result is:
Sending Gateway
receive: Im node
Sending Gateway
receive: Im node
Sending Gateway
receive: Im node
Sending Gateway
receive: Im node
Sending Gateway
receive: Im node
GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG
I have good power , good connection. i think the problem is working with LoRa.onreceive(); please help me. do you know better library?? or its about my code??