#include
#include
#include
#include
MDNSResponder mdns;
const char* ssid = "none"; // your connection name
const char* password = "none"; // your connection password
ESP8266WebServer server(80);
int gpio1_pin = 2; // D4 of nodemcu
int gpio2_pin = 13; // D7 of nodemcu
int gpio3_pin =14; // D5 of nodemcu
//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") == "root" ) // enter ur username and password you want
{
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 = "" + msg + "
";
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 = "
HOME AUTOMATION
Switch #1
";
content += "
Switch #2
";
content += "
Switch #3
";
content += " ";
content += "
";
if (server.hasHeader("User-Agent")){
content += "the user agent used is : " + server.header("User-Agent") + "
";
}
content += "You can access this page until you disconnect