Skip to content

Instantly share code, notes, and snippets.

@houmei
Created August 18, 2017 15:33
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
read GPS NMEA data to get UTC
//
// GPS clock
// UTCdata get from NMEA $GPGGA field $2
//
// for Arduino Leonardo / Seeeduino Lite(3.3V)
void setup() {
Serial1.begin(9600); // GPS port
while(!Serial1) delay(10);
Serial.begin(9600); // Console
while(!Serial) delay(10);
}
int hh,mm,ss ;
const int TD = 9;
void loop() {
if (Serial1.available()) {
String nmea = Serial1.readStringUntil('\n');
String nmeames = nmea.substring(0,6); // $GP***
if ( nmeames.equals("$GPGGA") ) { // get UTC
String shh = nmea.substring(7,9);
String smm = nmea.substring(9,11);
String sss = nmea.substring(11,13);
// Serial.println(shh + smm + sss);
hh = (shh.toInt() + TD)%24; // Timezone offset+
mm = smm.toInt();
ss = sss.toInt();
Serial.print(hh);
Serial.print(":");
Serial.print(mm);
Serial.print(":");
Serial.println(ss);
}
}
// if (Serial1.available()) Serial.write(Serial1.read());
// if (Serial.available()) Serial1.write(Serial.read());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment