//Incluimos las librerias
#include "DHTesp.h"
#include <LiquidCrystal_I2C.h>
#include <ESP32Servo.h>
//Instanciamos el DHT
DHTesp dht;
//Decaramos el variable que almacena el pin a conectar el DHT11
int pinDHT = 27;
// Define los pines para el sensor PIR y el LED
const int pirPin = 14;
const int ledPin = 5;
//Pin del Servomotor
const int servoPin1 = 13;
const int servoPin2 = 12;
LiquidCrystal_I2C lcd(0x27, 20, 4);
Servo myServo1;
Servo myServo2;
void setup()
{
Serial.begin(115200);
//Inicializamos el dht
dht.setup(pinDHT, DHTesp::DHT22);
lcd.begin(20, 4);
lcd.clear();
lcd.print("Temperatura");
lcd.setCursor(0,1);
lcd.print ("Humedad");
delay(500);
myServo1.attach(servoPin1);
myServo2.attach(servoPin2);
// Inicializa el sensor PIR como entrada
pinMode(pirPin, INPUT);
// Inicializa el LED como salida
pinMode(ledPin, OUTPUT);
}
void loop() {
//Obtenemos el arreglo de datos (humedad y temperatura)
TempAndHumidity data = dht.getTempAndHumidity();
//Mostramos los datos de la temperatura y humedad
Serial.println("Temperatura: " + String(data.temperature, 2) + "°C");
Serial.println("Humedad: " + String(data.humidity, 1) + "%");
Serial.println("---");
lcd.setCursor(0, 0);
lcd.print("Temp:" + String(data.temperature, 1) +" C ");
lcd.setCursor(0, 1);
lcd.print("Humedad: " + String(data.humidity, 1) + " % ");
// Lee el valor del sensor PIR
int pirValue = digitalRead(pirPin);
if(data.humidity < 20 && data.temperature > 40)
{
// Gira el servomotor 20 grados en sentido horario
myServo1.write(120);
myServo2.write(40);
// Espera un momento para que el servomotor alcance la posición deseada
delay(1000);
lcd.init();
lcd.backlight();
lcd.clear(); // clear display
lcd.setCursor(0, 0); // move cursor to (0, 0)
lcd.print("Temp:" + String(data.temperature, 1) +" C "); // print message at (0, 0)
lcd.setCursor(0, 1); // move cursor to (2, 1)
lcd.print("Temp:" + String(data.temperature, 1) +" C "); // print message at (2, 1)
delay(2000); // display the above for two
lcd.init();
lcd.backlight();
lcd.clear(); // clear display
lcd.setCursor(0, 2); // move cursor to (0, 0)
lcd.print("Servo1 a 120°"); // print message at (0, 0)
lcd.setCursor(0, 3); // move cursor to (0, 0)
lcd.print("Servo1 a 40°"); // print message at (0, 0)
delay(2000); // display the above for two seconds
}
else if(data.humidity < 20 || data.humidity >10 && data.temperature > 40)
{
// Gira el servomotor 20 grados en sentido horario
myServo1.write(35);
myServo2.write(160);
// Espera un momento para que el servomotor alcance la posición deseada
delay(1000);
lcd.init();
lcd.backlight();
lcd.clear(); // clear display
lcd.setCursor(0, 0); // move cursor to (0, 0)
lcd.print("Temp:" + String(data.temperature, 1) +" C "); // print message at (0, 0)
lcd.setCursor(0, 1); // move cursor to (2, 1)
lcd.print("Temp:" + String(data.temperature, 1) +" C "); // print message at (2, 1)
delay(2000); // display the above for two
lcd.init();
lcd.backlight();
lcd.clear(); // clear display
lcd.setCursor(0, 2); // move cursor to (0, 0)
lcd.print("Servo1 a 35°"); // print message at (0, 0)
lcd.setCursor(0, 3); // move cursor to (0, 0)
lcd.print("Servo1 a 160°"); // print message at (0, 0)
delay(2000); // display the above for two seconds
}
else if (pirValue == HIGH)
{
digitalWrite(ledPin, HIGH);
delay(1000); // Mantiene el LED encendido durante 1 segundo
digitalWrite(ledPin, LOW);
lcd.init();
lcd.backlight();
lcd.clear(); // clear display
lcd.setCursor(0, 0); // move cursor to (0, 0)
lcd.print("Hay movimiento"); // print message at (0, 0)
lcd.setCursor(0, 1); // move cursor to (0, 0)
lcd.print("Led encendido"); // print message at (0, 0)
delay(2000);
}
else if (data.humidity == 0 && data.temperature < -15 && pirValue == LOW)
{
// Gira el servomotor 20 grados en sentido horario
myServo1.write(90);
myServo2.write(90);
// Espera un momento para que el servomotor alcance la posición deseada
delay(1000);
lcd.init();
lcd.backlight();
lcd.clear(); // clear display
lcd.setCursor(0, 0); // move cursor to (0, 0)
lcd.print("Temp:" + String(data.temperature, 1) +" C "); // print message at (0, 0)
lcd.setCursor(0, 1); // move cursor to (2, 1)
lcd.print("Temp:" + String(data.temperature, 1) +" C "); // print message at (2, 1)
delay(2000); // display the above for two
lcd.init();
lcd.backlight();
lcd.clear(); // clear display
lcd.setCursor(0, 2); // move cursor to (0, 0)
lcd.print("Servo1 a 90°"); // print message at (0, 0)
lcd.setCursor(0, 3); // move cursor to (0, 0)
lcd.print("Servo1 a 90°"); // print message at (0, 0)
delay(2000);
}
}