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

m5-gps.ino 3.7KB

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