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

m5-gps.ino 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. tft.drawString("Not using WiFi", 10, 10);
  33. WiFi.begin(ssid, pw);
  34. }
  35. void loop()
  36. {
  37. while (Serial3.available() > 0)
  38. {
  39. if (gps.encode(Serial3.read()))
  40. {
  41. displayInfo();
  42. }
  43. }
  44. delay(1000);
  45. }
  46. void displayInfo()
  47. {
  48. if (gps.date.isValid())
  49. {
  50. String text = String(gps.date.day()) + "/" + String(gps.date.month()) + "/" + String(gps.date.year());
  51. if(text != last_date)
  52. {
  53. tft.fillRect(10, 210, 80, 15, TFT_BLACK);
  54. tft.drawString(text, 10, 210);
  55. last_date = text;
  56. }
  57. }
  58. if (gps.time.isValid())
  59. {
  60. String h = "";
  61. String m = "";
  62. if(gps.time.hour() < 10) h = "0";
  63. h = h + String(gps.time.hour());
  64. if(gps.time.minute() < 10) m = "0";
  65. m = m + String(gps.time.minute());
  66. String text = h + ":" + m;
  67. if(text != last_time)
  68. {
  69. tft.fillRect(250, 210, 50, 15, TFT_BLACK);
  70. tft.drawString(text, 250, 210);
  71. last_time = text;
  72. }
  73. }
  74. if (gps.location.isValid())
  75. {
  76. String lt = String(gps.location.lat(), 3);
  77. String ln = String(gps.location.lng(), 3);
  78. if(lt != last_lat || ln != last_lon)
  79. {
  80. String text = lt + ", " + ln;
  81. tft.fillRect(10, 60, 190, 15, TFT_BLACK);
  82. tft.drawString(text, 10, 60);
  83. last_lat = lt;
  84. last_lon = ln;
  85. if((WiFi.status() == WL_CONNECTED))
  86. {
  87. String wifi_text = "Connected to WiFi: " + String(ssid);
  88. tft.fillRect(10, 10, 250, 20, TFT_BLACK);
  89. tft.drawString(wifi_text, 10, 10);
  90. api_lookup();
  91. }
  92. }
  93. }
  94. }
  95. void api_lookup()
  96. {
  97. HTTPClient http;
  98. String api_key = "API KEY";
  99. String url = "http://api.openweathermap.org/geo/1.0/reverse?lat=" + last_lat + "&lon=" + last_lon + "&limit=1&appid=" + api_key;
  100. http.begin(url);
  101. int http_code = http.GET();
  102. if(http_code > 0)
  103. {
  104. if(http_code == HTTP_CODE_OK)
  105. {
  106. String payload = http.getString();
  107. payload.remove(0, 1);
  108. payload.remove(payload.length(), 1);
  109. Serial.println(payload);
  110. JsonDocument doc;
  111. deserializeJson(doc, payload);
  112. String name = doc["name"];
  113. String state = doc["state"];
  114. String loc_text = name + ", " + state;
  115. tft.fillRect(10, 80, 200, 20, TFT_BLACK);
  116. tft.drawString(loc_text, 10, 80);
  117. }
  118. }
  119. http.end();
  120. }
  121. void SERCOM3_0_Handler()
  122. {
  123. Serial3.IrqHandler();
  124. }
  125. void SERCOM3_1_Handler()
  126. {
  127. Serial3.IrqHandler();
  128. }
  129. void SERCOM3_2_Handler()
  130. {
  131. Serial3.IrqHandler();
  132. }
  133. void SERCOM3_3_Handler()
  134. {
  135. Serial3.IrqHandler();
  136. }