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

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

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

#define DHTTYPE DHT22
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=\"URL=/\"> ";

/***** définition des variables *****/
int DHTPin = 3;   
float Temperature;
float Humidity;
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éfinition du serveur web *****/

ESP8266WebServer server(80);

/***** définition de la sonde *****/

DHT dht(DHTPin, DHTTYPE);

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

void setup() {  
  pinMode(bouton, INPUT);  
  pinMode(led, OUTPUT);
  pinMode(relai, OUTPUT);
  digitalWrite(led, etat_led);   
  digitalWrite(relai, etat_relai);
  pinMode(DHTPin, INPUT);
  dht.begin();      
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    digitalWrite(led, LOW);
    delay(250);
    digitalWrite(led, HIGH);
    delay(250);
  }
  server.on("/", handle_OnConnect);
  server.on("/action", action);  
  server.onNotFound(handle_NotFound);
  server.begin();
}

/***** 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";
  }  
  Temperature = dht.readTemperature(); // Gets the values of the temperature
  Humidity = dht.readHumidity(); // Gets the values of the humidity 
  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 +="<h1>Sonde DHT22</h1>\n";    
  html +="<p>Temperature: ";
  html +=Temperature;
  html +="°C</p>";
  html +="<p>Humidity: ";
  html +=Humidity;
  html +="%</p>";
  html +="</div>\n";  
  html +="</body>\n";
  html +="</html>\n";
  return html;
}