//E6. Sistema de Riego Automático
//Diseñar e implementar un sistema de riego automático que controle
//una bomba de agua en función de la lectura de humedad del suelo.
//El sistema deberá ser capaz de medir la humedad utilizando un sensor
//conectado al ADC del ESP32 y ajustar la cantidad de agua suministrada a
//las plantas mediante el control de la velocidad de la bomba a través
//de PWM. El sistema debe activarse automáticamente cuando el nivel
//de humedad del suelo caiga por debajo de un umbral predefinido por el
//usuario y desactivarse cuando se alcance un nivel óptimo de humedad.
#include "DHTesp.h"
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Keypad.h>
const int DHT_PIN = 26;
const int led = 14;
DHTesp dhtSensor;
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// Variables para los umbrales de humedad
int umb = 30; // Humedad mínima inicial
int opt = 60; // Humedad óptima inicial
int a = 1;
// Teclado de entrada
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {19, 18, 5, 17};
byte colPins[COLS] = {16, 4, 0, 2};
Keypad teclado = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Variables para el menú
int opcionSeleccionada = 0;
int menu = 1;
int temp = 0;
void setup() {
Serial.begin(115200);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
pinMode(led, OUTPUT);
// initialize OLED display with I2C address 0x3C
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("failed to start SSD1306 OLED"));
while (1);
}
delay(2000);
oled.clearDisplay();
}
void loop() {
int b = 0;
while (b == 0) {
int opci = opcion();
if (opci == 1) {
oled.println("Usted Selecciono la opcion 1. ");
delay(1000);
cleandisplay();
oled.println("Ingrese la Humedad Mínima: ");
int umb = ingresarvalor();
}
else if (opci == 2) {
oled.println("Usted Selecciono la opcion 2. ");
delay(1000);
cleandisplay();
oled.println("Ingrese la Humedad Optima: ");
int opt = ingresarvalor();
}
else if (opci == 3) {
oled.println("Usted Selecciono la opcion 3. ");
delay(1000);
cleandisplay();
oled.println("Ingrese el tiempo en Segundos: ");
int temp = ingresarvalor();
}
else if (opci == 4) {
oled.println("Saliendo.. ");
delay(1000);
cleandisplay();
b = 1;
}
}
Riego();
}
void mostrarMenu() {
cleandisplay();
oled.println("1. Set Hum Minima");
oled.println("2. Set Hum Optima");
oled.println("3. Control Temporizado");
oled.println("4. Detener Sistema");
oled.display();
}
void Riego() {
int hum = dhtSensor.getHumidity();
if (hum < umb & a == 1) {
delay(temp*1000);
digitalWrite(led,HIGH);
Serial.print("La humedad es de: ");
Serial.println(hum);
Serial.println("Riego Encendido.");
a = 0;
}
else if (hum >= opt & a == 0) {
delay(temp*1000);
digitalWrite(led,LOW);
Serial.print("La humedad es de: ");
Serial.println(hum);
Serial.println("Riego Apagado.");
a = 1;
}
}
int ingresarvalor() {
String valor = "";
char tecla;
while (true) {
tecla = teclado.getKey();
if (tecla) {
if (tecla == '#') { // Fin de ingreso
break;
}
valor += tecla;
oled.setCursor(0, 20);
oled.display();
}
}
return valor.toInt();
}
int opcion() {
while (true){
mostrarMenu();
int opc = ingresarvalor();
cleandisplay();
if (opc == 1) {
return opc;
break;
}
else if (opc == 2) {
return opc;
break;
}
else if (opc == 3) {
return opc;
break;
}
else if (opc == 4) {
return opc;
break;
}
else {
oled.println("Opcion incorrecta");
}
}
}
void cleandisplay() {
oled.clearDisplay();
oled.setTextSize(1);
oled.setTextColor(WHITE);
oled.setCursor(0, 0);
}