from machine import Pin, I2C
from time import sleep, localtime
import dht
from i2c_lcd import I2cLcd
import utime
# --- Configuración LCD ---
I2C_ADDR = 0x27
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
lcd = I2cLcd(i2c, I2C_ADDR, 4, 20)
# --- Pines sensores de nivel y bombas ---
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)
Bomba_T1 = Pin(15, Pin.OUT)
Bomba_T2 = Pin(16, Pin.OUT)
# --- Sensor DHT22 ---
sensor_dht = dht.DHT22(Pin(6))
# --- Pines teclado 4x4 ---
filas = [Pin(2, Pin.OUT), Pin(3, Pin.OUT), Pin(4, Pin.OUT), Pin(5, Pin.OUT)]
columnas = [Pin(7, Pin.IN, Pin.PULL_DOWN), Pin(8, Pin.IN, Pin.PULL_DOWN),
Pin(9, Pin.IN, Pin.PULL_DOWN), Pin(10, Pin.IN, Pin.PULL_DOWN)]
teclas = [
["1", "2", "3", "A"],
["4", "5", "6", "B"],
["7", "8", "9", "C"],
["*", "0", "#", "D"]
]
# --- Variables del sistema ---
modo_operacion = "Automático"
hora_inicio = [15, 35, 0]
hora_fin = [15, 40, 0]
temp_ref = 23
humedad_ref = 50
estado_riego = "En espera"
Area = 10
Prof = 0.2
Caudal_bomba = 5
pantalla_actual = 1
cancelar_riego = False
# --- Funciones del sistema ---
def leer_teclado():
for i, fila in enumerate(filas):
fila.high()
for j, col in enumerate(columnas):
if col.value() == 1:
fila.low()
utime.sleep(0.2) # debounce
return teclas[i][j]
fila.low()
return None
def leer_sensores():
sensor_dht.measure()
return sensor_dht.temperature(), sensor_dht.humidity()
def estado_tanques():
estado = {
"T1": "Lleno" if N2_T1.value() else "Bajo",
"T2": "Lleno" if N2_T2.value() else "Bajo"
}
estado["Advertencia"] = "Tanques bajos!" if estado["T1"] == "Bajo" and estado["T2"] == "Bajo" else ""
return estado
def reloj():
tiempo = localtime()
return f"{tiempo[3]:02}:{tiempo[4]:02}:{tiempo[5]:02}"
def mostrar_pantalla(temp=None, humedad=None):
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}\n")
lcd.putstr(f"Fin: {hora_fin[0]:02}:{hora_fin[1]:02}:{hora_fin[2]:02}")
elif pantalla_actual == 3:
if temp is None or humedad is None:
temp, humedad = leer_sensores()
niveles = estado_tanques()
lcd.putstr(f"T:{temp:.1f}C H:{humedad:.1f}%\n")
lcd.putstr(f"T1:{niveles['T1']} T2:{niveles['T2']}")
elif pantalla_actual == 4:
lcd.putstr(f"Temp Ref: {temp_ref}C\nHumedad Ref: {humedad_ref}%")
elif pantalla_actual == 5:
if temp is None or humedad is None:
temp, humedad = leer_sensores()
volumen_agua = calcular_volumen_agua(humedad, temp)
tiempo_riego = calcular_tiempo_riego(volumen_agua)
lcd.putstr(f"Estado: {estado_riego}\n")
lcd.putstr(f"Vol:{volumen_agua:.1f}L {tiempo_riego:.1f}s")
# --- Bucle principal ---
while True:
tecla = leer_teclado()
if tecla == "#":
pantalla_actual = (pantalla_actual % 5) + 1
elif tecla == "A":
modo_operacion = "Automático"
elif tecla == "B":
modo_operacion = "Temporizado"
elif tecla == "*":
lcd.clear()
lcd.putstr("Valores guardados")
sleep(2)
mostrar_pantalla()
sleep(1)