-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path1-ESP32-with-GPS-AP.cpp
More file actions
75 lines (60 loc) · 2.22 KB
/
1-ESP32-with-GPS-AP.cpp
File metadata and controls
75 lines (60 loc) · 2.22 KB
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
#include <WiFi.h>
#include <TinyGPS++.h>
#include <HardwareSerial.h>
#include <WebServer.h>
// ==== Access Point (AP) mode : Replace with your WiFi credentials ====
const char* ssid = "ESP-32-GPS";
const char* password = "12345678";
// ==== Web server on port 80 ====
WebServer server(80);
// ==== GPS Setup ====
TinyGPSPlus gps;
HardwareSerial gpsSerial(2); // Use UART2
const int RXD2 = 16;
const int TXD2 = 17;
// HTML Page
String htmlPage() {
String page = "<!DOCTYPE html><html><head><meta name='viewport' content='width=device-width, initial-scale=1'>";
page += "<meta http-equiv='refresh' content='5'>"; // Auto-refresh every 5 seconds
page += "<style>body{font-family:Arial; text-align:center;}h2{color:#2F4F4F;}</style></head><body>";
page += "<h2>ESP32 GPS WebServer</h2>";
if (gps.location.isValid()) {
double lat = gps.location.lat();
double lng = gps.location.lng();
page += "<p><strong>Latitude:</strong> " + String(lat, 6) + "</p>";
page += "<p><strong>Longitude:</strong> " + String(lng, 6) + "</p>";
page += "<p><strong>Altitude:</strong> " + String(gps.altitude.meters()) + " meters</p>";
page += "<p><strong>Satellites:</strong> " + String(gps.satellites.value()) + "</p>";
page += "<p><strong>Speed:</strong> " + String(gps.speed.kmph()) + " km/h</p>";
// Google Maps Link
page += "<p><a href='https://www.google.com/maps?q=" + String(lat, 6) + "," + String(lng, 6) + "' target='_blank'>";
page += "click to -> Open in Google-Maps</a></p>";
} else {
page += "<p><strong>Waiting for valid GPS data...</strong></p>";
}
page += "<br><p>Auto-refresh every 5 seconds to get real-time GPS data.</p>";
page += "</body></html>";
return page;
}
void handleRoot() {
server.send(200, "text/html", htmlPage());
}
void setup() {
Serial.begin(115200);
gpsSerial.begin(9600, SERIAL_8N1, RXD2, TXD2);
// Connect to WiFi
WiFi.softAP(ssid, password);
Serial.println("WiFi started");
Serial.println("IP address: ");
Serial.println(WiFi.softAPIP());
// Start Web Server
server.on("/", handleRoot);
server.begin();
Serial.println("Web server started");
}
void loop() {
while (gpsSerial.available() > 0) {
gps.encode(gpsSerial.read());
}
server.handleClient();
}