import network
import socket
from utime import sleep
import dht
from machine import Pin, I2C
import ssd1306
from ssd1306 import SSD1306_I2C
import framebuf
import time
# ========================
# CONFIGURACIÓN OLED
# ========================
i2c = I2C(0, sda=Pin(4), scl=Pin(5), freq=400000)
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
print("Dispositivos I2C detectados:", [hex(addr) for addr in i2c.scan()])
OLED_ADDR = 0x3C
WIDTH = 128
HEIGHT = 64
try:
oled = SSD1306_I2C(WIDTH, HEIGHT, i2c, addr=OLED_ADDR)
print("Pantalla OLED inicializada correctamente")
except Exception as e:
print("Error al inicializar la pantalla:", e)
imagen_bytes = bytearray([
0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xf0, 0x0f, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xf0, 0x0f, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xf3, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xf3, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xf3, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xfb, 0xff,
0xf0, 0x0f, 0xfe, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x00, 0x3f, 0x7f, 0xff, 0xff, 0xff, 0xfb, 0xff,
0xf0, 0x0f, 0xfc, 0x00, 0x00, 0x00, 0x01, 0xff, 0x00, 0x3f, 0x7a, 0x4c, 0x71, 0xbb, 0x3b, 0xff,
0xf3, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x01, 0xff, 0x80, 0x1f, 0x0d, 0xb7, 0xb6, 0xba, 0xdb, 0xff,
0xf3, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x03, 0xff, 0x80, 0x0f, 0x7f, 0xf6, 0x36, 0xba, 0x1b, 0xff,
0xf3, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x03, 0xff, 0x80, 0x0f, 0x7b, 0xf5, 0xb6, 0xba, 0xfb, 0xff,
0xf0, 0x0f, 0xf0, 0x00, 0x00, 0x00, 0x03, 0xff, 0x80, 0x07, 0x7b, 0xf5, 0xb6, 0xd2, 0xfb, 0xff,
0xf0, 0x0f, 0xe0, 0x00, 0x00, 0x00, 0x03, 0xff, 0x80, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x03, 0xff, 0x80, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x01, 0xff, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x01, 0x7b, 0xff, 0xff, 0xfb, 0xff, 0xff,
0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x33, 0xff, 0xff, 0xfb, 0xff, 0xff,
0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0x3b, 0x18, 0x63, 0x8a, 0x73, 0xff,
0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x6a, 0xeb, 0x7d, 0x7b, 0xed, 0xff,
0xf1, 0xf1, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xff, 0x4a, 0x0b, 0xe1, 0x7b, 0xed, 0xff,
0xf1, 0xf1, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0x5a, 0xfb, 0xfd, 0x7b, 0xed, 0xff,
0xf0, 0xe1, 0x80, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0xff, 0x7b, 0x0b, 0x61, 0x8b, 0xe1, 0xff,
0xf0, 0xe1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xf0, 0xe1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xf2, 0x49, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xf2, 0x49, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xf2, 0x09, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff,
0xf3, 0x19, 0x00, 0x00, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0x6f, 0xff, 0xff, 0xff, 0xff, 0xff,
0xf3, 0x19, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xff, 0x6d, 0x64, 0xb5, 0xd6, 0x5f, 0xff,
0xf3, 0xf9, 0x80, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xff, 0x1b, 0xad, 0xfe, 0xfa, 0xdf, 0xff,
0xf3, 0xf9, 0x80, 0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0x6b, 0xad, 0xf6, 0xfa, 0xff, 0xff,
0xf3, 0xf9, 0x80, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x6f, 0xad, 0xfe, 0xfa, 0xdf, 0xff,
0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xff, 0x74, 0x6d, 0xf0, 0xfb, 0x3f, 0xff,
0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xf0, 0x3f, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xf0, 0x1f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xf3, 0x8f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xf3, 0xcf, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xf3, 0xcf, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xf3, 0x8f, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xf0, 0x1f, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xf0, 0x1f, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xf3, 0x8f, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xf3, 0xcf, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xf3, 0xc7, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xf3, 0xe7, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xf3, 0xe3, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
])
def mostrar_imagen():
try:
fb = framebuf.FrameBuffer(imagen_bytes, WIDTH, HEIGHT, framebuf.MONO_HLSB)
oled.fill(0)
oled.blit(fb, 0, 0)
oled.show()
print("Imagen mostrada correctamente")
except Exception as e:
print(f"Error al mostrar la imagen: {e}")
oled.fill(0)
oled.text("Error imagen", 10, HEIGHT//2 - 8)
oled.show()
def mostrar_contador(valor):
oled.fill(0)
texto = "{:04d}".format(valor)
x = (WIDTH - len(texto) * 8) // 2
y = (HEIGHT - 8) // 2
oled.text(texto, x, y)
oled.show()
contador = 0
print("Mostrando imagen en la pantalla...")
mostrar_imagen()
print("Iniciando bucle principal")
print("Mostrando imagen en la pantalla...")
mostrar_imagen()
time.sleep(4)
# ========================
# SENSOR DHT22
# ========================
dht_sensor = dht.DHT22(Pin(26)) # Ajusta el pin según tu conexión
# ========================
# CONEXIÓN WIFI
# ========================
from secrets import secrets
ssid = secrets['ssid']
password = secrets['password']
print("Conectando a WiFi...")
oled.fill(0)
oled.text("Conectando a WiFi", 0, 0)
oled.show()
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
while not wlan.isconnected():
sleep(0.5)
ip = wlan.ifconfig()[0]
print("Conectado con IP:", ip)
oled.text("Conectado!", 0, 10)
oled.text("IP: {}".format(ip), 0, 20)
oled.show()
sleep(2)
# ========================
# FUNCIÓN PARA HTML
# ========================
def web_page(temp, hum):
html = """<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="refresh" content="2">
<title>DHT22 en Vivo</title>
<style>
body {{ font-family: Arial; background: #f4f4f4; color: #333; text-align: center; padding: 20px; }}
.card {{ background: white; padding: 20px; border-radius: 10px; box-shadow: 0 2px 5px rgba(0,0,0,0.2); display: inline-block; }}
h1 {{ color: #007BFF; }}
.value {{ font-size: 2em; margin: 10px 0; }}
</style>
</head>
<body>
<h1>Monitor DHT22</h1>
<div class="card">
<p class="value">🌡️ Temp: <strong>{:.1f} °C</strong></p>
<p class="value">💧 Humedad: <strong>{:.1f} %</strong></p>
<p><small>Actualizado cada 2 segundos</small></p>
</div>
</body>
</html>""".format(temp, hum)
return html
# ========================
# SERVIDOR WEB
# ========================
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 80))
s.listen(5)
print("Servidor listo en IP:", ip)
oled.fill(0)
oled.text("Servidor listo!", 0, 0)
oled.text("IP:", 0, 10)
oled.text(ip, 0, 20)
oled.show()
# ========================
# BUCLE PRINCIPAL
# ========================
while True:
conn, addr = s.accept()
print('Petición de:', addr)
request = conn.recv(1024)
# Leer el sensor justo antes de mostrar
try:
dht_sensor.measure()
temp = dht_sensor.temperature()
hum = dht_sensor.humidity()
# Mostrar en OLED
oled.fill(0)
oled.text("Temp: {:.1f} C".format(temp), 0, 0)
oled.text("Hum: {:.1f} %".format(hum), 0, 10)
oled.text("IP: {}".format(ip), 0, 20)
oled.show()
# Responder al cliente web
response = web_page(temp, hum)
conn.send('HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n')
conn.sendall(response)
except Exception as e:
print("Error al leer sensor:", e)
conn.send('HTTP/1.1 500 Internal Server Error\r\n\r\n')
conn.close()
sleep(2) # Tiempo de actualización