#include "DHT.h"
#define PIN_DHT 2
#define TIPO_DHT DHT22
#define PIN_LED 8
#define PIN_BOTON 4
DHT sensor(PIN_DHT, TIPO_DHT);
float temperatura = 0;
float humedad = 0;
bool alarmaSilenciada = false;
bool lecturaValida = false;
bool estadoLED = false;
// Variables para antirrebote del boton
bool lecturaBotonAnterior = HIGH;
bool estadoBotonEstable = HIGH;
unsigned long tiempoAntirrebote = 0;
const unsigned long RETARDO_ANTIRREBOTE = 50;
unsigned long tiempoLectura = 0;
unsigned long tiempoParpadeo = 0;
unsigned long tiempoEnvio = 0;
void setup() {
Serial.begin(9600);
pinMode(PIN_LED, OUTPUT);
pinMode(PIN_BOTON, INPUT_PULLUP);
digitalWrite(PIN_LED, LOW);
sensor.begin();
Serial.println("MONITOREO AMBIENTAL INTELIGENTE");
Serial.println("Sistema iniciado");
Serial.println("-------------------------");
}
void loop() {
unsigned long ahora = millis();
// 1. Leer el boton con antirrebote
bool lecturaBoton = digitalRead(PIN_BOTON);
if (lecturaBoton != lecturaBotonAnterior) {
tiempoAntirrebote = ahora;
lecturaBotonAnterior = lecturaBoton;
}
if (ahora - tiempoAntirrebote >= RETARDO_ANTIRREBOTE) {
if (lecturaBoton != estadoBotonEstable) {
estadoBotonEstable = lecturaBoton;
// Una sola accion al presionar el boton
if (estadoBotonEstable == LOW) {
alarmaSilenciada = !alarmaSilenciada;
estadoLED = false;
digitalWrite(PIN_LED, LOW);
tiempoParpadeo = ahora;
if (alarmaSilenciada) {
Serial.println("Alarma silenciada");
} else {
Serial.println("Alarma reactivada");
}
}
}
}
// 2. Leer el sensor cada 2 segundos
if (ahora - tiempoLectura >= 2000) {
tiempoLectura = ahora;
float t = sensor.readTemperature();
float h = sensor.readHumidity();
lecturaValida = !isnan(t) && !isnan(h);
if (lecturaValida) {
temperatura = t;
humedad = h;
// Restablecer al volver a condiciones normales
if (temperatura <= 30 && humedad >= 40) {
alarmaSilenciada = false;
}
Serial.print("Temperatura: ");
Serial.print(temperatura);
Serial.println(" C");
Serial.print("Humedad: ");
Serial.print(humedad);
Serial.println(" %");
if (temperatura > 30 || humedad < 40) {
Serial.println("Estado: ALERTA AMBIENTAL");
} else {
Serial.println("Estado: NORMAL");
}
Serial.println("-------------------------");
} else {
Serial.println("Error al leer el sensor");
estadoLED = false;
digitalWrite(PIN_LED, LOW);
}
}
// 3. Determinar si existe una alerta
bool alerta = lecturaValida &&
(temperatura > 30 || humedad < 40);
bool alertaCritica = lecturaValida &&
(temperatura >= 35 || humedad < 25);
// 4. Controlar el LED segun la gravedad
if (!alerta || alarmaSilenciada) {
estadoLED = false;
digitalWrite(PIN_LED, LOW);
} else {
unsigned long intervalo;
if (temperatura >= 50 || humedad < 15) {
intervalo = 100; // Muy rapido
} else if (temperatura >= 40 || humedad < 20) {
intervalo = 200; // Rapido
} else if (alertaCritica) {
intervalo = 400; // Moderado-rapido
} else {
intervalo = 1000; // Lento
}
if (ahora - tiempoParpadeo >= intervalo) {
tiempoParpadeo = ahora;
estadoLED = !estadoLED;
digitalWrite(PIN_LED, estadoLED);
}
}
// 5. Simular envio de datos a plataforma IoT
if (lecturaValida && ahora - tiempoEnvio >= 5000) {
tiempoEnvio = ahora;
Serial.println("[PLATAFORMA IoT] Enviando datos:");
Serial.print("T=");
Serial.print(temperatura);
Serial.print(" C, H=");
Serial.print(humedad);
Serial.println(" %");
Serial.println("[PLATAFORMA IoT] Datos registrados");
Serial.println("-------------------------");
}
}