Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix duplicate packet reception on Arduino MKR 1310 #664

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mdias
Copy link

@mdias mdias commented Sep 6, 2023

Without this change there is a chance that we receive duplicate packets

@morganrallen
Copy link
Collaborator

Have you thoroughly checked the rest of your code to ensure something else isn't causing the duplicate? I've never encountered a situation where the IRQ doesn't clear.
You can test this by reading back the IRQ reg and printing out that value but note this also might fix your problem as it might just be the delay of two writes that's fixing and issue somewhere else.

@mdias
Copy link
Author

mdias commented Sep 6, 2023

The program I have that showed this happening rather easily was basically broadcasting a counter variable every second and checking for received packets all the time.
I have 2 Arduino MKR 1310 running the same program and both of them showed like 10-20% of the packets received being received twice.

I don't have enough knowledge to investigate further why the IRQs are not clearing, but after searching online I came across this issue describing the same problem but on a different board, and this fix was suggested. After trying the same fix on my board I could confirm that there are no longer duplicated received packets.

I understand that this looks like a hack and the problem may come from somewhere else, so I understand if you don't want to merge it as is. But the patch as is should also only affect the MKR 1310 board which is already misbehaving (the duplicates) with the original code.

@morganrallen
Copy link
Collaborator

There's an easy experiment you can try. Replace the new writeRegister call with delay(100). Does this also solve the problem?

@morganrallen
Copy link
Collaborator

It would also be helpful to understand this issue better if you could share the code around the call to endPacket

@mdias
Copy link
Author

mdias commented Sep 9, 2023

Replacing writeRegister with delay(100) results in some packets not being received at all, and some others are still duplicated.

My sample application is doing this when sending a packet:

void SendString(const char* str)
{
  digitalWrite(LED_BUILTIN, HIGH);

  LoRa.beginPacket();
  LoRa.print(str);
  LoRa.endPacket();

  digitalWrite(LED_BUILTIN, LOW);
}
...
static void sendData()
{
  char buf[16] = {0};
  if (secondsTimer()) // trigger only once a second
  {
    sprintf(buf, "%i", counter);
    DbgPrint("sending: "); // print debug info with Serial.print()
    DbgPrintLn(buf);

    ++counter;

    SendString(buf);
  }
}

On the receptor end, I receive data like so:

bool ReceiveString(String& outString)
{
  int packetSize = LoRa.parsePacket();
  if (packetSize <= 0)
  {
    return false;
  }

  while (LoRa.available())
  {  
    outString += (char)LoRa.read();
  }

  return true;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants