Programme

Le programme si dessous est utilisé pour faire fonctionner une application permettant de changer la couleur des leds, et de récupérer les données transmisent par le capteur de température.




// charger les bibliothèques du dht et et du module wifi de l'esp 
#include <"ESP8266WiFi.h">
#include "DHT.h"
// indiquer que l'on utilise le dht 11 et pas le dht 22
#define DHTTYPE DHT11  
// les codes du partage de connection du télephone mobile
const char* ssid     = "NomDeRéseau";
const char* password = "MotDePasse";

WiFiServer server(80);

// les informations du dht vont arriver dans le port D5
const int DHTPin = D5;
DHT dht(DHTPin, DHTTYPE);

static char celsiusTemp[7];

// Variable to store the HTTP request
String header;

// variables pour stocker les informations des boutons
String output5State = "off";
String output4State = "off";
String output0State = "off";

// defini les ports pour les variables
const int output5 = 5;
const int output4 = 4;
const int output0 = 0;
void setup() {
  Serial.begin(115200);
// initialisation des variables en les éteignant
  delay(10);

  dht.begin();
  pinMode(output5, OUTPUT);
  pinMode(output4, OUTPUT);
  pinMode(output0, OUTPUT);

  digitalWrite(output5, LOW);
  digitalWrite(output4, LOW);
  digitalWrite(output0, LOW);
  
  // connection au réseau wifi
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  // affiche dans le moniteur série l'adresse du site pour contrôler les leds
  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  server.begin();
}

void loop(){
  WiFiClient client = server.available();

  if (client) {                             // si une personne se connecte sur le site
    Serial.println("New Client.");          // envoie un message dans le moniteur série
    String currentLine = "";                // crée une variable pour les informations arrivant de l'utilisateur
    boolean blank_line = true;
    while (client.connected()) {            // pendant que la personne est connecté
      if (client.available()) {             // si il y a une information à lire 
        char c = client.read();             // ça la lit et 
        Serial.write(c);                    // ça l'affiche dans le moniteur serie
        header += c;
        if (c == '\n') {                    
          if (currentLine.length() == 0) {
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println("Connection: close");
            client.println();

               if (c == '\n' && blank_line) {
  
            float h = dht.readHumidity();
            // lit la température dans la variable
            float t = dht.readTemperature();
            
            if (isnan(t)) {
              Serial.println("Failed to read from DHT sensor!");
              strcpy(celsiusTemp,"Failed");
                     
            }
            else{
              float hic = dht.computeHeatIndex(t, h, false);       
              dtostrf(hic, 6, 2, celsiusTemp);             
                          
            }
            
            // sert à récuperer les valeurs des variable pour allumer/éteindre les leds 
            if (header.indexOf("GET /5/on") >= 0) {
              Serial.println("GPIO 5 on");
              output5State = "on";
              digitalWrite(output5, HIGH);
            } else if (header.indexOf("GET /5/off") >= 0) {
              Serial.println("GPIO 5 off");
              output5State = "off";
              digitalWrite(output5, LOW);
            } else if (header.indexOf("GET /4/on") >= 0) {
              Serial.println("GPIO 4 on");
              output4State = "on";
              digitalWrite(output4, HIGH);
            } else if (header.indexOf("GET /4/off") >= 0) {
              Serial.println("GPIO 4 off");
              output4State = "off";
              digitalWrite(output4, LOW);
            } else if (header.indexOf("GET /0/on") >= 0) {
              Serial.println("GPIO 0 on");
              output0State = "on";
              digitalWrite(output0, HIGH);
            } else if (header.indexOf("GET /0/off") >= 0) {
              Serial.println("GPIO 0 off");
              output0State = "off";
              digitalWrite(output0, LOW);
            }
            
            // afficher la page web HTML
            client.println("");
            client.println("");
            client.println("");
            // CSS pour changer la texture des boutons
            client.println("");

            
            client.println(" 
Temperature en C
"); client.print(celsiusTemp); client.println("
Couleur LED
"); client.println("

Rouge - State " + output5State + "

"); if (output5State=="off") { client.println("

"); } else { client.println("

"); } client.println("

Vert - State " + output4State + "

"); if (output4State=="off") { client.println("

"); } else { client.println("

"); } client.println("

Bleu - State " + output0State + "

"); if (output0State=="off") { client.println("

"); } else { client.println("

"); } client.println(""); client.println(""); client.println(); break; } else { currentLine = ""; } } else if (c != '\r') { currentLine += c; blank_line = false; } else if (c == '\n') { blank_line = true; } } } } header = ""; client.stop(); Serial.println("Client disconnected."); Serial.println(""); } }



LOLOLOLOLOL

LOLOLOLOLOL

LOLOLOLOLOL

Jedisoui HoleHola