BasicExample.ino 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include <TinyGPS++.h>
  2. /*
  3. This sample sketch should be the first you try out when you are testing a TinyGPS++
  4. (TinyGPSPlus) installation. In normal use, you feed TinyGPS++ objects characters from
  5. a serial NMEA GPS device, but this example uses static strings for simplicity.
  6. */
  7. // A sample NMEA stream.
  8. const char *gpsStream =
  9. "$GPRMC,045103.000,A,3014.1984,N,09749.2872,W,0.67,161.46,030913,,,A*7C\r\n"
  10. "$GPGGA,045104.000,3014.1985,N,09749.2873,W,1,09,1.2,211.6,M,-22.5,M,,0000*62\r\n"
  11. "$GPRMC,045200.000,A,3014.3820,N,09748.9514,W,36.88,65.02,030913,,,A*77\r\n"
  12. "$GPGGA,045201.000,3014.3864,N,09748.9411,W,1,10,1.2,200.8,M,-22.5,M,,0000*6C\r\n"
  13. "$GPRMC,045251.000,A,3014.4275,N,09749.0626,W,0.51,217.94,030913,,,A*7D\r\n"
  14. "$GPGGA,045252.000,3014.4273,N,09749.0628,W,1,09,1.3,206.9,M,-22.5,M,,0000*6F\r\n";
  15. // The TinyGPS++ object
  16. TinyGPSPlus gps;
  17. void setup()
  18. {
  19. Serial.begin(115200);
  20. Serial.println(F("BasicExample.ino"));
  21. Serial.println(F("Basic demonstration of TinyGPS++ (no device needed)"));
  22. Serial.print(F("Testing TinyGPS++ library v. ")); Serial.println(TinyGPSPlus::libraryVersion());
  23. Serial.println(F("by Mikal Hart"));
  24. Serial.println();
  25. while (*gpsStream)
  26. if (gps.encode(*gpsStream++))
  27. displayInfo();
  28. Serial.println();
  29. Serial.println(F("Done."));
  30. }
  31. void loop()
  32. {
  33. }
  34. void displayInfo()
  35. {
  36. Serial.print(F("Location: "));
  37. if (gps.location.isValid())
  38. {
  39. Serial.print(gps.location.lat(), 6);
  40. Serial.print(F(","));
  41. Serial.print(gps.location.lng(), 6);
  42. }
  43. else
  44. {
  45. Serial.print(F("INVALID"));
  46. }
  47. Serial.print(F(" Date/Time: "));
  48. if (gps.date.isValid())
  49. {
  50. Serial.print(gps.date.month());
  51. Serial.print(F("/"));
  52. Serial.print(gps.date.day());
  53. Serial.print(F("/"));
  54. Serial.print(gps.date.year());
  55. }
  56. else
  57. {
  58. Serial.print(F("INVALID"));
  59. }
  60. Serial.print(F(" "));
  61. if (gps.time.isValid())
  62. {
  63. if (gps.time.hour() < 10) Serial.print(F("0"));
  64. Serial.print(gps.time.hour());
  65. Serial.print(F(":"));
  66. if (gps.time.minute() < 10) Serial.print(F("0"));
  67. Serial.print(gps.time.minute());
  68. Serial.print(F(":"));
  69. if (gps.time.second() < 10) Serial.print(F("0"));
  70. Serial.print(gps.time.second());
  71. Serial.print(F("."));
  72. if (gps.time.centisecond() < 10) Serial.print(F("0"));
  73. Serial.print(gps.time.centisecond());
  74. }
  75. else
  76. {
  77. Serial.print(F("INVALID"));
  78. }
  79. Serial.println();
  80. }