/*Actividad 1: sensores y actuadores*/
//VARIABLES:
int LED_blue = 14; //PIN 14 LED azul
int LED_red = 15; //PIN 15 LED rojo
int botton_1 = 27; //PIN 27 Boton 1
int botton_2 = 26; //PIN 26 Boton 2
int dht_sensor = 4; //PIN 4 sensor DHT22
boolean state_LED = true; //variable estado LED
boolean botton1_state; //variable estado lectura boton1
boolean botton2_state; //variable estado lectura boton2
int limit_humd_down = 25; //Limite humedad 25%
int limit_humd_up = 60; //Limite humedad 60%
int limit_temp = 45; //Limite temperatura 45º
float humedad; //variable lectura Humedad
float temperatura; //variable lectura Temperatura
//INICIAR SENSOR DHT
#include <DHT.h> //Incluir libreria DHT
#define DHTTYPE DHT22 //Se define el modelo DHT22
#define DHTPIN dht_sensor //Se define el pin a utilizar
DHT dht(DHTPIN, DHTTYPE, 22);
void setup() {
Serial.begin(115200); //Se inicia conexión serial para mensajes por pantalla
Serial.println("Actividad 1: Manejo de sensores y actuadores en IoT");
dht.begin(); //Inicia sensor DHT
//DEFINICIÓN PINES
pinMode(LED_blue, OUTPUT);
pinMode(LED_red, OUTPUT);
pinMode(botton_1, INPUT);
pinMode(botton_2, INPUT);
pinMode(dht_sensor, INPUT);
}
void loop() {
//LECTURA DHT22
humedad = dht.readHumidity(); //Lectura humedad
temperatura = dht.readTemperature(); //Lectura temperatura
botton1_state = digitalRead(botton_1); //Lectura estado boton1
botton2_state = digitalRead(botton_2); //Lectura estado boton2
//Comparador estado botones
if(botton1_state == HIGH && botton2_state == HIGH){
state_LED = false; //Los dos botones han sido pulsados
}
else if(botton1_state == HIGH && botton2_state == LOW){
state_LED = true; //Un boton ha sido pulsado
}
else if(botton1_state == LOW && botton2_state == HIGH){
state_LED = true; //Un boton ha sido pulsado
}
else if(botton1_state == LOW && botton2_state == LOW){
state_LED = false; //Ningun boton ha sido pulsado
} //El resultado es el estado FALSE=LEDs apagados; TRUE=LEDs se encienden si cumplen condición
Serial.print("Humedad: "); //Muestra por pantalla resultado Humedad
Serial.print(humedad);
Serial.print( "Temperatura: "); //Muestra por pantalla resultado Temperatura
Serial.println(temperatura);
//Comparador alerta HUMEDAD
if ((humedad <= limit_humd_down || humedad >= limit_humd_up) && state_LED == false){
digitalWrite(LED_blue, LOW); //LED azul encendido
}
else{
digitalWrite(LED_blue, HIGH); //LED azul apagado
}
//Comparador alerta TEMPERATURA
if (temperatura > limit_temp && state_LED == false){
digitalWrite(LED_red, LOW); //LED rojo se enciende
}
else{
digitalWrite(LED_red, HIGH); //LED rojo apagado
}
delay(1000); //Espera de 1 segundo para siguiente ciclo
}