Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 11 of 11

Thread: Creating SD files with time-stamps

  1. #1
    Member
    Join Date
    Mar 2021
    Location
    Oceanside, CA
    Posts
    20

    Creating SD files with time-stamps

    I've been using the SD library with a project, mostly successfully, but I would like to add time stamps to files created and can't seem to find a way to do that with SD. So... I'm now trying to switch to SdFat.

    The idea is first to adapt the SdFat TeensyRtcTimestamp example to the minimum required to create a file with a timestamp on a Teensy 3.5 and write text to the file, then apply that technique to the final project. Several problems have occurred adapting the example:

    • Both the example and my edited version generate a list of errors as shown below, but they run anyway. Why the errors and how to get rid of them?
    • Among the errors is a warning to use TimeLib.h, not Time.h, but both the example and my code already use TimeLib.h so why the warning?

    Code:
    In file included from c:\program files (x86)\arduino\hardware\tools\arm\arm-none-eabi\include\sys\stat.h:9:0,
                     from c:\program files (x86)\arduino\hardware\tools\arm\arm-none-eabi\include\sys\_default_fcntl.h:188,
                     from c:\program files (x86)\arduino\hardware\tools\arm\arm-none-eabi\include\sys\fcntl.h:4,
                     from c:\program files (x86)\arduino\hardware\tools\arm\arm-none-eabi\include\fcntl.h:1,
                     from c:\users\rick\documents\arduino\libraries\arduino_118105\src\common\fsapiconstants.h:30,
                     from C:\Users\Rick\Documents\Arduino\libraries\arduino_118105\src/ExFatLib/ExFatFile.h:36,
                     from C:\Users\Rick\Documents\Arduino\libraries\arduino_118105\src/ExFatLib/ExFatVolume.h:28,
                     from C:\Users\Rick\Documents\Arduino\libraries\arduino_118105\src/ExFatLib/ExFatLib.h:27,
                     from C:\Users\Rick\Documents\Arduino\libraries\arduino_118105\src/SdFat.h:33,
                     from C:\Users\Rick\Documents\Arduino\Experimental\SdFat_create_with_time_stamp\SdFat_create_with_time_stamp.ino:1:
    C:\Users\Rick\Documents\Arduino\libraries\Time-master/time.h:1:2: warning: #warning "Please include TimeLib.h, not Time.h.  Future versions will remove Time.h" [-Wcpp]
     #warning "Please include TimeLib.h, not Time.h.  Future versions will remove Time.h"
      ^
    Sketch uses 33404 bytes (6%) of program storage space. Maximum is 524288 bytes.
    Global variables use 5564 bytes (2%) of dynamic memory, leaving 256572 bytes for local variables. Maximum is 262136 bytes.
    • The example sketch successfully creates a file with a correct time stamp. My sketch, shown below, creates a file with a blank timestamp. Not default, blank. How to get the correct time stamp?
    • The example successfully prints text to the file, my sketch attempts to print but the created file is empty.
    • This sketch executes a file.close() but once this process is working the final project will create a lengthy log file that won't get closed. The SD library has a file.flush() command for that situation that writes data to the file without closing it. How can this be done with SdFat?

    Code:
    #include <SdFat.h>
    #include <TimeLib.h>
    
    #define SD_CONFIG SdioConfig(FIFO_SDIO)
    
    SdFs sd;
    FsFile file;
    
    //------------------------------------------------------------------------------
    // Call back for file timestamps.  Only called for file create and sync().
    void dateTime(uint16_t* date, uint16_t* time, uint8_t* ms10) {
    
      // Return date using FS_DATE macro to format fields.
      *date = FS_DATE(year(), month(), day());
    
      // Return time using FS_TIME macro to format fields.
      *time = FS_TIME(hour(), minute(), second());
    
      // Return low time bits in units of 10 ms.
      *ms10 = second() & 1 ? 100 : 0;
    }
    
    void setup() {
    
    setSyncProvider(getTeensy3Time);
      
    Serial.begin(9600);
    while(!Serial);
    Serial.println(minute());  // This is just to be sure RTC actually sets the clock
                               // It does
    
    #define fileName "Foo.txt"
    
    // Set callback
    FsDateTime::setCallback(dateTime);
    sd.begin(SD_CONFIG);
    if(sd.exists(fileName)) sd.remove(fileName);
    sd.open(fileName, FILE_WRITE);
    
    file.print("Mary had a little lamb.");
    file.println();
    
    file.close();
    
    }
    
    void loop() {
      // put your main code here, to run repeatedly:
    
    }
    
    //------------------------------------------------------------------------------
    time_t getTeensy3Time(){
      return Teensy3Clock.get();
    }
    //------------------------------------------------------------------------------

  2. #2
    Senior Member
    Join Date
    Jul 2014
    Posts
    3,336
    upgrade to Td 1.54 and use SD.h, as it already uses SdFat
    The warning is that Bill SdFat correctly uses time.h which is also in Time library (where TimeLib.h is located)
    best, simply go into Time library, and delete Time.h, so warning disappears.

  3. #3
    Member
    Join Date
    Mar 2021
    Location
    Oceanside, CA
    Posts
    20
    Sorry for delay, had a Windows crash that discarded the most recent version of my main project. Thanks Microsoft.

    Followed your advice and went back to SD.h which was working with the project before. Upgraded to Td 1.54. Deleted Time.h.

    Error messages are no longer an issue so, back to the original objective; How to apply timestamps to created files?

  4. #4
    Senior Member
    Join Date
    Jul 2014
    Posts
    3,336
    Quote Originally Posted by choochoo22 View Post
    Sorry for delay, had a Windows crash that discarded the most recent version of my main project. Thanks Microsoft.

    Followed your advice and went back to SD.h which was working with the project before. Upgraded to Td 1.54. Deleted Time.h.

    Error messages are no longer an issue so, back to the original objective; How to apply timestamps to created files?
    The code in OP should be sufficient to tell SdFat to create proper time stamps
    (void dateTime(uint16_t* date, uint16_t* time, uint8_t* ms10) {...} with 'FsDateTime::setCallback(dateTime);' in setup() )

  5. #5
    Member
    Join Date
    Mar 2021
    Location
    Oceanside, CA
    Posts
    20
    The code in OP produces the same result as before, minus the error messages. The file Foo.txt is created with a blank time stamp and no content.

    Click image for larger version. 

Name:	New file creation.jpg 
Views:	35 
Size:	12.5 KB 
ID:	25281

  6. #6
    Senior Member
    Join Date
    Jul 2014
    Posts
    3,336
    maybe you should write
    Code:
    file=sd.open(fileName, FILE_WRITE);
    otherwise ....

  7. #7
    Member
    Join Date
    Mar 2021
    Location
    Oceanside, CA
    Posts
    20
    Indeed, that did the trick! Thank you very much.

    I would never have figured that out. The code in the example I was using as a guide was:

    Code:
      if (!file.open("RtcTest.txt", FILE_WRITE)) {
        Serial.println(F("file.open failed"));
        return;
      }
    Now I need to see if I can transport this success back to my main project which uses SD.h.

  8. #8
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,763
    I tried it with:
    Code:
    file.open(fileName, FILE_WRITE);
    and that works. It also has the correct timestamp on the file - using a T3.6.

    Pete

  9. #9
    Member
    Join Date
    Mar 2021
    Location
    Oceanside, CA
    Posts
    20
    Yes. That also works with my T3.5, thanks.

  10. #10
    Member
    Join Date
    Mar 2021
    Location
    Oceanside, CA
    Posts
    20
    After a few edits, this code also works on my T3.5 using the SD.h library. Thanks very much for the help.

    Code:
    #include <SD.h>
    #include <TimeLib.h>
    
    File dataFile;
    
    void setup() {
    
    setSyncProvider(getTeensy3Time); // Sync the time
    
    // This just prints the current minute to the serial monitor to 
    //  show that something happened and establish the RTC is working.
    Serial.begin(9600);
    while(!Serial);
    Serial.println(minute());  
    
    #define fileName "Foo.txt"
    
    // Set callback
    FsDateTime::setCallback(dateTime);
    
    // Initialize SD, remove old file, create new file for write
    SD.begin(BUILTIN_SDCARD);
    if(SD.exists(fileName)) SD.remove(fileName);
    dataFile = SD.open(fileName, FILE_WRITE);
    
    // Print something to file
    dataFile.print("Mary had a little lamb.");
    dataFile.println();
    
    // Make sure data is written to card
    dataFile.flush();
    // Or: dataFile.close();
    }
    
    void loop() {
      // nothing to do here
    
    }
    
    //------------------------------------------------------------------------------
    // Sets current program time from RTC
    time_t getTeensy3Time(){
      return Teensy3Clock.get();
    }
    
    //------------------------------------------------------------------------------
    // Call back for file timestamps.  Only called for file create and sync().
    void dateTime(uint16_t* date, uint16_t* time, uint8_t* ms10) {
    
      // Return date using FS_DATE macro to format fields.
      *date = FS_DATE(year(), month(), day());
    
      // Return time using FS_TIME macro to format fields.
      *time = FS_TIME(hour(), minute(), second());
    
      // Return low time bits in units of 10 ms.
      *ms10 = second() & 1 ? 100 : 0;
    }

  11. #11
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    15,259
    Quote Originally Posted by choochoo22 View Post
    After a few edits, this code also works on my T3.5 using the SD.h library. Thanks very much for the help.

    ...
    Posted code works on a @loglow TALLDOG adapted T_4.0 - except the hour is off by one? Showing file time of 3:56 PM when computer is showing 4:56PM.

    This is because TyCommander is doing the upload! :: Will have to make an issue there - done!

    >> Switching back to TeensyLoader and the time set on upload matched the PC Time.

    This is a Windows machine ... Time worked right the other day on a T_4.1?

    Another user new to TD 1.54 reported a MAC was showing hour off by one ????

    Same FILE timestamp result - Off by One Hour - on a T_4.1.

    Odd ... same result on another Show time sketch now ...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •