Browse Source

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

cube 8 months ago
parent
commit
79aa2f106e
1 changed files with 89 additions and 48 deletions
  1. 89
    48
      Seeed Wio Terminal/projects/m5-gps.ino

+ 89
- 48
Seeed Wio Terminal/projects/m5-gps.ino View File

@@ -1,6 +1,12 @@
1
+#include <ArduinoJson.h>
2
+
1 3
 #include <TinyGPS++.h>
2 4
 #include <wiring_private.h>
3 5
 #include "TFT_eSPI.h"
6
+#include <Arduino.h>
7
+#include <rpcWiFi.h>
8
+#include <HTTPClient.h>
9
+
4 10
 
5 11
 TFT_eSPI tft;
6 12
 
@@ -13,6 +19,14 @@ TinyGPSPlus gps;
13 19
 // Left side Grove connector shares pins with I2C1 of 40 pin connector.
14 20
 static Uart Serial3(&sercom3, PIN_WIRE_SCL, PIN_WIRE_SDA, SERCOM_RX_PAD_1, UART_TX_PAD_0);
15 21
 
22
+String last_date = "";
23
+String last_time = "";
24
+String last_lat = "";
25
+String last_lon = "";
26
+
27
+char* ssid = "SSID";
28
+char* pw = "PASSWORD";
29
+
16 30
 void setup()
17 31
 {
18 32
   Serial.begin(115200);
@@ -25,77 +39,104 @@ void setup()
25 39
   Serial3.begin(GPSBaud);
26 40
   pinPeripheral(PIN_WIRE_SCL, PIO_SERCOM_ALT);
27 41
   pinPeripheral(PIN_WIRE_SCL, PIO_SERCOM_ALT);
42
+
43
+  WiFi.begin(ssid, pw);
28 44
 }
29 45
  
30 46
 void loop()
31 47
 {
32
-  // This sketch displays information every time a new sentence is correctly encoded.
33 48
   while (Serial3.available() > 0)
49
+  {
34 50
     if (gps.encode(Serial3.read()))
51
+    {
35 52
       displayInfo();
36
-      delay(3000);
37
- 
38
-  if (millis() > 5000 && gps.charsProcessed() < 10)
39
-  {
40
-    Serial.println(F("No GPS detected: check wiring."));
41
-    tft.drawString("No GPS detected: check wiring.", 10, 10);
42
-    while(true);
53
+    }
43 54
   }
55
+
56
+  delay(1000);
44 57
 }
45 58
 
46 59
 void displayInfo()
47 60
 {
48
-  Serial.print(F("Location: ")); 
49
-  if (gps.location.isValid())
50
-  {
51
-    Serial.print(gps.location.lat(), 6);
52
-    Serial.print(F(","));
53
-    Serial.print(gps.location.lng(), 6);
54
-
55
-    String text = String(gps.location.lat(), 6) + ", " + String(gps.location.lng(), 6);
56
-    tft.fillRect(10, 30, 190, 30, TFT_BLACK);
57
-    tft.drawString(text, 10, 30);
58
-  }
59
-  else
61
+  if (gps.date.isValid())
60 62
   {
61
-    Serial.print(F("INVALID"));
63
+    String text = String(gps.date.day()) + "/" + String(gps.date.month()) + "/" + String(gps.date.year());
64
+    if(text != last_date)
65
+    {
66
+      tft.fillRect(10, 210, 80, 15, TFT_BLACK);
67
+      tft.drawString(text, 10, 210);
68
+      last_date = text;
69
+    }
62 70
   }
63 71
 
64
-  Serial.print(F("  Date/Time: "));
65
-  if (gps.date.isValid())
72
+  if (gps.time.isValid())
66 73
   {
67
-    Serial.print(gps.date.month());
68
-    Serial.print(F("/"));
69
-    Serial.print(gps.date.day());
70
-    Serial.print(F("/"));
71
-    Serial.print(gps.date.year());
74
+    String h = "";
75
+    String m = "";
76
+    if(gps.time.hour() < 10) h = "0";
77
+    h = h + String(gps.time.hour());
78
+    if(gps.time.minute() < 10) m = "0";
79
+    m = m + String(gps.time.minute());
80
+
81
+    String text = h + ":" + m;
82
+    if(text != last_time)
83
+    {
84
+      tft.fillRect(250, 210, 50, 15, TFT_BLACK);
85
+      tft.drawString(text, 250, 210);
86
+      last_time = text;
87
+    }
72 88
   }
73
-  else
89
+  
90
+  if (gps.location.isValid())
74 91
   {
75
-    Serial.print(F("INVALID"));
76
-  }
92
+    String lt = String(gps.location.lat(), 3);
93
+    String ln = String(gps.location.lng(), 3);
94
+    if(lt != last_lat || ln != last_lon)
95
+    {
96
+      String text = lt + ", " + ln;
97
+      tft.fillRect(10, 60, 190, 15, TFT_BLACK);
98
+      tft.drawString(text, 10, 60);
77 99
 
78
-  Serial.print(F(" "));
79
-  if (gps.time.isValid())
80
-  {
81
-    if (gps.time.hour() < 10) Serial.print(F("0"));
82
-    Serial.print(gps.time.hour());
83
-    Serial.print(F(":"));
84
-    if (gps.time.minute() < 10) Serial.print(F("0"));
85
-    Serial.print(gps.time.minute());
86
-    Serial.print(F(":"));
87
-    if (gps.time.second() < 10) Serial.print(F("0"));
88
-    Serial.print(gps.time.second());
89
-    Serial.print(F("."));
90
-    if (gps.time.centisecond() < 10) Serial.print(F("0"));
91
-    Serial.print(gps.time.centisecond());
100
+      last_lat = lt;
101
+      last_lon = ln;
102
+
103
+      if((WiFi.status() == WL_CONNECTED))
104
+      {
105
+        String wifi_text = "Connected to WiFi: " + String(ssid);
106
+        tft.drawString(wifi_text, 10, 10);
107
+        api_lookup();
108
+      }
109
+    }
92 110
   }
93
-  else
111
+}
112
+
113
+void api_lookup()
114
+{
115
+  HTTPClient http;
116
+  String api_key = "e86b43a62693d9c23eb781f3a06ff13c";
117
+  String url = "http://api.openweathermap.org/geo/1.0/reverse?lat=" + last_lat + "&lon=" + last_lon + "&limit=1&appid=" + api_key;
118
+  http.begin(url);
119
+  
120
+  int http_code = http.GET();
121
+  if(http_code > 0)
94 122
   {
95
-    Serial.print(F("INVALID"));
123
+    if(http_code == HTTP_CODE_OK)
124
+    {
125
+      String payload = http.getString();
126
+      payload.remove(0, 1);
127
+      payload.remove(payload.length(), 1);
128
+      Serial.println(payload);
129
+
130
+      JsonDocument doc;
131
+      deserializeJson(doc, payload);
132
+      String name = doc["name"];
133
+      String state = doc["state"];
134
+      String loc_text = name + ", " + state;
135
+      tft.drawString(loc_text, 10, 80);
136
+    }
96 137
   }
97 138
 
98
-  Serial.println();
139
+  http.end();
99 140
 }
100 141
 
101 142
 void SERCOM3_0_Handler()