0

This is a simple Arduino program which reads RFID tag and sends the data over a network (lora).

The program works fine in as i run it, the arduino reads RFID and sends it over the network (lora)

but

after a few hours it stops responding it neither reads nor sends the data over network (lora network).

I am very new to arduino and have no idea what causes the box to stop responding, kindly help.

When i restart the boxes or upload the code to it again, it starts behaving normally for a few hours and then goes back to not responding.

    /*
   Typical pin layout used:
   -----------------------------------------------------------------------------------------
               MFRC522      Arduino       Arduino   Arduino    Arduino          Arduino
               Reader/PCD   Uno           Mega      Nano v3    Leonardo/Micro   Pro Micro
   Signal      Pin          Pin           Pin       Pin        Pin              Pin
   -----------------------------------------------------------------------------------------
   RST/Reset   RST          9             5         D9         RESET/ICSP-5     RST
   SPI SS      SDA(SS)      10            53        D10        10               10
   SPI MOSI    MOSI         11 / ICSP-4   51        D11        ICSP-4           16
   SPI MISO    MISO         12 / ICSP-1   50        D12        ICSP-1           14
   SPI SCK     SCK          13 / ICSP-3   52        D13        ICSP-3           15
   Red LED                  A0
   Green LED                A2
   Blue LED                 A3
*/
/***********************************************************************************************/
//#include <SPI.h>
#include <MFRC522.h>
//#include <HID.h>

// Cooking API libraries
//#include <arduinoUtils.h>
//#include <arduinoUART.h>
//#include <arduinoMultiprotocol.h>
#include <arduinoLoRaWAN.h>

#define RST_PIN         9          // Configurable, see typical pin layout above
#define SS_PIN          10         // Configurable, see typical pin layout above
#define MAX             10         // Maximum Buffer size
#undef Debugg
/******** RFID Reader initialization**********************************************/
MFRC522 mfrc522(SS_PIN, RST_PIN);  // Create MFRC522 instance
//char rfid_old[MAX];
byte *buffer;

/******************************LoRa Initialization********************************/
// Pin definition for LoRaWAN module error LED:
const int error_led =  7;
//////////////////////////////////////////////
uint8_t socket = SOCKET0;
//////////////////////////////////////////////

// Device parameters for Back-End registration
////////////////////////////////////////////////////////////
char DEVICE_EUI[] = "0000000097E471E1"; // 0000000097E471E1 / 00000000518AD184
char DEVICE_ADDR[] = "97E471E1"; // 97E471E1 / 518AD184
char NWK_SESSION_KEY[] = "00EBB4C1FCDFF52878B1725CBDFF0BA1"; // Configured for TTN
char APP_SESSION_KEY[] = "4EDC3AB4F79C565685109ECD4AF4604C"; // Configured for TTN
char APP_KEY[] = "70B3D57ED0000300"; //APP_EUI
////////////////////////////////////////////////////////////
// Define port to use in Back-End: from 1 to 223
uint8_t PORT = 1;

// variable
uint8_t error;
uint32_t cFreq[] = { 867100000, 867300000, 867500000, 867700000, 867900000 };
char rfid_arr[MAX];
char stampleErrorChar[] = "4AD6A6A65784AEBA2B"; // this data is sent from the WService When Stample Error-> Error
/* *******************************START Initialize Indication LEDs******************************* */
int greenPin = 6;
int redPin = 8;

void setup() 
{
  Serial.begin(57600);
  //LoRaWAN example - Send Unconfirmed packets (no ACK)
  pinMode(error_led, OUTPUT);
  digitalWrite(error_led, LOW);

  LoRaSetup();
  RfidSetup();
}

//-----------------------------------------------------------------------------
// Name.: RfidSetup
// Descr: 
//
// In...:
// Out..:
// Ret..:
//-----------------------------------------------------------------------------
void RfidSetup()
{
  SPI.begin();                        // Init SPI bus
  mfrc522.PCD_Init();                 // Init MFRC522
  mfrc522.PCD_DumpVersionToSerial();  // Show details of PCD - MFRC522 Card Reader details
}
void LoRaSetup()
{
  LoRaWAN.ON(socket);
  LoRaWAN.factoryReset();

  // Channel parameters
  for (uint8_t i = 3; i < 8; i++) {
    LoRaWAN.setChannelStatus(i, (char*)"on");
    LoRaWAN.setChannelFreq(i, cFreq[i - 3]);
  }
  for (uint8_t i = 0; i < 3; i++) {
    LoRaWAN.setChannelDutyCycle(i, 302);
  }

  for (uint8_t i = 3; i < 8; i++) {
    LoRaWAN.setChannelDutyCycle(i, 99);
  }

  LoRaWAN.setPower(5); // [N/A, 14, 11, 8, 5, 2] dBm
  LoRaWAN.setADR((char*)"off"); // Adaptive data rate
  LoRaWAN.setDataRate(5); // [250, 440, 980, 1760, 3125, 5470, 11000];
  LoRaWAN.setDeviceEUI(DEVICE_EUI);
  //showErrorResult((char*)"2. Set Device EUI ", LoRaWAN.getDeviceEUI());
  LoRaWAN.setAppEUI(APP_KEY);
  //showErrorResult((char*)"3. Set APP EUI ", LoRaWAN.getAppEUI());
  LoRaWAN.setDeviceAddr(DEVICE_ADDR);

  LoRaWAN.setNwkSessionKey(NWK_SESSION_KEY);

  LoRaWAN.setAppSessionKey(APP_SESSION_KEY);
  //showErrorResult((char*)"6. Set App Session Key ",error);
  LoRaWAN.setRetries(3);
  LoRaWAN.saveConfig();
  LoRaWAN.joinABP();
}


void loop() 
{
  // Look for new cards
  if (! mfrc522.PICC_IsNewCardPresent()) {    
    return;
  }

  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) {
    return;
  }
  else
  {
     error = LoRaWAN.joinABP();
     if (error == 0) 
     {
       char* UId = GetUid(mfrc522);    
       if(UId != NULL)
       {
          BlinkGreen(500);
          delay(100);
          // Send it!
          error = LoRaWAN.sendUnconfirmed(PORT, UId);
          Serial.println(error);
          delay(100);
          if(error == 0)
          {
             Serial.println(LoRaWAN._dataReceived);
             BlinkGreen(1000);
          }
          else
          {
             BlinkRed(1000);
          }
        }
        delay(2000);
     }   
  }
}

char* GetUid(MFRC522 mfrc522)
{
    // Optional: Dump data about the cards
    //mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
    buffer = mfrc522.uid.uidByte;

    for (int i = 0; i < mfrc522.uid.size; i++)
    {
      sprintf(&rfid_arr[i * 2], "%02X", mfrc522.uid.uidByte[i]);
    }
    mfrc522.PICC_HaltA(); //command the card into sleep mode                                                                          
    return rfid_arr;
}

void BlinkGreen(int delayTime)
{
  digitalWrite(greenPin, HIGH);
  delay(delayTime);
  digitalWrite(greenPin, LOW);
}

void BlinkRed(int delayTime)
{
  digitalWrite(redPin, HIGH);
  delay(delayTime);
  digitalWrite(redPin, LOW);
}
void showErrorResult(char* errorMessage, uint8_t error)
{
  if(error == 0)
  {
    Serial.print(errorMessage);
    Serial.println("OK!");
  }
  else
  {
    Serial.print(errorMessage);
    Serial.print(" Error: ");
    Serial.println(error);  
  }
}
2
  • A pure guess from your symptoms would be either a memory leak or a counter overflowing. I'll look at the code and get back to you. Sep 19, 2016 at 14:25
  • Is you Arduino overheating? can you start a second couple of hour run within minutes of the first? Sep 19, 2016 at 15:54

2 Answers 2

1

@Matt,

thanks guys. But if rfid_arr size was an issue, wouldn't the box be giving problem the first time we use it? because it works for few hours before it goes into not responding state. The rfid tag data looks like below AEA00622 DE1CD403

0

The bad news is I can't see that much wrong with your code.

  1. You need to set the Red and Green pinModes(). You've set error, but then don't use it. I'm guessing the green LED is a new feature. similarly you could refactor the blink green and blink red functions into one and pass in the pin number.
  2. Are you sure that the mfrc522.uid.size is always less than 5? In GetUid() you assume that i*2 will fit into the buffer rfid_arr which is 10 bytes. sprint() always attaches a null onto the end of what it rights to the buffer so this will overflow if you write elements 9 and 10.

I really doubt either of those are the problem you are looking for, so would it be possible to stick loads of Serial.println() statements in and see where abouts it is crashing?

4
  • 1
    Make sure you put Serial.flush() after each Serial.println() or the crash itself may truncate your serial output and leave you looking in the wrong place.
    – Majenko
    Sep 19, 2016 at 14:53
  • 1
    Reply, thanks guys. But if rfid_arr size was an issue, wouldn't the box be giving problem the first time we use it? because it works for few hours before it goes into not responding state. The rfid tag data looks like below AEA00622 DE1CD403 Sep 19, 2016 at 15:17
  • Matt, I did not get you mate. I have not changed anything in the code and the issue is still there. What do you see is wrong? or more precisely what shall i change in the code? The rfid data looks like DE1CD403 Thanks a lot for your coooperation Sep 19, 2016 at 15:44
  • (I misread you comment, so...) What is the length of the RFID data is it 4 bytes or 8 bytes? Is mfrc522.uid.uidByte[i] an unsigned byte? if so your format string should be "%02hX". Otherwise if the value of mfrc522.uid.uidByte[i] is 0xFF then you will end up with rfid_arr[i * 2] = "0xFFFFFFFF" which if its the last byte will extend the array by 6 characters. So if the RFID length if 4 bytes its a data dependent error and if its 8 bytes then its just a question of time. Sep 19, 2016 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.