sandeepmistry / arduino-LoRa

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

Sending while in receive callback #393

Closed markusschweitzer closed 3 years ago

markusschweitzer commented 4 years ago

Hi, Im using the latest version from git and this example: https://github.com/sandeepmistry/arduino-LoRa/blob/master/examples/LoRaReceiverCallback/LoRaReceiverCallback.ino

When I try to send a packet in the receive callback, (beginPacket, write, endPacket) the receiver sends the packet, but doesnt receive anymore.

Calling Lora.idle() and LoRa.receive() after the sending helps for a few packets, but eventually it stops receiving.

Any idea how I can make this work?

best regards, markus

IoTThinks commented 4 years ago

Did you connect MCU to LoRa DIO0? By right, in the library, the sending will be skipped if a packet is receiving.

After sending, should call LoRa.receive() again to go back RX mode.

markusschweitzer commented 4 years ago

Yes, the ISR gets called correctly (DIO0 is connected), I read all the data and send a response packet :/

IoTThinks commented 4 years ago

By right, you can not send a message during a receive. This is chip limitation.

Send after the receiving is complete. And sending outside the ISR callback.

markusschweitzer commented 4 years ago

Ok, I wasnt aware of that. Is there any way to do this without a periodic call in the loop() function?

IoTThinks commented 4 years ago

By right, you should not do any processing inside the ISR also. ISR should set a flag only. Then you can process sending or receiving in the main loop or in a FreeRTOS task. Or you can create FreeRTOS task in the ISR.

But again, ISR should be as simple as setting a flag only if possible. :D Make it works before making it nice. Put sending part in loop is simpler. Delay the TX a bit after the RX.

void onReceive(int packetSize) {
  // received a packet
  Serial.print("Received packet '");

  // read packet
  for (int i = 0; i < packetSize; i++) {
    Serial.print((char)LoRa.read());   <========= SHOULD NOT DO THIS IN ISR
  }

  // print RSSI of packet
  Serial.print("' with RSSI ");
  Serial.println(LoRa.packetRssi());
}
markusschweitzer commented 4 years ago

I moved everything to the loop method (now without the interrupt, just parsePacket).

How much should I delay the TX after the whole packet is read (with LoRa.read())? It seems to get better wehn I add mor Serial.println in between...

IoTThinks commented 4 years ago

100ms is more than enough.

IoTThinks commented 3 years ago

Seems solved.