-
Notifications
You must be signed in to change notification settings - Fork 0
/
GPS data logger.ino
122 lines (113 loc) · 4.62 KB
/
GPS data logger.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
117
118
119
120
121
122
//ARDUINO IDE CODE
#include <SD.h> //Load SD card library
#include<SPI.h> //Load SPI Library
#include <Adafruit_GPS.h> //Install the adafruit GPS library
#include <SoftwareSerial.h> //Load the Software Serial library
SoftwareSerial mySerial(3,2); //Initialize the Software Serial port
Adafruit_GPS GPS(&mySerial); //Create the GPS Object
String NMEA1; //Variable for first NMEA sentence
String NMEA2; //Variable for second NMEA sentence
char c; //to read characters coming from the GPS
float deg; //Will hold positin data in simple degree format
float degWhole; //Variable for the whole part of position
float degDec; //Variable for the decimal part of degree
int chipSelect = 4; //chipSelect pin for the SD card Reader
File mySensorData; //Data object you will write your sesnor data to
void setup() {
Serial.begin(9600); //Turn on serial monitor
GPS.begin(9600); //Turn on GPS at 9600 baud
GPS.sendCommand("$PGCMD,33,0*6D"); //Turn off antenna update nuisance data
GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA); //Request RMC and GGA Sentences only
GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); //Set update rate to 1 hz
delay(1000);
pinMode(10, OUTPUT); //Must declare 10 an output and reserve it to keep SD card happy
SD.begin(chipSelect); //Initialize the SD card reader
if (SD.exists("NMEA.txt")) { //Delete old data files to start fresh
SD.remove("NMEA.txt");
}
if (SD.exists("GPSData.txt")) { //Delete old data files to start fresh
SD.remove("GPSData.txt");
}
}
void loop() {
readGPS();
if(GPS.fix==1) { //Only save data if we have a fix
mySensorData = SD.open("NMEA.txt", FILE_WRITE); //Open file on SD card for writing
mySensorData.println(NMEA1); //Write first NMEA to SD card
mySensorData.println(NMEA2); //Write Second NMEA to SD card
mySensorData.close(); //Close the file
mySensorData = SD.open("GPSData.txt", FILE_WRITE);
degWhole=float(int(GPS.longitude/100)); //gives me the whole degree part of Longitude
degDec = (GPS.longitude - degWhole*100)/60; //give me fractional part of longitude
deg = degWhole + degDec; //Gives complete correct decimal form of Longitude degrees
if (GPS.lon=='W') { //If you are in Western Hemisphere, longitude degrees should be negative
deg= (-1)*deg;
}
mySensorData.print(deg,4); //writing decimal degree longitude value to SD card
mySensorData.print(","); //write comma to SD card
degWhole=float(int(GPS.latitude/100)); //gives me the whole degree part of latitude
degDec = (GPS.latitude - degWhole*100)/60; //give me fractional part of latitude
deg = degWhole + degDec; //Gives complete correct decimal form of latitude degrees
if (GPS.lat=='S') { //If you are in Southern hemisphere latitude should be negative
deg= (-1)*deg;
}
mySensorData.print(deg,4); //writing decimal degree longitude value to SD card
mySensorData.print(","); //write comma to SD card
mySensorData.print(GPS.altitude); //write altitude to file
mySensorData.print(" "); //format with one white space to delimit data sets
mySensorData.close();
}
}
void readGPS() {
clearGPS();
while(!GPS.newNMEAreceived()) { //Loop until you have a good NMEA sentence
c=GPS.read();
}
GPS.parse(GPS.lastNMEA()); //Parse that last good NMEA sentence
NMEA1=GPS.lastNMEA();
while(!GPS.newNMEAreceived()) { //Loop until you have a good NMEA sentence
c=GPS.read();
}
GPS.parse(GPS.lastNMEA()); //Parse that last good NMEA sentence
NMEA2=GPS.lastNMEA();
Serial.println(NMEA1);
Serial.println(NMEA2);
Serial.println("");
}
void clearGPS() { //Clear old and corrupt data from serial port
while(!GPS.newNMEAreceived()) { //Loop until you have a good NMEA sentence
c=GPS.read();
}
GPS.parse(GPS.lastNMEA()); //Parse that last good NMEA sentence
while(!GPS.newNMEAreceived()) { //Loop until you have a good NMEA sentence
c=GPS.read();
}
GPS.parse(GPS.lastNMEA()); //Parse that last good NMEA sentence
while(!GPS.newNMEAreceived()) { //Loop until you have a good NMEA sentence
c=GPS.read();
}
GPS.parse(GPS.lastNMEA()); //Parse that last good NMEA sentence
}
-----------------------------------------------------------------------------------------------------------------------
//KML WRAPPER FOR GPS DATA(LAT&LONG)
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<Style id="yellowPoly">
<LineStyle>
<color>7f00ffff</color>
<width>4</width>
</LineStyle>
<PolyStyle>
<color>7f00ff00</color>
</PolyStyle>
</Style>
<Placemark><styleUrl>#yellowPoly</styleUrl>
<LineString>
<extrude>1</extrude>
<tesselate>1</tesselate>
<altitudeMode>absolute</altitudeMode>
<coordinates>
</coordinates>
</LineString></Placemark>
</Document></kml>