Timestamp using GPS according to GPS time SDfat library

Hello everybody,

I'm logging data from GPS using TinyGPS and SDFatlibrary, how can i stamp time and date in my file which is created.

Here is my code

#include <SdFat.h>
#include <TinyGPS++.h>
#include <SPI.h>
//#include <SD.h>
TinyGPSPlus gps;
File dataFile;
char ttime[32];
char ddate[32];
char filename[] = "000000000000000.tcx";
int dat = 0;
float lat0 = 0.0, lat1 = 0.0, lng0 = 0.0, lng1 = 0.0, dis = 0.0;
int LED = 5;
int LEDD = 6;
SdFat SD;
void setup()
{
  pinMode(LED, OUTPUT);
  pinMode(LEDD, OUTPUT);

  Serial.begin(9600);
  pinMode(7, INPUT_PULLUP);
  if (!SD.begin(10))
  {
    Serial.println("Card failed");
    return;
  }
  Serial.println("card initialized.");
}


void loop()
{


  printFloat(gps.location.lat(), gps.location.isValid(), 11, 6);
  printFloat(gps.location.lng(), gps.location.isValid(), 12, 6);
  printDateTime(gps.date, gps.time);

  Serial.println();


  smartDelay(1000);

  do {
    lat0 = (String(gps.location.lat(), 6)).toFloat();
    lng0 = (String(gps.location.lng(), 6)).toFloat();

  }
  while (lat0 == 0.0);


  if (lat0 !=  0.0 && dat == 1)
  {
    Serial.println("Co-ordinates Achived");
    Serial.println("Date Achived");
    dataFile = SD.open(filename , FILE_WRITE);
    Serial.println(filename);
    if (dataFile) {
      Serial.println("File created");
      dataFile.println(F("<Activity Sport=\"Other\">"));
      dataFile.println("<Id>");
      dataFile.print(String(ddate));
      dataFile.print("T");
      dataFile.print(String(ttime));
      dataFile.print(".000Z");
      dataFile.println("</Id>");
      dataFile.println("<Lap StartTime=\"");
      dataFile.print(String(ddate));
      dataFile.print("T");
      dataFile.print(String(ttime));
      dataFile.print(".000Z\">");
      dataFile.println("<Track>");
      Serial.println("UPTO HERE!!!!");

      digitalWrite(LED, HIGH);
    }
    else
    {
      digitalWrite(LED, LOW);
    }
    logging();
  }
  else {
    Serial.println("CANT OPEN FILE");
  }

}

void logging()
{
  Serial.println("came in loop 1");
  while (1)
  {
    if (digitalRead(7) == LOW)
      endtask();
    printDateTime(gps.date, gps.time);
    dataFile.println("<Trackpoint>");
    dataFile.println("<Time>");
    dataFile.print(String(ddate));
    dataFile.print("T");
    dataFile.print(String(ttime));
    dataFile.print(".000Z");
    dataFile.print("</Time>");
    dataFile.println("<Position>");
    {
      lat1 = (String(gps.location.lat(), 6)).toFloat();
      lng1 = (String(gps.location.lng(), 6)).toFloat();

      float distLat = abs(lat0 - lat1) * 111194.9;
      float distLong = 111194.9 * abs(lng0 - lng1) * cos(radians((lat0 + lat1) / 2));
      dis = dis + sqrt(pow(distLat, 2) + pow(distLong, 2));
      // Serial.println(dis, 6);
      lat0 = lat1;
      lng0 = lng1;
    }
    {
      dataFile.print("<LatitudeDegrees>");
      dataFile.print(String(lat1, 6));
      Serial.println(String(lat1, 6));
      dataFile.println("</LatitudeDegrees>");
      dataFile.print("<LongitudeDegrees>");
      dataFile.print(String(lng1, 6));
      Serial.println(String(lng1, 6));
      dataFile.println("</LongitudeDegrees>");
      dataFile.println("</Position>");

      Serial.print("Recorded");
      Serial.println("123456789");
    }
    ddate[0] = NULL;
    ttime[0] = NULL;
    smartDelay(1000);
  }
}


static void printFloat(float val, bool valid, int len, int prec)
{
  if (!valid)
  {
    while (len-- > 1)
      Serial.print('*');
    Serial.print(' ');
  }
  else
  {
    Serial.print(val, prec);
    int vi = abs((int)val);
    int flen = prec + (val < 0.0 ? 2 : 1); // . and -
    flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
    for (int i = flen; i < len; ++i)
      Serial.print(' ');
  }
  smartDelay(0);
}



void endtask()
{ dataFile.close();
  Serial.print("file close");
  digitalWrite(LEDD, HIGH);

  while (1);
}







static void printInt(unsigned long val, bool valid, int len)
{
  char sz[32] = "*****************";
  if (valid)
    sprintf(sz, "%ld", val);
  sz[len] = 0;
  for (int i = strlen(sz); i < len; ++i)
    sz[i] = ' ';
  if (len > 0)
    sz[len - 1] = ' ';
  Serial.print(sz);
  smartDelay(0);
}


static void printDateTime(TinyGPSDate &d, TinyGPSTime &t)
{
  if (!d.isValid())
  {
    Serial.print(F("********** "));
    dat = 0;
  }
  else
  {

    sprintf(ddate, "%02d%02d%02d", d.day(), d.month(), d.year());
    //Serial.print(ddate);

    dat = 1;
  }

  if (!t.isValid())
  {
    Serial.print(F("******** "));
  }
  else
  {

    sprintf(ttime, "%02d%02d%02d", t.hour(), t.minute(), t.second());
    //Serial.print(ttime);

  }

    sprintf(filename, "%02d-%02d-%02d-T-%02d-%02d-%02d.tcx",t.hour(), t.minute(), t.second(), d.day(),d.month(), d.year());
    smartDelay(0);
}

static void smartDelay(unsigned long ms)
{
  unsigned long start = millis();
  do
  {
    while (Serial.available())
      gps.encode(Serial.read());
  } while (millis() - start < ms);
}

I've also seen Timestamp example, but unable to figure our to merge that one with mine

help needed
Thanks

mudassir9999:
Hello everybody,

I'm logging data from GPS using TinyGPS and SDFatlibrary, how can i stamp time and date in my file which is created.

I've also seen Timestamp example, but unable to figure our to merge that one with mine

help needed
Thanks

You will have to update a global time Stamp every time you receive a time tick from the GPS. Then uses this time tick to answer the SdFat:dateTimeCallBack();

// date 1-Oct-09
uint16_t year = 2009;
uint8_t month = 10;
uint8_t day = 1;

// time 20:30:40
uint8_t hour = 20;
uint8_t minute = 30;
uint8_t second = 40;
//------------------------------------------------------------------------------
/*
 * User provided date time callback function.
C */
void dateTime(uint16_t* date, uint16_t* time) {
  // User gets date and time from GPS or real-time
  // clock in real callback function

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

  // return time using FAT_TIME macro to format fields
  *time = FAT_TIME(hour, minute, second);
}

void setup(){

SdFile::dateTimeCallback(dateTime); // register callback for file create/modification date/time

}


static void updateDateTime(TinyGPSDate &d, TinyGPSTime &t){
if (d.isValid()) {
  year = d.year()+2000;
  month = d.month();
  day = d.day(); 
  }

if (t.isValid()){
  second = t.second();
  minute = t.minute();
  hour = t.hour();      
  }
}

void loop(){

updateDateTime(gps.date, gps.time);

}

Chuck.

I getting this error in void setup()

Arduino: 1.6.7 (Windows 10), Board: "Arduino/Genuino Uno"

C:\Users\MUDASS~1\AppData\Local\Temp\arduino_0b53d3322cbff6bc6617e0c40e559a38\locked_newuno.ino: In function 'void setup()':

locked_newuno:59: error: a function-definition is not allowed here before '{' token

static void updateDateTime(TinyGPSDate &d, TinyGPSTime &t){

^

locked_newuno:272: error: expected '}' at end of input

}

^

exit status 1
a function-definition is not allowed here before '{' token

This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.

#include <SdFat.h>
#include <TinyGPS++.h>
#include <SPI.h>
//#include <SD.h>
TinyGPSPlus gps;
File dataFile;
char ttime[32];
char ddate[32];
char filename[] = "000000000000000.tcx";
int dat = 0;
float lat0 = 0.0, lat1 = 0.0, lng0 = 0.0, lng1 = 0.0, dis = 0.0;
int LED = 5;
int LEDD = 6;
SdFat SD;
// date 1-Oct-09
uint16_t year = 2009;
uint8_t month = 10;
uint8_t day = 1;

// time 20:30:40
uint8_t hour = 20;
uint8_t minute = 30;
uint8_t second = 40;
//------------------------------------------------------------------------------
/*
   User provided date time callback function.
  C */
void dateTime(uint16_t* date, uint16_t* time) {
  // User gets date and time from GPS or real-time
  // clock in real callback function

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

  // return time using FAT_TIME macro to format fields
  *time = FAT_TIME(hour, minute, second);
}
void setup()
{
  pinMode(LED, OUTPUT);
  pinMode(LEDD, OUTPUT);


  Serial.begin(9600);
  pinMode(7, INPUT_PULLUP);
  if (!SD.begin(10))
  {
    Serial.println("Card failed");
    return;
  }
  Serial.println("card initialized.");



  SdFile::dateTimeCallback(dateTime); // register callback for file create/modification date/time


  static void updateDateTime(TinyGPSDate & d, TinyGPSTime & t) {
    if (d.isValid()) {
      year = d.year() + 2000;
      month = d.month();
      day = d.day();
    }

    if (t.isValid()) {
      second = t.second();
      minute = t.minute();
      hour = t.hour();
    }
  }
}

void loop()
{

  updateDateTime(gps.date, gps.time);
  printFloat(gps.location.lat(), gps.location.isValid(), 11, 6);
  printFloat(gps.location.lng(), gps.location.isValid(), 12, 6);
  printDateTime(gps.date, gps.time);

  Serial.println();


  smartDelay(1000);

  do {
    lat0 = (String(gps.location.lat(), 6)).toFloat();
    lng0 = (String(gps.location.lng(), 6)).toFloat();

  }
  while (lat0 == 0.0);


  if (lat0 !=  0.0 && dat == 1)
  {
    Serial.println("Co-ordinates Achived");
    Serial.println("Date Achived");
    dataFile = SD.open(filename , FILE_WRITE);
    Serial.println(filename);
    if (dataFile) {
      Serial.println("File created");
      dataFile.println(F("<Activity Sport=\"Other\">"));
      dataFile.println("<Id>");
      dataFile.print(String(ddate));
      dataFile.print("T");
      dataFile.print(String(ttime));
      dataFile.print(".000Z");
      dataFile.println("</Id>");
      dataFile.println("<Lap StartTime=\"");
      dataFile.print(String(ddate));
      dataFile.print("T");
      dataFile.print(String(ttime));
      dataFile.print(".000Z\">");
      dataFile.println("<Track>");
      Serial.println("UPTO HERE!!!!");

      digitalWrite(LED, HIGH);
    }
    else
    {
      digitalWrite(LED, LOW);
    }
    logging();
  }
  else {
    Serial.println("CANT OPEN FILE");
  }

}

void logging()
{
  Serial.println("came in loop 1");
  while (1)
  {
    if (digitalRead(7) == LOW)
      endtask();
    printDateTime(gps.date, gps.time);
    dataFile.println("<Trackpoint>");
    dataFile.println("<Time>");
    dataFile.print(String(ddate));
    dataFile.print("T");
    dataFile.print(String(ttime));
    dataFile.print(".000Z");
    dataFile.print("</Time>");
    dataFile.println("<Position>");
    {
      lat1 = (String(gps.location.lat(), 6)).toFloat();
      lng1 = (String(gps.location.lng(), 6)).toFloat();

      float distLat = abs(lat0 - lat1) * 111194.9;
      float distLong = 111194.9 * abs(lng0 - lng1) * cos(radians((lat0 + lat1) / 2));
      dis = dis + sqrt(pow(distLat, 2) + pow(distLong, 2));
      // Serial.println(dis, 6);
      lat0 = lat1;
      lng0 = lng1;
    }
    {
      dataFile.print("<LatitudeDegrees>");
      dataFile.print(String(lat1, 6));
      Serial.println(String(lat1, 6));
      dataFile.println("</LatitudeDegrees>");
      dataFile.print("<LongitudeDegrees>");
      dataFile.print(String(lng1, 6));
      Serial.println(String(lng1, 6));
      dataFile.println("</LongitudeDegrees>");
      dataFile.println("</Position>");

      Serial.print("Recorded");
      Serial.println("123456789");
    }
    ddate[0] = NULL;
    ttime[0] = NULL;
    smartDelay(1000);
  }
}


static void printFloat(float val, bool valid, int len, int prec)
{
  if (!valid)
  {
    while (len-- > 1)
      Serial.print('*');
    Serial.print(' ');
  }
  else
  {
    Serial.print(val, prec);
    int vi = abs((int)val);
    int flen = prec + (val < 0.0 ? 2 : 1); // . and -
    flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
    for (int i = flen; i < len; ++i)
      Serial.print(' ');
  }
  smartDelay(0);
}



void endtask()
{ dataFile.close();
  Serial.print("file close");
  digitalWrite(LEDD, HIGH);

  while (1);
}







static void printInt(unsigned long val, bool valid, int len)
{
  char sz[32] = "*****************";
  if (valid)
    sprintf(sz, "%ld", val);
  sz[len] = 0;
  for (int i = strlen(sz); i < len; ++i)
    sz[i] = ' ';
  if (len > 0)
    sz[len - 1] = ' ';
  Serial.print(sz);
  smartDelay(0);
}


static void printDateTime(TinyGPSDate & d, TinyGPSTime & t)
{
  if (!d.isValid())
  {
    Serial.print(F("********** "));
    dat = 0;
  }
  else
  {

    sprintf(ddate, "%02d%02d%02d", d.day(), d.month(), d.year());
    //Serial.print(ddate);

    dat = 1;
  }

  if (!t.isValid())
  {
    Serial.print(F("******** "));
  }
  else
  {

    sprintf(ttime, "%02d%02d%02d", t.hour(), t.minute(), t.second());
    //Serial.print(ttime);

  }

  sprintf(filename, "%02d-%02d-%02d-T-%02d-%02d-%02d.tcx", t.hour(), t.minute(), t.second(), d.day(), d.month(), d.year());
  smartDelay(0);
}

static void smartDelay(unsigned long ms)
{
  unsigned long start = millis();
  do
  {
    while (Serial.available())
      gps.encode(Serial.read());
  } while (millis() - start < ms);
}

I kept "static void updateDateTime(TinyGPSDate & d, TinyGPSTime & t)" in void setup() and made the d.year() + 2000; to d.year()

and it's working now

Thank you for your help