FullExample.ino 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #include <TinyGPS++.h>
  2. #include <SoftwareSerial.h>
  3. /*
  4. This sample code 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("FullExample.ino"));
  19. Serial.println(F("An extensive example of many interesting TinyGPS++ features"));
  20. Serial.print(F("Testing TinyGPS++ library v. ")); Serial.println(TinyGPSPlus::libraryVersion());
  21. Serial.println(F("by Mikal Hart"));
  22. Serial.println();
  23. Serial.println(F("Sats HDOP Latitude Longitude Fix Date Time Date Alt Course Speed Card Distance Course Card Chars Sentences Checksum"));
  24. Serial.println(F(" (deg) (deg) Age Age (m) --- from GPS ---- ---- to London ---- RX RX Fail"));
  25. Serial.println(F("----------------------------------------------------------------------------------------------------------------------------------------"));
  26. }
  27. void loop()
  28. {
  29. static const double LONDON_LAT = 51.508131, LONDON_LON = -0.128002;
  30. printInt(gps.satellites.value(), gps.satellites.isValid(), 5);
  31. printFloat(gps.hdop.hdop(), gps.hdop.isValid(), 6, 1);
  32. printFloat(gps.location.lat(), gps.location.isValid(), 11, 6);
  33. printFloat(gps.location.lng(), gps.location.isValid(), 12, 6);
  34. printInt(gps.location.age(), gps.location.isValid(), 5);
  35. printDateTime(gps.date, gps.time);
  36. printFloat(gps.altitude.meters(), gps.altitude.isValid(), 7, 2);
  37. printFloat(gps.course.deg(), gps.course.isValid(), 7, 2);
  38. printFloat(gps.speed.kmph(), gps.speed.isValid(), 6, 2);
  39. printStr(gps.course.isValid() ? TinyGPSPlus::cardinal(gps.course.deg()) : "*** ", 6);
  40. unsigned long distanceKmToLondon =
  41. (unsigned long)TinyGPSPlus::distanceBetween(
  42. gps.location.lat(),
  43. gps.location.lng(),
  44. LONDON_LAT,
  45. LONDON_LON) / 1000;
  46. printInt(distanceKmToLondon, gps.location.isValid(), 9);
  47. double courseToLondon =
  48. TinyGPSPlus::courseTo(
  49. gps.location.lat(),
  50. gps.location.lng(),
  51. LONDON_LAT,
  52. LONDON_LON);
  53. printFloat(courseToLondon, gps.location.isValid(), 7, 2);
  54. const char *cardinalToLondon = TinyGPSPlus::cardinal(courseToLondon);
  55. printStr(gps.location.isValid() ? cardinalToLondon : "*** ", 6);
  56. printInt(gps.charsProcessed(), true, 6);
  57. printInt(gps.sentencesWithFix(), true, 10);
  58. printInt(gps.failedChecksum(), true, 9);
  59. Serial.println();
  60. smartDelay(1000);
  61. if (millis() > 5000 && gps.charsProcessed() < 10)
  62. Serial.println(F("No GPS data received: check wiring"));
  63. }
  64. // This custom version of delay() ensures that the gps object
  65. // is being "fed".
  66. static void smartDelay(unsigned long ms)
  67. {
  68. unsigned long start = millis();
  69. do
  70. {
  71. while (ss.available())
  72. gps.encode(ss.read());
  73. } while (millis() - start < ms);
  74. }
  75. static void printFloat(float val, bool valid, int len, int prec)
  76. {
  77. if (!valid)
  78. {
  79. while (len-- > 1)
  80. Serial.print('*');
  81. Serial.print(' ');
  82. }
  83. else
  84. {
  85. Serial.print(val, prec);
  86. int vi = abs((int)val);
  87. int flen = prec + (val < 0.0 ? 2 : 1); // . and -
  88. flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
  89. for (int i=flen; i<len; ++i)
  90. Serial.print(' ');
  91. }
  92. smartDelay(0);
  93. }
  94. static void printInt(unsigned long val, bool valid, int len)
  95. {
  96. char sz[32] = "*****************";
  97. if (valid)
  98. sprintf(sz, "%ld", val);
  99. sz[len] = 0;
  100. for (int i=strlen(sz); i<len; ++i)
  101. sz[i] = ' ';
  102. if (len > 0)
  103. sz[len-1] = ' ';
  104. Serial.print(sz);
  105. smartDelay(0);
  106. }
  107. static void printDateTime(TinyGPSDate &d, TinyGPSTime &t)
  108. {
  109. if (!d.isValid())
  110. {
  111. Serial.print(F("********** "));
  112. }
  113. else
  114. {
  115. char sz[32];
  116. sprintf(sz, "%02d/%02d/%02d ", d.month(), d.day(), d.year());
  117. Serial.print(sz);
  118. }
  119. if (!t.isValid())
  120. {
  121. Serial.print(F("******** "));
  122. }
  123. else
  124. {
  125. char sz[32];
  126. sprintf(sz, "%02d:%02d:%02d ", t.hour(), t.minute(), t.second());
  127. Serial.print(sz);
  128. }
  129. printInt(d.age(), d.isValid(), 5);
  130. smartDelay(0);
  131. }
  132. static void printStr(const char *str, int len)
  133. {
  134. int slen = strlen(str);
  135. for (int i=0; i<len; ++i)
  136. Serial.print(i<slen ? str[i] : ' ');
  137. smartDelay(0);
  138. }