|
| 1 | +// A simple sketch to read GPS data and parse the $GPRMC string |
| 2 | +// see http://www.ladyada.net/make/gpsshield for more info |
| 3 | + |
| 4 | +// If using Arduino IDE prior to version 1.0, |
| 5 | +// make sure to install newsoftserial from Mikal Hart |
| 6 | +// http://arduiniana.org/libraries/NewSoftSerial/ |
| 7 | +#if ARDUINO >= 100 |
| 8 | + #include "Arduino.h" |
| 9 | + #include "SoftwareSerial.h" |
| 10 | +#else |
| 11 | + #include "WProgram.h" |
| 12 | + #include "NewSoftSerial.h" |
| 13 | +#endif |
| 14 | + |
| 15 | +// Use pins 2 and 3 to talk to the GPS. 2 is the TX pin, 3 is the RX pin |
| 16 | +#if ARDUINO >= 100 |
| 17 | +SoftwareSerial mySerial = SoftwareSerial(2, 3); |
| 18 | +#else |
| 19 | +NewSoftSerial mySerial = NewSoftSerial(2, 3); |
| 20 | +#endif |
| 21 | + |
| 22 | +// Use pin 4 to control power to the GPS |
| 23 | +#define powerpin 4 |
| 24 | + |
| 25 | +// Set the GPSRATE to the baud rate of the GPS module. Most are 4800 |
| 26 | +// but some are 38400 or other. Check the datasheet! |
| 27 | +#define GPSRATE 4800 |
| 28 | +//#define GPSRATE 38400 |
| 29 | + |
| 30 | +// The buffer size that will hold a GPS sentence. They tend to be 80 characters long |
| 31 | +// so 90 is plenty. |
| 32 | +#define BUFFSIZ 90 // plenty big |
| 33 | + |
| 34 | + |
| 35 | +// global variables |
| 36 | +char buffer[BUFFSIZ]; // string buffer for the sentence |
| 37 | +char *parseptr; // a character pointer for parsing |
| 38 | +char buffidx; // an indexer into the buffer |
| 39 | + |
| 40 | +// The time, date, location data, etc. |
| 41 | +uint8_t hour, minute, second, year, month, date; |
| 42 | +uint32_t latitude, longitude, trackangle; |
| 43 | +uint8_t groundspeed; |
| 44 | +char latdir, longdir; |
| 45 | +char status; |
| 46 | + |
| 47 | +void setup() |
| 48 | +{ |
| 49 | + if (powerpin) { |
| 50 | + pinMode(powerpin, OUTPUT); |
| 51 | + } |
| 52 | + |
| 53 | + // Use the pin 13 LED as an indicator |
| 54 | + pinMode(13, OUTPUT); |
| 55 | + |
| 56 | + // connect to the serial terminal at 9600 baud |
| 57 | + Serial.begin(9600); |
| 58 | + |
| 59 | + // connect to the GPS at the desired rate |
| 60 | + mySerial.begin(GPSRATE); |
| 61 | + |
| 62 | + // prints title with ending line break |
| 63 | + Serial.println("GPS parser"); |
| 64 | + |
| 65 | + digitalWrite(powerpin, LOW); // pull low to turn on! |
| 66 | +} |
| 67 | + |
| 68 | +uint32_t parsedecimal(char *str) { |
| 69 | + uint32_t d = 0; |
| 70 | + |
| 71 | + while (str[0] != 0) { |
| 72 | + if ((str[0] > '9') || (str[0] < '0')) |
| 73 | + return d; |
| 74 | + d *= 10; |
| 75 | + d += str[0] - '0'; |
| 76 | + str++; |
| 77 | + } |
| 78 | + return d; |
| 79 | +} |
| 80 | + |
| 81 | +void readline(void) { |
| 82 | + char c; |
| 83 | + |
| 84 | + buffidx = 0; // start at begninning |
| 85 | + while (1) { |
| 86 | + c=mySerial.read(); |
| 87 | + if (c == -1) |
| 88 | + continue; |
| 89 | + Serial.print(c); |
| 90 | + if (c == '\n') |
| 91 | + continue; |
| 92 | + if ((buffidx == BUFFSIZ-1) || (c == '\r')) { |
| 93 | + buffer[buffidx] = 0; |
| 94 | + return; |
| 95 | + } |
| 96 | + buffer[buffidx++]= c; |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +void loop() |
| 101 | +{ |
| 102 | + uint32_t tmp; |
| 103 | + |
| 104 | + Serial.print("\n\rRead: "); |
| 105 | + readline(); |
| 106 | + |
| 107 | + // check if $GPRMC (global positioning fixed data) |
| 108 | + if (strncmp(buffer, "$GPRMC",6) == 0) { |
| 109 | + |
| 110 | + // hhmmss time data |
| 111 | + parseptr = buffer+7; |
| 112 | + tmp = parsedecimal(parseptr); |
| 113 | + hour = tmp / 10000; |
| 114 | + minute = (tmp / 100) % 100; |
| 115 | + second = tmp % 100; |
| 116 | + |
| 117 | + parseptr = strchr(parseptr, ',') + 1; |
| 118 | + status = parseptr[0]; |
| 119 | + parseptr += 2; |
| 120 | + |
| 121 | + // grab latitude & long data |
| 122 | + // latitude |
| 123 | + latitude = parsedecimal(parseptr); |
| 124 | + if (latitude != 0) { |
| 125 | + latitude *= 10000; |
| 126 | + parseptr = strchr(parseptr, '.')+1; |
| 127 | + latitude += parsedecimal(parseptr); |
| 128 | + } |
| 129 | + parseptr = strchr(parseptr, ',') + 1; |
| 130 | + // read latitude N/S data |
| 131 | + if (parseptr[0] != ',') { |
| 132 | + latdir = parseptr[0]; |
| 133 | + } |
| 134 | + |
| 135 | + //Serial.println(latdir); |
| 136 | + |
| 137 | + // longitude |
| 138 | + parseptr = strchr(parseptr, ',')+1; |
| 139 | + longitude = parsedecimal(parseptr); |
| 140 | + if (longitude != 0) { |
| 141 | + longitude *= 10000; |
| 142 | + parseptr = strchr(parseptr, '.')+1; |
| 143 | + longitude += parsedecimal(parseptr); |
| 144 | + } |
| 145 | + parseptr = strchr(parseptr, ',')+1; |
| 146 | + // read longitude E/W data |
| 147 | + if (parseptr[0] != ',') { |
| 148 | + longdir = parseptr[0]; |
| 149 | + } |
| 150 | + |
| 151 | + |
| 152 | + // groundspeed |
| 153 | + parseptr = strchr(parseptr, ',')+1; |
| 154 | + groundspeed = parsedecimal(parseptr); |
| 155 | + |
| 156 | + // track angle |
| 157 | + parseptr = strchr(parseptr, ',')+1; |
| 158 | + trackangle = parsedecimal(parseptr); |
| 159 | + |
| 160 | + |
| 161 | + // date |
| 162 | + parseptr = strchr(parseptr, ',')+1; |
| 163 | + tmp = parsedecimal(parseptr); |
| 164 | + date = tmp / 10000; |
| 165 | + month = (tmp / 100) % 100; |
| 166 | + year = tmp % 100; |
| 167 | + |
| 168 | + Serial.print("\n\tTime: "); |
| 169 | + Serial.print(hour, DEC); Serial.print(':'); |
| 170 | + Serial.print(minute, DEC); Serial.print(':'); |
| 171 | + Serial.println(second, DEC); |
| 172 | + Serial.print("\tDate: "); |
| 173 | + Serial.print(month, DEC); Serial.print('/'); |
| 174 | + Serial.print(date, DEC); Serial.print('/'); |
| 175 | + Serial.println(year, DEC); |
| 176 | + |
| 177 | + Serial.print("\tLat: "); |
| 178 | + if (latdir == 'N') |
| 179 | + Serial.print('+'); |
| 180 | + else if (latdir == 'S') |
| 181 | + Serial.print('-'); |
| 182 | + |
| 183 | + Serial.print(latitude/1000000, DEC); Serial.print("* "); |
| 184 | + Serial.print((latitude/10000)%100, DEC); Serial.print('\''); Serial.print(' '); |
| 185 | + Serial.print((latitude%10000)*6/1000, DEC); Serial.print('.'); |
| 186 | + Serial.print(((latitude%10000)*6/10)%100, DEC); Serial.println('"'); |
| 187 | + |
| 188 | + Serial.print("\tLong: "); |
| 189 | + if (longdir == 'E') |
| 190 | + Serial.print('+'); |
| 191 | + else if (longdir == 'W') |
| 192 | + Serial.print('-'); |
| 193 | + Serial.print(longitude/1000000, DEC); Serial.print("* "); |
| 194 | + Serial.print((longitude/10000)%100, DEC); Serial.print('\''); Serial.print(' '); |
| 195 | + Serial.print((longitude%10000)*6/1000, DEC); Serial.print('.'); |
| 196 | + Serial.print(((longitude%10000)*6/10)%100, DEC); Serial.println('"'); |
| 197 | + |
| 198 | + } |
| 199 | + //Serial.println(buffer); |
| 200 | +} |
0 commit comments