Go Down

Topic: Which SD library to create file with date? (Read 11277 times) previous topic - next topic

Stan09

I have  Adafruit datalogger shield with real-time clock.
I need create files on SD with correct file creation dates. As I understand, SD library recommended by Adafruit cannot create files with dates.
With so many SD libraries available which one I should stick to for creating a file with proper date?

fat16lib

Sdfat http://code.google.com/p/sdfatlib/downloads/list supports timestamps on the Adafruit Data Logging Shield.

Look at the AnalogLogger example in the SdFat/examples folder. 

Set USE_DS1307 nonzero at line 7
Code: [Select]
#define USE_DS1307       1  // set nonzero to use DS1307 RTC

At line 40 change the comments like this:
Code: [Select]

//#error remove this line and uncomment the next two lines.
#include <Wire.h>
#include <RTClib.h>

MarkT

Apropos of this, I had a go at using the file timestamp support in the Arduino 0022 SD library but couldn't figure it out - there are various functions in the source that suggest a current time callback is supported - is this so and if so how to use it?
[ I won't respond to messages, use the forum please ]

fat16lib

#3
Sep 20, 2011, 05:08 pm Last Edit: Sep 20, 2011, 05:10 pm by fat16lib Reason: 1
The SD.h 0022 library is a wrapper for an old version of SdFat.  The Arduino developers did not choose to expose timestamp callback support in their wrapper.

Here is a sketch that works with their 0022 version of SD.h.  It will produce a file, TEST_SD.TXT, that will be timestamped with creation and modify time.  Modify time will be maintained if you write the file.
Code: [Select]
#include <SD.h>
#include <Wire.h>
#include <RTClib.h>

File file;  // test file
const uint8_t SD_CS = 10; // SD chip select
RTC_DS1307 RTC;  // define the Real Time Clock object
//------------------------------------------------------------------------------
// call back for file timestamps
void dateTime(uint16_t* date, uint16_t* time) {
 DateTime now = RTC.now();

 // return date using FAT_DATE macro to format fields
 *date = FAT_DATE(now.year(), now.month(), now.day());

 // return time using FAT_TIME macro to format fields
 *time = FAT_TIME(now.hour(), now.minute(), now.second());
}
//------------------------------------------------------------------------------
void setup() {
 Serial.begin(9600);
 Wire.begin();
 if (!RTC.begin()) {
   Serial.println("RTC failed");
   while(1);
 };
 // set date time callback function
 SdFile::dateTimeCallback(dateTime);

 if (!SD.begin(SD_CS)) {
   Serial.println("SD.begin failed");
   while(1);
 }
 file = SD.open("TEST_SD.TXT", FILE_WRITE);
 file.close();
 Serial.println("Done");
}
//------------------------------------------------------------------------------
void loop() {}

lemorlenny

Many thanks for this info!.

Lenny


The SD.h 0022 library is a wrapper for an old version of SdFat.  The Arduino developers did not choose to expose timestamp callback support in their wrapper.

Here is a sketch that works with their 0022 version of SD.h.  It will produce a file, TEST_SD.TXT, that will be timestamped with creation and modify time.  Modify time will be maintained if you write the file.
Code: [Select]
#include <SD.h>
#include <Wire.h>
#include <RTClib.h>

File file;  // test file
const uint8_t SD_CS = 10; // SD chip select
RTC_DS1307 RTC;  // define the Real Time Clock object
//------------------------------------------------------------------------------
// call back for file timestamps
void dateTime(uint16_t* date, uint16_t* time) {
 DateTime now = RTC.now();

 // return date using FAT_DATE macro to format fields
 *date = FAT_DATE(now.year(), now.month(), now.day());

 // return time using FAT_TIME macro to format fields
 *time = FAT_TIME(now.hour(), now.minute(), now.second());
}
//------------------------------------------------------------------------------
void setup() {
 Serial.begin(9600);
 Wire.begin();
 if (!RTC.begin()) {
   Serial.println("RTC failed");
   while(1);
 };
 // set date time callback function
 SdFile::dateTimeCallback(dateTime);

 if (!SD.begin(SD_CS)) {
   Serial.println("SD.begin failed");
   while(1);
 }
 file = SD.open("TEST_SD.TXT", FILE_WRITE);
 file.close();
 Serial.println("Done");
}
//------------------------------------------------------------------------------
void loop() {}


Jantje

Is this still the way to add timestamps to sd files with Arduino 1.0 or later?
Best regards
Jantje
Do not PM me a question unless you are prepared to pay for consultancy.
Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

fat16lib

The version of SdFat used in 1.0 and 1.01 is the same old version used in 022 and before.  Only the SD.h wrapper has changed so this is still the best way.

If you had access to the SdFat API for files you could use the timestamp function to set all date/time fields on a per file basis.
Quote

bool SdBaseFile::timestamp    (    uint8_t     flags,
      uint16_t     year,
      uint8_t     month,
      uint8_t     day,
      uint8_t     hour,
      uint8_t     minute,
      uint8_t     second
   )       

Set a file's timestamps in its directory entry.

Parameters:
    [in]   flags   Values for flags are constructed by a bitwise-inclusive OR of flags from the following list

T_ACCESS - Set the file's last access date.

T_CREATE - Set the file's creation date and time.

T_WRITE - Set the file's last write/modification date and time.

Parameters:
    [in]   year   Valid range 1980 - 2107 inclusive.
    [in]   month   Valid range 1 - 12 inclusive.
    [in]   day   Valid range 1 - 31 inclusive.
    [in]   hour   Valid range 0 - 23 inclusive.
    [in]   minute   Valid range 0 - 59 inclusive.
    [in]   second   Valid range 0 - 59 inclusive

Note:
    It is possible to set an invalid date since there is no check for the number of days in a month.
    Modify and access timestamps may be overwritten if a date time callback function has been set by dateTimeCallback().

Returns:
    The value one, true, is returned for success and the value zero, false, is returned for failure.


Or copy timestamps from another file:
Quote

bool SdBaseFile::timestamp    (    SdBaseFile *     file   )    

Copy a file's timestamps

Parameters:
    [in]   file   File to copy timestamps from.

Note:
    Modify and access timestamps may be overwritten if a date time callback function has been set by dateTimeCallback().

Returns:
    The value one, true, is returned for success and the value zero, false, is returned for failure.



Jantje

fatlib
Thanks for the great library and quick response.
I'll try the callback first. if that doesn't satisfy my needs I'll look at using the fatlib natively.

Greetings from Belgium
Jantje
Do not PM me a question unless you are prepared to pay for consultancy.
Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

pito

#8
Jul 30, 2012, 10:00 pm Last Edit: Jul 30, 2012, 10:10 pm by pito Reason: 1
FYI - I am using an external RTC, so I do this as well (I think I must not call RTC with each timestamp request then, comes from sdfat examples):

Code: [Select]

...
void dateTime(uint16_t* date, uint16_t* time) {
  time_t t = now();
 // return date using FAT_DATE macro to format fields
 *date = FAT_DATE(year(t), month(t), day(t));  
 // return time using FAT_TIME macro to format fields
 *time = FAT_TIME(hour(t), minute(t), second(t));
}
...
 setSyncProvider(rtc.get_tm);   // the function to get the actual time from my RTC

 if(timeStatus()!= timeSet)
    Serial.println(F("Unable to sync with the RTC"));
 else
    Serial.println(F("RTC has set the system time"));  

    setSyncInterval(1000);         // set the number of seconds between re-sync of time

    SdFile::dateTimeCallback(dateTime);
...

p.

pito

..a question to the TIME and RTC libs, related to the timestamps as well:
http://arduino.cc/forum/index.php/topic,116661.0.html
p.

fat16lib

pito,

See my reply in your original post about RTC libs.

saunj

I have been using the Rogue Robotics RS-232 SD modules which don't do timestamps either. As a work-around I put year and then month sub-diectories on the SD (e.g. 12/07) before use and construct a filename for the day with its date in it (e.g. AR31.CSV). Each record has date and time fields. Most of the projects have used a dallas RTC, but i have also used a watch crystal and a CD4060 and even an actual watch (detecting the 1-sec current surge when it advances the hands). My latest does use the Adafruit logger shield, which is much cheaper, and its SD library.
John

Jantje

Fatlib
It works great now. I'm using my gps module to get the time.
Greetings from Belgium
Jantje
Do not PM me a question unless you are prepared to pay for consultancy.
Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

ribbery45

It has been so long I am not talking about arduino. I have a project with arduino and I will expose it yet. But, one part of the project (programmes) is to have a log file in SD card named by date.

Go Up