In this lesson we bring together a lot of the material from the first 26 lessons to create an instrument package that could be deployed in a demonstration project. We will wire wrap up an Arduino Nano, a Virtuabotix SD card reader, and the Adafruit Ultimate GPS to create a system that will track and log position and altitude, and save the data in a format that can be displayed on Google Earth.
You will want to place the Adafruit GPS, the SD Card Reader, and the Arduino Nano into a perf board. Then, you will want to carefully wire wrap the components as follows:
Connecting the Adafruit Ultimate GPS Unit to Arduino
GPS Pin
Arduino Pin
Vin
5V
GND
GND
RX
Pin 2
TX
Pin 3
Connecting the SD Card Reader
Sd Card Reader Pin
Arduino Pin
Details
GND
GND
Common Ground
3.3 V – (NOT USED)
+5
5V
Power
CS
4
Chip Select
MOSI
11
SPI Data
SCK
13
Clock
MISO
12
SPI Data
GND
GND
Common Ground
Now the code you developed in LESSON 26 should run on this prototype. The code creates a Google Earth friendly set of coordinates. Just put a KML wrapper on the coordinates as described in LESSON 26. Putting it all together, I took the system outside and walked around, and this is the data track I got.
In this lesson we will learn how to take the data we are logging from our GPS, and display it properly on Google Earth. This lesson builds on Lesson 22, Lesson 23 and Lesson 24. You will need to complete those lessons before undertaking this lesson. In this series of lessons we are using the Arduino, Adafruit Ultimate GPS, Virtuabotix SD card reader, and a Battery Clip to power the arduino during your mobile jaunts. You will need a 9V battery. If you are going to be doing a lot of mobile work, it is probably worth it to invest in a good 9v Rechargable battery system.
When you get your gear gathered up, you will be ready to get this project going. First, hook the GPS and Card Reader up as we did in Lesson 23. We will start with the software that we used in Lesson 23. This software correctly reads data from the GPS and then logs the data into two data files on the SD card. The problem, though, we did not really do any fine tuning of the data file we were creating.
In this lesson we want to create a data file that we can display as a track in Google Earth. In order for this to work, we have to save the data in the manner a KML file wants to see. We have to save the data as:
Decimal Longitude, Decimal latitude, Altitude
I find it works to not use a line ending, but put a single white space after the Altitude. That is, delimit with commas as shown above, but then use one white space to delimit between successive lines of data.
Lesson 24 explained in great detail how to interpret the NMEA sentences. The challenge here is that the Arduino is very poor at parsing strings. The NMEA sentence would be easy to parse in Python, because Python is so good at easily manipulating strings. Since it is tedious to manipulate strings in Arduino, we are going to try and parse the data using numbers and math, not strings. To explain this, I will give an example for Latitude, and Longitude will work in a similar manner. The latitude data we get from the parsed data from the Adafruit GPS looks like this:
3051.8007
From lesson 24, we know that this represents 30 degrees and 51.8007 minutes. In order to parse this in Arduino using just numbers I do the following. First create three variables deg, degWhole, and degDec. The variable deg will hold the final answer, degWhole holds the whole part of the number, and degDec whole the part to the right of the decimal. Adding degWhole to degDec gives you deg, your final answer. So, lets assume x = 3051.8007, our Latitude from the GPS.
Arduino
1
degWhole=float(int(x/100));
Dividing by 100 moves the decimal to the left two spots, and taking the int removes anything to the right of the decimal. Then converting back to float gives us a round number with no values to the right of the decimal. For 3051.8007 it turns it into 30.0, the whole number part of degrees. Now to get the fractional part, we need to take minutes and divide by 60. This will always be a number less than one. To get fractional part of the latitude in degrees, we do the following:
Arduino
1
degDec=(x-degWhole*100)/60;
We have to multiply degWhoe by 100 to get the decimal moved back to the right by 2. Then we subtract that number from the original latitude, and we are left with minutes. Then divide by 60 and we have converted minutes to a fraction of a degree. We now have the whole part and the fractional part of the answer, so we just add those together:
Arduino
1
deg=degWhole+degDec;
We now have a nice decimal number, deg, which is the decimal representation of our Latitude. We have one more thing to deal with. If the GPS is reporting ‘N’, leave the number positive. If the GPS is reporting ‘S’ (that we are in the Southern Hemisphere), you need to make your latitude number negative. Similarly, when you are doing your longitude, you need to make the longitude negative if you are in the Western Hemisphere.
Finally, when you write the file, Google Earth will want you to store the data as:
Longitude, Latitude,altitude
It is curious that this is reverse from what you would do if you were entering coordinates into the Google Earth search bar. That wants Latitude first.
In any event, with the parsing and formatting described above, you can create a file that is almost ready to read into Google Earth. The code below will create your coordinates in the correct format for KML, which Google Earth will read. Please watch the video for a complete description of the code.
Arduino
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
#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
SoftwareSerialmySerial(3,2);//Initialize the Software Serial port
Adafruit_GPSGPS(&mySerial);//Create the GPS Object
StringNMEA1;//Variable for first NMEA sentence
StringNMEA2;//Variable for second NMEA sentence
charc;//to read characters coming from the GPS
floatdeg;//Will hold positin data in simple degree format
floatdegWhole;//Variable for the whole part of position
floatdegDec;//Variable for the decimal part of degree
intchipSelect=4;//chipSelect pin for the SD card Reader
FilemySensorData;//Data object you will write your sesnor data to
voidsetup(){
Serial.begin(115200);//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");
}
}
voidloop(){
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();
}
}
voidreadGPS(){
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("");
}
voidclearGPS(){//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
}
The final thing we have to do is to put a “wrapper” around the coordinates to turn the coordinates into a .kml file Google Earth will like. I just do this manually. I open the text file on the SD card created by the code above, and then just paste it into this KML wrapper, and save the file with a .kml extension. The KML wrapper is as follows:
C
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
<?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>
Just paste your coordinate data in the file above between <coordinates> and </coordinates>, save with a .kml extension, and you should be able to open it with Google Earth and see your track. Good luck!
GPS units spit out data in a dizzying array of formats. It can become a challenge to take the output from a GPS unit, like the Adafruit Ultimate GPS breakout board, and get that data to play nicely with mapping programs like Google earth. It is as if no one is speaking the same language. In this lesson we will look at what the data coming off the GPS means, and how you can work with it, and get it to display properly in programs like Google Earth.
When you connect a GPS board up, and look at the data coming off of it, you are likely to see something like this:
This is what the data looks like when your GPS does not have a fix. When it does have a fix, there will be all types of numbers between the commas in the lines above. So, how do we make sense of all this? The first thing is to learn some of the lingo. Each line above is referred to as a NMEA sentence. There are different NMEA sentence type, and the type is indicated by the first characters before the comma. So, $GPGSA is a certain type of NMEA sentence, and $GPRMC is a different type of NMEA sentence. To make this whole thing a manageable problem, the first thing we must realize is that the data that we want will be in the $GPRMC sentence and the $GPGGA sentence. If your GPS unit has options to turn the other sentences off, then turn them off to simplify the data getting thrown at you. If you can not turn the other sentences off, then just ignore them, and focus on the two you care about, $GPRMC, $GPGGA.
If your GPS has a fix, then your GPS sentences should look something like this:
The $GPRMC is simply telling what sentence type is on the line. The next number, represents the Coordinated Universal Time (UTC). It works like this, 194530.000 would be 19:45 and 30.0 seconds in UTC. Since converting from UTC to your local time is simply a matter of knowing what time zone you are in, you can see that one thing you get from a GPS is a very accurate clock. The next letter just lets you know if your signal is Active (A) or Void (V). An ‘A’ indicates you are getting a signal and things are working properly. A ‘V’ means you do not have a signal.
Now, for the good stuff, the next number and following letter gives you your lattitude. 3051.8007,N should be interpreted as follows:
Your Latitude is: 30 degrees, 51.007 minutes, in the Northern Hemisphere.
Similarly, the next number, 10035.9989,W, is Longitude. This is interpretted as:
Your Longitude is: 100 degrees 35.9989 minutes, in the Western Hemisphere.
Degrees could be a one, two or three digit number. Hence you have to be careful parsing this data. What is always the case is that the first two numerals to the left of the decimal place, and the numbers to the right of the decimal represent the minutes. So, always take the number starting two to the left of the decimal, and those following all the way to the comma, and that is your minutes. Unfortunately, if you simply try to put 3051.8007,N,10035.9989,W, into Google Earth, it will not recognize it. The simplest thing you can do to get Google Earth to recognize and properly display the coordinate would be to enter the following:
30 51.8007N, 100 35.9989W
Notice the space inserted between degrees and minutes, and no comma before hemisphere designation. This would properly display that single point via the Google Earth search bar. The challenge however is that if you want to log data from your GPS and then show a track of where you have been, you will have to create a KML or KMZ file. These files are even more particular about the format of the coordinate data. These files want both latitude and longitude as decimal numbers, so we must convert degrees, minutes to decimal degrees. You can do this simply by realizing that there are sixty minutes in a degree. So, for the case above, 51.8007 Minutes = 51.8007/60 degrees = .86335 degrees. So, to make a KML file that Google Earth will like, 30 51.8007N should be converted to 30.86335. We still have to deal with the N and W. KML files do not know what to do with the N, S, E, W. So, you must do the conversion. On Latitude, if you have a N, leave Latitude positive, if you have a S make your Latitude negative. On Longitude, if you have an E leave your number positive. If you have a W make your Longitude negative. Following these rules:
C
1
3051.8007,N,10035.9989,W
Should become:
30.8633, -100.5999
Those numbers will not only display properly in a KML file, they will also work if directly typed into the search bar on Google Earth.
We will talk more about KML files in the next lesson, but we will state here that in the KML file, you should put Longitude first, followed by Latitude. Opposite of how you put the numbers in Google earth, which wants latitude followed by longitude.
OK, that is the hard part, now for the rest of the characters in the sentence. The next number, 1.49 in the example above is the speed in knots. I was walking, so you can see this is a small number in the above example. The next number represents track angle, not something I find useful or use. Then after that, you see the number 310714. This is the date, which would be DD/MM/YY so this date is July 31, 2014. You can see on my NMEA sentence, I just get commas after that. Some units report magnetic variation here, but mine doesn’t. Then finally, the final characters represent a checksum, something we do not need to worry with.
So there, our first NMEA sentence parsed. You can see that you get just about everything you need in this one sentence.
So why do we need the $GPGGA sentence you ask. Well, it has a couple of nuggets of info we need, particularly if we are doing High Altitude Ballooning/Edge of Space work. Lets look at the $GPGGA sentence:
The next numbers represent Latitude and Longitude, just as in the $GPRMC sentence above, so I will not go through that again.
Now the next number, 1 above, is the fix quality. a 1 means you have a fix and a 0 means you do not have a fix. If there is a different number it is more details on the type of fix you have. Next number is how many satellites you are tracking. For us, 4. The larger number here the better. Next up, 2.18 is the horizontal dilution of position. Something I don’t worry with. Next one is an important one . . . 746.4 is my altitude in meters above mean seal level. This is hugely important for projects like high altitude ballooning. It is a direct indication of your altitude. Next number is the altitude of mean sea level where you are. This is something you do not need to worry about in most applications but is related to a geometric approximation of the earth as an ellipsoid. Look up WGS84 Ellipsoid if you really want to understand this number.
OK, lets bring this all home now. Using the $GPRMC and $GPGGA NMEA sentences, we can get our latitude and longitude coordinates, and convert them to numbers that can be recognized by Google Earth, or put into a KML file. We also have our speed in knots, and our altitude. These are all the things we would want for projects like unmanned aerial drones, or high altitude balloons.
In this lesson we will extend what we did in lesson 22 to include a data logger with our GPS. You should be sure and do lesson 22 first, as that shows you how to hook up and program the GPS. In this project we will connect an SD Card Reader/Writer, and will power the whole thing with a 9 Volt battery. With this, you can walk around, and the device will constantly monitor and log your position. You can then bring it back inside, and look at where you have been using Google Earth.
To start with, lets talk about what you will need to do this project. As described earlier, you need to start by doing lesson 22 which will get you going on the GPS hookup and initial software. Now, to move forward, you will need an Arduino, Adafruit Ultimate GPS, Virtuabotix SD card reader, and a Battery Clip to power the arduino during your mobile jaunts. You will need a 9V battery. If you are going to be doing a lot of mobile work, it is probably worth it to invest in a good 9v Rechargable battery system.
When you get your gear gathered up, you will be ready to get this project going. First, hook the GPS up to the arduino as we did in lesson 22:
Connecting the Adafruit Ultimate GPS Unit to Arduino
GPS Pin
Arduino Pin
Vin
5V
GND
GND
RX
Pin 2
TX
Pin 3
You also need to hook up the SC Card Reader/Writer. You should be familiar with this from lesson 21.
Connecting the SD Card Reader
Sd Card Reader Pin
Arduino Pin
Details
GND
GND
Common Ground
3.3 V – (NOT USED)
+5
5V
Power
CS
4
Chip Select
MOSI
11
SPI Data
SCK
13
Clock
MISO
12
SPI Data
GND
GND
Common Ground
Remember that this SD card reader has two rows of pins. Don’t let that confuse you, both rows are equivalent. You can connect to either row.
Most of the code has already been written in Lesson 22. In this lesson, the main thing we will be doing is to write the GPS data to the SD card. This way, as we walk around outside, it will log our positions to the SD card. We will create two files. One will hold the raw NMEA sentences, and the other our location data, as parsed by the adafruit GPS library. You should be able to figure most of this out from what you already know from lesson 22 and lesson 21. In the video above, we take you through this code step-by-step, so watch the video to understand what we are doing. If you get stuck, you can look at the code below.
Arduino
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
#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
SoftwareSerialmySerial(3,2);//Initialize the Software Serial port
Adafruit_GPSGPS(&mySerial);//Create the GPS Object
StringNMEA1;//Variable for first NMEA sentence
StringNMEA2;//Variable for second NMEA sentence
charc;//to read characters coming from the GPS
intchipSelect=4;//chipSelect pin for the SD card Reader
FilemySensorData;//Data object you will write your sesnor data to
voidsetup(){
Serial.begin(115200);//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");
}
}
voidloop(){
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);
mySensorData.print(GPS.latitude,4);//Write measured latitude to file
mySensorData.print(GPS.lat);//Which hemisphere N or S
mySensorData.print(",");
mySensorData.print(GPS.longitude,4);//Write measured longitude to file
mySensorData.print(GPS.lon);//Which Hemisphere E or W
mySensorData.print(",");
mySensorData.println(GPS.altitude);
mySensorData.close();
}
}
voidreadGPS(){
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("");
}
voidclearGPS(){//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
OK, it is time for us to take our projects up to that next level. We are going to build a GPS tracker from scratch. This is going to take several lessons to complete, but it will build on what you already know, and is really not going to be that difficult of a project. We will be using the most excellent Adafruit Ultimate GPS module. This is an excellent GPS. I like it because it is affordable, easy to use, and is one of the few that will work at extreme elevations, making it ideal for our Edge of Space/High Altitude Balloon work.
This unit is pretty easy to hook up, as you can see in the Table below:
Connecting the Adafruit Ultimate GPS Unit to Arduino
GPS Pin
Arduino Pin
Vin
5V
GND
GND
RX
Pin 2
TX
Pin 3
Our goal in this lesson is to get the GPS connected, and get it reading NMEA sentences. NMEA is a data format used by GPS and mapping devices and software to keep track of position coordinates. There are lots of different NMEA sentences, but the two that contain the most useful information are the $GPRMC and $GPGGA sentences.
Our interest is in creating a location tracker for our High Altitude Balloon work, and the $GPRMC and $GPGGA sentences contain all the information and data we would need for that work. These sentences contain the lattitude, longitude, time, altitude, and velocity.
The GPS modules are pretty easy to work with. When you apply power to the GPS, it immediately starts spitting out NMEA sentences to its serial port. Our job on the arduino side is to simply read these data strings, and then parse them into useful data. The thing that is a challenge is that they constantly spit out data, whether you want it, or whether you are ready for it or not. In developing the software, we have to be mindful the the data is always spewing out of the GPS. Typically, we will have other components on our package, like temperature, pressure and inertial sensors. While our arduino is out making measurements on these other sensors, data continues to stream in from the GPS, likely overflowing our serial buffer. When we return to make a GPS measurement, it is very likely that the serial buffer will have old or corrupt GPS data in it. Hence, we must be mindful to deal with this issue in developing our software.
The video above takes you step-by-step through connecting and reading the NMEA sentences step-by-step. Then in the next lesson we will parse and log the data to create a portable GPS tracker. The code below is explained in the video. You need to watch the video to understand this code, and so you will be able to begin to work with this code to create a portable GPS tracker.
In order for this software to work, you need to download and install theAdafruit GPS Library.
Arduino
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
//Make sure to install the adafruit GPS library from https://github.com/adafruit/Adafruit-GPS-Library
#include <Adafruit_GPS.h> //Load the GPS Library. Make sure you have installed the library form the adafruit site above
#include <SoftwareSerial.h> //Load the Software Serial Library. This library in effect gives the arduino additional serial ports
SoftwareSerialmySerial(3,2);//Initialize SoftwareSerial, and tell it you will be connecting through pins 2 and 3
Adafruit_GPSGPS(&mySerial);//Create GPS object
StringNMEA1;//We will use this variable to hold our first NMEA sentence
StringNMEA2;//We will use this variable to hold our second NMEA sentence
charc;//Used to read the characters spewing from the GPS module
voidsetup()
{
Serial.begin(115200);//Turn on the Serial Monitor
GPS.begin(9600);//Turn GPS on at baud rate of 9600
GPS.sendCommand("$PGCMD,33,0*6D");// Turn Off GPS Antenna Update
GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);//Tell GPS we want only $GPRMC and $GPGGA NMEA sentences
readGPS();//This is a function we define below which reads two NMEA sentences from GPS
}
voidreadGPS(){//This function will read and remember two NMEA sentences from GPS
clearGPS();//Serial port probably has old or corrupt data, so begin by clearing it all out
while(!GPS.newNMEAreceived()){//Keep reading characters in this loop until a good NMEA sentence is received
c=GPS.read();//read a character from the GPS
}
GPS.parse(GPS.lastNMEA());//Once you get a good NMEA, parse it
NMEA1=GPS.lastNMEA();//Once parsed, save NMEA sentence into NMEA1
while(!GPS.newNMEAreceived()){//Go out and get the second NMEA sentence, should be different type than the first one read above.
c=GPS.read();
}
GPS.parse(GPS.lastNMEA());
NMEA2=GPS.lastNMEA();
Serial.println(NMEA1);
Serial.println(NMEA2);
Serial.println("");
}
voidclearGPS(){//Since between GPS reads, we still have data streaming in, we need to clear the old data by reading a few sentences, and discarding these
while(!GPS.newNMEAreceived()){
c=GPS.read();
}
GPS.parse(GPS.lastNMEA());
while(!GPS.newNMEAreceived()){
c=GPS.read();
}
GPS.parse(GPS.lastNMEA());
}
Making The World a Better Place One High Tech Project at a Time. Enjoy!
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok