Send as frequently as possible and still not miss any incoming messages

Hi all,

First, thanks for your help with my previous questions. My two MKRWAN1310s are happily communicating.

I need to be able communicate both ways, and while sending data as fast as possible, I need to be able to not miss any incoming messages. (I know that part of the LoRa protocol is that datarate depends on conditions; that’s not what I’m talking about).

I think what I need is a better understanding of the LoRa library. Specifically, how can I make sure I don’t miss any incoming data?

Here is my test program. Note that I send data pretty infrequently. If I send data too frequently, I stop receiving data.

/*
 * Simple duplex communication example between two MKR WAN 1310
 * 
 * Load the same code into two devices and watch them communicate with each other
 * 
 * Change log:
 * 
 * 30 aug 2020 - ms - Created
 * 
 * This example is in the public domain
 */

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

void setup() {
  Serial.begin(9600);
  // while (!Serial); // optional

  Serial.println("LoRa MKR WAN 1310 Duplex");

  if (!LoRa.begin(915E6)) {
    Serial.println("Starting LoRa failed!");
    while (1);
  }
}

long timeLastSent = 0;
long interval = 1000;

void loop() {

  // don't send too often
  if (millis() - timeLastSent > interval) {
    sendTime();
    timeLastSent = millis();
    interval = 500 + random(1000); // a random interval each time
  }

  // but always be ready to receive
  checkReceive();

}

void checkReceive() {

  // Anything received?
  int packetSize = LoRa.parsePacket();
  
  if (packetSize) {
    Serial.print("Received packet ");
    while (LoRa.available()) {
      char inChar = (char)LoRa.read();
      Serial.print(inChar);
    }
    Serial.println();
  }
}

void sendTime() {
  long timeNow = millis();
  Serial.print("will send ");
  Serial.println(timeNow);

  LoRa.beginPacket();
  LoRa.print(timeNow);
  LoRa.endPacket();
}