2 #include <Adafruit_GPS.h>
3 #include <SoftwareSerial.h>
8 // Ladyada's logger modified by Bill Greiman to use the SdFat library
10 // This code shows how to listen to the GPS module in an interrupt
11 // which allows the program to have more 'freedom' - just parse
12 // when a new NMEA sentence is available! Then access data when
15 // Tested and works great with the Adafruit Ultimate GPS Shield
16 // using MTK33x9 chipset
17 // ------> http://www.adafruit.com/products/
18 // Pick one up today at the Adafruit electronics shop
19 // and help support open source hardware & software! -ada
21 SoftwareSerial mySerial(8, 7);
22 Adafruit_GPS GPS(&mySerial);
24 // Set GPSECHO to 'false' to turn off echoing the GPS data to the Serial console
25 // Set to 'true' if you want to debug and listen to the raw GPS sentences
27 /* set to true to only log to SD when GPS has a fix, for debugging, keep it false */
28 #define LOG_FIXONLY false
36 // read a Hex value and return the decimal equivalent
37 uint8_t parseHex(char c) {
48 // blink out an error code
49 void error(uint8_t errno) {
52 putstring("SD error: ");
53 Serial.print(card.errorCode(), HEX);
55 Serial.println(card.errorData(), HEX);
60 for (i=0; i<errno; i++) {
61 digitalWrite(ledPin, HIGH);
63 digitalWrite(ledPin, LOW);
66 for (i=errno; i<10; i++) {
73 // for Leonardos, if you want to debug SD issues, uncomment this line
74 // to see serial output
77 // connect at 115200 so we can read the GPS fast enough and echo without dropping chars
80 Serial.println("\r\nUltimate GPSlogger Shield");
81 pinMode(ledPin, OUTPUT);
83 // make sure that the default chip select pin is set to
84 // output, even if you don't use it:
87 // see if the card is present and can be initialized:
88 if (!SD.begin(chipSelect, 11, 12, 13)) {
89 //if (!SD.begin(chipSelect)) { // if you're using an UNO, you can use this line instead
90 Serial.println("Card init. failed!");
94 strcpy(filename, "GPSLOG00.TXT");
95 for (uint8_t i = 0; i < 100; i++) {
96 filename[6] = '0' + i/10;
97 filename[7] = '0' + i%10;
98 // create if does not exist, do not open existing, write, sync after write
99 if (! SD.exists(filename)) {
104 logfile = SD.open(filename, FILE_WRITE);
106 Serial.print("Couldnt create "); Serial.println(filename);
109 Serial.print("Writing to "); Serial.println(filename);
111 // connect to the GPS at the desired rate
114 // uncomment this line to turn on RMC (recommended minimum) and GGA (fix data) including altitude
115 GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
116 // uncomment this line to turn on only the "minimum recommended" data
117 //GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY);
118 // For logging data, we don't suggest using anything but either RMC only or RMC+GGA
119 // to keep the log files at a reasonable size
120 // Set the update rate
121 GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); // 1 or 5 Hz update rate
123 // Turn off updates on antenna status, if the firmware permits it
124 GPS.sendCommand(PGCMD_NOANTENNA);
126 Serial.println("Ready!");
132 if (c) Serial.print(c);
134 // if a sentence is received, we can check the checksum, parse it...
135 if (GPS.newNMEAreceived()) {
136 // a tricky thing here is if we print the NMEA sentence, or data
137 // we end up not listening and catching other sentences!
138 // so be very wary if using OUTPUT_ALLDATA and trying to print out data
139 //Serial.println(GPS.lastNMEA()); // this also sets the newNMEAreceived() flag to false
141 if (!GPS.parse(GPS.lastNMEA())) // this also sets the newNMEAreceived() flag to false
142 return; // we can fail to parse a sentence in which case we should just wait for another
145 Serial.println("OK");
146 if (LOG_FIXONLY && !GPS.fix) {
147 Serial.print("No Fix");
152 Serial.println("Log");
154 char *stringptr = GPS.lastNMEA();
155 uint8_t stringsize = strlen(stringptr);
156 if (stringsize != logfile.write((uint8_t *)stringptr, stringsize)) //write the string to the SD file
158 if (strstr(stringptr, "RMC")) logfile.flush();