85 lines
2 KiB
Arduino
85 lines
2 KiB
Arduino
|
#include <TinyGPS++.h>
|
||
|
#include <SoftwareSerial.h>
|
||
|
|
||
|
// NOTE: RX and TX must be swapped with respect to the pin definitions on the NE-6M itself.
|
||
|
#define PIN_GPS_RX 4
|
||
|
#define PIN_GPS_TX 3
|
||
|
|
||
|
#define BAUD_PC 115200
|
||
|
#define BAUD_GPS 9600
|
||
|
|
||
|
TinyGPSPlus gps;
|
||
|
|
||
|
SoftwareSerial serial_gps(PIN_GPS_RX, PIN_GPS_TX);
|
||
|
|
||
|
void setup();
|
||
|
void loop();
|
||
|
void gps_display();
|
||
|
|
||
|
void setup() {
|
||
|
Serial.begin(BAUD_PC);
|
||
|
serial_gps.begin(BAUD_GPS);
|
||
|
|
||
|
Serial.print("TinyGPS++ v");
|
||
|
Serial.print(TinyGPSPlus::libraryVersion());
|
||
|
Serial.println(" Test");
|
||
|
}
|
||
|
|
||
|
void loop() {
|
||
|
while(serial_gps.available() > 0) {
|
||
|
if(!gps.encode(serial_gps.read()))
|
||
|
continue;
|
||
|
|
||
|
gps_display();
|
||
|
}
|
||
|
|
||
|
if (millis() > 5000 && gps.charsProcessed() < 10) {
|
||
|
Serial.println(F("No GPS detected: check wiring."));
|
||
|
while(true);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void gps_display() {
|
||
|
if(!gps.location.isUpdated()) return;
|
||
|
// From the device example
|
||
|
Serial.print(F("Location: "));
|
||
|
if (gps.location.isValid()) {
|
||
|
Serial.print(gps.location.lat(), 6);
|
||
|
Serial.print(F(","));
|
||
|
Serial.print(gps.location.lng(), 6);
|
||
|
Serial.print(F(" "));
|
||
|
}
|
||
|
else
|
||
|
Serial.print(F("(invalid) "));
|
||
|
|
||
|
Serial.print(F("DateTime: "));
|
||
|
if (gps.date.isValid()) {
|
||
|
Serial.print(gps.date.month());
|
||
|
Serial.print(F("/"));
|
||
|
Serial.print(gps.date.day());
|
||
|
Serial.print(F("/"));
|
||
|
Serial.print(gps.date.year());
|
||
|
}
|
||
|
else
|
||
|
Serial.print(F("(invalid) "));
|
||
|
|
||
|
Serial.print(F(" "));
|
||
|
if (gps.time.isValid()) {
|
||
|
if (gps.time.hour() < 10) Serial.print(F("0"));
|
||
|
Serial.print(gps.time.hour());
|
||
|
Serial.print(F(":"));
|
||
|
if (gps.time.minute() < 10) Serial.print(F("0"));
|
||
|
Serial.print(gps.time.minute());
|
||
|
Serial.print(F(":"));
|
||
|
if (gps.time.second() < 10) Serial.print(F("0"));
|
||
|
Serial.print(gps.time.second());
|
||
|
Serial.print(F("."));
|
||
|
if (gps.time.centisecond() < 10) Serial.print(F("0"));
|
||
|
Serial.print(gps.time.centisecond());
|
||
|
}
|
||
|
else
|
||
|
Serial.print(F("(invalid)"));
|
||
|
|
||
|
Serial.println();
|
||
|
}
|