Pages: [1]   Go Down
Author Topic: Which SD library to create file with date?  (Read 6948 times)
0 Members and 1 Guest are viewing this topic.
USA, OH
Offline Offline
Jr. Member
**
Karma: 0
Posts: 56
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

0
Offline Offline
Edison Member
*
Karma: 63
Posts: 1598
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#define USE_DS1307       1  // set nonzero to use DS1307 RTC

At line 40 change the comments like this:
Code:
//#error remove this line and uncomment the next two lines.
#include <Wire.h>
#include <RTClib.h>
Logged

0
Offline Offline
Shannon Member
****
Karma: 199
Posts: 11639
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

[ I won't respond to messages, use the forum please ]

0
Offline Offline
Edison Member
*
Karma: 63
Posts: 1598
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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() {}
« Last Edit: September 20, 2011, 10:10:32 am by fat16lib » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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() {}
Logged

Belgium
Offline Offline
Edison Member
*
Karma: 68
Posts: 1893
Arduino rocks; but with my plugin it can fly rocking the world ;-)
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Is this still the way to add timestamps to sd files with Arduino 1.0 or later?
Best regards
Jantje
Logged

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 -

0
Offline Offline
Edison Member
*
Karma: 63
Posts: 1598
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.


Logged

Belgium
Offline Offline
Edison Member
*
Karma: 68
Posts: 1893
Arduino rocks; but with my plugin it can fly rocking the world ;-)
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

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 -

Rapa Nui
Offline Offline
Edison Member
*
Karma: 60
Posts: 2061
Pukao hats cleaning services
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
...
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.
« Last Edit: July 30, 2012, 03:10:57 pm by pito » Logged

Rapa Nui
Offline Offline
Edison Member
*
Karma: 60
Posts: 2061
Pukao hats cleaning services
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

..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.
Logged

0
Offline Offline
Edison Member
*
Karma: 63
Posts: 1598
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

pito,

See my reply in your original post about RTC libs.
Logged

San Diego, California, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Born to be a nerd
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Belgium
Offline Offline
Edison Member
*
Karma: 68
Posts: 1893
Arduino rocks; but with my plugin it can fly rocking the world ;-)
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Fatlib
It works great now. I'm using my gps module to get the time.
Greetings from Belgium
Jantje
Logged

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 -

Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: