Rev Author Line No. Line
4617 kakl 1 /*
2 SD card datalogger GPSRL04A
3  
4 The circuit:
5 * analog sensors are connected to analog ins PC0, PC1
6 * SD card attached to SPI bus as follows:
7 ** MOSI - pin PB3
8 ** MISO - pin PB4
9 ** CLK - pin PB5
10 ** CS - pin PD4
11  
12 */
13  
14 #include <SD.h>
15  
16 const int chipSelect = 4; // CS is PD4
17 int LED = 5; // LED is conected from PD5 to LED at GPS module
18 unsigned int n = 0; // measurement counter
19 char inChar; // input character from GPS
20 String dataString = ""; // concantenated string with NMEA messages and measured values
21 String filename = "log.csv"; // generated filename for log file
22 int coll = 0; // collons counter in NMEA messages
23  
24 void setup()
25 {
26 // Open serial communications and wait for port to open:
27 Serial.begin(9600);
28 while (!Serial) {;}
29  
30 Serial.print("Initializing SD card..."); // inserting a SD Card always reset the processor and call setup
31 // make sure that the default chip select pin is set to
32 // output, even if you don't use it:
33 pinMode(10, OUTPUT); // PB2
34 pinMode(LED, OUTPUT);
35  
36 // see if the card is present and can be initialized:
37 if (!SD.begin(chipSelect))
38 {
39 Serial.println("Card failed, or not present");
40 for(n=0; n<10; n++)
41 {
42 digitalWrite(LED, HIGH); // turn the LED on (HIGH is the voltage level)
43 delay(2000); // wait for a while
44 digitalWrite(LED, LOW); // turn the LED off by making the voltage LOW
45 delay(100);
46 }
47 asm volatile (" jmp 0");
48 // don't do anything more:
49 return;
50 }
51 Serial.println("card initialized.");
52  
53 dataString = "";
54 ReadGPRMC(); // read time from GPS
55 if (dataString[10]!='A')
56 {
57 Serial.println("no fix");
58 for(n=0; n<10; n++)
59 {
60 digitalWrite(LED, HIGH); // turn the LED on (HIGH is the voltage level)
61 delay(20); // wait for a while
62 digitalWrite(LED, LOW); // turn the LED off by making the voltage LOW
63 delay(20);
64 }
65 asm volatile (" jmp 0");
66 // don't do anything more:
67 }
68  
69 // derive file name fom time (YYMMDDHH.CSV)
70 int len = dataString.length()-1;
71 filename = dataString.substring(len-2,len) + dataString.substring(len-4,len-2) + dataString.substring(len-6,len-4) + dataString.substring(0,2) + ".CSV ";
72 }
73  
74 void loop()
75 {
76 dataString = ""; // make a string for assembling the data to log
77 ReadGPRMC(); // read NMEA sentences from GPS
78 ReadGPGGA();
79  
80 digitalWrite(LED, HIGH); // turn the LED on (HIGH is the voltage level)
81 delay(20); // wait for a while
82 digitalWrite(LED, LOW); // turn the LED off by making the voltage LOW
83 delay(20); // wait for a while
84  
85 // make a string for assembling the data to log:
86 dataString += String(n++);
87 dataString += ",";
88  
89 // read analog values from sensors and append to the string:
90 for (int analogPin = 0; analogPin < 2; analogPin++)
91 {
92 int sensor = analogRead(analogPin);
93 dataString += String(sensor);
94 if (analogPin < 1) // suppress last coma
95 {
96 dataString += ",";
97 }
98 }
99  
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 char fileNameCharArray[filename.length()];
103 filename.toCharArray(fileNameCharArray, filename.length());
104 File dataFile = SD.open(fileNameCharArray, FILE_WRITE);
105  
106 // if the file is available, write to it:
107 if (dataFile)
108 {
109 dataFile.println(dataString);
110 dataFile.close();
111 // print to the serial port too:
112 Serial.println(dataString);
113 }
114 // if the file isn't open, pop up an error:
115 else {
116 Serial.println("error opening datalog.CSV");
117 for(n=0; n<10; n++)
118 {
119 digitalWrite(LED, HIGH); // turn the LED on (HIGH is the voltage level)
120 delay(20); // wait for a while
121 digitalWrite(LED, LOW); // turn the LED off by making the voltage LOW
122 delay(20);
123 }
124 asm volatile (" jmp 0");
125 // don't do anything more:
126  
127 }
128 }
129  
130  
131 // function for reading $GPRMC NMEA message
132 void ReadGPRMC()
133 {
134 // $GPRMC,091451.00,A,4915.64143,N,01441.50397,E,0.053,,090215,,,A*74
135  
136 coll = 0;
137  
138 while(1) // wait for $GPRMC
139 {
140 // get incoming byte:
141 while (!Serial.available());
142 if (Serial.read() != '$') continue;
143 while (!Serial.available());
144 if (Serial.read() != 'G') continue;
145 while (!Serial.available());
146 if (Serial.read() != 'P') continue;
147 while (!Serial.available());
148 if (Serial.read() != 'R') continue;
149 while (!Serial.available());
150 if (Serial.read() != 'M') continue;
151 while (!Serial.available());
152 if (Serial.read() != 'C') continue;
153 while (!Serial.available());
154 if (Serial.read() != ',') continue;
155 break;
156 }
157  
158 do
159 {
160 while (!Serial.available());
161 inChar = (char)Serial.read();
162 if (inChar == ',') coll++;
163 dataString += inChar;
164 }
165 while (coll < 9); // read only 9 coma separated values
166 }
167  
168  
169 // function for reading $GPGGA NMEA message
170 void ReadGPGGA()
171 {
172 // $GPGGA,091451.00,4915.64143,N,01441.50397,E,1,09,0.90,443.2,M,44.0,M,,*50
173  
174 coll = 0;
175  
176 while(1) // wait for $GPGGA
177 {
178 while (!Serial.available());
179 if (Serial.read() != '$') continue;
180 while (!Serial.available());
181 if (Serial.read() != 'G') continue;
182 while (!Serial.available());
183 if (Serial.read() != 'P') continue;
184 while (!Serial.available());
185 if (Serial.read() != 'G') continue;
186 while (!Serial.available());
187 if (Serial.read() != 'G') continue;
188 while (!Serial.available());
189 if (Serial.read() != 'A') continue;
190 while (!Serial.available());
191 if (Serial.read() != ',') continue;
192 break;
193 }
194  
195 do
196 {
197 while (!Serial.available());
198 inChar = (char)Serial.read();
199 if (inChar == ',') coll++;
200 if (coll > 4) dataString += inChar; // skip first 5 coma separated values
201 }
202 while (coll < 12); // read only 7 coma separated values
203 }
204  
205  
206  
207