Skip to content

Instantly share code, notes, and snippets.

@makenai
Created March 16, 2016 04:24
Show Gist options
  • Save makenai/5a64cedc8326c3887fb9 to your computer and use it in GitHub Desktop.
Save makenai/5a64cedc8326c3887fb9 to your computer and use it in GitHub Desktop.
#include <SoftwareSerial.h>
SoftwareSerial gpsSerial(10, 11);
const int sentenceSize = 80;
char sentence[sentenceSize];
void setup()
{
Serial.begin(9600);
gpsSerial.begin(9600);
}
void loop()
{
static int i = 0;
if (gpsSerial.available())
{
char ch = gpsSerial.read();
if (ch != '\n' && i < sentenceSize)
{
sentence[i] = ch;
i++;
}
else
{
sentence[i] = '\0';
i = 0;
displayGPS();
}
}
}
void displayGPS()
{
char field[20];
getField(field, 0);
if (strcmp(field, "$GPRMC") == 0)
{
Serial.print("Lat: ");
getField(field, 3); // number
Serial.print(field);
getField(field, 4); // N/S
Serial.print(field);
Serial.print(" Long: ");
getField(field, 5); // number
Serial.print(field);
getField(field, 6); // E/W
Serial.println(field);
}
}
void getField(char* buffer, int index)
{
int sentencePos = 0;
int fieldPos = 0;
int commaCount = 0;
while (sentencePos < sentenceSize)
{
if (sentence[sentencePos] == ',')
{
commaCount ++;
sentencePos ++;
}
if (commaCount == index)
{
buffer[fieldPos] = sentence[sentencePos];
fieldPos ++;
}
sentencePos ++;
}
buffer[fieldPos] = '\0';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment