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

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