#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#include <Keypad.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
#define DHTPIN 5
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
DHT dht(DHTPIN, DHTTYPE);
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
//byte rowPins[ROWS] = {11, 10, 9, 8}; //connect to the row pinouts of the keypad
//byte colPins[COLS] = {7, 6, 5, 4}; //connect to the column pinouts of the keypad
byte rowPins[ROWS] = {13, 12, 11, 10}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 8, 7, 6}; //connect to the column pinouts of the keypad
Keypad teclado = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
float tmax, tmin, hmax, hmin;
const int clave = 135;
int introPassword(void)
{
int numDigitos = 3;
int valor = 0;
lcd.setCursor(0,0);
lcd.print(" Introduzca");
lcd.setCursor(0,1);
lcd.print("contrase");
lcd.print((char)155);
lcd.print("a = ");
while(numDigitos > 0)
{
char tecla = teclado.getKey();
if(tecla)
{
valor = valor*10 + (tecla-48);
lcd.print('*');
delay(100);
numDigitos--;
}
}
return valor;
}
//************ medida de la humedad relativa **********************
void humedad(void)
{
float h = dht.readHumidity();
if (isnan(h))
{
lcd.clear();
lcd.print("Failed to read from DHT sensor!");
return;
}
if(h > hmax)
hmax = h;
if(h < hmin)
hmin = h;
lcd.setCursor(0,2);
lcd.print("Humedad: ");
lcd.print(h,1);
lcd.print(" %");
//delay(2000);
}
//*****************************
void temperatura(void)
{
float t = dht.readTemperature();
if (isnan(t))
{
lcd.print("Failed to read from DHT sensor!");
return;
}
if(t > tmax)
tmax = t;
if(t < tmin)
tmin = t;
lcd.setCursor(0,1);
lcd.print("Temp. = ");
lcd.print(t, 1);
lcd.print((char)32);
lcd.print((char)223);
lcd.print("C");
}
void ver_tmaxMin(void)
{
lcd.clear();
lcd.setCursor (0,0);
lcd.print("Tmax = ");
lcd.print(tmax, 1);
lcd.print((char)32);
lcd.print((char)223);
lcd.print("C");
lcd.setCursor(0,1);
lcd.print("Tmin = ");
lcd.print(tmin,1);
lcd.print((char)32);
lcd.print((char)223);
lcd.print("C");
delay(2000);
lcd.clear();
}
void menu(void)
{
lcd.clear();
lcd.print("Temper.- humedad: 1");
lcd.print(" Humedad max-min: 3");
lcd.print(" Temper. max-min: 2");
char tecla = teclado.getKey();
switch(tecla)
{
case '1':
temperatura();
humedad();
break;
}
}
void setup()
{
dht.begin();
float temperatura = dht.readTemperature();
float humedad = dht.readHumidity();
if(isnan(temperatura) || isnan(humedad))
{
lcd.clear();
lcd.print("Fallo sensor DHT");
while(1);
}
else
{
tmax = temperatura;
tmin = temperatura;
hmax = humedad;
hmin = humedad;
}
lcd.init();
lcd.backlight();
}
void loop()
{
int passWord = introPassword();
if(passWord == clave)
{
while(1)
{
lcd.clear();
temperatura();
humedad();
delay(1000);
char tecla = teclado.getKey();
if(tecla);
{
if(tecla == '1')
ver_tmaxMin();
}
}
}
//delay(1000);
}