from machine import Pin, I2C
from ssd1306 import SSD1306_I2C
import time
import dht
# === Configuración del display SSD1306 ===
i2c = I2C(0, scl=Pin(5), sda=Pin(4), freq=400000)
oled = SSD1306_I2C(128, 64, i2c)
# === Configuración del sensor DHT22 ===
sensor_dht = dht.DHT22(Pin(19)) # GP19
# === Función para mostrar temperatura y humedad con barras ===
def mostrar_temp_hum(temp, hum):
oled.fill(0)
oled.text("Temp: {:.1f}C".format(temp), 0, 0)
oled.text("Hum: {:.1f}%".format(hum), 0, 10)
# Termómetro (barra vertical)
oled.rect(100, 5, 8, 54, 1)
escala_temp = min(max(int(temp * 54 / 50), 0), 54)
for i in range(escala_temp):
oled.fill_rect(102, 58 - i, 4, 1, 1)
# Humedómetro (barra vertical)
oled.rect(112, 5, 8, 54, 1)
escala_hum = min(max(int(hum * 54 / 100), 0), 54)
for i in range(escala_hum):
oled.fill_rect(114, 58 - i, 4, 1, 1)
oled.show()
# === Bucle principal ===
while True:
try:
sensor_dht.measure()
temp = sensor_dht.temperature()
hum = sensor_dht.humidity()
print("Temp: {:.1f}C Hum: {:.1f}%".format(temp, hum))
mostrar_temp_hum(temp, hum)
except OSError as e:
print("Error al leer DHT22:", e)
time.sleep(2)