KitchenSink.ino 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #include <TinyGPS++.h>
  2. #include <SoftwareSerial.h>
  3. /*
  4. This sample code demonstrates just about every built-in operation of TinyGPS++ (TinyGPSPlus).
  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. // For stats that happen every 5 seconds
  15. unsigned long last = 0UL;
  16. void setup()
  17. {
  18. Serial.begin(115200);
  19. ss.begin(GPSBaud);
  20. Serial.println(F("KitchenSink.ino"));
  21. Serial.println(F("Demonstrating nearly every feature of TinyGPS++"));
  22. Serial.print(F("Testing TinyGPS++ library v. ")); Serial.println(TinyGPSPlus::libraryVersion());
  23. Serial.println(F("by Mikal Hart"));
  24. Serial.println();
  25. }
  26. void loop()
  27. {
  28. // Dispatch incoming characters
  29. while (ss.available() > 0)
  30. gps.encode(ss.read());
  31. if (gps.location.isUpdated())
  32. {
  33. Serial.print(F("LOCATION Fix Age="));
  34. Serial.print(gps.location.age());
  35. Serial.print(F("ms Raw Lat="));
  36. Serial.print(gps.location.rawLat().negative ? "-" : "+");
  37. Serial.print(gps.location.rawLat().deg);
  38. Serial.print("[+");
  39. Serial.print(gps.location.rawLat().billionths);
  40. Serial.print(F(" billionths], Raw Long="));
  41. Serial.print(gps.location.rawLng().negative ? "-" : "+");
  42. Serial.print(gps.location.rawLng().deg);
  43. Serial.print("[+");
  44. Serial.print(gps.location.rawLng().billionths);
  45. Serial.print(F(" billionths], Lat="));
  46. Serial.print(gps.location.lat(), 6);
  47. Serial.print(F(" Long="));
  48. Serial.println(gps.location.lng(), 6);
  49. }
  50. else if (gps.date.isUpdated())
  51. {
  52. Serial.print(F("DATE Fix Age="));
  53. Serial.print(gps.date.age());
  54. Serial.print(F("ms Raw="));
  55. Serial.print(gps.date.value());
  56. Serial.print(F(" Year="));
  57. Serial.print(gps.date.year());
  58. Serial.print(F(" Month="));
  59. Serial.print(gps.date.month());
  60. Serial.print(F(" Day="));
  61. Serial.println(gps.date.day());
  62. }
  63. else if (gps.time.isUpdated())
  64. {
  65. Serial.print(F("TIME Fix Age="));
  66. Serial.print(gps.time.age());
  67. Serial.print(F("ms Raw="));
  68. Serial.print(gps.time.value());
  69. Serial.print(F(" Hour="));
  70. Serial.print(gps.time.hour());
  71. Serial.print(F(" Minute="));
  72. Serial.print(gps.time.minute());
  73. Serial.print(F(" Second="));
  74. Serial.print(gps.time.second());
  75. Serial.print(F(" Hundredths="));
  76. Serial.println(gps.time.centisecond());
  77. }
  78. else if (gps.speed.isUpdated())
  79. {
  80. Serial.print(F("SPEED Fix Age="));
  81. Serial.print(gps.speed.age());
  82. Serial.print(F("ms Raw="));
  83. Serial.print(gps.speed.value());
  84. Serial.print(F(" Knots="));
  85. Serial.print(gps.speed.knots());
  86. Serial.print(F(" MPH="));
  87. Serial.print(gps.speed.mph());
  88. Serial.print(F(" m/s="));
  89. Serial.print(gps.speed.mps());
  90. Serial.print(F(" km/h="));
  91. Serial.println(gps.speed.kmph());
  92. }
  93. else if (gps.course.isUpdated())
  94. {
  95. Serial.print(F("COURSE Fix Age="));
  96. Serial.print(gps.course.age());
  97. Serial.print(F("ms Raw="));
  98. Serial.print(gps.course.value());
  99. Serial.print(F(" Deg="));
  100. Serial.println(gps.course.deg());
  101. }
  102. else if (gps.altitude.isUpdated())
  103. {
  104. Serial.print(F("ALTITUDE Fix Age="));
  105. Serial.print(gps.altitude.age());
  106. Serial.print(F("ms Raw="));
  107. Serial.print(gps.altitude.value());
  108. Serial.print(F(" Meters="));
  109. Serial.print(gps.altitude.meters());
  110. Serial.print(F(" Miles="));
  111. Serial.print(gps.altitude.miles());
  112. Serial.print(F(" KM="));
  113. Serial.print(gps.altitude.kilometers());
  114. Serial.print(F(" Feet="));
  115. Serial.println(gps.altitude.feet());
  116. }
  117. else if (gps.satellites.isUpdated())
  118. {
  119. Serial.print(F("SATELLITES Fix Age="));
  120. Serial.print(gps.satellites.age());
  121. Serial.print(F("ms Value="));
  122. Serial.println(gps.satellites.value());
  123. }
  124. else if (gps.hdop.isUpdated())
  125. {
  126. Serial.print(F("HDOP Fix Age="));
  127. Serial.print(gps.hdop.age());
  128. Serial.print(F("ms raw="));
  129. Serial.print(gps.hdop.value());
  130. Serial.print(F(" hdop="));
  131. Serial.println(gps.hdop.hdop());
  132. }
  133. else if (millis() - last > 5000)
  134. {
  135. Serial.println();
  136. if (gps.location.isValid())
  137. {
  138. static const double LONDON_LAT = 51.508131, LONDON_LON = -0.128002;
  139. double distanceToLondon =
  140. TinyGPSPlus::distanceBetween(
  141. gps.location.lat(),
  142. gps.location.lng(),
  143. LONDON_LAT,
  144. LONDON_LON);
  145. double courseToLondon =
  146. TinyGPSPlus::courseTo(
  147. gps.location.lat(),
  148. gps.location.lng(),
  149. LONDON_LAT,
  150. LONDON_LON);
  151. Serial.print(F("LONDON Distance="));
  152. Serial.print(distanceToLondon/1000, 6);
  153. Serial.print(F(" km Course-to="));
  154. Serial.print(courseToLondon, 6);
  155. Serial.print(F(" degrees ["));
  156. Serial.print(TinyGPSPlus::cardinal(courseToLondon));
  157. Serial.println(F("]"));
  158. }
  159. Serial.print(F("DIAGS Chars="));
  160. Serial.print(gps.charsProcessed());
  161. Serial.print(F(" Sentences-with-Fix="));
  162. Serial.print(gps.sentencesWithFix());
  163. Serial.print(F(" Failed-checksum="));
  164. Serial.print(gps.failedChecksum());
  165. Serial.print(F(" Passed-checksum="));
  166. Serial.println(gps.passedChecksum());
  167. if (gps.charsProcessed() < 10)
  168. Serial.println(F("WARNING: No GPS data. Check wiring."));
  169. last = millis();
  170. Serial.println();
  171. }
  172. }