from machine import Pin, I2C
from picozero import Speaker
import dht
import ssd1306
import time
# === Configuración de pines ===
DHT_PIN = 15 # Pin DATA del DHT22
i2c = I2C(1, scl=Pin(3), sda=Pin(2)) # I2C1: SCL=GP3, SDA=GP2
# Inicializar sensores
sensor = dht.DHT22(Pin(DHT_PIN))
#Creamos el objeto oled con los atributos de la clase: SSD1306
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
buzzer=Speaker(4)#Asignamos el pin 4 al buzzer
# Función para mostrar temperatura y humedad
def mostrar_en_oled(temp, hum):
oled.fill(0)
oled.text("Temp & Humedad", 0, 0)
oled.text("Temp: {:.1f}C".format(temp), 0, 20)
oled.text("Hum: {:.1f} %".format(hum), 0, 40)
oled.show()
def alerta_tempHum(temp,hum):
oled.fill(0)
buzzer.on()
oled.text("!!ALERTA VALORES",0,0)
oled.text("CRITICOS!!",0,10)
oled.text("temp--> {:.1f}[C] ".format(temp), 0, 30)
oled.text("Hum--> {:.1f} %".format(hum), 0, 50)
oled.show()
# Bucle principal
while True:
try:
sensor.measure()
temp = sensor.temperature()
hum = sensor.humidity()
if(temp>30 or temp<10 or hum<40 or hum>60):
alerta_tempHum(temp,hum)
else:
buzzer.off()
print("Temperatura: {:.1f}°C | Humedad: {:.1f}%".format(temp, hum))
mostrar_en_oled(temp, hum)
except Exception as e:
print("Error leyendo DHT22:", e)
time.sleep(1)