I have a lot of boards and usually need to re-use code even for different projects.

m5-gps.ino 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #include <ArduinoJson.h>
  2. #include <TinyGPS++.h>
  3. #include <wiring_private.h>
  4. #include "TFT_eSPI.h"
  5. #include <Arduino.h>
  6. #include <rpcWiFi.h>
  7. #include <HTTPClient.h>
  8. TFT_eSPI tft;
  9. static const uint32_t GPSBaud = 115200;
  10. // The TinyGPS++ object
  11. TinyGPSPlus gps;
  12. // The serial connection to the GPS device - Left side Grove connector.
  13. // Left side Grove connector shares pins with I2C1 of 40 pin connector.
  14. static Uart Serial3(&sercom3, PIN_WIRE_SCL, PIN_WIRE_SDA, SERCOM_RX_PAD_1, UART_TX_PAD_0);
  15. String last_date = "";
  16. String last_time = "";
  17. String last_lat = "";
  18. String last_lon = "";
  19. char* ssid = "SSID";
  20. char* pw = "PASSWORD";
  21. void setup()
  22. {
  23. Serial.begin(115200);
  24. tft.begin();
  25. tft.setRotation(3);
  26. tft.fillScreen(TFT_BLACK);
  27. tft.setFreeFont(&FreeSans9pt7b);
  28. tft.setTextColor(TFT_WHITE);
  29. Serial3.begin(GPSBaud);
  30. pinPeripheral(PIN_WIRE_SCL, PIO_SERCOM_ALT);
  31. pinPeripheral(PIN_WIRE_SCL, PIO_SERCOM_ALT);
  32. WiFi.begin(ssid, pw);
  33. }
  34. void loop()
  35. {
  36. while (Serial3.available() > 0)
  37. {
  38. if (gps.encode(Serial3.read()))
  39. {
  40. displayInfo();
  41. }
  42. }
  43. delay(1000);
  44. }
  45. void displayInfo()
  46. {
  47. if (gps.date.isValid())
  48. {
  49. String text = String(gps.date.day()) + "/" + String(gps.date.month()) + "/" + String(gps.date.year());
  50. if(text != last_date)
  51. {
  52. tft.fillRect(10, 210, 80, 15, TFT_BLACK);
  53. tft.drawString(text, 10, 210);
  54. last_date = text;
  55. }
  56. }
  57. if (gps.time.isValid())
  58. {
  59. String h = "";
  60. String m = "";
  61. if(gps.time.hour() < 10) h = "0";
  62. h = h + String(gps.time.hour());
  63. if(gps.time.minute() < 10) m = "0";
  64. m = m + String(gps.time.minute());
  65. String text = h + ":" + m;
  66. if(text != last_time)
  67. {
  68. tft.fillRect(250, 210, 50, 15, TFT_BLACK);
  69. tft.drawString(text, 250, 210);
  70. last_time = text;
  71. }
  72. }
  73. if (gps.location.isValid())
  74. {
  75. String lt = String(gps.location.lat(), 3);
  76. String ln = String(gps.location.lng(), 3);
  77. if(lt != last_lat || ln != last_lon)
  78. {
  79. String text = lt + ", " + ln;
  80. tft.fillRect(10, 60, 190, 15, TFT_BLACK);
  81. tft.drawString(text, 10, 60);
  82. last_lat = lt;
  83. last_lon = ln;
  84. if((WiFi.status() == WL_CONNECTED))
  85. {
  86. String wifi_text = "Connected to WiFi: " + String(ssid);
  87. tft.drawString(wifi_text, 10, 10);
  88. api_lookup();
  89. }
  90. }
  91. }
  92. }
  93. void api_lookup()
  94. {
  95. HTTPClient http;
  96. String api_key = "e86b43a62693d9c23eb781f3a06ff13c";
  97. String url = "http://api.openweathermap.org/geo/1.0/reverse?lat=" + last_lat + "&lon=" + last_lon + "&limit=1&appid=" + api_key;
  98. http.begin(url);
  99. int http_code = http.GET();
  100. if(http_code > 0)
  101. {
  102. if(http_code == HTTP_CODE_OK)
  103. {
  104. String payload = http.getString();
  105. payload.remove(0, 1);
  106. payload.remove(payload.length(), 1);
  107. Serial.println(payload);
  108. JsonDocument doc;
  109. deserializeJson(doc, payload);
  110. String name = doc["name"];
  111. String state = doc["state"];
  112. String loc_text = name + ", " + state;
  113. tft.drawString(loc_text, 10, 80);
  114. }
  115. }
  116. http.end();
  117. }
  118. void SERCOM3_0_Handler()
  119. {
  120. Serial3.IrqHandler();
  121. }
  122. void SERCOM3_1_Handler()
  123. {
  124. Serial3.IrqHandler();
  125. }
  126. void SERCOM3_2_Handler()
  127. {
  128. Serial3.IrqHandler();
  129. }
  130. void SERCOM3_3_Handler()
  131. {
  132. Serial3.IrqHandler();
  133. }