ricaun / LoRaWanPacket

LoRaWanPacket Encode / Decoder
MIT License
10 stars 2 forks source link

Downlink problems #3

Closed Samuel-ZDM closed 3 years ago

Samuel-ZDM commented 3 years ago

I'm receiving 5 bytes via downlink and doing the transformation within the onReceive function:

void onReceive(int packetSize)
{
  busy = 0;
  Serial.println("Receive!");
  LoRaWanPacket.clear();
  uint8_t *bufferReceive;
  while (LoRa.available())
  {
    LoRaWanPacket.write(LoRa.read());
  }
  int port = LoRaWanPacket.decode();
  int length = LoRaWanPacket.length();
  switch (port)
  {
  case 0:
    break;
  case 1:
    Serial.print("Length = ");
    Serial.println(length);
    bufferReceive = LoRaWanPacket.buffer();
    Serial.print("Value INTEIRO = ");
    Serial.println(*bufferReceive);
    Serial.print("Value EM HEX = ");
    Serial.print(bufferReceive[0], HEX);
    Serial.print(bufferReceive[1], HEX);
    Serial.print(bufferReceive[2], HEX);

    if (bufferReceive[0] == 1)
    {
      Serial.println("Recebi");
      for (int i = 1; i < length; i++)
      {
        char str[1];

        Serial.print("Valor em Hex");

        Serial.println(bufferReceive[i], HEX);

        int lo = (char)(bufferReceive[i] & 0x0F);
        int hi = (char)((bufferReceive[i] >> 4) & 0x0F);

        sprintf(str, "%x", hi);
        TTN_response[j] = str[0];

        j++;

        sprintf(str, "%x", lo);
        TTN_response[j] = str[0];

        j++;

      }
       Serial.println("TTNResponse");
        Serial.println(String(TTN_response));

      j = 0;

      canSleep = 1;
    }
  }

  Serial.print("Value = ");
  Serial.println(value);
}

Even before this function is finished, the device resets and starts all over again. I don't know why you're giving this. In the device loop, it looks like this:

void loop ()
{
   if (canSleep &&! LoRa_busy ())
   {

     Serial.println ("Downlink value");
     canSleep = 0;
     valueRTC = strtoul (TTN_response, NULL, 16);

     Serial.println (valueRTC);
     Serial.println ("I'm going to enter the Time config");
     Serial.println ("Updating RTC");
     getDateToRTC (valueRTC);
     getTimeToRTC (valueRTC);
     delay (10);
     configTimeRTC ();

     confgRTC ();

     endProcess = 1;
   }
   LoRa_loop ();
   if (runEvery (5000))
   {
     if (! LoRa_busy ())
     {

       Serial.println ("Sleep");
       delay (10);
       LoRa_endSleep ();
       LoRa_sendMessage ();
       Serial.println ("Send Message!");
     }
   }
} 

I am doing this because I am updating an RTC timer.

ricaun commented 3 years ago

Hello,

Looking at the code looks ok, but I have some questions:

😄

Samuel-ZDM commented 3 years ago

What microcontroller are you using?

Atmega328p

Where on the code the mic is resetting?

When the device receives a message, it does not finish executing the onReceive function and restarts the card. The weird part of it all is that sometimes it happens sometimes it doesn't. That's why I put that if with the variable canSleep and the return of the Lora_Busy function, because I thought I wasn't finishing something and I was having a problem.

Could you share the Serial output?

Length = 5 12:33:57.403 -> Value INTEIRO = 1 12:33:57.436 -> Value EM HEX = 16081Recebi 12:33:57.469 -> Valor em Hex60 12:33:57.469 -> 0 12:33:57.502 -> Valor em Hex81 12:33:57.502 -> 1 12:33:57.502 -> Valor em HexE2 12:33:57.536 -> 2 12:33:57.536 -> Valo⸮ LoRa init succeeded. 12:33:57.735 -> 12:34:02.482 -> Sleep 12:34:02.516 -> 0.26

If you remove the RTC the code works?!

Yes, but I tested the RTC using only LoRa, without the LoRaWAN protocol and it worked. I also tested in an ESP using LMIC and it worked too.

ricaun commented 3 years ago

That's really strange, probably is something in your onReceive implementation.

Some invalid buffer maybe. I never see the atmega328p reset yourself.

Realy curious!

Samuel-ZDM commented 3 years ago

It worked! Error with serial communication. I believe she was crashing the system. I removed some Serial.println and it worked.

Thank you very much.