0% found this document useful (0 votes)
44 views2 pages

Arduino Esp8266 12e Led

This document describes code for connecting an ESP8266 WiFi module to a wireless network and controlling an LED via a web server. The code initializes WiFi and sets up a server on port 80. It then enters a loop to check for incoming client requests, and will turn the LED on or off depending on the URL requested, responding with a web page indicating the current state.

Uploaded by

Mekkati Mekkati
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views2 pages

Arduino Esp8266 12e Led

This document describes code for connecting an ESP8266 WiFi module to a wireless network and controlling an LED via a web server. The code initializes WiFi and sets up a server on port 80. It then enters a loop to check for incoming client requests, and will turn the LED on or off depending on the URL requested, responding with a web page indicating the current state.

Uploaded by

Mekkati Mekkati
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <ESP8266WiFi.

h>
 
const char* ssid = "SSID";
const char* password = "PASSWORD";
 
int ledPin = 16;
WiFiServer server(80);
 
void setup()
{
  // initialisation de la communication série
  Serial.begin(115200);
 
  delay(100);

  // initialisation de la sortie pour la led


  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
 
  // Connexion wifi
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
 
  WiFi.begin(ssid, password);

  // connection  en cours ...


  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }

  // Wifi connecter
  Serial.println("WiFi connecter");
 
  // Démmarrage du serveur.
  server.begin();
  Serial.println("Serveur demarrer !");
 
  // Affichage de l'adresse IP
  Serial.print("Utiliser cette adresse URL pour la connexion :");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");
 
}
 
void loop()
{
WiFiClient client;

 
  // Vérification si le client est connecter.
  client = server.available();
  if (!client)
  {
    return;
  }
 
  // Attendre si le client envoie des données ...
  Serial.println("nouveau client");
  while(!client.available()){
    delay(1);
  }
 
  String request = client.readStringUntil('\r');
  Serial.println(request);
  client.flush();

  int value = LOW;


  if (request.indexOf("/LED=ON") != -1)  {
    digitalWrite(ledPin, HIGH); // allumer la led
    value = HIGH;
  }
  if (request.indexOf("/LED=OFF") != -1)  {
    digitalWrite(ledPin, LOW); // éteindre la led
    value = LOW;
  }
 
  // Réponse
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println("");
  client.println("<!DOCTYPE HTML>");
  client.println("<html>");
 
  client.print("Etat de la led : ");
 
  if(value == HIGH) {
    client.print("On");
  } else {
    client.print("Off");
  }
  client.println("<br><br>");
  client.println("<a href=\"/LED=ON\"\"><button>Allumer </button></a>");
  client.println("<a href=\"/LED=OFF\"\"><button>Eteindre </button></a><br />");
  client.println("</html>");
 
  delay(1);
  Serial.println("Client deconnecter");
  Serial.println("");
 
}

You might also like