Rev Author Line No. Line
3885 kakl 1 /*
2 SD card datalogger
3  
4 This example shows how to log data from three analog sensors
5 to an SD card using the SD library.
6  
7 The circuit:
8 * analog sensors on analog ins 0, 1, and 2
9 * SD card attached to SPI bus as follows:
10 ** MOSI - pin 11
11 ** MISO - pin 12
12 ** CLK - pin 13
13 ** CS - pin 4
14  
15 created 24 Nov 2010
16 modified 9 Apr 2012
17 by Tom Igoe
18  
19 This example code is in the public domain.
20  
21 */
22  
23 #include <SD.h>
24  
25  
26 // On the Ethernet Shield, CS is pin 4. Note that even if it's not
27 // used as the CS pin, the hardware CS pin (10 on most Arduino boards,
28 // 53 on the Mega) must be left as an output or the SD library
29 // functions will not work.
30 const int chipSelect = 4;
31 int led = 5;
32 unsigned int n = 0;
33 char inChar;
34 String dataString = "";
35 String filename = "log.csv";
36 int coll = 0;
37  
38 void setup()
39 {
40 // Open serial communications and wait for port to open:
41 Serial.begin(9600);
42 while (!Serial) {
43 ; // wait for serial port to connect. Needed for Leonardo only
44 }
45  
46  
47 Serial.print("Initializing SD card...");
48 // make sure that the default chip select pin is set to
49 // output, even if you don't use it:
50 pinMode(10, OUTPUT);
51 pinMode(led, OUTPUT);
52  
53 // see if the card is present and can be initialized:
54 if (!SD.begin(chipSelect)) {
55 Serial.println("Card failed, or not present");
56 // don't do anything more:
57 return;
58 }
59 Serial.println("card initialized.");
60  
3886 kakl 61 dataString = "";
62 ReadGPRMC();
63  
3885 kakl 64 // filename = dataString.substring(0,5) + dataString.substring(dataString.length()-5,dataString.length()) + filename;
65 int len = dataString.length()-1;
66 filename = dataString.substring(len-2,len) + dataString.substring(len-4,len-2) + dataString.substring(len-6,len-4) + dataString.substring(0,2) + ".CSV ";
67 }
68  
69 void loop()
70 {
3886 kakl 71 dataString = "";
72 ReadGPRMC();
73 ReadGPGGA();
74  
3885 kakl 75 digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
76 delay(20); // wait for a second
77 digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
78 delay(20); // wait for a second
79  
80 // make a string for assembling the data to log:
81 // String dataString = "";
82  
83 dataString += String(n++);
84 dataString += ",";
85  
86 // read three sensors and append to the string:
87 for (int analogPin = 0; analogPin < 2; analogPin++)
88 {
89 int sensor = analogRead(analogPin);
90 dataString += String(sensor);
91 if (analogPin < 1)
92 {
93 dataString += ",";
94 }
95 }
96  
97 char fileNameCharArray[filename.length()];
98 filename.toCharArray(fileNameCharArray, filename.length());
99 File dataFile = SD.open(fileNameCharArray, FILE_WRITE);
100 // open the file. note that only one file can be open at a time,
101 // so you have to close this one before opening another.
102 //File dataFile = SD.open(filename, FILE_WRITE);
103  
104 // if the file is available, write to it:
105 if (dataFile) {
106 dataFile.println(dataString);
107 dataFile.close();
108 // print to the serial port too:
109 Serial.println(dataString);
110 }
111 // if the file isn't open, pop up an error:
112 else {
113 Serial.println("error opening datalog.txt");
114 }
115 }
116  
117  
3886 kakl 118 void ReadGPRMC()
3885 kakl 119 {
3886 kakl 120 // $GPRMC,091451.00,A,4915.64143,N,01441.50397,E,0.053,,090215,,,A*74
121  
3885 kakl 122 coll = 0;
123  
124 while(1)
125 {
126 // get incoming byte:
127 while (!Serial.available());
128 if (Serial.read() != '$') continue;
129 while (!Serial.available());
130 if (Serial.read() != 'G') continue;
131 while (!Serial.available());
132 if (Serial.read() != 'P') continue;
133 while (!Serial.available());
134 if (Serial.read() != 'R') continue;
135 while (!Serial.available());
136 if (Serial.read() != 'M') continue;
137 while (!Serial.available());
138 if (Serial.read() != 'C') continue;
139 while (!Serial.available());
140 if (Serial.read() != ',') continue;
141 break;
142 }
143  
144 do
145 {
146 while (!Serial.available());
147 inChar = (char)Serial.read();
148 if (inChar == ',') coll++;
149 dataString += inChar;
150 }
151 while (coll < 9);
152 }
153  
154  
3886 kakl 155 void ReadGPGGA()
156 {
157 // $GPGGA,091451.00,4915.64143,N,01441.50397,E,1,09,0.90,443.2,M,44.0,M,,*50
3885 kakl 158  
3886 kakl 159 coll = 0;
160  
161 while(1)
162 {
163 // get incoming byte:
164 while (!Serial.available());
165 if (Serial.read() != '$') continue;
166 while (!Serial.available());
167 if (Serial.read() != 'G') continue;
168 while (!Serial.available());
169 if (Serial.read() != 'P') continue;
170 while (!Serial.available());
171 if (Serial.read() != 'G') continue;
172 while (!Serial.available());
173 if (Serial.read() != 'G') continue;
174 while (!Serial.available());
175 if (Serial.read() != 'A') continue;
176 while (!Serial.available());
177 if (Serial.read() != ',') continue;
178 break;
179 }
180  
181 do
182 {
183 while (!Serial.available());
184 inChar = (char)Serial.read();
185 if (inChar == ',') coll++;
186 if (coll > 4) dataString += inChar;
187 }
188 while (coll < 12);
189 }
3885 kakl 190  
191  
192  
3886 kakl 193