0% encontró este documento útil (0 votos)
16 vistas2 páginas

Boton Web Server

Código para conectar arduino esp32 a internet

Cargado por

emysamjasfriend
Derechos de autor
© © All Rights Reserved
Nos tomamos en serio los derechos de los contenidos. Si sospechas que se trata de tu contenido, reclámalo aquí.
Formatos disponibles
Descarga como TXT, PDF, TXT o lee en línea desde Scribd
0% encontró este documento útil (0 votos)
16 vistas2 páginas

Boton Web Server

Código para conectar arduino esp32 a internet

Cargado por

emysamjasfriend
Derechos de autor
© © All Rights Reserved
Nos tomamos en serio los derechos de los contenidos. Si sospechas que se trata de tu contenido, reclámalo aquí.
Formatos disponibles
Descarga como TXT, PDF, TXT o lee en línea desde Scribd
Está en la página 1/ 2

#include <WiFi.

h>
#define LED 2

String p="off";

// Pon los datos de tu red


const char *ssid = "SSID";
const char *password = "PASSWORD";

WiFiServer server(80);

void setup() {

pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);

Serial.begin(115200);
Serial.println();
Serial.println("Configurando");

WiFi.begin(ssid, password);

Serial.print("Conectandome");
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}

Serial.println();
Serial.print("Conectado, La dirección IP es: ");
Serial.println(WiFi.localIP());

server.begin();
Serial.println("Servidor iniciado");
}

void loop() {
WiFiClient client = server.available();

if (client) {
Serial.println("Nuevo cliente.");
String currentLine = "";
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);

if (c == '\n') {

if (currentLine.length() == 0) {
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-
width, initial-scale=1\">");
client.println("<link rel=\"icon\" href=\"data:,\">");

client.println("<style>html{font-family: Helvetica; display: inline-


block; margin: 0px auto; text-align: center;}");
client.println(".button {border: none;color: white;padding: 15px 32px;
text-align: center;");
client.println("text-decoration: none;display: inline-block;font-size:
16px; margin: 4px 2px;cursor: pointer;}");
client.println(".button1 {background-color: #4CAF50;} /* Green */");
client.println(".button2 {background-color: #008CBA;} /* Blue */");
client.println("</style></head>");

// Creación de botones
client.print("<body><h1>WebServer con ESP32</h1>");

if(p=="off"){
client.println("<button type='button' class='button button1'
onClick=location.href='/LED=ON'> ENCENDER </button><br><br>");
}
else{
client.println("<button type='button' class='button button2'
onClick=location.href='/LED=OFF'> APAGAR </button><br><br>");
}

client.print("</body></html>");
client.println();

break;
} else {
currentLine = "";
}
} else if (c != '\r') {
currentLine += c;
}

// Revisando el datos recibido del url


if (currentLine.indexOf("GET /LED=ON") != -1) {
digitalWrite(LED, HIGH);
p="on";
}
if (currentLine.indexOf("GET /LED=OFF") != -1) {
digitalWrite(LED, LOW);
p="off";
}
}
}

client.stop();
Serial.println("Cliente Desconectado");
}
}

También podría gustarte