Skip to content

Instantly share code, notes, and snippets.

@ledlogic
Last active December 7, 2015 19:42
Show Gist options
  • Save ledlogic/726ec7105ee5cc41f3cd to your computer and use it in GitHub Desktop.
Save ledlogic/726ec7105ee5cc41f3cd to your computer and use it in GitHub Desktop.
A logger for GPS
#include "SoftwareSerial.h"
#include "dGPS.h"
#include "SPI.h"
#include "SD.h"
#include "stdlib.h"
// SD card
const int CS_pin = 10;
dGPS dgps = dGPS(); // Construct dGPS class
// Serial for debugging only
const boolean serial = false;
int checkDelayMs = 1000;
char plat[11];
char plng[11];
void setup() {
if (serial) {
Serial.end(); // Close any previously established connections
Serial.begin(9600); // Serial output back to computer. On.
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
}
//Check if card ready
if (!SD.begin(CS_pin)) {
return;
}
// init gps
dgps.init(); // Run initialization routine for dGPS.
delay(checkDelayMs); // Delay 1 sec between pings
}
void loop() {
dgps.update(0, 0);
long date = dgps.Date();
long time = dgps.Time();
float lat = dgps.Lat();
float lng = dgps.Lon();
char slat[11];
dtostrf(lat, 10, 6, slat);
char slng[11];
dtostrf(lng, 10, 6, slng);
String sdate = String(date);
if (sdate.length() == 6) {
sdate = "20" + String(sdate.substring(4, 6)) + String(sdate.substring(2, 4)) + String(sdate.substring(0, 2));
} else {
sdate = "20" + String(sdate.substring(3, 5)) + String(sdate.substring(1, 3)) + "0" + String(sdate.substring(0, 1));
}
String filename = sdate + ".csv";
String stime = String(time);
if (stime.length() == 4) {
stime = "00" + String(stime);
} else if (stime.length() == 5) {
stime = "0" + String(stime);
}
// Length (with one extra character for the null terminator)
int ifilename = filename.length() + 1;
// Prepare the character array (the buffer)
char cfilename[ifilename];
// Copy it over
filename.toCharArray(cfilename, ifilename);
if (serial) {
Serial.print(F("Filename: "));
Serial.print(cfilename);
Serial.println(F(""));
Serial.print(F("UTC Date(DDMMYY): "));
Serial.print(sdate); // UTC date. Date is in format: DDMMYY (D - Day; M - Month; Y-Year)
Serial.println(F(""));
Serial.print(F("UTC Time(HHMMSS): "));
Serial.print(stime); // Time returns the UTC time (GMT) in HHMMSS, 24 hour format (H-Hour; M-Minute; S-Second)
Serial.println(F(""));
Serial.print(F("Latitude (DD.MMSSSS): "));
Serial.print(slat); // Latitude - in DD.MMSSSS format (decimal-degrees format) (D-Degree; M-Minute; S-Second)
Serial.println(F(""));
Serial.print(F("Longitude (DD.MMSSSS): "));
Serial.print(slng); // Longitude - in DD.MMSSSS format (decimal-degrees format) (D-Degree; M-Minute; S-Second)
Serial.println(F(""));
}
// if previous coordinates saved
if (plat != "") {
if (strcmp(plat, slat) == 0 && strcmp(plng, slng) == 0) {
delay(checkDelayMs);
return;
}
}
// save coordinate for comparison next time
strcpy(plat, slat);
strcpy(plng, slng);
File logFile = SD.open(cfilename, FILE_WRITE);
if (logFile) {
if (serial) {
Serial.print(F("Saving to filename: "));
Serial.print(cfilename);
Serial.println(F(""));
}
logFile.print(sdate);
logFile.print(",");
logFile.print(stime);
logFile.print(",");
logFile.print(String(slat));
logFile.print(",");
logFile.print(String(slng));
logFile.println("");
logFile.close();
} else {
if (serial) {
Serial.println("Couldn't access file");
}
}
delay(checkDelayMs);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment