/*
--Programar un menú en un ESP32
--La navegacion por el menu sera por medio de un encoder
--La visualizacion del menu sera en un LCD 16x4 I2C
*/
//**************************************************
/*
Tabla de direcciones LCD_I2C
Binario Direccion_HEX A2 A1 A0
100000 0x20 0 0 0
100001 0x21 0 0 1
100010 0x22 0 1 0
100011 0x23 0 1 1
100100 0x24 1 0 0
100101 0x25 1 0 1
100110 0x26 1 1 0
100111 0x27 1 1 1
*/
//***************************************************
/*
ENCODER
CLK Pin 33
DT Pin 34
SW Pin 35
*/
//****************************************************
/*
Menú
1.- Temperatura ambiental
_ _ _ _ _ _ _ _ _ _
Temp: 28.0 °C
Min: 00.0 °C
Max: 55.0 °C
Sensor DHT22
2.- Humedad relativa
_ _ _ _ _ _ _ _ _ _
Hum R: 10.0 %
Min: 00.0 %
Max: 99.9 %
Sensor DHT22 return
3.- Humedad del suelo
_ _ _ _ _ _ _ _ _ _
Hum S: 10.0 %
Min: 00.0 %
Max: 99.9 %
Sensor DHT22 return
4.- Velocidad del viento
*/
//*****************************************************
//Librerias
#include <LiquidCrystal_I2C.h> //Libreria para utilizar LCD por I2C
#include <DHT.h> //Libreria para utilizar sensor DHT22
#define clk 33
#define dt 34
#define sw 35
#define DHTPIN 26
LiquidCrystal_I2C lcd(0x27, 20, 4); //Configurando variable lcd y definiendo direccion del LCD y tamaño
DHT dht(26, DHT22); //Pin 26
//int pinDHT = 26
String opciones[] = {"1.-Temperatura A.", "2.-Humedad R.", "3.-Humedad S.", "4.-Velocidad V."};
int max_opciones = sizeof(opciones) / sizeof(opciones[0]);
int state_clk_old;
int state_sw_old;
int count = 0;
float temp_min = 100;
float temp_max = -100;
void setup()
{
dht.begin();
lcd.init(); //Inicializa el LCD
lcd.clear(); //Limpia pantalla LCD
lcd.backlight(); //Enciende backlight
pinMode(clk, INPUT); //CLK como entrada (corresponde al puerto 33 del ESP32)
pinMode(dt, INPUT); //dt como entrada (corresponde al puerto 34 del ESP32)
pinMode(sw, INPUT_PULLUP); //sw como entrada (corresponde al puerto 35 del ESP32), Pullup interno
state_clk_old = digitalRead(clk); //Variable = lectura del puerto 33
state_sw_old = digitalRead(sw); //Variable = lectura del puerto 35
//Imprimiendo Mensaje de Bienvenida LCD
lcd.setCursor (4,0); //Coloca el cursor en las coordenadas (x, y)
lcd.print("Agricultura"); //Imprime el mensaje en LCD
lcd.setCursor (8,2);
lcd.print("4.0");
lcd.setCursor (16,3);
lcd.print("IoT");
delay(2000);
mostrar_menu();
}
void loop()
{
int state_sw = digitalRead(sw);
encoder(); //funcion encoder
//Condicion para detectar flanco de bajada
if(state_sw_old == HIGH && state_sw == LOW)
{
submenu();
}
state_sw_old = state_sw;
}
//Funciones
void encoder()
{
int state_clk = digitalRead(clk); //Variable = lectura del puerto 33
int state_dt = digitalRead(dt); //Variable = lectura del puerto 34
//Condición para detectar flanco de bajada
if(state_clk_old == HIGH && state_clk == LOW)
{
if(state_dt == LOW)
{
count--;
}
else
{
count++;
}
if(count < 0) count = max_opciones - 1;
if(count > max_opciones - 1) count = 0;
mostrar_menu();
}
delay(5);
state_clk_old = state_clk;
}
/*void boton()
{
int state_sw = digitalRead(sw);
//Condicion para detectar flanco de bajada
if(state_sw_old == HIGH && state_sw == LOW)
{
submenu();
}
state_sw_old = state_sw;
}*/
void mostrar_menu()
{
lcd.clear();
lcd.setCursor(8,0);
lcd.print("Menu");
lcd.setCursor(0,2);
lcd.print(opciones[count]);
}
void submenu()
{
if(count == 0)
{
float temp = dht.readTemperature();
if (temp < temp_min) {
temp_min= temp;
}
if (temp > temp_max) {
temp_max = temp;
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Temp: ");
lcd.setCursor(7,0);
lcd.print(temp);
lcd.setCursor(0,1);
lcd.print("Temp_Min: ");
lcd.setCursor(10,1);
lcd.print(temp_min);
lcd.setCursor(0,2);
lcd.print("Temp_Max: ");
lcd.setCursor(10,2);
lcd.print(temp_max);
lcd.setCursor(0,3);
lcd.print("Sensor DHT22");
delay(2000); // this speeds up the simulation
}
if(count == 1)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Hum R: 10.0 %");
lcd.setCursor(0,1);
lcd.print("Min: 00.0 %");
lcd.setCursor(0,2);
lcd.print("Max: 99.9 %");
lcd.setCursor(8,3);
lcd.print("Sensor DHT22");
}
if(count == 2)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Hum R: 10.0 %");
lcd.setCursor(0,1);
lcd.print("Min: 00.0 %");
lcd.setCursor(0,2);
lcd.print("Max: 99.9 %");
lcd.setCursor(8,3);
lcd.print("Sensor DHT22");
}
if(count == 3)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("no disponible");
}
}