/***** importation des librairies *****/

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>

/***** définition des constantes *****/

const char* ssid = "votre_ssid";             // remplacer par votre ssid
const char* password = "votre_password";     // remplacer par votre votre password 
const unsigned long debounceDelay = 50; 
const int led = 13;
const int relai = 12;
const int bouton = 0;
const String page = "<meta http-equiv=\"refresh\" content=\"0;URL=/\"> ";

/***** définition des variables *****/

int etat_led = HIGH; 
int etat_relai = LOW;        
int etat_bouton;  
char* couleur_bouton;          
int dernier_etat_bouton = LOW;
unsigned long dernier_temps_rebond = 0;
String relai_etat, commande;

/***** déclaration du serveur web *****/

ESP8266WebServer server(80);

/***** setup *****/ 

void setup() {
  Serial.begin(115200);
  pinMode(bouton, INPUT);  
  pinMode(led, OUTPUT);
  pinMode(relai, OUTPUT);
  digitalWrite(led, etat_led);   
  digitalWrite(relai, etat_relai);
  Serial.println("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    digitalWrite(led, LOW);
    delay(250);
    Serial.print(".");
    digitalWrite(led, HIGH);
    delay(250);
  }
  Serial.println("");
  Serial.println("WiFi connected..!");
  Serial.print("IP: ");  Serial.println(WiFi.localIP());

  server.on("/", handle_OnConnect);
  server.on("/action", action);  
  server.onNotFound(handle_NotFound);

  server.begin();
  Serial.println("HTTP server started");

}

/***** loop *****/ 

void loop() {  
  server.handleClient();  
  int lecture = digitalRead(bouton);
  if (lecture != dernier_etat_bouton) {   
    dernier_temps_rebond = millis();
  }
  if ((millis() - dernier_temps_rebond) > debounceDelay) {    
    if (lecture != etat_bouton) {
      etat_bouton = lecture;      
      if (etat_bouton == HIGH) {        
        etat_led = !etat_led;
        etat_relai = !etat_relai;
      }
    }
  }
  // set the LED:
  digitalWrite(led, etat_led);
  digitalWrite(relai, etat_relai);  
  dernier_etat_bouton = lecture;  
}

void handle_OnConnect() {
  if (etat_relai == HIGH){ 
    relai_etat = "on";
    couleur_bouton = "#FEC2C2";
    commande = "OFF";
  } else {
    relai_etat = "off";
    couleur_bouton = "#CDF296";
    commande = "ON";
  }  
  server.send(200, "text/html", pageHTML()); 
}

void handle_NotFound(){
  server.send(404, "text/plain", "Not found");
}

void action(){  
  etat_led = !etat_led;  
  etat_relai = !etat_relai;
  server.send(200, "text/html", page); 
}

/***** fonction pageHTML *****/ 

String pageHTML(){
  String html = "<!DOCTYPE html> <html>\n";
  html +="<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
  html +="<title>sonoff basic r2</title>\n";
  html +="<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
  html +="body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;}\n";
  html +="p {font-size: 24px;color: #444444;margin-bottom: 10px;}\n";
  html +="button {background-color: ";
  html +=couleur_bouton;
  html +=";}\n";
  html +="</style>\n";
  html +="<script>\n";
  html +="setInterval(loadDoc,200);\n";
  html +="function loadDoc() {\n";
  html +="var xhttp = new XMLHttpRequest();\n";
  html +="xhttp.onreadystatechange = function() {\n";
  html +="if (this.readyState == 4 && this.status == 200) {\n";
  html +="document.getElementById(\"webpage\").innerHTML =this.responseText}\n";
  html +="};\n";
  html +="xhttp.open(\"GET\", \"/\", true);\n";
  html +="xhttp.send();\n";
  html +="}\n";
  html +="</script>\n";
  html +="</head>\n";
  html +="<body>\n";
  html +="<div id=\"webpage\">\n";
  html +="<h1>sonoff basic r2</h1>\n";    
  html +="<p> Le switch est : ";
  html +=relai_etat;
  html +="</p>";
  html +="<p><a href=\"action\"><button>";
  html +=commande;
  html +="</button></a></p>";
  html +="</div>\n";  
  html +="</body>\n";
  html +="</html>\n";
  return html;
}