Skip to content

Instantly share code, notes, and snippets.

@Hypnopompia
Last active Mar 23, 2016
Embed
What would you like to do?
Electron Asset Tracker Read Test
// echo all the raw GPS data to the USB serial port?
#define GPSECHO false
#define gpsSerial Serial1
#define PMTK_SET_NMEA_OUTPUT_OFF "$PMTK314,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*28" // turn off output
#define PMTK_SET_NMEA_OUTPUT_RMCGGA "$PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*28" // turn on GPRMC and GGA
#define PMTK_SET_NMEA_UPDATE_1HZ "$PMTK220,1000*1F" // Set the update rate 1 Hz update rate
#define PGCMD_NOANTENNA "$PGCMD,33,0*6C" // Mute antenna status events
#define MAXLINELENGTH 200
char incoming[MAXLINELENGTH];
uint8_t incomingidx=0;
bool readingString = false; // Keep track if we're supposed to be reading an NMEA string to detect errors.
void setup() {
pinMode(D6, OUTPUT);
digitalWrite(D6, LOW);
gpsSerial.begin(9600);
Serial.begin(115200);
delay(10);
gpsSerial.println(PMTK_SET_NMEA_OUTPUT_OFF); // turn off output
delay(100);
gpsSerial.println(PMTK_SET_NMEA_OUTPUT_RMCGGA); // turn on GPRMC and GGA
delay(100);
gpsSerial.println(PMTK_SET_NMEA_UPDATE_1HZ); // Set the update rate 1 Hz update rate
delay(100);
gpsSerial.println(PGCMD_NOANTENNA); // Mute antenna status events
delay(100);
}
void loop() {
if(!gpsSerial.available()) return;
char c = gpsSerial.read();
if (c == '\r') { // Ignore \r
c = '\n';
}
if (GPSECHO && c) {
Serial.print(c);
return;
}
if (c == '$') { // New NMEA sentence
if (incomingidx != 0) { // We got a start character in the middle of a sentence? WTF?
Serial.println("Unexpected start of NMEA!");
incomingidx = 0; // Start over
readingString = false; // Dont worry about assembling data into incoming until we get another NMEA start character ($)
} else {
readingString = true; // Now that we have a NMEA start char, keep reading until there's an error or we have a full string.
incoming[incomingidx++] = c;
incoming[incomingidx] = 0;
}
} else if (c == '\n') { // We have a newline, this should be a full NMEA sentence (checksum not validated though)
Serial.println((char *)incoming); // Since we're outputting the NMEA string, we can expect the next message to get lost.
readingString = false; // Dont worry about assembling data into incoming until we get another NMEA start character ($)
incomingidx = 0; // Start over
} else if (true == readingString) { // Unless we're supposed to be reading, throw away incoming characters.
if (incomingidx == 0) { // I was expecting a start character ($)
Serial.println("Missing NMEA start!");
readingString = false; // Dont worry about assembling data into incoming until we get another NMEA start character ($)
} else { // Only append to the incoming buffer if we've received a start NMEA character
incoming[incomingidx++] = c;
incoming[incomingidx] = 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment