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

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