Wiki

Reso-nance numérique | Arts et cultures libres

Outils du site


Panneau latéral

materiel:esp8266:webserver:accueil

ESP8266 : Serveur Web

Serveur Web #1

Éteindre ou allumer une LED avec le navigateur Web. L'ESP8266 crée un réseau Wi-Fi, sur lequel va se connecter un ordinateur ou un téléphone mobile. Il suffit ensuite d'ouvrir le navigateur Web et d'entrer dans la barre d'adresse, l'adresse suivante : http://192.168.4.1/OFF pour éteindre la LED ou bien http://192.168.4.1/ON pour l'allumer.

/*
 *  ESP8266 - Web Server
 *  
 *  Connect your computer to the Wi-Fi Network "Bonjour_IOT" with "12345678" password
 *  Open your favorite Web browser and enter "192.168.4.1/OFF" to switch off the Builtin LED or
 *  192.168.4.1/ON" to switch it on
 *  
 */
 
#include <ESP8266WiFi.h>
 
WiFiServer server(80); // Initialize the server on Port 80
 
int LED_PIN = 2;
 
void setup() {
  pinMode(LED_PIN, OUTPUT);
  WiFi.mode(WIFI_AP); // Our ESP8266-12E is an AccessPoint
  WiFi.softAP("Hello_IOT", "12345678"); // Provide the SSID and password
  server.begin(); // Start the HTTP Server
  Serial.begin(115200); // Start communication between the ESP8266-12E and the monitor window
  IPAddress HTTPS_ServerIP= WiFi.softAPIP(); // Obtain the IP of the Server
  Serial.println();
  Serial.print("Server IP is: "); // Print the IP to the monitor window
  Serial.println(HTTPS_ServerIP);
}
 
void loop() {
  WiFiClient client = server.available();
 
  if (!client) {
    return;
  }
 
  Serial.println("Somebody has connected :)");
 
  //Read what the browser has sent into a String class and print the request to the monitor
  //String request = client.readString();
  String request = client.readStringUntil('\r');
 
  //Looking under the hood
  Serial.println(request);
 
  // Handle the Request
  if (request.indexOf("/OFF") != -1) digitalWrite(LED_PIN, HIGH);
  else if (request.indexOf("/ON") != -1) digitalWrite(LED_PIN, LOW);
 
  client.flush(); //clear previous info in the stream
  client.print("ok"); // Send the response to the client
  delay(10);
  Serial.println("Client disconnected"); // Looking under the hood          
}

Serveur Web #2

Même chose avec un peu plus de contrôle.

/*
 * Access Point (AP) Web Server
 * 
 * This example demonstrates how to turn the ESP8266 into an access point (AP), and serve up web pages to any connected client.
 * 
 * From https://learn.sparkfun.com/tutorials/esp8266-thing-development-board-hookup-guide/example-sketch-web-server
*/
 
#include <ESP8266WiFi.h>
 
// Pin Definitions
const int LED_PIN = 2; // LED
const int ANALOG_PIN = A0; // The only analog pin on the Thing
const int DIGITAL_PIN = 12; // Digital pin to be read
 
WiFiServer server(80);
 
void setup() 
{
  // Pins
  Serial.begin(115200);
  pinMode(DIGITAL_PIN, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW);
 
  // WiFi
  WiFi.mode(WIFI_AP);
  WiFi.softAP("Hello_IoT", "12345678"); // Provide the (SSID, password); .
  server.begin();
}
 
void loop() 
{
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
 
  // Read the first line of the request
  String req = client.readStringUntil('\r');
  Serial.println(req);
  client.flush();
 
  // Match the request
  int val = -1; // We'll use 'val' to keep track of both the
                // request type (read/set) and value if set.
  if (req.indexOf("/led/0") != -1)
    val = 1; // Will write LED low
  else if (req.indexOf("/led/1") != -1)
    val = 0; // Will write LED high
  else if (req.indexOf("/read") != -1)
    val = -2; // Will print pin reads
  // Otherwise request will be invalid. We'll say as much in HTML
 
  // Set GPIO5 according to the request
  if (val >= 0)
    digitalWrite(LED_PIN, val);
 
  client.flush();
 
  // Prepare the response. Start with the common header:
  String s = "HTTP/1.1 200 OK\r\n";
  s += "Content-Type: text/html\r\n\r\n";
  s += "<!DOCTYPE HTML>\r\n<html>\r\n";
  // If we're setting the LED, print out a message saying we did
  if (val >= 0)
  {
    s += "LED is now ";
    s += (val)?"on":"off";
  }
  else if (val == -2)
  { // If we're reading pins, print out those values:
    s += "Analog Pin = ";
    s += String(analogRead(ANALOG_PIN));
    s += "<br>"; // Go to the next line.
    s += "Digital Pin 12 = ";
    s += String(digitalRead(DIGITAL_PIN));
  }
  else
  {
    s += "Invalid Request.<br> Try /led/1, /led/0, or /read.";
  }
  s += "</html>\n";
 
  // Send the response to the client
  client.print(s);
  delay(1);
  Serial.println("Client disconnected");
 
  // The client will actually be disconnected 
  // when the function returns and 'client' object is detroyed
}
/home/resonancg/www/wiki/data/pages/materiel/esp8266/webserver/accueil.txt · Dernière modification: 2018/01/25 21:38 de resonance