GPSdate and time as Filename using SDFat.h

Hello Everybody,
I am trying to build GPS data logger using Quectel L80 module with Arduino Mega, I can successfully log data into the Sd card, I assigned filename as 000.tcx, eveytime I have to delete the file from SD and log data again..

I want to create a file name as the time and date and log that data into it.

Here is my code.

#include <Arduino.h>
#include <NMEAGPS.h>
#include <SPI.h>
#include <SdFat.h>


NMEAGPS  gps; // This parses the GPS characters
gps_fix  fix; // This contains all the GPS fields

#define gps_port Serial1


SdFat SD;
File  dataFile;
char filename[] = "000.tcx";


const int xpin = A0;                  // x-axis of the accelerometer
const int ypin = A1;                  // y-axis
const int zpin = A2;                  // z-axis
int x, y, z, total;

void setup()  {
  Serial.begin(9600);
  Serial.flush();

  gps_port.begin( 9600 );
  gps.send_P( &gps_port, F("PMTK251,115200") );  // set baud rate
  gps_port.flush();                              // wait for the command to go out
  delay( 100 );                                  // wait for the GPS device to change speeds
  gps_port.end();                                // empty the input buffer, too

  gps_port.begin( 115200 );                      // use the new baud rate
  gps.send_P( &gps_port, F("PMTK220,100") );     // set 10Hz update rate

  pinMode(37, INPUT_PULLUP);

  if (!SD.begin(4)) {
    Serial.println( F("Card failed, or not present") );
    // don't do anything more:
    return;
  }
  Serial.println( F("card initialized.") );
  Serial.println( F("Code Running") );

} // setup

void firstStage()
{
  Serial.println( F("Co-ordinates Achieved") );
  // sprintf(filename, "%02d-%02d-%02d-T-%02d-%02d-%02d.tcx", fix.dateTime.hours , fix.dateTime.minutes, fix.dateTime.hours, fix.dateTime.date , fix.dateTime.month , fix.dateTime.full_year());
  dataFile = SD.open(filename , FILE_WRITE);
  Serial.println(filename);

  if (dataFile) {
    Serial.println( F("File created\n"
                      "LOGGING DATA!\n") );

    dataFile.println( F("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
                        "<TrainingCenterDatabase xmlns=\""
                        "http://www.garmin.com/"
                        "xmlschemas/TrainingCenterDatabase/v2\">\n"
                        "<Activities>\n"
                        "<Activity Sport=\"Other\">") );
    dataFile.println( F("<Id>") );

    timing();

    dataFile.println( F("0Z"
                        "</Id>\n"
                        "<Lap StartTime=\"") );
    timing();
    dataFile.println( F("0Z\">"
                        "<Track>") );
  }
} // firstStage

void secondStage()
{
  if (dataFile.isOpen()) {
    dataFile.println( F("<Trackpoint>\n"
                        "<Time>") );
    timing();
    
    dataFile.print  ( F("0Z"
                        "</Time>"
                        "<Position>\n"
                        "<LatitudeDegrees>") );
    dataFile.print  ( fix.latitude(), 5 );
    dataFile.print  ( F("</LatitudeDegrees>\n"
                        "<LongitudeDegrees>") );
    dataFile.print  ( fix.longitude(), 5 );
    dataFile.print  ( F("</LongitudeDegrees>\n"
                        "</Position>\n"
                        "<HeartRateBpm>\n"
                        "<Value>") );
    dataFile.print  ( '0' );
    dataFile.print  ( F("</Value>\n"
                        "</HeartRateBpm>\n"
                        "<SensorState>"
                        "Present"
                        "</SensorState>\n") );

    analogReference(EXTERNAL);
    x     = analogRead(xpin);
    y     = analogRead(ypin);
    z     = analogRead(zpin);
    total = sqrt( x * x + y * y + z * z );

    dataFile.print  ( F("<Impact>"
                        "<Gforce>") );
    dataFile.print  ( total );
    dataFile.print  ( F("</Gforce>\n"
                        "<Xaxis>") );
    dataFile.print  ( x );
    dataFile.print  ( F("</Xaxis>\n"
                        "<Yaxis>") );
    dataFile.print  ( y );
    dataFile.print  ( F("</Yaxis>\n"
                        "<Zaxis>") );
    dataFile.print  ( z );
    dataFile.println( F("</Zaxis>\n"
                        "</Impact>\n"
                        "</Trackpoint>") );
  }

} // secondStage

void doSomeWork() {

  if (fix.valid.date && fix.valid.time && fix.valid.location)
  {
    if (!dataFile.isOpen())
      firstStage();

    if (dataFile.isOpen())
      secondStage();
  }
  else {
    // No valid location data yet!
    Serial.print  ( '?' );
  }

  Serial.println();

} // doSomeWork

void GPSloop()
{
  while (gps.available( gps_port) ) {
    fix = gps.read();
    doSomeWork();
  }

} // GPSloop

void loop()
{
  GPSloop();

  // Testing the button could go almost anywhere.
  //    Putting it here will close dataFile the fastest,
  //    because it won't have to wait for the next
  //    GPS update.
  if (digitalRead(37) == LOW) {
    endtask();

    // stop here!
    while (1);
      
  }

} // loop

void endtask()
{
  if (dataFile.isOpen()) {
    dataFile.println( F("</Track>\n"
                        "</Lap>\n"
                        "</Activity>\n"
                        "</Activities>\n"
                        "</TrainingCenterDatabase>") );
    dataFile.close();
    Serial.println( F("file closed") );
  } else {
    Serial.println( F("(file never opened)") );
  }

} // endtask
void timing () {
  if ( fix.dateTime.full_year() < 10 )
    dataFile.print( '0' );
  dataFile.print  ( fix.dateTime.full_year() );
  dataFile.print  ( '-' );
  if ( fix.dateTime.month < 10 )
    dataFile.print( '0' );
  dataFile.print  ( fix.dateTime.month );
  dataFile.print  ( '-' );
  if ( fix.dateTime.date  < 10 )
    dataFile.print( '0' );
  dataFile.print  ( fix.dateTime.date );
  dataFile.print  ( 'T' );
  if ( fix.dateTime.hours < 10 )
    dataFile.print( '0' );
  dataFile.print  ( fix.dateTime.hours );
  dataFile.print  ( ':' );
  if ( fix.dateTime.minutes < 10 )
    dataFile.print( '0' );
  dataFile.print  ( fix.dateTime.minutes );
  dataFile.print  ( ':' );
  if ( fix.dateTime.seconds < 10 )
    dataFile.print( '0' );
  dataFile.print  ( fix.dateTime.seconds );
  dataFile.print  ( '.' );
    if ( fix.dateTime_cs < 10 )
    dataFile.print( '0' );
  dataFile.print  ( fix.dateTime_cs );
}

Thank you

You have got 8+3 chars.

Your code must convert time/date to a valid filename. Use your own format (or unixtime?, which is 10 chars)

You have got 8+3 chars.

What do you mean by that.?

Your code must convert time/date to a valid filename. Use your own format (or unixtime?, which is 10 chars)

I actually used sprintf function with my format plugged in. But Arduino is not creating a file.

here is the line of code

// sprintf(filename, "%02d-%02d-%02d-T-%02d-%02d-%02d.tcx", fix.dateTime.hours , fix.dateTime.minutes, fix.dateTime.hours, fix.dateTime.date , fix.dateTime.month , fix.dateTime.full_year());
  dataFile = SD.open(filename , FILE_WRITE);

I want to achieve in this format :

tt-mm-hhTdd-mm-yy

Thank you

Does sdconfig.h say anything regarding long filenames ?

mudassir9999:
What do you mean by that.?

8.3_filename For exemple: 12345678.123 -> Datalogg.txt

mudassir9999:
I want to achieve in this format

You can see this example