ESP32. WiFi. WebServer. LED on/off. Static IP. Soft Access Point

4.- ESP32 Soft Access Point. Web Server. Web page on/off LED2. Check status LED2.

Now ESP32 is an Acces Point. ESP32 creates the network 192.168.4.X
I have named this network "juan".
It is independent of the Router.
If we want to connect with this ESP32 we must change the WiFi configuration of our mobile. [Setting / WiFi ]
We must connect to the network "juan" password "123456789".
[If our mobile is connected to the Router (192.168.1.X), we cannot connect to ESP32, we must change our mobile to the 192.168.4.X network]

// Juan A. Villalpando.
// KIO4.COM
// Creación de un Punto de Acceso.

#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiAP.h>
 
const char* ssid     = "Juan";
const char* password = "123456789";

WiFiServer server(80); // Port 80

#define LED2  2    // LED2 is a Built-in LED.
String estado = "";

void setup() {
  Serial.begin(115200);
  pinMode(LED2, OUTPUT);

  // Conecta a la red wifi.
  Serial.println();
  Serial.print("Setting Access Point:  ");
  Serial.println(ssid);
 
  WiFi.softAP(ssid, password);
  IPAddress myIP = WiFi.softAPIP();

  // Esta es la IP
  Serial.print("This is IP to connect to the WebServer: ");
  Serial.print("http://");
  Serial.println(myIP);

  // Start Web Server.
  server.begin();
  Serial.println("Web Server started.");
}
 
void loop() {
  // Check if a client has connected..
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
   
  Serial.print("New client: ");
  Serial.println(client.remoteIP());
   
  // Espera hasta que el cliente envíe datos.
  // while(!client.available()){ delay(1); }

  /////////////////////////////////////////////////////
  // Read the information sent by the client.
  String req = client.readStringUntil('\r');
  Serial.println(req);

  // Make the client's request.
       if (req.indexOf("on2") != -1) {digitalWrite(LED2, HIGH); estado = "ON";}
       if (req.indexOf("off2") != -1){digitalWrite(LED2, LOW); estado = "OFF";}
     if (req.indexOf("consulta") != -1){
         if (digitalRead(LED2)){estado = "LED2 now is ON";}
         else {estado = "LED2 now is OFF";}
          }
     
  //////////////////////////////////////////////
  //  WEB PAGE. ////////////////////////////
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println(""); //  Important.
  client.println("<!DOCTYPE HTML>");
  client.println("<html>");
  client.println("<head><meta charset=utf-8></head>");
  client.println("<body><center><font face='Arial'>");
  client.println("<h1>Servidor web con ESP32.</h1>");
  client.println("<h2><font color='#009900'>KIO4.COM - Juan A. Villalpando</font></h2>");
  client.println("<h3>Página web.</h3>");
  client.println("<br><br>");
  client.println("<a href='on2'><button>Click to ON LED2</button></a>");
  client.println("<a href='off2'><button>Click to OFF LED2</button></a>");
  client.println("<a href='consulta'><button>Consult status LED2</button></a>");
  client.println("<br><br>");
  client.println(estado);
  client.println("</font></center></body></html>");

  Serial.print("Client disconnected: ");
  Serial.println(client.remoteIP());
  client.flush();
  client.stop();
}
1 Like