#include <WiFi.h>
//------------------Servidor Web en puerto 80---------------------
WiFiServer server(80);
//---------------------Credenciales de WiFi-----------------------
const char* ssid = "LSKJ-804";
const char* password = "LSKJ804vivaldi*";
//---------------------VARIABLES GLOBALES-------------------------
int contconexion = 0;
const int analoga = 34;
const int LED_1 = 2;
const int LED_2 = 4;
int senal;
String currentLine = "";
String header; // Variable para guardar el HTTP request
String estadoSalida = "off";
//------------------------CODIGO HTML------------------------------
String paginaInicio = "<!DOCTYPE html>"
"<html>"
"<head>"
"<meta charset='utf-8' />"
"<META HTTP-EQUIV='Refresh' CONTENT='5'>"
"<title>Servidor Web ESP32</title>"
"</head>"
"<body>"
"<center>"
"<h3 style='font-size:10rem; color: blue;'>Servidor Web ESP32</h3>"
"<p><a href='/on'><button style='font-size:7rem;height:200px;width:400px'>ON</button></a></p>"
"<p><a href='/off'><button style='font-size:7rem;height:200px;width:400px'>OFF</button></a></p>"
"<p style='font-size:7rem; color: red; background-color: #B7B7B7;'>Temperatura:</p>"
"<p style='font-size:5rem; color: orange;'>";
String paginaFin = "</p></center>"
"</body>"
"</html>";
//---------------------------SETUP--------------------------------
void setup() {
//Declaramos los pines como entradas y salidas
pinMode(analoga, INPUT);
pinMode(LED_1, OUTPUT);
pinMode(LED_2, OUTPUT);
Serial.begin(115200);
Serial.println("");
//Empezamos conectándonos a una red WiFi network
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
}
//----------------------------LOOP----------------------------------
void loop(){
senal = analogRead(analoga);
WiFiClient client = server.available(); // Escucha a los clientes entrantes
if (client) { // Si se conecta un nuevo cliente
Serial.println("New Client.");
// //
while (client.connected()) { // loop mientras el cliente está conectado
if (client.available()) { // si hay bytes para leer desde el cliente
char c = client.read(); // lee un byte
Serial.write(c); // imprime ese byte en el monitor serial
header += c;
if (c == '\n') { // si el byte es un caracter de salto de linea
// si la nueva linea está en blanco significa que es el fin del
// HTTP request del cliente, entonces respondemos:
if (currentLine.length() == 0) {
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
// enciende y apaga el LED
if (header.indexOf("GET /on") >= 0) {
Serial.println("LED on");
estadoSalida = "on";
digitalWrite(LED_1, HIGH);
} else if (header.indexOf("GET /off") >= 0) {
Serial.println("LED off");
estadoSalida = "off";
digitalWrite(LED_1, LOW);
}
// Muestra la página web
client.println(paginaInicio + String(senal) + paginaFin);
// la respuesta HTTP temina con una linea en blanco
client.println();
break;
} else { // si tenemos una nueva linea limpiamos currentLine
currentLine = "";
}
} else if (c != '\r') { // si C es distinto al caracter de retorno de carro
currentLine += c; // lo agrega al final de currentLine
}
}
}
header = ""; // Limpiamos la variable header
// Cerramos la conexión
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
}