from machine import Pin, I2C, PWM
from time import sleep
from dht import DHT22
from pico_i2c_lcd import I2cLcd
# --------------------------------------------------
# 1. CONFIGURACION DE LA PANTALLA LCD
# --------------------------------------------------
# SDA conectado a GP0
# SCL conectado a GP1
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
# Direccion de la pantalla LCD
I2C_ADDR = 0x27
# Pantalla de 2 filas y 16 columnas
lcd = I2cLcd(i2c, I2C_ADDR, 2, 16)
# --------------------------------------------------
# 2. CONFIGURACION DEL SENSOR DHT11
# --------------------------------------------------
# El pin DATA del DHT11 esta conectado a GP15
sensor = DHT22(Pin(15))
# --------------------------------------------------
# 3. CONFIGURACION DE LOS LEDS
# --------------------------------------------------
# LED verde conectado a GP10
led_verde = Pin(12, Pin.OUT)
# LED amarillo conectado a GP11
led_amarillo = Pin(13, Pin.OUT)
# LED rojo conectado a GP12
led_rojo = Pin(14, Pin.OUT)
# --------------------------------------------------
# 4. CONFIGURACION DEL SERVOMOTOR
# --------------------------------------------------
# Cable de señal del servo conectado a GP16
servo = PWM(Pin(16))
# Frecuencia para el servomotor
servo.freq(50)
# Valores para trabajar en simulacion Wokwi
min_duty = 1638
max_duty = 8192
# --------------------------------------------------
# 5. FUNCION PARA MOVER EL SERVOMOTOR
# --------------------------------------------------
def mover_servo(angulo):
# Calcula que parte del recorrido representa el angulo
proporcion = angulo / 180
# Calcula la diferencia entre el valor maximo y minimo
recorrido = max_duty - min_duty
# Calcula cuanto debemos avanzar
avance = proporcion * recorrido
# Calcula el duty final
duty = int(min_duty + avance)
# Envia la señal PWM al servo
servo.duty_u16(duty)
# --------------------------------------------------
# 6. FUNCION PARA APAGAR TODOS LOS LEDS
# --------------------------------------------------
def apagar_leds():
led_verde.value(0)
led_amarillo.value(0)
led_rojo.value(0)
# --------------------------------------------------
# 7. MENSAJE INICIAL
# --------------------------------------------------
apagar_leds()
lcd.clear()
lcd.putstr("Sistema listo")
lcd.move_to(0, 1)
lcd.putstr("Estacion Nombre estudiante")
sleep(2)
# --------------------------------------------------
# 8. PROGRAMA PRINCIPAL
# --------------------------------------------------
while True:
try:
# Realizar una nueva medicion
sensor.measure()
# Guardar temperatura y humedad
temperatura = sensor.temperature()
humedad = sensor.humidity()
# Mostrar tambien los valores en la consola
print("Temperatura:", temperatura, "C")
print("Humedad:", humedad, "%")
# Antes de evaluar apagamos los tres LEDs
apagar_leds()
# Limpiar la LCD
lcd.clear()
# ------------------------------------------
# CONDICION 1: HUMEDAD ALTA
# ------------------------------------------
if humedad > 70:
led_amarillo.value(1)
# Servo en posicion central
mover_servo(90)
lcd.putstr("Humedad alta")
lcd.move_to(0, 1)
lcd.putstr("T:" + str(temperatura) +
" H:" + str(humedad))
# ------------------------------------------
# CONDICION 2: AMBIENTE FRIO
# ------------------------------------------
elif temperatura < 20:
led_verde.value(1)
# Servo hacia un extremo
mover_servo(0)
lcd.putstr("Ambiente frio")
lcd.move_to(0, 1)
lcd.putstr("T:" + str(temperatura) +
" H:" + str(humedad))
# ------------------------------------------
# CONDICION 3: AMBIENTE NORMAL
# ------------------------------------------
elif temperatura <= 28:
led_verde.value(1)
# Servo en posicion central
mover_servo(90)
lcd.putstr("Ambiente normal")
lcd.move_to(0, 1)
lcd.putstr("T:" + str(temperatura) +
" H:" + str(humedad))
# ------------------------------------------
# CONDICION 4: TEMPERATURA ALTA
# ------------------------------------------
else:
led_rojo.value(1)
# Servo hacia el otro extremo
mover_servo(180)
lcd.putstr("Alerta calor")
lcd.move_to(0, 1)
lcd.putstr("T:" + str(temperatura) +
" H:" + str(humedad))
# Esperar antes de realizar una nueva lectura
sleep(2)
# --------------------------------------------------
# SI OCURRE UN ERROR CON EL SENSOR
# --------------------------------------------------
except OSError:
apagar_leds()
lcd.clear()
lcd.putstr("Error sensor")
lcd.move_to(0, 1)
lcd.putstr("Revise DHT11")
print("Error al leer el sensor DHT11")
sleep(2)