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

3 Likes

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.

1 Like

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.

  • If we wanted to obtain the source code of the page, we would put these blocks:
    esp32_web
    esp32_web2
1 Like

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

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

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.");
}
1 Like

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

1 Like

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.");
}			
			
2 Likes

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)

3 Likes

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.");
}      
1 Like

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.

1 Like

I want these apps work offline only with arduino and esp
Thank you

In the page I am sending from
question Why did you add a Sd Card to the setup
Was it because you the do not have to add the info on the SD Card into ever sketch you write to the ESP32.
other wise all the code for the WebServer would be lost if it was not included in the next sketch?
If so how does it know to load the WebSever details before uploading the next sketch?
I hope this make sence
Keven G

11.- ESP32 Soft Access Point. Web Server. Buttons on/off LED12 and LED14. Check status.

In
4.- ESP32 Soft Access Point. Web Server. Web page on/off LED2. Check status LED2.
we saw how to create an Access Point and turn on/off an LED from a web page.

In
5.- App sends on/off LED12 and LED14. WiFi. WebServer. Static IP.
we saw how to create a Router client and turn on/off LEDs from the App buttons.

Now we are going to use that two example. We are going to create an Access Point and turn on/off two LEDs using the application buttons.


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

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

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);

// 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() {
// If disconnected, try to reconnect every 30 seconds.
  if ((WiFi.status() != WL_CONNECTED) && (millis() > wait30)) {
    Serial.println("Trying to set softAP again...");
    WiFi.softAP(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.");
}

p125_wemos_led2_SoftAP.aia (2.4 KB)

  • Build and install the application (Do not use MIT Companion).
  • Configure your device for the WiFi network "Juan" (123456789)

Hello,
Firstly I thank you for the super quick response.
I do believe you have answered my question.
my understanding is (not using SD Card setup).
is that every time you upload a sketch it must also include the code for the Web Server other wise there will be no Web Server to carry out the code of the sketch you have just uploaded.
Correct
wildhorse

to explain my situation is that I

  1. hardware (ESP32 ESPRESSIF using WIFI) to be out in the field anywhere.
  2. I need to upload new code (sketch (.bin file)) to these ESP32 from a android device via an app that I will develope.
  3. these uploads could happen 1 x year or up to 10 x year.
    I hope this explains my questions
    thanks
  • Try loading the Web Server code in the memory of the ESP32 and the web page in a SdCard.

  • If you can access the Internet, try loading a page with FRAME and the FRAME content on an external website.

  • Find information about OTA (Over The Air), you load a code in ESP32 from PC, the next update of that code can be done by web, if you connect to PC. Example: upload a Sketch: blink from PC, now you can update all blink code from WiFi without connecting ESP to PC.

In this tutorial in Spanish, I wrote an OTA example:
http://kio4.com/arduino/230_Wemos_OTA.htm

1 Like

returns this
GET /favicon.ico HTTP/1.1
I'm guessing this isn't correct. I was expecting it include "on2" or "off2"
can you help with this??

Does this example work for you?

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

Hi i need help to build a code for WifiCam.aia in my esp32 cam so it can turn ON/OFF the LED (Flash) can be connected to the MIT inventor app

Here examples with ESP32 Cam.