I am working on a project for school that has a trackable device on a person that will print their location onto a webserver. Right now when I create the webserver the latitude and longitude are listed as zero. I listed my code down below!
float latitude,longitude;
#include <WiFi.h>
#include <WebServer.h>
#include <TinyGPS++.h>
#define GPS_BAUD 115200
// SSID & Password
const char* ssid = "Engineering"; // Enter your SSID here
const char* password = "2024Cl@$$2025"; //Enter your Password here
TinyGPSPlus gps;
HardwareSerial gpsSerial(2);
#define RXD2 16
#define TXD2 17
WebServer server(80); // Object of WebServer(HTTP port, 80 is defult)
void setup() {
Serial.begin(115200);
delay(100);
Serial.println("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
Serial.println("Try Connecting to ");
Serial.println(ssid);
gpsSerial.begin(GPS_BAUD, SERIAL_8N1, RXD2, TXD2);
Serial.println("Serial 2 started at 115200 baud rate");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected..!");
Serial.print("Got IP: ");
Serial.println(WiFi.localIP());
server.on("/", handle_OnConnect);
server.onNotFound(handle_NotFound);
server.begin();
Serial.println("HTTP server started");
}
void loop() {
server.handleClient();
gps.encode(gpsSerial.read());
Serial.println(gps.location.lat());
Serial.println(gps.location.lng());
}
void handle_OnConnect() {
gps.encode(gpsSerial.read());
latitude=gps.location.lat();
longitude=gps.location.lng();
server.send(200, "text/html", SendHTML(latitude,longitude));
}
void handle_NotFound(){
server.send(404, "text/plain", "Not found");
}
static int test(){
return latitude;
}
String SendHTML(float latitude, float longitude){
String ptr = " \n";
ptr +="<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">\n";
ptr +="ESP32 Latitude and Longitude \n";
ptr +="html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
ptr +="body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;}\n";
ptr +="p {font-size: 24px;color: #444444;margin-bottom: 10px;}\n";
ptr +="\n";
ptr +="\n";
ptr +="\n";
ptr +="<div id="webpage">\n";
ptr +="
Latitude and Longitude
\n";ptr +="
Latitude: ";
ptr +=latitude;
ptr +="
Longitude: ";
ptr +=longitude;
ptr +="\n";
ptr +=test();
ptr +="\n";
ptr +="\n";
return ptr;
}