louino

esp32 webserver to led

Jul 26th, 2021 (edited)
718
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  #include <WiFi.h>
  2. const char* ssid = "NOM DU RESEAU WIFI";
  3. const char* password = "MDP DU RESEAU WIFI";
  4. WiFiServer server(80);
  5. void setup() {
  6.   Serial.begin(115200);
  7.   pinMode(26, OUTPUT);
  8.   pinMode(25, OUTPUT);
  9.   pinMode(33, OUTPUT);
  10.   pinMode(32, OUTPUT);
  11.  
  12.   // connexion au wifi
  13.   Serial.println();
  14.   Serial.print("lancement connexion au reseau wifi :  ");
  15.   Serial.println(ssid);
  16.   WiFi.mode(WIFI_STA);
  17.   WiFi.begin(ssid, password);
  18.   while (WiFi.status() != WL_CONNECTED) {
  19.     delay(500);
  20.     Serial.print(".");
  21.   }
  22.   Serial.println("");
  23.   Serial.println("connexion au WiFi OK");
  24.   // demarrage serveur web
  25.   server.begin();
  26.   Serial.println("le serveur web est lancĂ©, attente fourniture ip locale de l esp ...");
  27.   delay(3000);
  28.   Serial.println(WiFi.localIP());
  29. }
  30.  
  31. void loop() {
  32.  WiFiClient client = server.available();
  33.  if (client) {
  34.    Serial.println("detection dune nouvelle demande client web .....");
  35.    if (client.available()) {
  36.      String request = client.readStringUntil('\r');
  37.      Serial.println(request);
  38.      //client.flush();
  39.      client.println("HTTP/1.1 200 OK");
  40.      client.println("Content-Type: text/html");
  41.      client.println("Connection: close");
  42.      client.println();
  43.      client.println("<!DOCTYPE HTML>");
  44.      client.println("<html><body>OK</body></html>");
  45.  
  46.      if (request.indexOf("/1/LOW") != -1){
  47.        digitalWrite(26, LOW);
  48.      }
  49.      if (request.indexOf("/1/HIGH") != -1){
  50.        digitalWrite(26, HIGH);
  51.      }
  52.      if (request.indexOf("/2/LOW") != -1){
  53.        digitalWrite(25, LOW);
  54.      }
  55.      if (request.indexOf("/2/HIGH") != -1){
  56.        digitalWrite(25, HIGH);
  57.      }
  58.      if (request.indexOf("/3/LOW") != -1){
  59.        digitalWrite(33, LOW);
  60.      }
  61.      if (request.indexOf("/3/HIGH") != -1){
  62.        digitalWrite(33, HIGH);
  63.      }
  64.      if (request.indexOf("/4/LOW") != -1){
  65.        digitalWrite(32, LOW);
  66.      }
  67.      if (request.indexOf("/4/HIGH") != -1){
  68.        digitalWrite(32, HIGH);
  69.      }
  70.  
  71.    }
  72.    client.stop();
  73.    Serial.println("client web deconnecte.");
  74.  }
  75.  delay(100);
  76. }
RAW Paste Data