COM_GPS.ino 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. Description: Use COM.GPS Module to get the coordinate data and time of the current location.
  3. Please install library before compiling:
  4. TinyGPSPlus: file in M5stack lib examples -> modules -> GPS -> TinyGPSPlus-1.0.2.zip (unzip the lib zip file to the Arduino Lib path)
  5. */
  6. #include <M5Stack.h>
  7. #include <TinyGPS++.h>
  8. // A sample NMEA stream.
  9. const char *gpsStream =
  10. "$GPRMC,045103.000,A,3014.1984,N,09749.2872,W,0.67,161.46,030913,,,A*7C\r\n";
  11. // The TinyGPS++ object
  12. TinyGPSPlus gps;
  13. void setup()
  14. {
  15. M5.begin();
  16. Serial2.begin(9600, SERIAL_8N1, 5, 13);
  17. M5.Lcd.setTextColor(GREEN, BLACK);
  18. // while (*gpsStream)
  19. // if (gps.encode(*gpsStream++))
  20. // displayInfo();
  21. }
  22. void loop()
  23. {
  24. displayInfo();
  25. smartDelay(1000);
  26. }
  27. static void smartDelay(unsigned long ms)
  28. {
  29. unsigned long start = millis();
  30. do
  31. {
  32. while (Serial2.available() > 0)
  33. gps.encode(Serial2.read());
  34. } while (millis() - start < ms);
  35. M5.Lcd.clear();
  36. }
  37. void displayInfo()
  38. {
  39. M5.Lcd.setCursor(0, 40, 4);
  40. M5.Lcd.print(F("Latitude: "));
  41. if (gps.location.isValid())
  42. {
  43. M5.Lcd.print(gps.location.lat(), 6);
  44. }
  45. else
  46. {
  47. M5.Lcd.print(F("INVALID"));
  48. }
  49. M5.Lcd.println();
  50. M5.Lcd.print(F("Longitude: "));
  51. if (gps.location.isValid())
  52. {
  53. M5.Lcd.print(gps.location.lng(), 6);
  54. }
  55. else
  56. {
  57. M5.Lcd.print(F("INVALID"));
  58. }
  59. M5.Lcd.println();
  60. M5.Lcd.print(F("Altitude: "));
  61. if (gps.altitude.isValid())
  62. {
  63. M5.Lcd.print(gps.altitude.meters());
  64. }
  65. else
  66. {
  67. M5.Lcd.print(F("INVALID"));
  68. }
  69. M5.Lcd.println();
  70. M5.Lcd.print(F("Satellites: "));
  71. if (gps.satellites.isValid())
  72. {
  73. M5.Lcd.print(gps.satellites.value());
  74. }
  75. else
  76. {
  77. M5.Lcd.print(F("INVALID"));
  78. }
  79. M5.Lcd.println();
  80. M5.Lcd.print(F("Date: "));
  81. if (gps.date.isValid())
  82. {
  83. M5.Lcd.print(gps.date.month());
  84. M5.Lcd.print(F("/"));
  85. M5.Lcd.print(gps.date.day());
  86. M5.Lcd.print(F("/"));
  87. M5.Lcd.print(gps.date.year());
  88. }
  89. else
  90. {
  91. M5.Lcd.print(F("INVALID"));
  92. }
  93. M5.Lcd.println();
  94. M5.Lcd.print(F("Time: "));
  95. if (gps.time.isValid())
  96. {
  97. if (gps.time.hour() < 10) M5.Lcd.print(F("0"));
  98. M5.Lcd.print(gps.time.hour());
  99. M5.Lcd.print(F(":"));
  100. if (gps.time.minute() < 10) M5.Lcd.print(F("0"));
  101. M5.Lcd.print(gps.time.minute());
  102. M5.Lcd.print(F(":"));
  103. if (gps.time.second() < 10) M5.Lcd.print(F("0"));
  104. M5.Lcd.print(gps.time.second());
  105. M5.Lcd.print(F("."));
  106. if (gps.time.centisecond() < 10) M5.Lcd.print(F("0"));
  107. M5.Lcd.print(gps.time.centisecond());
  108. }
  109. else
  110. {
  111. M5.Lcd.print(F("INVALID"));
  112. }
  113. }