#include <WiFi.h>
#include <WebServer.h>
//Conectado a la red WiFi
//Direccion IP: 192.168.121.48
//Servidor HTTP iniciado
//Conectando a Galaxy A13 04B4 esto debe salir despues de darle click a reset
.
// Configuración de la red WiFi
const char* ssid = "Galaxy A13 04B4";
const char* password = "12345678";
// Configuración del servidor web
WebServer server(80);
// Pin del potenciómetro
const int potPin = 32;
void setup() {
// Inicializar el monitor serial
Serial.begin(9600);
// Conectar a la red WiFi
Serial.print("Conectando a ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("");
Serial.println("Conectado a la red WiFi");
Serial.print("Dirección IP: ");
Serial.println(WiFi.localIP());
// Configurar el servidor web
server.on("/", handleRoot);
server.on("/potValue", handlePotValue);
server.begin();
Serial.println("Servidor HTTP iniciado");
}
void loop() {
// Manejar las solicitudes del servidor web
server.handleClient();
}
void handleRoot() {
// Generar la respuesta HTML con JavaScript para actualizar el valor
String html = "<html><body><h1>Medicion del Potenciometro</h1>";
html += "<p>Valor: <span id='potValue'>0</span></p>";
html += "<script>";
html += "setInterval(function() {";
html += " fetch('/potValue').then(response => response.text()).then(data => {";
html += " document.getElementById('potValue').innerText = data;";
html += " });";
html += "}, 1000);"; // Actualizar cada segundo
html += "</script>";
html += "</body></html>";
// Enviar la respuesta al cliente
server.send(200, "text/html", html);
}
void handlePotValue() {
// Leer el valor del potenciómetro
int potValue = analogRead(potPin);
// Enviar el valor como respuesta
server.send(200, "text/plain", String(potValue));
}