DeviceExample.ino 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include <TinyGPS++.h>
  2. #include <SoftwareSerial.h>
  3. /*
  4. This sample sketch demonstrates the normal use of a TinyGPS++ (TinyGPSPlus) object.
  5. It requires the use of SoftwareSerial, and assumes that you have a
  6. 4800-baud serial GPS device hooked up on pins 4(rx) and 3(tx).
  7. */
  8. static const int RXPin = 4, TXPin = 3;
  9. static const uint32_t GPSBaud = 4800;
  10. // The TinyGPS++ object
  11. TinyGPSPlus gps;
  12. // The serial connection to the GPS device
  13. SoftwareSerial ss(RXPin, TXPin);
  14. void setup()
  15. {
  16. Serial.begin(115200);
  17. ss.begin(GPSBaud);
  18. Serial.println(F("DeviceExample.ino"));
  19. Serial.println(F("A simple demonstration of TinyGPS++ with an attached GPS module"));
  20. Serial.print(F("Testing TinyGPS++ library v. ")); Serial.println(TinyGPSPlus::libraryVersion());
  21. Serial.println(F("by Mikal Hart"));
  22. Serial.println();
  23. }
  24. void loop()
  25. {
  26. // This sketch displays information every time a new sentence is correctly encoded.
  27. while (ss.available() > 0)
  28. if (gps.encode(ss.read()))
  29. displayInfo();
  30. if (millis() > 5000 && gps.charsProcessed() < 10)
  31. {
  32. Serial.println(F("No GPS detected: check wiring."));
  33. while(true);
  34. }
  35. }
  36. void displayInfo()
  37. {
  38. Serial.print(F("Location: "));
  39. if (gps.location.isValid())
  40. {
  41. Serial.print(gps.location.lat(), 6);
  42. Serial.print(F(","));
  43. Serial.print(gps.location.lng(), 6);
  44. }
  45. else
  46. {
  47. Serial.print(F("INVALID"));
  48. }
  49. Serial.print(F(" Date/Time: "));
  50. if (gps.date.isValid())
  51. {
  52. Serial.print(gps.date.month());
  53. Serial.print(F("/"));
  54. Serial.print(gps.date.day());
  55. Serial.print(F("/"));
  56. Serial.print(gps.date.year());
  57. }
  58. else
  59. {
  60. Serial.print(F("INVALID"));
  61. }
  62. Serial.print(F(" "));
  63. if (gps.time.isValid())
  64. {
  65. if (gps.time.hour() < 10) Serial.print(F("0"));
  66. Serial.print(gps.time.hour());
  67. Serial.print(F(":"));
  68. if (gps.time.minute() < 10) Serial.print(F("0"));
  69. Serial.print(gps.time.minute());
  70. Serial.print(F(":"));
  71. if (gps.time.second() < 10) Serial.print(F("0"));
  72. Serial.print(gps.time.second());
  73. Serial.print(F("."));
  74. if (gps.time.centisecond() < 10) Serial.print(F("0"));
  75. Serial.print(gps.time.centisecond());
  76. }
  77. else
  78. {
  79. Serial.print(F("INVALID"));
  80. }
  81. Serial.println();
  82. }