Adafruit Datalogger Randomly Stops - Code Issue?

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
CodenameAter
 
Posts: 23
Joined: Tue Jun 09, 2015 11:20 am

Adafruit Datalogger Randomly Stops - Code Issue?

Post by CodenameAter »

Hello,

I started noticing some of my Dataloggers (https://www.adafruit.com/products/1141) stop working after a while. Some days they run for a few days and others they don't even run for the entire day. However, as soon as I power off my Mega2560 and turn it back on, it appears to be working again. My understanding from reading around is there is some sort of limitation on how much I can actually write to an SD card but I may be misinformed. Is there way to work around this?

Hardware: Mega2560 with an Adafruit Datalogger, Seeedstudio 125kHz RFID modules connected to all four serial ports.

Here is the part of the code for the Datalogger:

Code: Select all

//////////Time to SD Code//////////
void timeToSd() {
      if (dataFile) {
        //Serial.begin(57600);
        DateTime now = RTC.now();
        Serial.print(now.month(), DEC);
        Serial.print("/");
        Serial.print(now.day(), DEC);
        Serial.print("/");
        Serial.print(now.year(), DEC);
        Serial.print(" | ");
        Serial.print(now.hour(), DEC);
        Serial.print(":");
        Serial.print(now.minute(), DEC);
        Serial.print(":");
        Serial.println(now.second(), DEC);
        Serial.println("");
          dataFile.print(now.month(), DEC);
          dataFile.print('/');
          dataFile.print(now.day(), DEC);
          dataFile.print('/');
          dataFile.print(now.year(), DEC);
          dataFile.print(',');
          dataFile.print(now.hour(), DEC);
          dataFile.print(':');
          dataFile.print(now.minute(), DEC);
          dataFile.print(':');
          dataFile.print(now.second(), DEC);
          dataFile.println(","); //"println" should be at the end, not at the "Time: "
                XBee.print(now.month(), DEC);
                XBee.print('/');
                XBee.print(now.day(), DEC);
                XBee.print('/');
                XBee.print(now.year(), DEC);
                XBee.print(',');
                XBee.print(now.hour(), DEC);
                XBee.print(':');
                XBee.print(now.minute(), DEC);
                XBee.print(':');
                XBee.print(now.second(), DEC);
                XBee.println(",");  
        } else {
        // if the file didn't open, print an error:
        Serial.println("error opening test.txt");
        }
}

Here is the entire source code. However, it is really long and repetitive.

Code: Select all


// RFID reader for Arduino
// Wiring version by BANNED <http://people.interaction-ivrea.it/h.BANNED>
// Modified for Arudino by djmatic
// Modified for Electronic Brick 125Khz by Ujwal2


#include <EEPROM.h> 
#include <SD.h>
#include <SPI.h>
#include <Wire.h>
#include <RTClib.h>
#include <SoftwareSerial.h>
File dataFile;

int val = 0;
char code[10];
int bytesread = 0;
File myFile;
RTC_DS1307 RTC;
SoftwareSerial XBee(2, 3); 


//New Block Marker// - DO NOT TOUCH
char NewD[]="NewDataBlock";           

//Aviary// CHANGE FOR AVIARY
char Avy[]="COOP"; //ex. COOP for Barn Coop

//Empty Data Field//
char BkN[]="N/A"; //ex. ="N/A";

//Location IDs// UNIQUE TO ARDUINO
char P0_L[]="Bowl_01"; //Location 0 Name
char P1_L[]="Bowl_02"; //Location 1 Name (ex. Nest_1)
char P2_L[]="Bowl_03"; //Location 2 Name (ex. Nest_2)
char P3_L[]="Bowl_04"; //Location 3 Name (ex. Nest_3)

//Location Type// UNIQUE TO ARDUINO
char P0_T[]="N/A"; //Location 0 Type
char P1_T[]="N/A"; //Location 1 Name
char P2_T[]="N/A"; //Location 2 Name
char P3_T[]="N/A"; //Location 3 Name

//////////Time to SD Code//////////
void timeToSd() {
      if (dataFile) {
        //Serial.begin(57600);
        DateTime now = RTC.now();
        Serial.print(now.month(), DEC);
        Serial.print("/");
        Serial.print(now.day(), DEC);
        Serial.print("/");
        Serial.print(now.year(), DEC);
        Serial.print(" | ");
        Serial.print(now.hour(), DEC);
        Serial.print(":");
        Serial.print(now.minute(), DEC);
        Serial.print(":");
        Serial.println(now.second(), DEC);
        Serial.println("");
          dataFile.print(now.month(), DEC);
          dataFile.print('/');
          dataFile.print(now.day(), DEC);
          dataFile.print('/');
          dataFile.print(now.year(), DEC);
          dataFile.print(',');
          dataFile.print(now.hour(), DEC);
          dataFile.print(':');
          dataFile.print(now.minute(), DEC);
          dataFile.print(':');
          dataFile.print(now.second(), DEC);
          dataFile.println(","); //"println" should be at the end, not at the "Time: "
                XBee.print(now.month(), DEC);
                XBee.print('/');
                XBee.print(now.day(), DEC);
                XBee.print('/');
                XBee.print(now.year(), DEC);
                XBee.print(',');
                XBee.print(now.hour(), DEC);
                XBee.print(':');
                XBee.print(now.minute(), DEC);
                XBee.print(':');
                XBee.print(now.second(), DEC);
                XBee.println(",");  
        } else {
        // if the file didn't open, print an error:
        Serial.println("error opening test.txt");
        }
}

////////////////////////////////////////////////////////////////////////////////////////////////////

void setup() {
  XBee.begin(9600);
  Serial.begin(9600); // RFID reader works at 9600 BAUD rate
  Serial1.begin(9600);
  Serial2.begin(9600);
  Serial3.begin(9600);
  Wire.begin();
  RTC.begin();
  
 
  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(SS, OUTPUT);
  
  // see if the card is present and can be initialized:
  if (!SD.begin(10,11,12,13)) { // this will need to be changed to chipselect for non-mega boards
    Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1) ;
  }
  Serial.println("card initialized.");
  Serial.println("");
  
  // Open up the file we're going to log to!
  dataFile = SD.open("datalog.txt", FILE_WRITE);
  if (! dataFile) {
    Serial.println("error opening datalog.txt");
    // Wait forever since we cant write data
    while (1) ; 
  }else {
    
    DateTime now = RTC.now();
    //New Data Block 0 Pin//
    Serial.print("Starting New Data Block: "); 
    Serial.print(Avy);
    Serial.print(" , ");
    Serial.print(BkN);
    Serial.print(" , ");
    Serial.print(P0_L);
    Serial.print(" , ");
    Serial.print(P0_T);
    Serial.print(" , ");
      dataFile.print(Avy);
      dataFile.print("-");
      dataFile.print(NewD);
      dataFile.print(",");
      dataFile.print(BkN);
      dataFile.print(",");
      dataFile.print(P0_L);
      dataFile.print(",");
      dataFile.print(P0_T);
      dataFile.print(",");
          XBee.print(Avy);
          XBee.print("-");
          XBee.print(NewD);
          XBee.print(",");
          XBee.print(BkN);
          XBee.print(",");
          XBee.print(P0_L);
          XBee.print(",");
          XBee.print(P0_T);
          XBee.print(",");
    
      timeToSd();
    //New Data Block 1st Pin//
    Serial.print("Starting New Data Block: "); 
    Serial.print(Avy);
    Serial.print(" , ");
    Serial.print(BkN);
    Serial.print(" , ");
    Serial.print(P1_L);
    Serial.print(" , ");
    Serial.print(P1_T);
    Serial.print(" , ");
      dataFile.print(Avy);
      dataFile.print("-");
      dataFile.print(NewD);
      dataFile.print(",");
      dataFile.print(BkN);
      dataFile.print(",");
      dataFile.print(P1_L);
      dataFile.print(",");
      dataFile.print(P1_T);
      dataFile.print(",");
          XBee.print(Avy);
          XBee.print("-");
          XBee.print(NewD);
          XBee.print(",");
          XBee.print(BkN);
          XBee.print(",");
          XBee.print(P1_L);
          XBee.print(",");
          XBee.print(P1_T);
          XBee.print(",");

        timeToSd();
    //New Data Block 2nd Pin//
    Serial.print("Starting New Data Block: "); 
    Serial.print(Avy);
    Serial.print(" , ");
    Serial.print(BkN); 
    Serial.print(" , ");
    Serial.print(P2_L);
    Serial.print(" , ");
    Serial.print(P2_T);
    Serial.print(" , ");
      dataFile.print(Avy);
      dataFile.print("-");
      dataFile.print(NewD);
      dataFile.print(",");
      dataFile.print(BkN);
      dataFile.print(",");
      dataFile.print(P2_L);
      dataFile.print(",");
      dataFile.print(P2_T);
      dataFile.print(",");
            XBee.print(Avy);
            XBee.print("-");
            XBee.print(NewD);
            XBee.print(",");
            XBee.print(BkN);
            XBee.print(",");
            XBee.print(P2_L);
            XBee.print(",");
            XBee.print(P2_T);
            XBee.print(",");
        timeToSd();
    //New Data Block 3rd Pin//
    Serial.print("Starting New Data Block: "); 
    Serial.print(Avy);
    Serial.print(" , ");
    Serial.print(BkN);
    Serial.print(" , ");
    Serial.print(P3_L);
    Serial.print(" , ");
    Serial.print(P3_T);
    Serial.print(" , ");
      dataFile.print(Avy);
      dataFile.print("-");
      dataFile.print(NewD);
      dataFile.print(",");
      dataFile.print(BkN);
      dataFile.print(",");
      dataFile.print(P3_L);
      dataFile.print(",");
      dataFile.print(P3_T);
      dataFile.print(",");
            XBee.print(Avy);
            XBee.print("-");
            XBee.print(NewD);
            XBee.print(",");
            XBee.print(BkN);
            XBee.print(",");
            XBee.print(P3_L);
            XBee.print(",");
            XBee.print(P3_T);
            XBee.print(",");
        timeToSd();
  }
}

//////////////////////////////////////////////////////////////////////////////////////////////
 
void loop() {
  readRfidPort1();
  readRfidPort4();
  readRfidPort3();
  readRfidPort2();
}  


//////////////////////////////////////////////////////////////////////////////////////////////
void readRfidPort1() { //1st Pin (RX = pin 0 & TX = pin 1)
  if(Serial.available() > 0) { // if data available from reader
    if((val = Serial.read()) == 2) { // check for header
    bytesread = 0;
    while(bytesread<10) { // read 10 digit code
      if( Serial.available() > 0) {
        val = Serial.read();
          if((val == 2)||(val == 3)) { // if header or stop bytes before the 10 digit reading
          break; // stop reading
          }
          code[bytesread] = val; // add the digit
          bytesread++; // ready to read next digit
      }
    }
    if(bytesread == 10) { // if 10 digit read is complete
    //codeToSd();
    //timeToSd();
    Serial.print("RFID Aviary-Tag: ");
    Serial.print(Avy);
    Serial.print("-");
    Serial.println(code);
    Serial.print("No Data: ");
    Serial.println(BkN);
    Serial.print("Location & Location Type: "); 
    Serial.print(P0_L);
    Serial.print(" | ");
    Serial.println(P0_T);
    Serial.print("Writing Time to File: ");  
    digitalWrite(13, HIGH); //indicate it by glowing LED at pin 13
        delay(1500); // delay 1.5 seconds
        dataFile.print(Avy);
        dataFile.print("-");
        dataFile.print(code);
        dataFile.print(",");
        dataFile.print(BkN);
        dataFile.print(",");
        dataFile.print(P0_L);
        dataFile.print(",");
        dataFile.print(P0_T);
        dataFile.print(",");
            XBee.print(Avy);
            XBee.print("-");
            XBee.print(code);
            XBee.print(",");
            XBee.print(BkN);
            XBee.print(",");
            XBee.print(P0_L);
            XBee.print(",");
            XBee.print(P0_T);
            XBee.print(",");            
    timeToSd();
    dataFile.flush();
    digitalWrite(13, LOW); // glow off LED
    }
    bytesread = 0;
    }
  
} 
}
//////////////////////////////////////////////////////////////////////////////////////////////

void readRfidPort4 () { // 2nd Pin (RX = pin 15 & TX = pin 14)
  if(Serial3.available() > 0) { // if data available from reader
    if((val = Serial3.read()) == 2) { // check for header
    bytesread = 0;
    while(bytesread<10) { // read 10 digit code
      if( Serial3.available() > 0) {
        val = Serial3.read();
          if((val == 2)||(val == 3)) { // if header or stop bytes before the 10 digit reading
          break; // stop reading
          }
          code[bytesread] = val; // add the digit
          bytesread++; // ready to read next digit
      }
    }
    if(bytesread == 10) { // if 10 digit read is complete
    //codeToSd();
    //timeToSd();
    Serial.print("RFID Aviary-Tag: ");
    Serial.print(Avy);
    Serial.print("-");
    Serial.println(code);
    Serial.print("No Data: ");
    Serial.println(BkN);
    Serial.print("Location & Location Type: "); 
    Serial.print(P1_L);
    Serial.print(" | ");
    Serial.println(P1_T);
    Serial.print("Writing Time to File: ");  
    digitalWrite(13, HIGH); //indicate it by glowing LED at pin 13
        delay(1500); // delay 1.5 seconds
        dataFile.print(Avy);
        dataFile.print("-");
        dataFile.print(code);
        dataFile.print(",");
        dataFile.print(BkN);
        dataFile.print(",");
        dataFile.print(P1_L);
        dataFile.print(",");
        dataFile.print(P1_T);
        dataFile.print(",");
            XBee.print(Avy);
            XBee.print("-");
            XBee.print(code);
            XBee.print(",");
            XBee.print(BkN);
            XBee.print(",");
            XBee.print(P1_L);
            XBee.print(",");
            XBee.print(P1_T);
            XBee.print(",");            
    timeToSd();
    dataFile.flush();
    digitalWrite(13, LOW); // glow off LED
    }
    bytesread = 0;
    }
  
} 
}

//////////////////////////////////////////////////////////////////////////////////////////////

void readRfidPort3 () { // 3rd Pin (RX = pin 17 & TX = pin 16)
  if(Serial2.available() > 0) { // if data available from reader
    if((val = Serial2.read()) == 2) { // check for header
    bytesread = 0;
    while(bytesread<10) { // read 10 digit code
      if( Serial2.available() > 0) {
        val = Serial2.read();
          if((val == 2)||(val == 3)) { // if header or stop bytes before the 10 digit reading
          break; // stop reading
          }
          code[bytesread] = val; // add the digit
          bytesread++; // ready to read next digit
      }
    }
    if(bytesread == 10) { // if 10 digit read is complete
    //codeToSd();
    //timeToSd();
     Serial.print("RFID Aviary-Tag: ");
    Serial.print(Avy);
    Serial.print("-");
    Serial.println(code);
    Serial.print("No Data: ");
    Serial.println(BkN);
    Serial.print("Location & Location Type: "); 
    Serial.print(P2_L);
    Serial.print(" | ");
    Serial.println(P2_T);
    Serial.print("Writing Time to File: ");  
    digitalWrite(13, HIGH); //indicate it by glowing LED at pin 13
        delay(1500); // delay 1.5 seconds
        dataFile.print(Avy);
        dataFile.print("-");
        dataFile.print(code);
        dataFile.print(",");
        dataFile.print(BkN);
        dataFile.print(",");
        dataFile.print(P2_L);
        dataFile.print(",");
        dataFile.print(P2_T);
        dataFile.print(",");
            XBee.print(Avy);
            XBee.print("-");
            XBee.print(code);
            XBee.print(",");
            XBee.print(BkN);
            XBee.print(",");
            XBee.print(P2_L);
            XBee.print(",");
            XBee.print(P2_T);
            XBee.print(",");          
    timeToSd();
    dataFile.flush();
    digitalWrite(13, LOW); // glow off LED
    }
    bytesread = 0;
    }
  
}
}

////////////////////////////////////////////////////////////////////////////////////////////// 

void readRfidPort2() { // 4th Pin (RX = pin 19 & TX = pin 18)
  if(Serial1.available() > 0) { // if data available from reader
    if((val = Serial1.read()) == 2) { // check for header
    bytesread = 0;
    while(bytesread<10) { // read 10 digit code
      if( Serial1.available() > 0) {
        val = Serial1.read();
          if((val == 2)||(val == 3)) { // if header or stop bytes before the 10 digit reading
          break; // stop reading
          }
          code[bytesread] = val; // add the digit
          bytesread++; // ready to read next digit
      }
    }
    if(bytesread == 10) { // if 10 digit read is complete
    //codeToSd();
    //timeToSd();
    Serial.print("RFID Aviary-Tag: ");
    Serial.print(Avy);
    Serial.print("-");
    Serial.println(code);
    Serial.print("No Data: ");
    Serial.println(BkN);
    Serial.print("Location & Location Type: "); 
    Serial.print(P3_L);
    Serial.print(" | ");
    Serial.println(P3_T);
    Serial.print("Writing Time to File: ");  
    digitalWrite(13, HIGH); //indicate it by glowing LED at pin 13
        delay(1500); // delay 1.5 seconds
        dataFile.print(Avy);
        dataFile.print("-");
        dataFile.print(code);
        dataFile.print(",");
        dataFile.print(BkN);
        dataFile.print(",");
        dataFile.print(P3_L);
        dataFile.print(",");
        dataFile.print(P3_T);
        dataFile.print(",");
            XBee.print(Avy);
            XBee.print("-");
            XBee.print(code);
            XBee.print(",");
            XBee.print(BkN);
            XBee.print(",");
            XBee.print(P3_L);
            XBee.print(",");
            XBee.print(P3_T);
            XBee.print(",");     
   
   timeToSd();
   dataFile.flush();
   digitalWrite(13, LOW); // glow off LED
    }
    bytesread = 0;
    }
  
  }
}




User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: Adafruit Datalogger Randomly Stops - Code Issue?

Post by adafruit_support_mike »

There may be limits to the amount of data you can send to an SD card with one function call, but you should be able to write data to a file indefinitely. Individual .print() calls shouldn't cause any problems.

I'd be more inclined to look for a dip in the power to the microcontroller on the hardware side, or a memory leak on the software side. This tutorial shows how to track free memory:

https://learn.adafruit.com/memories-of-an-arduino/

User avatar
jim_lee
 
Posts: 709
Joined: Thu May 24, 2012 8:24 pm

Re: Adafruit Datalogger Randomly Stops - Code Issue?

Post by jim_lee »

You know, if you changed
void readRfidPortX();
to
void readRfidPort(SoftwareSerial* serialPort);

you could probably get away with just one of them. Would be a lot easier to maintain.

..just a thought. Haven't found any glaring issues yet.

-jim lee

User avatar
CodenameAter
 
Posts: 23
Joined: Tue Jun 09, 2015 11:20 am

Re: Adafruit Datalogger Randomly Stops - Code Issue?

Post by CodenameAter »

So I finally got around to testing this. About a week after I posted this my entire project was taking down and put on the back burner.

So, after testing the last few days, I found only some data loggers have this problem. I've flipped our power sources and microcontrollers, it is still some data loggers causing the issues whereas others work perfectly fine. To me, this would suggest that it is not likely a memory problem if the data logger is causing the issue. I've taken the three data loggers out for now and am using the four that work but I own 7 and need four. I can't rule out it isn't an issue that appears overtime but I know for sure, one of the four data loggers still deployed was one of the first I ever bought.

Is there anything potentially isolated to the data logger that could cause this issue?

User avatar
CodenameAter
 
Posts: 23
Joined: Tue Jun 09, 2015 11:20 am

Re: Adafruit Datalogger Randomly Stops - Code Issue?

Post by CodenameAter »

jim_lee wrote:You know, if you changed
void readRfidPortX();
to
void readRfidPort(SoftwareSerial* serialPort);

you could probably get away with just one of them. Would be a lot easier to maintain.

..just a thought. Haven't found any glaring issues yet.

-jim lee
Thanks Jim. So I tried to go over your recommendation last night once I brought my units back from the field.

If I understand correctly, if I replace:

Code: Select all

void readRfidPort()
with...

Code: Select all

void readRfidPort(SoftwareSerial* serialPort)
If I understand this correctly, this would avoid me having four different sections for each port and compile them all into one.

My only concern is that for each port, it is assigned to a specific location and has its own individual identification. How would I change the script so that if Serial 2, 3 or 4 are triggered, it would add in the appropriate location name?

FYI, the part I am referring to is the (P0_L)

Code: Select all

  //New Data Block 0 Pin//
    Serial.print("Starting New Data Block: "); 
    Serial.print(Avy);
    Serial.print(" , ");
    Serial.print(BkN);
    Serial.print(" , ");
    Serial.print(P0_L);
    Serial.print(" , ");
    Serial.print(P0_T);
    Serial.print(" , ");
      dataFile.print(Avy);
      dataFile.print("-");
      dataFile.print(NewD);
      dataFile.print(",");
      dataFile.print(BkN);
      dataFile.print(",");
      dataFile.print(P0_L);
      dataFile.print(",");
      dataFile.print(P0_T);
      dataFile.print(",");

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”