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();
}
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.");
}
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.");
}
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
hardware (ESP32 ESPRESSIF using WIFI) to be out in the field anywhere.
I need to upload new code (sketch (.bin file)) to these ESP32 from a android device via an app that I will develope.
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.