]> git.piffa.net Git - arduino/blob - books/pdummies/Libraries/Adafruit_GPS/examples/shield_sdlog/shield_sdlog.ino
first commit
[arduino] / books / pdummies / Libraries / Adafruit_GPS / examples / shield_sdlog / shield_sdlog.ino
1
2 #include <Adafruit_GPS.h>
3 #include <SoftwareSerial.h>
4 #include <SD.h>
5 #include <avr/sleep.h>
6 #include "GPSconfig.h"
7
8 // Ladyada's logger modified by Bill Greiman to use the SdFat library
9 //
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
13 // desired.
14 //
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
20
21 SoftwareSerial mySerial(8, 7);
22 Adafruit_GPS GPS(&mySerial);
23
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
26 #define GPSECHO  true
27 /* set to true to only log to SD when GPS has a fix, for debugging, keep it false */
28 #define LOG_FIXONLY false  
29
30 // Set the pins used
31 #define chipSelect 10
32 #define ledPin 13
33
34 File logfile;
35
36 // read a Hex value and return the decimal equivalent
37 uint8_t parseHex(char c) {
38   if (c < '0')
39     return 0;
40   if (c <= '9')
41     return c - '0';
42   if (c < 'A')
43     return 0;
44   if (c <= 'F')
45     return (c - 'A')+10;
46 }
47
48 // blink out an error code
49 void error(uint8_t errno) {
50 /*
51   if (SD.errorCode()) {
52     putstring("SD error: ");
53     Serial.print(card.errorCode(), HEX);
54     Serial.print(',');
55     Serial.println(card.errorData(), HEX);
56   }
57   */
58   while(1) {
59     uint8_t i;
60     for (i=0; i<errno; i++) {
61       digitalWrite(ledPin, HIGH);
62       delay(100);
63       digitalWrite(ledPin, LOW);
64       delay(100);
65     }
66     for (i=errno; i<10; i++) {
67       delay(200);
68     }
69   }
70 }
71
72 void setup() {
73   // for Leonardos, if you want to debug SD issues, uncomment this line
74   // to see serial output
75   //while (!Serial);
76   
77   // connect at 115200 so we can read the GPS fast enough and echo without dropping chars
78   // also spit it out
79   Serial.begin(115200);
80   Serial.println("\r\nUltimate GPSlogger Shield");
81   pinMode(ledPin, OUTPUT);
82
83   // make sure that the default chip select pin is set to
84   // output, even if you don't use it:
85   pinMode(10, OUTPUT);
86   
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!");
91     error(2);
92   }
93   char filename[15];
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)) {
100       break;
101     }
102   }
103
104   logfile = SD.open(filename, FILE_WRITE);
105   if( ! logfile ) {
106     Serial.print("Couldnt create "); Serial.println(filename);
107     error(3);
108   }
109   Serial.print("Writing to "); Serial.println(filename);
110   
111   // connect to the GPS at the desired rate
112   GPS.begin(9600);
113
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
122
123   // Turn off updates on antenna status, if the firmware permits it
124   GPS.sendCommand(PGCMD_NOANTENNA);
125   
126   Serial.println("Ready!");
127 }
128
129 void loop() {
130   char c = GPS.read();
131   if (GPSECHO)
132      if (c)   Serial.print(c);
133
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
140         
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
143     
144     // Sentence parsed! 
145     Serial.println("OK");
146     if (LOG_FIXONLY && !GPS.fix) {
147         Serial.print("No Fix");
148         return;
149     }
150
151     // Rad. lets log it!
152     Serial.println("Log");
153     
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
157       error(4);
158     if (strstr(stringptr, "RMC"))   logfile.flush();
159     Serial.println();
160   }
161 }
162
163
164 /* End code */