Skip to content

Instantly share code, notes, and snippets.

@gsampallo
Created July 10, 2020 16:31
Show Gist options
  • Save gsampallo/9cf807bc4b14fdf2e8b46b60cebce68c to your computer and use it in GitHub Desktop.
Save gsampallo/9cf807bc4b14fdf2e8b46b60cebce68c to your computer and use it in GitHub Desktop.
DPRE - Crear un servidor web con NodeMCU/ESP8266 y que responda solicitudes.
#include <ESP8266WiFi.h>
const char* ssid = "NOMBRE_RED";
const char* password = "PASSWORD_RED";
WiFiServer servidor(80);
void setup() {
Serial.begin(115200);
WiFi.begin(ssid,password);
Serial.print("Conectando");
while(WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}
Serial.println("Conectado");
Serial.print("Direccion IP: ");
Serial.println(WiFi.localIP());
servidor.begin();
}
void loop() {
WiFiClient cliente = servidor.available();
if(!cliente) {
return;
}
Serial.println("Se conecto un cliente");
while(!cliente.available()) {
delay(1);
}
String request = cliente.readStringUntil('\r');
Serial.print("Requisito: ");
Serial.println(request);
cliente.flush();
String respuesta = "Hola mundo";
if(request.indexOf("/uptime") != -1) {
respuesta = "UPTIME = "+String(millis());
}
cliente.print(respuesta);
delay(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment