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