-->
Page 1 of 1

webserver authentification to control led

PostPosted: Tue Feb 27, 2018 2:12 am
by cyan
hi ! i'm trying to mix the sketch webserver"simple authentification" from the ide arduino, with another one that i found to control led. It's working , but i can't figure out how to update the main page when i click on/off.

Thanks for your help !

here is my code :

Code: Select all// simple webserver to control a led with two button with a basic authentification page
// source code: arduino esp8266 webserver authentification and
// https://raw.githubusercontent.com/RuiSantosdotme/Random-Nerd-Tutorials/master/Projects/ESP8266_Web_Server_Arduino_IDE.ino


#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 commande lumiere<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 eclairage</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",[](){          //here i don't know how to write  for update the state button
  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();
}

Re: webserver authentification to control led

PostPosted: Sat Mar 03, 2018 12:45 am
by cyan
sorry, i found some mistakes on the code which i fix. But i steel got the problem of the server crash

Code: Select all   
// simple webserver to control a led with two button with a basic authentification page
// source code: arduino ,esp8266webserver, simple authentification and
// https://raw.githubusercontent.com/RuiSantosdotme/Random-Nerd-Tutorials/master/Projects/ESP8266_Web_Server_Arduino_IDE.ino


#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 commande lumiere<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 eclairage</H2><br>";
    content += "<input type=\"button\" name=\"bouton_on\" value=\"Turn LED ON \"  onclick=\"location.href='/bouton_on'\"<br>";
    content += "<p>";
    content += "<input type=\"button\" 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>";
 
  }
   server.send(200, "text/html", content);
 
  }


//=======================================================
//                 handle not 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",  [](){        //area that i suppose being where the code should be
  digitalWrite(LedPin, HIGH);
   });

   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();
}   
 


Thanks