]> git.piffa.net Git - arduino/blob - books/pdummies/Libraries/Adafruit_GPS/examples/leo_parsing/leo_parsing.ino
first commit
[arduino] / books / pdummies / Libraries / Adafruit_GPS / examples / leo_parsing / leo_parsing.ino
1 // Test code for Adafruit GPS modules using MTK3329/MTK3339 driver
2 //
3 // This code shows how to listen to the GPS module in an interrupt
4 // which allows the program to have more 'freedom' - just parse
5 // when a new NMEA sentence is available! Then access data when
6 // desired.
7 //
8 // Tested and works great with the Adafruit Ultimate GPS module
9 // using MTK33x9 chipset
10 //    ------> http://www.adafruit.com/products/746
11 // Pick one up today at the Adafruit electronics shop 
12 // and help support open source hardware & software! -ada
13
14 //This code is intended for use with Arduino Leonardo and other ATmega32U4-based Arduinos
15
16 #include <Adafruit_GPS.h>
17 #include <SoftwareSerial.h>
18
19 // Connect the GPS Power pin to 5V
20 // Connect the GPS Ground pin to ground
21 // If using software serial (sketch example default):
22 //   Connect the GPS TX (transmit) pin to Digital 8
23 //   Connect the GPS RX (receive) pin to Digital 7
24 // If using hardware serial:
25 //   Connect the GPS TX (transmit) pin to Arduino RX1 (Digital 0)
26 //   Connect the GPS RX (receive) pin to matching TX1 (Digital 1)
27
28 // If using software serial, keep these lines enabled
29 // (you can change the pin numbers to match your wiring):
30 //SoftwareSerial mySerial(8, 7);
31 //Adafruit_GPS GPS(&mySerial);
32
33 // If using hardware serial, comment
34 // out the above two lines and enable these two lines instead:
35 Adafruit_GPS GPS(&Serial1);
36 HardwareSerial mySerial = Serial1;
37
38 // Set GPSECHO to 'false' to turn off echoing the GPS data to the Serial console
39 // Set to 'true' if you want to debug and listen to the raw GPS sentences
40 #define GPSECHO  true
41
42 void setup()  
43 {
44     
45   // connect at 115200 so we can read the GPS fast enough and echo without dropping chars
46   // also spit it out
47   Serial.begin(115200);
48   delay(5000);
49   Serial.println("Adafruit GPS library basic test!");
50
51   // 9600 NMEA is the default baud rate for Adafruit MTK GPS's- some use 4800
52   GPS.begin(9600);
53   
54   // uncomment this line to turn on RMC (recommended minimum) and GGA (fix data) including altitude
55   GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
56   // uncomment this line to turn on only the "minimum recommended" data
57   //GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY);
58   // For parsing data, we don't suggest using anything but either RMC only or RMC+GGA since
59   // the parser doesn't care about other sentences at this time
60   
61   // Set the update rate
62   GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);   // 1 Hz update rate
63   // For the parsing code to work nicely and have time to sort thru the data, and
64   // print it out we don't suggest using anything higher than 1 Hz
65
66   // Request updates on antenna status, comment out to keep quiet
67   GPS.sendCommand(PGCMD_ANTENNA);
68
69   delay(1000);
70   // Ask for firmware version
71   mySerial.println(PMTK_Q_RELEASE);
72 }
73
74 uint32_t timer = millis();
75 void loop()                     // run over and over again
76 {
77   char c = GPS.read();
78   // if you want to debug, this is a good time to do it!
79   if ((c) && (GPSECHO))
80     Serial.write(c); 
81   
82   // if a sentence is received, we can check the checksum, parse it...
83   if (GPS.newNMEAreceived()) {
84     // a tricky thing here is if we print the NMEA sentence, or data
85     // we end up not listening and catching other sentences! 
86     // so be very wary if using OUTPUT_ALLDATA and trytng to print out data
87     //Serial.println(GPS.lastNMEA());   // this also sets the newNMEAreceived() flag to false
88   
89     if (!GPS.parse(GPS.lastNMEA()))   // this also sets the newNMEAreceived() flag to false
90       return;  // we can fail to parse a sentence in which case we should just wait for another
91   }
92
93   // if millis() or timer wraps around, we'll just reset it
94   if (timer > millis())  timer = millis();
95
96   // approximately every 2 seconds or so, print out the current stats
97   if (millis() - timer > 2000) { 
98     timer = millis(); // reset the timer
99     
100     Serial.print("\nTime: ");
101     Serial.print(GPS.hour, DEC); Serial.print(':');
102     Serial.print(GPS.minute, DEC); Serial.print(':');
103     Serial.print(GPS.seconds, DEC); Serial.print('.');
104     Serial.println(GPS.milliseconds);
105     Serial.print("Date: ");
106     Serial.print(GPS.day, DEC); Serial.print('/');
107     Serial.print(GPS.month, DEC); Serial.print("/20");
108     Serial.println(GPS.year, DEC);
109     Serial.print("Fix: "); Serial.print((int)GPS.fix);
110     Serial.print(" quality: "); Serial.println((int)GPS.fixquality); 
111     if (GPS.fix) {
112       Serial.print("Location: ");
113       Serial.print(GPS.latitude, 4); Serial.print(GPS.lat);
114       Serial.print(", "); 
115       Serial.print(GPS.longitude, 4); Serial.println(GPS.lon);
116       
117       Serial.print("Speed (knots): "); Serial.println(GPS.speed);
118       Serial.print("Angle: "); Serial.println(GPS.angle);
119       Serial.print("Altitude: "); Serial.println(GPS.altitude);
120       Serial.print("Satellites: "); Serial.println((int)GPS.satellites);
121     }
122   }
123 }