from machine import Pin, I2C
from time import sleep, localtime
import dht
from i2c_lcd import I2cLcd
# Dirección I2C de la pantalla LCD
I2C_ADDR = 0x27
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
lcd = I2cLcd(i2c, I2C_ADDR, 4, 20)
# Sensores de nivel de los tanques
N1_T1 = Pin(11, Pin.IN, Pin.PULL_UP)
N2_T1 = Pin(12, Pin.IN, Pin.PULL_UP)
N1_T2 = Pin(13, Pin.IN, Pin.PULL_UP)
N2_T2 = Pin(14, Pin.IN, Pin.PULL_UP)
# Sensor DHT22 (temperatura y humedad)
sensor_dht = dht.DHT22(Pin(6))
# Configuración del teclado matricial
filas = [Pin(pin, Pin.OUT) for pin in [2, 3, 4, 5]]
columnas = [Pin(pin, Pin.IN, Pin.PULL_UP) for pin in [7, 8, 9, 10]]
# Mapeo de teclas del teclado 4x4
teclas = [
["1", "2", "3", "A"],
["4", "5", "6", "B"],
["7", "8", "9", "C"],
["*", "0", "#", "D"]
]
pantalla_actual = 1
modo_operacion = "Automático"
# Parámetros de operación
hora_inicio = [15, 35, 0]
hora_fin = [15, 40, 0]
temp_ref = 23
humedad_ref = 50
estado_riego = "En espera"
# Parámetros físicos
Area = 1
Prof = 0.1
Caudal_bomba = 200
def leer_teclado():
while True:
for i, fila in enumerate(filas):
fila.low()
sleep(0.01)
for j, columna in enumerate(columnas):
if not columna.value():
fila.high()
return teclas[i][j]
fila.high()
sleep(0.1)
def reloj():
"""Obtiene la hora actual del sistema con formato HH:MM:SS"""
tiempo = localtime()
return f"{tiempo[3]:02}:{tiempo[4]:02}:{tiempo[5]:02}"
def editar_valores():
"""Permite ingresar valores y desplazarse con 'D'."""
global hora_inicio, hora_fin, temp_ref, humedad_ref, modo_operacion
cursor_pos = 0
nuevo_valor = ""
lcd.clear()
lcd.putstr("Editar valores...\nD=Sig | *=Guardar")
sleep(2)
while True:
lcd.clear()
if pantalla_actual == 1:
lcd.putstr(f"Modo: {modo_operacion}\nA=Auto | B=Temp")
elif pantalla_actual == 2:
lcd.putstr(f"Inicio: {hora_inicio[0]:02}:{hora_inicio[1]:02}:{hora_inicio[2]:02}\nFin: {hora_fin[0]:02}:{hora_fin[1]:02}:{hora_fin[2]:02}")
elif pantalla_actual == 4:
lcd.putstr(f"Temp Ref: {temp_ref}C\nHumedad Ref: {humedad_ref}%")
sleep(1)
while True:
tecla = leer_teclado()
if pantalla_actual == 1:
if tecla == "A":
modo_operacion = "Automático"
elif tecla == "B":
modo_operacion = "Temporizado"
elif tecla == "*":
break
elif tecla.isdigit():
nuevo_valor += tecla
if len(nuevo_valor) == 2:
if pantalla_actual == 2:
if cursor_pos < 3:
hora_inicio[cursor_pos] = min(int(nuevo_valor), 23 if cursor_pos == 0 else 59)
else:
hora_fin[cursor_pos - 3] = min(int(nuevo_valor), 23 if cursor_pos == 0 else 59)
elif pantalla_actual == 4:
if cursor_pos == 0:
temp_ref = min(int(nuevo_valor), 50)
else:
humedad_ref = min(int(nuevo_valor), 100)
nuevo_valor = ""
elif tecla == "D":
cursor_pos = (cursor_pos + 1) % 6 if pantalla_actual == 2 else (cursor_pos + 1) % 2
elif tecla == "*":
break
lcd.clear()
if pantalla_actual == 1:
lcd.putstr(f"Modo: {modo_operacion}\nA=Auto | B=Temp")
elif pantalla_actual == 2:
lcd.putstr(f"Inicio: {hora_inicio[0]:02}:{hora_inicio[1]:02}:{hora_inicio[2]:02}\nFin: {hora_fin[0]:02}:{hora_fin[1]:02}:{hora_fin[2]:02}")
elif pantalla_actual == 4:
lcd.putstr(f"Temp Ref: {temp_ref}C\nHumedad Ref: {humedad_ref}%")
sleep(1)
if leer_teclado() == "*":
break
lcd.clear()
lcd.putstr("Valores guardados...")
sleep(2)
def mostrar_pantalla():
lcd.clear()
if pantalla_actual == 1:
lcd.putstr(f"Hora: {reloj()}\nModo: {modo_operacion}")
elif pantalla_actual == 2:
lcd.putstr(f"Inicio: {hora_inicio[0]:02}:{hora_inicio[1]:02}:{hora_inicio[2]:02}\nFin: {hora_fin[0]:02}:{hora_fin[1]:02}:{hora_fin[2]:02}")
elif pantalla_actual == 3:
sensor_dht.measure()
humedad_actual = sensor_dht.humidity()
temperatura_actual = sensor_dht.temperature()
lcd.putstr(f"Humedad: {humedad_actual:.1f}%\nTemp: {temperatura_actual:.1f}C")
elif pantalla_actual == 4:
lcd.putstr(f"Temp Ref: {temp_ref}C\nHumedad Ref: {humedad_ref}%")
elif pantalla_actual == 5:
sensor_dht.measure()
humedad_actual = sensor_dht.humidity()
temperatura_actual = sensor_dht.temperature()
volumen_agua = Area * Prof * 1000 * ((humedad_ref - humedad_actual) / 100)
tiempo_riego = max(10, (volumen_agua * (1 + (temperatura_actual - temp_ref) / temp_ref)) / Caudal_bomba)
lcd.putstr(f"Estado: {estado_riego}\nVolumen: {volumen_agua:.1f} mL\nTiempo: {tiempo_riego:.1f} s")
sleep(2)
while True:
tecla = leer_teclado()
if tecla == "#":
pantalla_actual = pantalla_actual + 1 if pantalla_actual < 5 else 1
elif tecla == "C":
editar_valores()
mostrar_pantalla()
elif tecla == "*":
lcd.clear()
lcd.putstr("Valores guardados...")
sleep(2)
mostrar_pantalla()
sleep(1)