import time
import dht
from machine import Pin, I2C
import ssd1306
# Configura el sensor DHT22 en GPIO15
sensor = dht.DHT22(Pin(15))
# Configura la pantalla OLED (I2C en GPIO0=SDA, GPIO1=SCL)
i2c = I2C(0, scl=Pin(1), sda=Pin(0), freq=400000)
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
while True:
try:
sensor.measure()
temp = sensor.temperature()
hum = sensor.humidity()
# Mostrar en consola
print("Temperatura: {:.1f}°C Humedad: {:.1f}%".format(temp, hum))
# Mostrar en pantalla OLED
oled.fill(0) # Limpiar pantalla
oled.text("Estacion Clima", 0, 0)
oled.text("Temp: {:.1f} C".format(temp), 0, 20)
oled.text("Humedad: {:.1f}%".format(hum), 0, 35)
oled.show()
except OSError as e:
print("Fallo al leer el sensor:", e)
time.sleep(2)