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