sandeepmistry / arduino-LoRa

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

lora communication #468

Closed hoseinaghajari closed 2 years ago

hoseinaghajari commented 3 years ago

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 can send data whenever I want and the important thing for me is Low power consumption SO I can set my device in SLEEP_MODE after sending message. But I don't know how can I have low power consumption in receiving mode? see this: Rx..................>> 55

in this mode the device is always ON. so how can I alert my device for receiving data? I mean me device always sleep until a receiving packet has arrive.

IoTThinks commented 3 years ago

Use receivedCallback instead. If DIO0 is high, wake the MCU up.

However, it will take 18mA when listening.

hoseinaghajari commented 3 years ago

@IoTThinks you mean that use onReceive() function? According this picture from library example. is it necessary to use LoRa.receive(); after onReceive() ? 9

if yes; what about loop function? if I want to send message and waiting for answer, So it is necessary to call LoRa.receive(); after onReceive() In loop function? like this: 10

IoTThinks commented 3 years ago

You can read duplex callback example.

Btw, there are two RX modes.

After sending a message, you must set LoRa.receive() if using RX continuous (aka Receive callback).

If you use Single RX, call parsePacket as frequent as possible.

IoTThinks commented 3 years ago

Note that you can only send or receive a packet at a time. Aka half duplex.

hoseinaghajari commented 3 years ago

my problem is the sender cant receive well after sending message. just that.

IoTThinks commented 3 years ago

Your initial code is wrong btw.

Show the real photo of your board and antenna.

If already use the exact sample codes, then try to use an external power for LoRa. Likely insufficient power.

Arduino Nano?

hoseinaghajari commented 3 years ago

arduino uno

sender code:

sender

receiver code.

rec

I think I have to add LoRa.onReceive(onReceive); LoRa.receive(); in void_setup() in sender. yes?

IoTThinks commented 3 years ago

Read this again https://github.com/sandeepmistry/arduino-LoRa/blob/master/examples/LoRaDuplexCallback/LoRaDuplexCallback.ino

onReceive(LoRa.parsePacket()) is wrong to me.

hoseinaghajari commented 3 years ago

thanks to you. I test again and tell you the result.

hoseinaghajari commented 3 years ago

how could I switch between tx mode and rx mode? this is from datasheet. Does library do this? wwwe

IoTThinks commented 3 years ago

You can read the source code of this library. Especially in BeginPacket (to send), ParsePacket(to receive in single RX) and LoRa.receive() (to receive in Continuous RX).

hoseinaghajari commented 3 years ago

I tested last night and I had problem again. I think the library can't support this kind of communication. I send message to node2 and node2 send his answer ( with Answer(); ) to node1. but after 3 or 4 times it corrupt. Node1: lnf_t

Node2: lnf2_t

the result from node1: LNF2 or: ln2

hoseinaghajari commented 3 years ago

I have good power, I have good connection, but why? why it can send 6 or 7 time at first?

IoTThinks commented 3 years ago

I believe you are a newbie in programming. No offence, btw. You need to try and explore for yourself a while. Use the exact examples at first.

BTW, the bellow codes may have problems. It will have problems depending on "chances".

Wrong usage: image

Wrong usage: image

and don't try to send inside an ISR. image

hoseinaghajari commented 3 years ago

it is same as library example, why you said its wrong usage? Library example: 68

MY CODE 119299033-6a4df480-bc88-11eb-9111-4338b892be47

hoseinaghajari commented 3 years ago

Is it need sth to add while changing tx mode to rx mode?

IoTThinks commented 3 years ago

How many times the LoRa.receive() is called in your code and in the example code in 3 seconds? LoRa.receive() is to set the LoRa into continuous RX mode.

You should find the datasheet of SX1276/1278 to read about the Single RX mode and Continuous mode. Read the source code and try the exact example code.

MY CODE 119299033-6a4df480-bc88-11eb-9111-4338b892be47

hoseinaghajari commented 3 years ago

hi guys. I fixed the problem and it work, as a reminder; node1 send hi to node2 and node2 answer it. it work now but take a look at my code please and tell me why ir stop working after 60 minutes? I Send 300 times at 1 hour. after around 300 times sending, the Node2 cant receive message. why?

Node1(Sender):

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

unsigned char Hi[] = "Hi I'm Node1";
String inString = "";

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

void loop() {

  if(runEvery(10000))
  Send(Hi);

   int packetSize = LoRa.parsePacket();
  if (packetSize){
  Receive();
  }
}

void  Send(String Byte){
 ​LoRa.beginPacket();
 ​LoRa.print(Byte);
 ​LoRa.endPacket();
 ​Serial.println(Byte);
 ​}

void Receive(){
    Serial.print("received:  ");
    String incoming = "";
  while (LoRa.available()) {
    incoming += (char)LoRa.read();
               }
Serial.println(incoming);             ​
        }  

  boolean runEvery(unsigned long interval){
  static unsigned long previousMillis = 0;
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval){
    previousMillis = currentMillis;
    return true;
  }
  return false;
} 

Node2(Receiver):

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

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

void loop() {
    int packetSize = LoRa.parsePacket();
  if(packetSize){
  Receive();
  Answer();
  }
}

void Send(String Byte){
  LoRa.beginPacket();
  LoRa.print(Byte);
  LoRa.endPacket();
  }

void Receive(){
  String incoming = "";
  while (LoRa.available()) {
    incoming += (char)LoRa.read();
  }
  Serial.print("Received: " + incoming);
  }

void Answer(){
    String sendANswer = "I got it";
     Serial.print("   Send: ");
               Serial.println(sendANswer);  
                Send(sendANswer);
}

after around 300 times sending, the Node2 cant receive message. why?

hoseinaghajari commented 3 years ago

Dear @sandeepmistry please help me.

IoTThinks commented 3 years ago

May be related and solved issue: https://github.com/sandeepmistry/arduino-LoRa/issues/363

hoseinaghajari commented 2 years ago

this problem was solved for me.