// Learn about the ESP32 WiFi simulation in
// https://docs.wokwi.com/guides/esp32-wifi
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define PIN_SENSOR 5
boolean estadoSensor = false;
boolean firstLoop = true;
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);
AsyncWebServer server(80);
void spinner() {
static int8_t counter = 0;
const char* glyphs = "\xa1\xa5\xdb";
LCD.setCursor(15, 1);
LCD.print(glyphs[counter++]);
if (counter == strlen(glyphs)) {
counter = 0;
}
}
void setup() {
pinMode(PIN_SENSOR, INPUT);
Serial.begin(115200);
LCD.init();
LCD.backlight();
LCD.setCursor(0, 0);
LCD.print("Connecting to ");
LCD.setCursor(0, 1);
LCD.print("WiFi ");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(250);
spinner();
}
Serial.println("");
Serial.println("WiFi conectado");
Serial.print("IP: ");
Serial.println(WiFi.localIP());
LCD.clear();
LCD.setCursor(0, 0);
LCD.println("Online");
delay(1000);
}
void loadServerRouter() {
server.on("/pir-status", HTTP_GET, [](AsyncWebServerRequest* request) {
Serial.println("ESP32 Web Server: New request received:");
Serial.println("GET /pir-status");
String estadoStr = "APAGADO";
if (estadoSensor) {
estadoStr = "ENCENDIDO";
}
request->send(200, "text/html", "<html><body><h1>Estado del sensor: " + estadoStr + "</h1></body></html>");
});
}
void loop() {
boolean valorActual = digitalRead(PIN_SENSOR);
if (firstLoop) {
if (valorActual) {
LCD.clear();
LCD.setCursor(0, 0);
LCD.println("Movimiento");
LCD.setCursor(0, 1);
LCD.println("detectado!");
estadoSensor = HIGH;
} else {
LCD.clear();
LCD.setCursor(0, 0);
LCD.println("No hay");
LCD.setCursor(0, 1);
LCD.println("movimiento");
estadoSensor = LOW;
}
firstLoop = false;
}
if (valorActual) {
if (!estadoSensor) {
LCD.clear();
LCD.setCursor(0, 0);
LCD.println("Movimiento");
LCD.setCursor(0, 1);
LCD.println("detectado!");
estadoSensor = HIGH;
}
} else {
if (estadoSensor) {
LCD.clear();
LCD.setCursor(0, 0);
LCD.println("No hay");
LCD.setCursor(0, 1);
LCD.println("movimiento");
estadoSensor = LOW;
}
}
}