SatelliteTracker.ino 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #include <TinyGPS++.h>
  2. #include <SoftwareSerial.h>
  3. /*
  4. This sample code demonstrates how to use an array of TinyGPSCustom objects
  5. to monitor all the visible satellites.
  6. Satellite numbers, elevation, azimuth, and signal-to-noise ratio are not
  7. normally tracked by TinyGPS++, but by using TinyGPSCustom we get around this.
  8. The simple code also demonstrates how to use arrays of TinyGPSCustom objects,
  9. each monitoring a different field of the $GPGSV sentence.
  10. It requires the use of SoftwareSerial, and assumes that you have a
  11. 4800-baud serial GPS device hooked up on pins 4(RX) and 3(TX).
  12. */
  13. static const int RXPin = 4, TXPin = 3;
  14. static const uint32_t GPSBaud = 4800;
  15. // The TinyGPS++ object
  16. TinyGPSPlus gps;
  17. // The serial connection to the GPS device
  18. SoftwareSerial ss(RXPin, TXPin);
  19. /*
  20. From http://aprs.gids.nl/nmea/:
  21. $GPGSV
  22. GPS Satellites in view
  23. eg. $GPGSV,3,1,11,03,03,111,00,04,15,270,00,06,01,010,00,13,06,292,00*74
  24. $GPGSV,3,2,11,14,25,170,00,16,57,208,39,18,67,296,40,19,40,246,00*74
  25. $GPGSV,3,3,11,22,42,067,42,24,14,311,43,27,05,244,00,,,,*4D
  26. 1 = Total number of messages of this type in this cycle
  27. 2 = Message number
  28. 3 = Total number of SVs in view
  29. 4 = SV PRN number
  30. 5 = Elevation in degrees, 90 maximum
  31. 6 = Azimuth, degrees from true north, 000 to 359
  32. 7 = SNR, 00-99 dB (null when not tracking)
  33. 8-11 = Information about second SV, same as field 4-7
  34. 12-15= Information about third SV, same as field 4-7
  35. 16-19= Information about fourth SV, same as field 4-7
  36. */
  37. static const int MAX_SATELLITES = 40;
  38. TinyGPSCustom totalGPGSVMessages(gps, "GPGSV", 1); // $GPGSV sentence, first element
  39. TinyGPSCustom messageNumber(gps, "GPGSV", 2); // $GPGSV sentence, second element
  40. TinyGPSCustom satsInView(gps, "GPGSV", 3); // $GPGSV sentence, third element
  41. TinyGPSCustom satNumber[4]; // to be initialized later
  42. TinyGPSCustom elevation[4];
  43. TinyGPSCustom azimuth[4];
  44. TinyGPSCustom snr[4];
  45. struct
  46. {
  47. bool active;
  48. int elevation;
  49. int azimuth;
  50. int snr;
  51. } sats[MAX_SATELLITES];
  52. void setup()
  53. {
  54. Serial.begin(115200);
  55. ss.begin(GPSBaud);
  56. Serial.println(F("SatelliteTracker.ino"));
  57. Serial.println(F("Monitoring satellite location and signal strength using TinyGPSCustom"));
  58. Serial.print(F("Testing TinyGPS++ library v. ")); Serial.println(TinyGPSPlus::libraryVersion());
  59. Serial.println(F("by Mikal Hart"));
  60. Serial.println();
  61. // Initialize all the uninitialized TinyGPSCustom objects
  62. for (int i=0; i<4; ++i)
  63. {
  64. satNumber[i].begin(gps, "GPGSV", 4 + 4 * i); // offsets 4, 8, 12, 16
  65. elevation[i].begin(gps, "GPGSV", 5 + 4 * i); // offsets 5, 9, 13, 17
  66. azimuth[i].begin( gps, "GPGSV", 6 + 4 * i); // offsets 6, 10, 14, 18
  67. snr[i].begin( gps, "GPGSV", 7 + 4 * i); // offsets 7, 11, 15, 19
  68. }
  69. }
  70. void loop()
  71. {
  72. // Dispatch incoming characters
  73. if (ss.available() > 0)
  74. {
  75. gps.encode(ss.read());
  76. if (totalGPGSVMessages.isUpdated())
  77. {
  78. for (int i=0; i<4; ++i)
  79. {
  80. int no = atoi(satNumber[i].value());
  81. // Serial.print(F("SatNumber is ")); Serial.println(no);
  82. if (no >= 1 && no <= MAX_SATELLITES)
  83. {
  84. sats[no-1].elevation = atoi(elevation[i].value());
  85. sats[no-1].azimuth = atoi(azimuth[i].value());
  86. sats[no-1].snr = atoi(snr[i].value());
  87. sats[no-1].active = true;
  88. }
  89. }
  90. int totalMessages = atoi(totalGPGSVMessages.value());
  91. int currentMessage = atoi(messageNumber.value());
  92. if (totalMessages == currentMessage)
  93. {
  94. Serial.print(F("Sats=")); Serial.print(gps.satellites.value());
  95. Serial.print(F(" Nums="));
  96. for (int i=0; i<MAX_SATELLITES; ++i)
  97. if (sats[i].active)
  98. {
  99. Serial.print(i+1);
  100. Serial.print(F(" "));
  101. }
  102. Serial.print(F(" Elevation="));
  103. for (int i=0; i<MAX_SATELLITES; ++i)
  104. if (sats[i].active)
  105. {
  106. Serial.print(sats[i].elevation);
  107. Serial.print(F(" "));
  108. }
  109. Serial.print(F(" Azimuth="));
  110. for (int i=0; i<MAX_SATELLITES; ++i)
  111. if (sats[i].active)
  112. {
  113. Serial.print(sats[i].azimuth);
  114. Serial.print(F(" "));
  115. }
  116. Serial.print(F(" SNR="));
  117. for (int i=0; i<MAX_SATELLITES; ++i)
  118. if (sats[i].active)
  119. {
  120. Serial.print(sats[i].snr);
  121. Serial.print(F(" "));
  122. }
  123. Serial.println();
  124. for (int i=0; i<MAX_SATELLITES; ++i)
  125. sats[i].active = false;
  126. }
  127. }
  128. }
  129. }