-
Notifications
You must be signed in to change notification settings - Fork 1
/
GPS_Recorder.ino
executable file
·116 lines (90 loc) · 2.52 KB
/
GPS_Recorder.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**
* @author Jason Favrod <mail@jasonfavrod.com>
* @version 1.0.0
*/
#include <TinyGPS.h>
#include <SoftwareSerial.h>
#include <SPI.h>
#include <SD.h>
SoftwareSerial mySerial(3,4);
TinyGPS gps;
File myFile;
void gpsdump(TinyGPS &gps);
void setup()
{
Serial.begin(9600);
mySerial.begin(9600);
while (!Serial) {
;
}
Serial.print("initializing SD card...");
if (!SD.begin(10)) {
Serial.println("initialization failed!");
while(1);
}
Serial.println("initialization done.\n");
delay(1000);
Serial.println("uBlox Neo 6M");
Serial.print("TinyGPS library v. ");
Serial.println(TinyGPS::library_version());
Serial.print("loaded size: ");
Serial.println(sizeof(TinyGPS));
Serial.println();
}
void loop()
{
bool newdata = false;
unsigned long start = millis();
while (millis() - start < 5000)
{
if (mySerial.available())
{
char c = mySerial.read();
if (gps.encode(c))
newdata = true;
}
}
if (newdata)
gpsdump(gps);
else
Serial.println("No Data");
}
void gpsdump(TinyGPS &gps)
{
float lat, lon;
unsigned long age, date, time, chars;
int year;
byte month, day, hour, minute, second, hundredths;
String record = "";
gps.f_get_position(&lat, &lon, &age);
gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age);
record = String(record + String(lat, 6));
record = String(record + ",");
record = String(record + String(lon, 6));
record = String(record + ",");
record = String(record + year);
record = String(record + "-");
if (month < 10) record = String(record + "0");
record = String(record + static_cast<int>(month));
record = String(record + "-");
if (day < 10) record = String(record + "0");
record = String(record + static_cast<int>(day));
record = String(record + "T");
if (hour < 10) record = String(record + "0");
record = String(record + static_cast<int>(hour));
record = String(record + ":");
if (minute < 10) record = String(record + "0");
record = String(record + static_cast<int>(minute));
record = String(record + ":");
if (second < 10) record = String(record + "0");
record = String(record + static_cast<int>(second));
record = (record + "Z");
Serial.println(record);
myFile = SD.open("data.csv", FILE_WRITE);
if (myFile) {
myFile.println(record);
myFile.close();
}
else
Serial.println(" error opening data file!");
}