-
Notifications
You must be signed in to change notification settings - Fork 0
/
ESP32_GPS.ino
195 lines (159 loc) · 5.48 KB
/
ESP32_GPS.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <TinyGPSPlus.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <WiFiClientSecure.h>
#include <HTTPSRedirect.h>
#include <HTTPClient.h>
const char* ssid = "wife-i";
const char* password = "73555608";
const char* serverName = "https://location-live-tracking-muf7kziviq-as.a.run.app/receive_data";
const char* trackerId = "2";
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
TinyGPSPlus gps;
WiFiClientSecure client;
typedef struct {
double lat;
double lon;
unsigned long timestamp;
} CachedGPSData;
CachedGPSData cachedData[1000]; // Adjust the size as needed
int cacheIndex = 0;
TaskHandle_t Task1, Task2;
void coreTask1(void* param) {
while (true) {
updateGPSAndDisplay();
delay(1000);
}
}
void coreTask2(void* param) {
while (true) {
handleWiFiAndAPI();
delay(1000);
}
}
void setup() {
Serial.begin(9600);
Serial2.begin(9600);
WiFi.begin(ssid, password);
Serial.println("Connecting");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
delay(3000);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 10);
client.setInsecure();
xTaskCreatePinnedToCore(coreTask1, "Task1", 10000, NULL, 1, &Task1, 0);
xTaskCreatePinnedToCore(coreTask2, "Task2", 10000, NULL, 1, &Task2, 1);
}
void loop() {}
void updateGPSAndDisplay() {
while (Serial2.available() > 0)
if (gps.encode(Serial2.read()))
displayInfo();
if (millis() > 5000 && gps.charsProcessed() < 10) {
Serial.println(F("No GPS detected: check wiring."));
}
}
void displayInfo() {
if (gps.location.isValid()) {
display.clearDisplay();
display.setCursor(0, 0);
display.print("Lat: ");
display.print(gps.location.lat(), 6);
display.setCursor(0, 16);
display.print("Lon: ");
display.print(gps.location.lng(), 6);
display.setCursor(0, 32);
display.print("Speed: ");
display.print(gps.speed.kmph());
display.display();
} else {
display.clearDisplay();
display.setCursor(0, 0);
display.print("No GPS data");
display.display();
}
}
String getTimestampString(unsigned long timestamp) {
char buffer[21];
int year = gps.date.year();
int month = gps.date.month();
int day = gps.date.day();
int hour = gps.time.hour();
int minute = gps.time.minute();
int second = gps.time.second();
snprintf(buffer, sizeof(buffer), "%04d-%02d-%02d %02d:%02d:%02d", year, month, day, hour, minute, second);
return String(buffer);
}
void handleWiFiAndAPI() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(client, serverName);
http.addHeader("Content-Type", "application/json");
http.addHeader("Authorization", "Bearer secret");
bool requestSuccessful = false;
int httpResponseCode;
// Send cached data first
while (cacheIndex > 0) {
Serial.println("Sending cached requests.. index:"+String(cacheIndex));
String postData = "{\"tracker_id\": \""+ String(trackerId) + "\", \"lat\": \"" + String(cachedData[cacheIndex - 1].lat, 6) + "\", \"lon\": \"" + String(cachedData[cacheIndex - 1].lon, 6) + "\", \"timestamp\": \"" + getTimestampString(cachedData[cacheIndex - 1].timestamp) + "\"}";
httpResponseCode = http.POST(postData);
Serial.println(postData);
if (httpResponseCode == 200 || httpResponseCode == 201) {
Serial.println("HTTP POST Successful");
requestSuccessful = true;
String response = http.getString(); // Get the response payload
Serial.println("Response: " + response); // Print the response payload
cacheIndex--;
} else {
requestSuccessful = false;
Serial.println("Error in sending HTTP POST request: " + String(httpResponseCode));
break;
}
}
// Send current GPS data if available
if (gps.location.isValid()) {
String postData = "{\"tracker_id\": \""+ String(trackerId) + "\", \"lat\": \"" + String(gps.location.lat(), 6) + "\", \"lon\": \"" + String(gps.location.lng(), 6) + "\", \"timestamp\": \"" + getTimestampString(millis() / 1000) + "\"}";
Serial.println(postData);
httpResponseCode = http.POST(postData);
if (httpResponseCode == 200 || httpResponseCode == 201) {
requestSuccessful = true;
Serial.println("HTTP POST Successful");
} else {
requestSuccessful = false;
Serial.println("Error in sending HTTP POST request: " + String(httpResponseCode));
Serial.println("Caching request.. index: "+ String(cacheIndex));
// Cache the GPS data
if (cacheIndex < 1000) {
cachedData[cacheIndex].lat = gps.location.lat();
cachedData[cacheIndex].lon = gps.location.lng();
cachedData[cacheIndex].timestamp = millis() / 1000;
cacheIndex++;
} else {
Serial.println("Cache is full, unable to store more data.");
}
}
String response = http.getString(); // Get the response payload
Serial.println("Response: " + response); // Print the response payload
} else {
Serial.println("GPS not calibrated yet!");
}
http.end();
} else {
Serial.println("Wi-Fi is not connected");
}
}