from machine import Pin, I2C
import time
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
from machine import Pin, ADC, I2C, PWM
# CONFIGURACIÓN DE PINES
I2C_SDA = 21
I2C_SCL = 22
LCD_ADDR = 0x27 # Dirección I2C del módulo LCD
sensor_humedad = ADC(Pin(34))
sensor_lluvia = ADC(Pin(35))
sensor_gas = Pin(32, Pin.IN)
bomba = Pin(27, Pin.OUT)
servo = PWM(Pin(13), freq=50)
btn_si = Pin(25, Pin.IN, Pin.PULL_UP)
btn_no = Pin(33, Pin.IN, Pin.PULL_UP)
# CLASE LCD I2C (20x4)
class I2cLcd:
def __init__(self, i2c, addr, filas, cols):
self.i2c = i2c
self.addr = addr
self.filas = filas
self.cols = cols
self.backlight = 0x08
self._enviar(0x33, 0)
self._enviar(0x32, 0)
self._enviar(0x28, 0)
self._enviar(0x0C, 0)
self._enviar(0x06, 0)
self.limpiar()
def _enviar(self, dato, modo):
high = (dato & 0xF0) | modo | self.backlight
low = ((dato << 4) & 0xF0) | modo | self.backlight
self._escribir(high)
self._escribir(low)
def _escribir(self, data):
self.i2c.writeto(self.addr, bytes([data | 0x04]))
time.sleep_us(50)
self.i2c.writeto(self.addr, bytes([data & ~0x04]))
time.sleep_us(50)
def limpiar(self):
self._enviar(0x01, 0)
time.sleep_ms(2)
def imprimir(self, texto, fila=0, col=0):
direcciones = [0x00, 0x40, 0x14, 0x54]
pos = 0x80 + direcciones[fila] + col
self._enviar(pos, 0)
for ch in texto:
self._enviar(ord(ch), 1)
i2c = I2C(0, scl=Pin(I2C_SCL), sda=Pin(I2C_SDA), freq=400000)
lcd = I2cLcd(i2c, LCD_ADDR, 4, 20)
# FUNCIONES DEL SISTEMA
def mover_servo(grados):
duty = int((grados/180)*75 + 40)
servo.duty(duty)
def leer_humedad():
return int((sensor_humedad.read()/4095)*100)
def leer_lluvia():
return sensor_lluvia.read() > 2000
def mostrar_inicio():
lcd.limpiar()
lcd.imprimir(" SISTEMA DE RIEGO ", 0, 0)
lcd.imprimir("Quieres visualizar", 1, 0)
lcd.imprimir("las caracteristicas?", 2, 0)
lcd.imprimir("SI=Azul NO=Rojo", 3, 0)
def mostrar_final():
lcd.limpiar()
lcd.imprimir("Trabajo Finalizado", 1, 0)
# VARIABLES DE ESTADO
estado_gas = False
estado_humedad = "OK"
estado_lluvia = False
# PROGRAMA PRINCIPAL
mostrar_inicio()
# Esperar respuesta usuario
while True:
if not btn_si.value():
opcion = "SI"
break
if not btn_no.value():
opcion = "NO"
break
if opcion == "NO":
mostrar_final()
else:
while True:
humedad = leer_humedad()
lluvia = leer_lluvia()
gas = sensor_gas.value()
# Mostrar menú principal
lcd.limpiar()
lcd.imprimir("Humedad: {}%".format(humedad), 0, 0)
lcd.imprimir("Lluvia: {}".format("SI" if lluvia else "NO"), 1, 0)
lcd.imprimir("Gas: {}".format("ALARMA" if gas else "OK"), 2, 0)
lcd.imprimir("Servo: 90 grados", 3, 0)
mover_servo(90)
# --- DETECCIÓN DE GAS ---
if gas and not estado_gas:
estado_gas = True
lcd.limpiar()
lcd.imprimir("!!! ALERTA GAS !!!", 1, 0)
lcd.imprimir("Riego Activado", 2, 0)
bomba.on(); time.sleep(5); bomba.off()
elif not gas:
estado_gas = False
# --- DETECCIÓN DE HUMEDAD ---
if humedad < 40 and estado_humedad != "BAJA":
estado_humedad = "BAJA"
lcd.limpiar()
lcd.imprimir("Humedad Baja", 1, 0)
lcd.imprimir("Bomba Encendida", 2, 0)
bomba.on(); time.sleep(5); bomba.off()
elif humedad >= 40 and estado_humedad != "OK":
estado_humedad = "OK"
lcd.limpiar()
lcd.imprimir("Humedad Optima", 1, 0)
# --- DETECCIÓN DE LLUVIA ---
if lluvia and not estado_lluvia:
estado_lluvia = True
lcd.limpiar()
lcd.imprimir("Lluvia Detectada", 1, 0)
lcd.imprimir("Riego Detenido", 2, 0)
time.sleep(3)
elif not lluvia and estado_lluvia:
estado_lluvia = False
# Salida con botón NO
if not btn_no.value():
mostrar_final()
break
time.sleep(2)