| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- #include <ArduinoJson.h>
-
- #include <TinyGPS++.h>
- #include <wiring_private.h>
- #include "TFT_eSPI.h"
- #include <Arduino.h>
- #include <rpcWiFi.h>
- #include <HTTPClient.h>
-
-
- TFT_eSPI tft;
-
- static const uint32_t GPSBaud = 115200;
-
- // The TinyGPS++ object
- TinyGPSPlus gps;
-
- // The serial connection to the GPS device - Left side Grove connector.
- // Left side Grove connector shares pins with I2C1 of 40 pin connector.
- static Uart Serial3(&sercom3, PIN_WIRE_SCL, PIN_WIRE_SDA, SERCOM_RX_PAD_1, UART_TX_PAD_0);
-
- String last_date = "";
- String last_time = "";
- String last_lat = "";
- String last_lon = "";
-
- char* ssid = "SSID";
- char* pw = "PASSWORD";
-
- void setup()
- {
- Serial.begin(115200);
- tft.begin();
- tft.setRotation(3);
- tft.fillScreen(TFT_BLACK);
- tft.setFreeFont(&FreeSans9pt7b);
- tft.setTextColor(TFT_WHITE);
-
- Serial3.begin(GPSBaud);
- pinPeripheral(PIN_WIRE_SCL, PIO_SERCOM_ALT);
- pinPeripheral(PIN_WIRE_SCL, PIO_SERCOM_ALT);
-
- tft.drawString("Not using WiFi", 10, 10);
- WiFi.begin(ssid, pw);
- }
-
- void loop()
- {
- while (Serial3.available() > 0)
- {
- if (gps.encode(Serial3.read()))
- {
- displayInfo();
- }
- }
-
- delay(1000);
- }
-
- void displayInfo()
- {
- if (gps.date.isValid())
- {
- String text = String(gps.date.day()) + "/" + String(gps.date.month()) + "/" + String(gps.date.year());
- if(text != last_date)
- {
- tft.fillRect(10, 210, 80, 15, TFT_BLACK);
- tft.drawString(text, 10, 210);
- last_date = text;
- }
- }
-
- if (gps.time.isValid())
- {
- String h = "";
- String m = "";
- if(gps.time.hour() < 10) h = "0";
- h = h + String(gps.time.hour());
- if(gps.time.minute() < 10) m = "0";
- m = m + String(gps.time.minute());
-
- String text = h + ":" + m;
- if(text != last_time)
- {
- tft.fillRect(250, 210, 50, 15, TFT_BLACK);
- tft.drawString(text, 250, 210);
- last_time = text;
- }
- }
-
- if (gps.location.isValid())
- {
- String lt = String(gps.location.lat(), 3);
- String ln = String(gps.location.lng(), 3);
- if(lt != last_lat || ln != last_lon)
- {
- String text = lt + ", " + ln;
- tft.fillRect(10, 60, 190, 15, TFT_BLACK);
- tft.drawString(text, 10, 60);
-
- last_lat = lt;
- last_lon = ln;
-
- if((WiFi.status() == WL_CONNECTED))
- {
- String wifi_text = "Connected to WiFi: " + String(ssid);
- tft.fillRect(10, 10, 250, 20, TFT_BLACK);
- tft.drawString(wifi_text, 10, 10);
- api_lookup();
- }
- }
- }
- }
-
- void api_lookup()
- {
- HTTPClient http;
- String api_key = "API KEY";
- String url = "http://api.openweathermap.org/geo/1.0/reverse?lat=" + last_lat + "&lon=" + last_lon + "&limit=1&appid=" + api_key;
- http.begin(url);
-
- int http_code = http.GET();
- if(http_code > 0)
- {
- if(http_code == HTTP_CODE_OK)
- {
- String payload = http.getString();
- payload.remove(0, 1);
- payload.remove(payload.length(), 1);
- Serial.println(payload);
-
- JsonDocument doc;
- deserializeJson(doc, payload);
- String name = doc["name"];
- String state = doc["state"];
- String loc_text = name + ", " + state;
- tft.fillRect(10, 80, 200, 20, TFT_BLACK);
- tft.drawString(loc_text, 10, 80);
- }
- }
-
- http.end();
- }
-
- void SERCOM3_0_Handler()
- {
- Serial3.IrqHandler();
- }
- void SERCOM3_1_Handler()
- {
- Serial3.IrqHandler();
- }
- void SERCOM3_2_Handler()
- {
- Serial3.IrqHandler();
- }
- void SERCOM3_3_Handler()
- {
- Serial3.IrqHandler();
- }
|