#include <DHT.h>
#include <Servo.h>
#define DHTPIN 2 // Pin de datos del sensor DHT22
#define DHTTYPE DHT22 // Tipo de sensor DHT22
#define FAN_PIN 3 // Pin del LED que simula el ventilador
#define WINDOW1_PIN 9 // Pin del servomotor para la ventana 1
#define WINDOW2_PIN 10 // Pin del servomotor para la ventana 2
DHT dht(DHTPIN, DHTTYPE);
Servo window1;
Servo window2;
void setup() {
Serial.begin(9600);
dht.begin();
pinMode(FAN_PIN, OUTPUT);
digitalWrite(FAN_PIN, LOW); // Ventilador apagado inicialmente
window1.attach(WINDOW1_PIN);
window2.attach(WINDOW2_PIN);
// Inicialmente, ventanas cerradas
window1.write(0);
window2.write(0);
}
void loop() {
delay(2000); // Esperar entre lecturas
// Leer temperatura y humedad
float h = dht.readHumidity();
float t = dht.readTemperature();
// Verificar si la lectura falló
if (isnan(h) || isnan(t)) {
Serial.println("Error al leer el sensor DHT22.");
return;
}
// Mostrar en el monitor serial
Serial.print("Humedad: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperatura: ");
Serial.print(t);
Serial.println(" *C");
// Controlar ventilador (LED)
if (t > 30) {
digitalWrite(FAN_PIN, HIGH); // Encender ventilador
} else {
digitalWrite(FAN_PIN, LOW); // Apagar ventilador
}
// Controlar ventanas (servomotores)
if (t > 25) {
window1.write(90); // Abrir ventana 1
window2.write(90); // Abrir ventana 2
} else {
window1.write(0); // Cerrar ventana 1
window2.write(0); // Cerrar ventana 2
}
if (h > 60) {
// Si la humedad es alta, abrir las ventanas
window1.write(90);
window2.write(90);
} else {
// Si la humedad es baja, mantener las ventanas cerradas
window1.write(0);
window2.write(0);
}
}