Hello!
I am making a GPS system. Right now I am trying to set up a way to store coordinates (i.e. latitude and longitude) in variables. I think structs are the way to go?
Im not sure exactly how I can do this with structs though. Im thinking maybe do something like "coord[1].latitude = Lat;".
Here is what I have so far (its not my full code, but just my test code for my webpage):
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <WiFiAP.h>
const char* ssid = "GPSTracker";
const char* password = "GPS";
float HOME_LAT = 0;
float HOME_LON = 0;
WebServer server(80);
struct coords[20] {
float latitude;
float longitude;
}
//coords[1].latitude
//Check if header is present and correct
bool is_authentified() {
Serial.println("Enter is_authentified");
if (server.hasHeader("Cookie")) {
Serial.print("Found cookie: ");
String cookie = server.header("Cookie");
Serial.println(cookie);
if (cookie.indexOf("ESPSESSIONID=1") != -1) {
Serial.println("Authentification Successful");
return true;
}
}
Serial.println("Authentification Failed");
return false;
}
//login page, also called for disconnect
void handleLogin() {
String msg;
if (server.hasHeader("Cookie")) {
Serial.print("Found cookie: ");
String cookie = server.header("Cookie");
Serial.println(cookie);
}
if (server.hasArg("DISCONNECT")) {
Serial.println("Disconnection");
server.sendHeader("Location", "/login");
server.sendHeader("Cache-Control", "no-cache");
server.sendHeader("Set-Cookie", "ESPSESSIONID=0");
server.send(301);
return;
}
if (server.hasArg("USERNAME") && server.hasArg("PASSWORD")) {
if (server.arg("USERNAME") == "admin" && server.arg("PASSWORD") == "admin") {
server.sendHeader("Location", "/");
server.sendHeader("Cache-Control", "no-cache");
server.sendHeader("Set-Cookie", "ESPSESSIONID=1");
server.send(301);
Serial.println("Log in Successful");
return;
}
msg = "Wrong username/password! try again.";
Serial.println("Log in Failed");
}
String content = "<html><body><form action='/login' method='POST'>Log in to see your GPS Tracker";
content += "User:<input type='text' name='USERNAME' placeholder='user name'><br>";
content += "Password:<input type='password' name='PASSWORD' placeholder='password'><br>";
content += "<input type='submit' name='SUBMIT' value='Submit'></form>" + msg + "<br>";
server.send(200, "text/html", content);
}
//root page can be accessed only if authentification is ok
void handleRoot() {
Serial.println("Enter handleRoot");
String header;
if (!is_authentified()) {
server.sendHeader("Location", "/login");
server.sendHeader("Cache-Control", "no-cache");
server.send(301);
return;
}
String content = "<html><body><H1>GPS Tracker!</H1><br>";
content += "<H2>Welcome to your GPS Tracker website!</H2><br>";
content += "<body>Welcome to your GPS Tracker website!<body><br>";
content += "<body>Click <a href='/changecoord'>here</a> to change a waypoint.</body><br>";
content += "<body>Click <a href='/addcoord'>here</a> to add a waypoint.</body><br>";
content += "<body>Waypoint Latitude: " + String(HOME_LAT,4) + ", Waypoint Longitude: " + String(HOME_LON,4) + "</body></html><br><br><br>";
if (server.hasHeader("User-Agent")) {
content += "the user agent used is : " + server.header("User-Agent") + "<br><br>";
}
content += "You can access this page until you <a href=\"/login?DISCONNECT=YES\">disconnect</a></body></html>";
server.send(200, "text/html", content);
}
void handleChangeCoord() {
String msg;
if (server.hasHeader("Cookie")) {
Serial.print("Found cookie: ");
String cookie = server.header("Cookie");
Serial.println(cookie);
}
if (server.hasArg("NUMBER") && server.hasArg("LATITUDE") && server.hasArg("LONGITUDE")) {
//if (server.arg("LATITUDE") == "admin" && server.arg("LONGITUDE") == "admin") {
String ID = server.arg("NUMBER");
String Lat = server.arg("LATITUDE");
String Lon = server.arg("LONGITUDE");
Serial.println("Latitude " + Lat);
Serial.println("Longitude " + Lon);
HOME_LAT = Lat.toFloat();
HOME_LON = Lon.toFloat();
int ids = ID.toInt();
coords[ids].latitude = HOME_LAT;
server.sendHeader("Location", "/");
server.sendHeader("Cache-Control", "no-cache");
server.sendHeader("Set-Cookie", "ESPSESSIONID=1");
server.send(301);
Serial.println("coordinates done");
return;
}
String content = "<html><body><form action='/changecoord' method='POST'>Type your waypoint ID then type the new coordinates.<br>";
content += "ID:<input type='text' name='NUMBER' placeholder='ID number'><br>";
content += "Latitude:<input type='text' name='LATITUDE' placeholder='latitude'><br>";
content += "Longitude:<input type='text' name='LONGITUDE' placeholder='longitude'><br>";
content += "<input type='submit' name='SUBMIT' value='Submit'></form>" + msg + "<br>";
server.send(200, "text/html", content);
}
//no need authentification
void handleNotFound() {
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
}
void setup(void) {
Serial.begin(115200);
WiFi.softAP(ssid, password);
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
server.on("/", handleRoot);
server.on("/login", handleLogin);
server.on("/changecoord", handleChangeCoord);
server.on("/addcoord", handleAddCoord);
server.onNotFound(handleNotFound);
//here the list of headers to be recorded
const char * headerkeys[] = {"User-Agent", "Cookie"} ;
size_t headerkeyssize = sizeof(headerkeys) / sizeof(char*);
//ask server to track these headers
server.collectHeaders(headerkeys, headerkeyssize);
server.begin();
Serial.println("HTTP server started");
}
void loop(void) {
server.handleClient();
delay(2);//allow the cpu to switch to other tasks
}
Any suggestions for how to do that?
Thanks in advance!