Update 'Seeed Wio Terminal/projects/m5-gps.ino'

This commit is contained in:
2025-03-24 22:36:53 +00:00
parent a1e279f68e
commit 79aa2f106e

View File

@@ -1,6 +1,12 @@
#include <ArduinoJson.h>
#include <TinyGPS++.h> #include <TinyGPS++.h>
#include <wiring_private.h> #include <wiring_private.h>
#include "TFT_eSPI.h" #include "TFT_eSPI.h"
#include <Arduino.h>
#include <rpcWiFi.h>
#include <HTTPClient.h>
TFT_eSPI tft; TFT_eSPI tft;
@@ -13,6 +19,14 @@ TinyGPSPlus gps;
// Left side Grove connector shares pins with I2C1 of 40 pin 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); 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() void setup()
{ {
Serial.begin(115200); Serial.begin(115200);
@@ -25,77 +39,104 @@ void setup()
Serial3.begin(GPSBaud); Serial3.begin(GPSBaud);
pinPeripheral(PIN_WIRE_SCL, PIO_SERCOM_ALT); pinPeripheral(PIN_WIRE_SCL, PIO_SERCOM_ALT);
pinPeripheral(PIN_WIRE_SCL, PIO_SERCOM_ALT); pinPeripheral(PIN_WIRE_SCL, PIO_SERCOM_ALT);
WiFi.begin(ssid, pw);
} }
void loop() void loop()
{ {
// This sketch displays information every time a new sentence is correctly encoded.
while (Serial3.available() > 0) while (Serial3.available() > 0)
if (gps.encode(Serial3.read()))
displayInfo();
delay(3000);
if (millis() > 5000 && gps.charsProcessed() < 10)
{ {
Serial.println(F("No GPS detected: check wiring.")); if (gps.encode(Serial3.read()))
tft.drawString("No GPS detected: check wiring.", 10, 10); {
while(true); displayInfo();
}
} }
delay(1000);
} }
void displayInfo() void displayInfo()
{ {
Serial.print(F("Location: "));
if (gps.location.isValid())
{
Serial.print(gps.location.lat(), 6);
Serial.print(F(","));
Serial.print(gps.location.lng(), 6);
String text = String(gps.location.lat(), 6) + ", " + String(gps.location.lng(), 6);
tft.fillRect(10, 30, 190, 30, TFT_BLACK);
tft.drawString(text, 10, 30);
}
else
{
Serial.print(F("INVALID"));
}
Serial.print(F(" Date/Time: "));
if (gps.date.isValid()) if (gps.date.isValid())
{ {
Serial.print(gps.date.month()); String text = String(gps.date.day()) + "/" + String(gps.date.month()) + "/" + String(gps.date.year());
Serial.print(F("/")); if(text != last_date)
Serial.print(gps.date.day()); {
Serial.print(F("/")); tft.fillRect(10, 210, 80, 15, TFT_BLACK);
Serial.print(gps.date.year()); tft.drawString(text, 10, 210);
} last_date = text;
else }
{
Serial.print(F("INVALID"));
} }
Serial.print(F(" "));
if (gps.time.isValid()) if (gps.time.isValid())
{ {
if (gps.time.hour() < 10) Serial.print(F("0")); String h = "";
Serial.print(gps.time.hour()); String m = "";
Serial.print(F(":")); if(gps.time.hour() < 10) h = "0";
if (gps.time.minute() < 10) Serial.print(F("0")); h = h + String(gps.time.hour());
Serial.print(gps.time.minute()); if(gps.time.minute() < 10) m = "0";
Serial.print(F(":")); m = m + String(gps.time.minute());
if (gps.time.second() < 10) Serial.print(F("0"));
Serial.print(gps.time.second()); String text = h + ":" + m;
Serial.print(F(".")); if(text != last_time)
if (gps.time.centisecond() < 10) Serial.print(F("0")); {
Serial.print(gps.time.centisecond()); tft.fillRect(250, 210, 50, 15, TFT_BLACK);
tft.drawString(text, 250, 210);
last_time = text;
}
} }
else
if (gps.location.isValid())
{ {
Serial.print(F("INVALID")); 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.drawString(wifi_text, 10, 10);
api_lookup();
}
}
}
}
void api_lookup()
{
HTTPClient http;
String api_key = "e86b43a62693d9c23eb781f3a06ff13c";
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.drawString(loc_text, 10, 80);
}
} }
Serial.println(); http.end();
} }
void SERCOM3_0_Handler() void SERCOM3_0_Handler()