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

Hello friends,

I’ll write in this topic about WiFi on ESP32, LED on/off, Static IP, SoftAP,…

0.- Board D1 R32 ESP32.
This is a cheap card ($ 5) with the dimensions and shape of the Arduino UNO, but with the ESP32. I used this card in the ESP32 and BLE tutorials.

Search images: D1 R32 ESP32

This card has classic Bluetooth, BLE, WiFi, ADC, DAC, and more feature.

This card can be programmed with the Arduino IDE.

base64_11

1.- Software.

I will use this library:
https://github.com/espressif/arduino-esp32/blob/master/libraries/WiFi/

I will start with this structure: A Router as an Access Point (it is not necessary to be connected to the Internet, it can work as local network), three Stations, clients of the Router. In ESP32 I will install the Web Server.

2.- Web Server in ESP32 with a web page.
Through this page we can turn on-off an LED. Check the status of the LED.

  • ESP32 Station of Router.
  • Install Web Server in ESP32, port 80.
  • Set a web page in Web Server.
  • We can turn on/off LED2 (LED2 is a BUILT-IN LED in Board) through a web page.
  • We can also check the status of LED2.
// Juan A. Villalpando.
// KIO4.COM
// Enciende y apaga LED. Botones.

#include <WiFi.h>
const char* ssid = "Name_WiFi_net";
const char* password = "Password_WiFi_net";

WiFiServer server(80); // Port 80

#define LED2  2    // LED2 is a Built-in LED.
String estado = "";
int wait30 = 30000; // time to reconnect when connection is lost.

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

// Connect WiFi net.
  Serial.println();
  Serial.print("Connecting with ");
  Serial.println(ssid);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("Connected with WiFi.");
 
  // Start Web Server.
  server.begin();
  Serial.println("Web Server started.");
 
  // Esta es la IP
  Serial.print("This is IP to connect to the WebServer: ");
  Serial.print("http://");
  Serial.println(WiFi.localIP());
}
 
void loop() {
// If disconnected, try to reconnect every 30 seconds.
  if ((WiFi.status() != WL_CONNECTED) && (millis() > wait30)) {
    Serial.println("Trying to reconnect WiFi...");
    WiFi.disconnect();
    WiFi.begin(ssid, password);
    wait30 = millis() + 30000;
  } 
  // 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();
}

Now in a Browser, we write the local IP of the server. We will get a page with three buttons, we can turn on-off and check the status of LED2.

3.- Web Server with Static IP.

This code is similar to the previous one. The difference is that I have configured the Web Server with a Static IP, in my example 192.168.1.115

// Juan A. Villalpando.
// KIO4.COM
// Enciende y apaga LED. Botones.

#include <WiFi.h>
const char* ssid = "Name_WiFi_net";
const char* password = "Password_WiFi_net";

// Setting Static IP.
        IPAddress local_IP(192, 168, 1, 115);
        IPAddress gateway(192, 168, 1, 1);
        IPAddress subnet(255, 255, 255, 0); 
        IPAddress primaryDNS(8, 8, 8, 8); //opcional 
        IPAddress secondaryDNS(8, 8, 4, 4); //opcional 

WiFiServer server(80); // Port 80

#define LED2  2    // LED2 is a Built-in LED.
String estado = "";
int wait30 = 30000; // time to reconnect when connection is lost.

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

// Setting Static IP.
  if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
    Serial.println("Error in configuration.");
  }

// Connect WiFi net.
  Serial.println();
  Serial.print("Connecting with ");
  Serial.println(ssid);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("Connected with WiFi.");
 
  // Start Web Server.
  server.begin();
  Serial.println("Web Server started.");
 
  // Esta es la IP
  Serial.print("This is IP to connect to the WebServer: ");
  Serial.print("http://");
  Serial.println(WiFi.localIP());
}
 
void loop() {
// If disconnected, try to reconnect every 30 seconds.
  if ((WiFi.status() != WL_CONNECTED) && (millis() > wait30)) {
    Serial.println("Trying to reconnect WiFi...");
    WiFi.disconnect();
    WiFi.begin(ssid, password);
    wait30 = millis() + 30000;
  } 
  // 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();
}

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]

softap

esp32_softap

// 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();
}

5.- App sends on/off LED12 and LED14. WiFi. WebServer. Static IP.

p125_wemos_led2.aia (2.4 KB)

  • ESP32 is a Station, client of Router.
  • Static IP 192.168.1.115
  • Web Server, port 80.
  • LED12, LED14 on/off
  • Check status LEDs.

// Juan A. Villalpando.
// KIO4.COM
// Enciende y apaga LED. Botones.

#include <WiFi.h>
 
const char* ssid = "Nombre_red_WiFi";
const char* password = "contraseña_red";

// Setting Static IP.
        IPAddress local_IP(192, 168, 1, 115);
        IPAddress gateway(192, 168, 1, 1);
        IPAddress subnet(255, 255, 255, 0); 
        IPAddress primaryDNS(8, 8, 8, 8); //opcional 
        IPAddress secondaryDNS(8, 8, 4, 4); //opcional 

WiFiServer server(80); // Port 80

#define LED12  12    // LED12
#define LED14  14    // LED14
String estado = "";
int wait30 = 30000; // time to reconnect when connection is lost.

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

// Setting Static IP.
  if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
    Serial.println("Error in configuration.");
  }

// Connect WiFi net.
  Serial.println();
  Serial.print("Connecting with ");
  Serial.println(ssid);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("Connected with WiFi.");
 
  // Start Web Server.
  server.begin();
  Serial.println("Web Server started.");
 
  // This is IP
  Serial.print("This is IP to connect to the WebServer: ");
  Serial.print("http://");
  Serial.println(WiFi.localIP());
}
 
void loop() {
// If disconnected, try to reconnect every 30 seconds.
  if ((WiFi.status() != WL_CONNECTED) && (millis() > wait30)) {
    Serial.println("Trying to reconnect WiFi...");
    WiFi.disconnect();
    WiFi.begin(ssid, password);
    wait30 = millis() + 30000;
  } 
  // 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("on12") != -1) {digitalWrite(LED12, HIGH); estado = "LED12 ON";}
       if (req.indexOf("off12") != -1){digitalWrite(LED12, LOW); estado = "LED12 OFF";}
       if (req.indexOf("on14") != -1) {digitalWrite(LED14, HIGH); estado = "LED14 ON";}
       if (req.indexOf("off14") != -1){digitalWrite(LED14, LOW); estado = "LED14 OFF";}
       if (req.indexOf("consulta") != -1){
           estado ="";
           if (digitalRead(LED12) == HIGH) {estado = "LED12 ON,";} else {estado = "LED12 OFF,";}
           if (digitalRead(LED14) == HIGH) {estado = estado + "LED14 ON";} else {estado = estado + "LED14 OFF";}
           }
           
//////////////////////////////////////////////
  // Página WEB. ////////////////////////////
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println(""); //  Comillas importantes.
  client.println(estado); //  Return status.

  client.flush();
  client.stop();
  Serial.println("Client disconnected.");
}

6.- App get status of 2 Push Buttons. WiFi. WebServer. Static IP.

  • ESP32 is a Station, client of Router.
  • Static IP 192.168.1.115
  • Web Server, port 80.
  • Check status Push Buttons.

esp32_wifi_pulsadores2

// Juan A. Villalpando.
// KIO4.COM
// Estado de dos pulsadores

#include <WiFi.h>
const char* ssid = "Nombre_de_tu_red_wifi";
const char* password = "La_clave_de_tu_red_wifi";

// Setting Static IP.
        IPAddress local_IP(192, 168, 1, 115);
        IPAddress gateway(192, 168, 1, 1);
        IPAddress subnet(255, 255, 255, 0); 
        IPAddress primaryDNS(8, 8, 8, 8); //opcional 
        IPAddress secondaryDNS(8, 8, 4, 4); //opcional 

WiFiServer server(80); // Port 80

#define pulsa16 16   // PushButton 16.
#define pulsa17 17   // PushButton 17.
String estado = "";
int wait30 = 30000; // time to reconnect when connection is lost.

void setup() {
  Serial.begin(115200);
  pinMode(pulsa16, INPUT);
  pinMode(pulsa17, INPUT);

// Setting Static IP.
  if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
    Serial.println("Error in configuration.");
  }

// Connect WiFi net.
  Serial.println();
  Serial.print("Connecting with ");
  Serial.println(ssid);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("Connected with WiFi.");
 
  // Start Web Server.
  server.begin();
  Serial.println("Web Server started.");
 
  // This is IP
  Serial.print("This is IP to connect to the WebServer: ");
  Serial.print("http://");
  Serial.println(WiFi.localIP());
}
 
void loop() {
// If disconnected, try to reconnect every 30 seconds.
  if ((WiFi.status() != WL_CONNECTED) && (millis() > wait30)) {
    Serial.println("Trying to reconnect WiFi...");
    WiFi.disconnect();
    WiFi.begin(ssid, password);
    wait30 = millis() + 30000;
  } 
  // 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("consulta") != -1){
           estado ="";
           if (digitalRead(pulsa16) == HIGH) {estado = "PushBtn16 ON,";} else {estado = "PushBtn16 OFF,";}
           if (digitalRead(pulsa17) == HIGH) {estado = estado + "PushBtn17 ON";} else {estado = estado + "PushBtn17 OFF";}
           }
           
//////////////////////////////////////////////
  // Página WEB. ////////////////////////////
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println(""); //  Comillas importantes.
  client.println(estado); //  Return status.

  client.flush();
  client.stop();
  Serial.println("Client disconnected.");
}			

ooooooooooooooooooo0000000oooooooooooooooooooo

  • Instead of putting a Button we can put a Clock.

esp32_wifi_pulsadores3

7.- App sends a message to ESP32. Screen LCD with I2C. WebServer. Static IP.

  • ESP32 is a Station, client of Router.
  • Static IP 192.168.1.115
  • Web Server, port 80.
  • Write a message. Message shows in Screen LCD and Serial Monitor.

  • If message is: on12, off12, on14, off14 then ON/OFF LED12 and LED14.

  • Check status of LEDS

  • Library for LCD with ESP32:
    http://kio4.com/arduino/140_Wemos_LCD.htm

Search images LCD I2C .

esp32_wifi_lcd3

// Juan A. Villalpando.
// KIO4.COM
// Enciende y apaga LED. Botones.LCD I2C.

#include <WiFi.h>
const char* ssid = "Nombre_de_tu_red_wifi";
const char* password = "La_clave_de_tu_red_wifi";

// Setting Static IP.
        IPAddress local_IP(192, 168, 1, 115);
        IPAddress gateway(192, 168, 1, 1);
        IPAddress subnet(255, 255, 255, 0); 
        IPAddress primaryDNS(8, 8, 8, 8); //opcional 
        IPAddress secondaryDNS(8, 8, 4, 4); //opcional 

WiFiServer server(80); // Port 80

#define LED12  12    // LED12
#define LED14  14    // LED14
String estado = "";
int wait30 = 30000; // time to reconnect when connection is lost.

#include <LiquidCrystal_I2C.h>
int columnas = 16;
int filas = 2;
LiquidCrystal_I2C lcd(0x27, columnas, filas);  
// LiquidCrystal_I2C lcd(0x3F, columnas, filas); 

void setup() {
  Serial.begin(115200);
  pinMode(LED12, OUTPUT);
  pinMode(LED14, OUTPUT);
  lcd.init();                     
  lcd.backlight();

// Setting Static IP.
  if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
    Serial.println("Error in configuration.");
  }

// Connect WiFi net.
  Serial.println();
  Serial.print("Connecting with ");
  Serial.println(ssid);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("Connected with WiFi.");
 
  // Start Web Server.
  server.begin();
  Serial.println("Web Server started.");
 
  // This is IP
  Serial.print("This is IP to connect to the WebServer: ");
  Serial.print("http://");
  Serial.println(WiFi.localIP());
}
 
void loop() {
// If disconnected, try to reconnect every 30 seconds.
  if ((WiFi.status() != WL_CONNECTED) && (millis() > wait30)) {
    Serial.println("Trying to reconnect WiFi...");
    WiFi.disconnect();
    WiFi.begin(ssid, password);
    wait30 = millis() + 30000;
  } 
  // 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 information sends by client.
  String req = client.readStringUntil('\r');
  Serial.println(req);
  req.replace("+", " ");          // Para que los espacios no salgan con +
  req.replace(" HTTP/1.1", "");   // Para quitar HTTP/1.1
  req.replace("GET /", "");       // Para quitar GET /

  lcd.clear(); // Clear Screen
  lcd.setCursor(0, 0); // Start cursor
  lcd.print("Mensaje");
  lcd.setCursor(0,1); // Cursor other line.
  lcd.print(req);

  // Make the client's request.
       if (req.indexOf("on12") != -1) {digitalWrite(LED12, HIGH); estado = "LED12 ON";}
       if (req.indexOf("off12") != -1){digitalWrite(LED12, LOW); estado = "LED12 OFF";}
       if (req.indexOf("on14") != -1) {digitalWrite(LED14, HIGH); estado = "LED14 ON";}
       if (req.indexOf("off14") != -1){digitalWrite(LED14, LOW); estado = "LED14 OFF";}
       if (req.indexOf("consulta") != -1){
           estado ="";
           if (digitalRead(LED12) == HIGH) {estado = "LED12 ON,";} else {estado = "LED12 OFF,";}
           if (digitalRead(LED14) == HIGH) {estado = estado + "LED14 ON";} else {estado = estado + "LED14 OFF";}
           }
           
//////////////////////////////////////////////
  // Página WEB. ////////////////////////////
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println(""); //  Comillas importantes.
  client.println(estado); //  Return status.

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

8.- Now everything together. Proposal.

Create an app:

  • With 4 Buttons to turn on-off LED12 and LED13.
  • A TextBox to send a message and display it on the LCD I2C.
  • Two Buttons to check the status of the LEDs and the PushButtons.

p125wemos_LCD_mensaje_Propuesta.aia (2.5 KB) (unfinished)

9.- Get the value of a potentiometer. AnilogRead.

  • ESP32 is a Station, client of Router.
  • Static IP 192.168.1.115
  • Web Server, port 80.
  • We can get the value of a potentiometer manually by means of a Button or automatically by means of a Clock with Interval = 500

p125wemos_Potenciometro.aia (2.1 KB)

esp32_wifi_potenciometro2

Variables:
valor, get values from 0 to 4095 (default resolution is 2 ^12)
valor_map, map valor from 0 to 330
Ent_Anilog, anilogic input is terminal 34.

Careful: ESP 32 has two ADC SAR, when we work with WiFi we can only use the SAR ADC1 (GPIOs 32 - 39). Read this.

// Juan A. Villalpando.
// http://kio4.com/arduino/212_Wemos_ServoPotenciometro.htm
// Potenciometro.

#include <WiFi.h>

const char* ssid = "Nombre_Red";
const char* password = "Contraseña_Red";

// Setting Static IP.
        IPAddress local_IP(192, 168, 1, 115);
        IPAddress gateway(192, 168, 1, 1);
        IPAddress subnet(255, 255, 255, 0); 
        IPAddress primaryDNS(8, 8, 8, 8); //opcional 
        IPAddress secondaryDNS(8, 8, 4, 4); //opcional 

WiFiServer server(80); // Port 80

int wait30 = 30000; // time to reconnect when connection is lost.

const int Ent_Anilogica = 34; // analogic input.
int valor = 0; // valor from 0 to 4095
float valor_map = 0; // map valor from 0 to 330

void setup() {
  Serial.begin(115200);

// Setting Static IP.
  if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
    Serial.println("Error in configuration.");
  }

// Connect WiFi net.
  Serial.println();
  Serial.print("Connecting with ");
  Serial.println(ssid);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("Connected with WiFi.");
 
  // Start Web Server.
  server.begin();
  Serial.println("Web Server started.");
 
  // This is IP
  Serial.print("This is IP to connect to the WebServer: ");
  Serial.print("http://");
  Serial.println(WiFi.localIP());
}
 
void loop() {
// If disconnected, try to reconnect every 30 seconds.
  if ((WiFi.status() != WL_CONNECTED) && (millis() > wait30)) {
    Serial.println("Trying to reconnect WiFi...");
    WiFi.disconnect();
    WiFi.begin(ssid, password);
    wait30 = millis() + 30000;
  } 
  // 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("consulta") != -1){
       valor = analogRead(Ent_Anilogica);
       valor_map = map(valor, 0, 4095, 0, 330);
       Serial.println(valor);
           }
           
//////////////////////////////////////////////
  // Página WEB. ////////////////////////////
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println(""); //  Comillas importantes.
  client.println(valor_map/100); //  Return value.

  //client.flush();
  client.stop();
  Serial.println("Client disconnected.");
}      

10.- Web Server. Web page in SdCard.

  • In the previous examples the web page was "inside" the code, in this example the web page will be a file saved in the SdCard.