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