from machine import Pin, SoftI2C
import dht
import ssd1306  # Librería para la pantalla OLED
from time import sleep
 
# Configuración del sensor DHT11 en GPIO 4
sensor_dht = dht.DHT11(Pin(4))
 
# Configuración de la pantalla OLED (I2C en GPIO 21 y 22)
i2c = SoftI2C(scl=Pin(22), sda=Pin(21))
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
 
while True:
    try:
        sensor_dht.measure()
        temperatura = sensor_dht.temperature()
        humedad = sensor_dht.humidity()
       
        # Mostrar datos en consola
        print(f"Temperatura: {temperatura}°C  Humedad: {humedad}%")
       
        # Limpiar la pantalla
        oled.fill(0)
       
        # Escribir en la pantalla OLED
        oled.text("Estacion Meteo", 10, 10)
        oled.text(f"Temp: {temperatura} C", 10, 30)
        oled.text(f"Humedad: {humedad} %", 10, 45)
       
        # Actualizar la pantalla
        oled.show()
       
    except Exception as e:
        print("Error:", e)
 
    sleep(2)  # Leer cada 2 segundos