#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#define LDR_DIG 2
#define LDR_ANA A3
#define DHT_PIN 8
#define Estufa 3
#define Ventilador 4
#define Luces 5
// LDR Characteristics
const float GAMMA = 0.7;
const float RL10 = 50;
//LCD I2C, Puerto, Tamaño
LiquidCrystal_I2C lcd(0x27, 20, 4);
// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
DHT dht(DHT_PIN, DHTTYPE);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println(F("DHTxx test!"));
dht.begin();
pinMode(LDR_DIG, INPUT);
pinMode(Ventilador, OUTPUT);
pinMode(Estufa, OUTPUT);
pinMode(Luces, OUTPUT);
lcd.init();
lcd.backlight();
}
void loop() {
// put your main code here, to run repeatedly:
int analogValue = analogRead(LDR_ANA);
float voltage = analogValue / 1024. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
// lcd.setCursor(2, 0);
// lcd.print("Room: ");
// if (lux > 50) {
// lcd.print("Light!");
// } else {
// lcd.print("Dark ");
// }
lcd.setCursor(0, 1);
lcd.print("L:");
lcd.print(lux);
//lcd.print(" ");
if (lux > 50) {
lcd.print(" Luz Off ");
digitalWrite(13,LOW);
digitalWrite(Luces,LOW);
} else {
lcd.print(" Luz On ");
digitalWrite(13,HIGH);
digitalWrite(Luces,HIGH);
}
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("°C "));
Serial.print(f);
Serial.print(F("°F Heat index: "));
Serial.print(hic);
Serial.print(F("°C "));
Serial.print(hif);
Serial.println(F("°F"));
lcd.setCursor(0, 0);
lcd.print("T");
lcd.print(t);
lcd.print((char)223);
lcd.print("C H");
lcd.print(h);
lcd.print(F("%"));
if (t<16){
digitalWrite(Estufa,HIGH);
}
else{
digitalWrite(Estufa,LOW);
}
if (t>29){
digitalWrite(Ventilador,HIGH);
}
else{
digitalWrite(Ventilador,LOW);
}
delay(100);
}