SatElevTracker.ino 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #include <TinyGPS++.h>
  2. #include <SoftwareSerial.h>
  3. /*
  4. This sample code tracks satellite elevations using TinyGPSCustom objects.
  5. Satellite numbers and elevations are not normally tracked by TinyGPS++, but
  6. by using TinyGPSCustom we get around this.
  7. It requires the use of SoftwareSerial and assumes that you have a
  8. 4800-baud serial GPS device hooked up on pins 4(RX) and 3(TX).
  9. */
  10. static const int RXPin = 4, TXPin = 3;
  11. static const uint32_t GPSBaud = 4800;
  12. static const int MAX_SATELLITES = 40;
  13. static const int PAGE_LENGTH = 40;
  14. // The TinyGPS++ object
  15. TinyGPSPlus gps;
  16. // The serial connection to the GPS device
  17. SoftwareSerial ss(RXPin, TXPin);
  18. TinyGPSCustom totalGPGSVMessages(gps, "GPGSV", 1); // $GPGSV sentence, first element
  19. TinyGPSCustom messageNumber(gps, "GPGSV", 2); // $GPGSV sentence, second element
  20. TinyGPSCustom satNumber[4]; // to be initialized later
  21. TinyGPSCustom elevation[4];
  22. bool anyChanges = false;
  23. unsigned long linecount = 0;
  24. struct
  25. {
  26. int elevation;
  27. bool active;
  28. } sats[MAX_SATELLITES];
  29. void setup()
  30. {
  31. Serial.begin(115200);
  32. ss.begin(GPSBaud);
  33. Serial.println(F("SatElevTracker.ino"));
  34. Serial.println(F("Displays GPS satellite elevations as they change"));
  35. Serial.print(F("Testing TinyGPS++ library v. ")); Serial.println(TinyGPSPlus::libraryVersion());
  36. Serial.println(F("by Mikal Hart"));
  37. Serial.println();
  38. // Initialize all the uninitialized TinyGPSCustom objects
  39. for (int i=0; i<4; ++i)
  40. {
  41. satNumber[i].begin(gps, "GPGSV", 4 + 4 * i); // offsets 4, 8, 12, 16
  42. elevation[i].begin(gps, "GPGSV", 5 + 4 * i); // offsets 5, 9, 13, 17
  43. }
  44. }
  45. void loop()
  46. {
  47. // Dispatch incoming characters
  48. if (ss.available() > 0)
  49. {
  50. gps.encode(ss.read());
  51. if (totalGPGSVMessages.isUpdated())
  52. {
  53. for (int i=0; i<4; ++i)
  54. {
  55. int no = atoi(satNumber[i].value());
  56. if (no >= 1 && no <= MAX_SATELLITES)
  57. {
  58. int elev = atoi(elevation[i].value());
  59. sats[no-1].active = true;
  60. if (sats[no-1].elevation != elev)
  61. {
  62. sats[no-1].elevation = elev;
  63. anyChanges = true;
  64. }
  65. }
  66. }
  67. int totalMessages = atoi(totalGPGSVMessages.value());
  68. int currentMessage = atoi(messageNumber.value());
  69. if (totalMessages == currentMessage && anyChanges)
  70. {
  71. if (linecount++ % PAGE_LENGTH == 0)
  72. printHeader();
  73. TimePrint();
  74. for (int i=0; i<MAX_SATELLITES; ++i)
  75. {
  76. Serial.print(F(" "));
  77. if (sats[i].active)
  78. IntPrint(sats[i].elevation, 2);
  79. else
  80. Serial.print(F(" "));
  81. sats[i].active = false;
  82. }
  83. Serial.println();
  84. anyChanges = false;
  85. }
  86. }
  87. }
  88. }
  89. void IntPrint(int n, int len)
  90. {
  91. int digs = n < 0 ? 2 : 1;
  92. for (int i=10; i<=abs(n); i*=10)
  93. ++digs;
  94. while (digs++ < len)
  95. Serial.print(F(" "));
  96. Serial.print(n);
  97. Serial.print(F(" "));
  98. }
  99. void TimePrint()
  100. {
  101. if (gps.time.isValid())
  102. {
  103. if (gps.time.hour() < 10)
  104. Serial.print(F("0"));
  105. Serial.print(gps.time.hour());
  106. Serial.print(F(":"));
  107. if (gps.time.minute() < 10)
  108. Serial.print(F("0"));
  109. Serial.print(gps.time.minute());
  110. Serial.print(F(":"));
  111. if (gps.time.second() < 10)
  112. Serial.print(F("0"));
  113. Serial.print(gps.time.second());
  114. Serial.print(F(" "));
  115. }
  116. else
  117. {
  118. Serial.print(F("(unknown)"));
  119. }
  120. }
  121. void printHeader()
  122. {
  123. Serial.println();
  124. Serial.print(F("Time "));
  125. for (int i=0; i<MAX_SATELLITES; ++i)
  126. {
  127. Serial.print(F(" "));
  128. IntPrint(i+1, 2);
  129. }
  130. Serial.println();
  131. Serial.print(F("---------"));
  132. for (int i=0; i<MAX_SATELLITES; ++i)
  133. Serial.print(F("----"));
  134. Serial.println();
  135. }