authentification & led
Posted: Mon Feb 26, 2018 3:37 pm
hello ! i'm trying to mix the exemple from the ide arduino (webserver simple authentification, with another sktech i found (command a led) and it's half working, i don't know how to write, and having the reponse and staying on the main control page with bouton_on and off. The web page crash
Thanks for your help !
here the code :
Thanks for your help !
here the code :
Code: Select all
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
const char* ssid = " ";
const char* password = " ";
int LedPin=D3;
ESP8266WebServer server( );
//Check if header is present and correct
bool is_authentified(){
if (server.hasHeader("Cookie")){
String cookie = server.header("Cookie");
if (cookie.indexOf("ESPSESSIONID=1") != -1) {
return true;
}
}
return false;
}
//=======================================================
// handle login
//=======================================================
//login page, also called for disconnect
void handleLogin(){
String msg;
if (server.hasHeader("Cookie")){
String cookie = server.header("Cookie");
}
if (server.hasArg("DISCONNECT")){
String header = "HTTP/1.1 301 OK\r\nSet-Cookie: ESPSESSIONID=0\r\nLocation: /login\r\nCache-Control: no-cache\r\n\r\n";
server.sendContent(header);
return;
}
if (server.hasArg("USERNAME") && server.hasArg("PASSWORD")){
if (server.arg("USERNAME") == "123" && server.arg("PASSWORD") == "123" ){
String header = "HTTP/1.1 301 OK\r\nSet-Cookie: ESPSESSIONID=1\r\nLocation: /\r\nCache-Control: no-cache\r\n\r\n";
server.sendContent(header);
return;
}
msg = "identifiant et/ou mot de passe incorrects";
}
String identification = "<html><center><body><form action='/login' method='POST'><h1>Autentification <br></h1>";
identification += "identifiant:<input type='text' name='USERNAME'><br>";
identification += "<p>";
identification += "mot de passe:<input type='password' name='PASSWORD'><br>";
identification += "<br>";
identification += "<br>";
identification += "<input type='submit' name='SUBMIT' value='valider'></form>" + msg + "<br>";
server.send(200, "text/html", identification);
}
//=======================================================
// handle root
//=======================================================
//root page can be accessed only if authentification is ok
void handleRoot(){
String header;
if (!is_authentified()){
String header = "HTTP/1.1 301 OK\r\nLocation: /login\r\nCache-Control: no-cache\r\n\r\n";
server.sendContent(header);
return;
}
String content = " ";
if (server.hasHeader("User-Agent")){
content += "<!DOCTYPE HTML>\r\n<html>\r\n";
content += "<html><body><center><H2>Commande</H2><br>";
content += "<input type=\"submit\" name=\"bouton_on\" value=\"Turn LED ON \" onclick=\"location.href='/bouton_on'\"><br>";
content += "<p>";
content += "<input type=\"submit\" name=\"bouton_off\" value=\"Turn LED OFF \" onclick=\"location.href='/bouton_off'\"><br>";
content += "<br>";
content += "<p><a href=\"/login?DISCONNECT=YES\">deconnexion</a></body></p></html>";
// content += "</html>";
}
server.send(200, "text/html", content);
}
//=======================================================
// handle no found
//=======================================================
//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);
}
//=======================================================
// setup
//=======================================================
void setup(void){
pinMode(LedPin,OUTPUT);
digitalWrite(LedPin,LOW);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
server.on("/", handleRoot);
server.on("/login", handleLogin);
server.on("/bouton_on",[](){
server.send(200,"text/html", );
});
server.on("/bouton_off", [](){
digitalWrite(LedPin, LOW);
});
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();
}
//=======================================================
// loop
//=======================================================
void loop(void){
server.handleClient();
}