import time
from machine import Pin, I2C, PWM
import dht
import neopixel
from i2c_lcd import I2cLcd
# 1. Configurar los pines físicos I2C estándar para la ESP32 en Wokwi
i2c = I2C(sda=Pin(22), scl=Pin(21), freq=400000)
# 2. Inicializar el LCD pasándole el objeto i2c y la dirección por defecto (0x27)
lcd = I2cLcd(i2c, 0x27, 2, 16)
# 3. Limpiar cualquier residuo y escribir el texto de prueba
lcd.clear()
pin_servo = Pin(18, Pin.OUT)
servo = PWM(pin_servo, freq=50)
servo_angulo = 0
tira = neopixel.NeoPixel(Pin(25), 8)
sensor = dht.DHT22(Pin(15))
temp_max = 70
temp_min = 10
temp_rango = temp_max - temp_min
temp = ""
led_red = Pin(23, Pin.OUT)
rele = Pin(2, Pin.OUT)
bot_reset = Pin(34, Pin.IN, Pin.PULL_UP)
text1 = "Presionar Boton"
text2 = ""
rele_activo = 0
# Configura los colores de los LEDS (R, G, B)
colores = [
(0, 255, 0), # LED 0 (Verde)
(0, 255, 0), # LED 1 (Verde)
(0, 255, 0), # LED 2 (Verde)
(0, 255, 0), # LED 3 (Verde)
(0, 255, 0), # LED 4 (Verde)
(255, 255, 0), # LED 5 (Amarillo)
(255, 255, 0), # LED 6 (Amarillo)
(255, 0, 0) # LED 7 (Rojo)
]
def MostrarTira(temp):
# 1. Apaga todos los LEDs
tira.fill((0, 0, 0))
led = 0
if temp > temp_min and temp < temp_max:
led = round((temp - temp_min) * 8 / temp_rango)
else:
if temp > temp_max:
led = 8
if temp < temp_min:
tira.write()
for i in range(led):
# 2. Encendemos SOLO el LED actual buscando su color en la lista
tira[i] = colores[i]
# 3. Envía los datos para mostrar el cambio
tira.write()
while True:
sensor.measure()
temp = sensor.temperature()
humedad = sensor.humidity()
text2 = f"H:{humedad:.1f}% T:{temp:.1f}C"
MostrarTira(temp)
if temp > temp_min and temp < temp_max:
led_red.off()
if rele_activo == 0:
text1 = "Presionar Boton"
if bot_reset.value() == 0:
text1 = "Motor Listo"
rele_activo = 1
rele.on()
print("Boton presionado")
if temp > temp_max:
led_red.on()
rele.off()
rele_activo = 0
text1 = "Temp. MAX!"
if temp < temp_min:
text1 = "Temp. MIN!"
duty_calculado = int(26 + (servo_angulo / 180) * (123 - 26))
servo.duty(duty_calculado)
servo_angulo += 180
if servo_angulo >= 190:
servo_angulo = 0
lcd.clear()
lcd.putstr(f"{text1}\n{text2}")
time.sleep(3)