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

