import machine
from machine import Pin, SoftI2C
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
import time
import dht
# Inicialización de hardware y variables
sensor = dht.DHT22(Pin(15))
# Configuramos los pines GPIO para el teclado matricial
fila = [2, 4, 5, 19]
columna = [12, 27, 26, 25]
fila_gpio = [Pin(pin, Pin.OUT) for pin in fila]
columna_gpio = [Pin(pin, Pin.IN, Pin.PULL_UP) for pin in columna]
# Iniciamos la pantalla LCD
i2c = SoftI2C(scl=Pin(22), sda=Pin(21))
lcd = I2cLcd(i2c, 0x27, 4, 20)
# Definir la disposición de las teclas en el teclado matricial
teclado = [['1', '2', '3', 'A'], ['4', '5', '6', 'B'], ['7', '8', '9', 'C'], ['*', '0', '#', 'D']]
# Establecemos el set point
set_point = 0 # Set point predeterminado
# Iniciamos el zumbador
buzzer = Pin(18, Pin.OUT)
# Función para ingresar el set point
def input_set_point():
global set_point
lcd.clear()
lcd.putstr("Ingrese el set point:")
input_str = ''
cursor_position = 0
while True:
for i in range(4):
fila_gpio[i].value(0)
for j in range(4):
if not columna_gpio[j].value():
key = teclado[i][j]
if key == 'A':
# La letra 'A' para guardar el set point
numeric_input = ''.join(char for char in input_str if char.isdigit() or char == '.')
if numeric_input:
set_point = float(numeric_input)
lcd.clear()
lcd.putstr("Set point guardado")
time.sleep(1)
return
else:
lcd.clear()
lcd.putstr("Entrada inválida.")
time.sleep(1)
lcd.clear()
lcd.putstr("Ingrese el set point:")
lcd.move_to(0, 1)
lcd.putstr(input_str)
break
elif key == 'B':
# La letra 'B' para borrar el último carácter
input_str = input_str[:-1]
cursor_position -= 1
elif key == 'C':
# La letra 'C' para borrar la entrada
input_str = ''
cursor_position = 0
elif key == 'D':
# La letra 'D' para salir sin guardar
lcd.clear()
return
else:
input_str = input_str[:cursor_position] + key + input_str[cursor_position:]
cursor_position += 1
lcd.clear()
lcd.putstr("Ingrese el set point:")
lcd.move_to(0, 1)
lcd.putstr(input_str)
break
fila_gpio[i].value(1)
# Restablecer el sistema
def reset_system():
global set_point
set_point = 0
# Variables para controlar el estado del sistema
waiting_for_set_point = True
reset_pressed = False
while True:
# Verificación del set point
if waiting_for_set_point:
input_set_point()
waiting_for_set_point = False
# Temperatura y humedad
sensor.measure()
temperatura = sensor.temperature()
humedad = sensor.humidity()
# No mostrar las mediciones en el LCD hasta que se haya ingresado el set point
if not waiting_for_set_point:
# Mostrar los datos en la consola
print("Temperatura: {:.1f}°C, Humedad: {:.1f}%, Set point: {:.1f}C".format(temperatura, humedad, set_point))
# Definimos una tecla para restablecer el sistema
for i in range(4):
fila_gpio[i].value(0)
for j in range(4):
if not columna_gpio[j].value():
key = teclado[i][j]
if key == 'D': # La tecla "D" funciona para volver a ingresar el set point
reset_pressed = True
fila_gpio[i].value(1)
if reset_pressed:
reset_system()
waiting_for_set_point = True # Permitir ingresar un nuevo set point
reset_pressed = False
time.sleep(1)