import time
import socket
import network
import machine
import framebuf
from utime import sleep
import dht
from secrets import secrets # Archivo con ssid y password
from ssd1306 import SSD1306_I2C
ANCHO = 128
ALTO = 64
i2c = machine.I2C(0, scl=machine.Pin(5), sda=machine.Pin(4), freq=2000000)
oled = SSD1306_I2C(ANCHO, ALTO, i2c)
logo_rufus3 = bytearray([
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0xff, 0xff, 0xff,
0xff, 0xf8, 0x3f, 0xff, 0xff, 0xff, 0xf0, 0x1f, 0xff, 0xff,
0xff, 0xf0, 0x1f, 0xff, 0xff, 0xff, 0xf0, 0x1f, 0xff, 0xff,
0xff, 0xfc, 0x1f, 0xff, 0xff, 0xff, 0xfc, 0x0f, 0xff, 0xff,
0xff, 0xfc, 0x07, 0xff, 0xff, 0xff, 0xf8, 0x03, 0xff, 0xff,
0xff, 0xf8, 0x01, 0xff, 0xff, 0xff, 0xf8, 0x00, 0xff, 0xff,
0xff, 0xf8, 0x00, 0x7f, 0xff, 0xff, 0xf8, 0x00, 0x7f, 0xff,
0xff, 0xf8, 0x00, 0x7f, 0xff, 0xff, 0xfa, 0x00, 0x7f, 0xff,
0xff, 0xf2, 0x00, 0x7f, 0xff, 0xff, 0xfc, 0x00, 0x7f, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
])
fb_logo = framebuf.FrameBuffer(logo_rufus3, 40, 20, framebuf.MONO_HLSB)
sensor_interno = machine.ADC(4)
sensor_dht = dht.DHT22(machine.Pin(15)) # DHT11 en GPIO15
ssid = secrets['ssid']
password = secrets['password']
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
print('Conectando a red WiFi...')
while not wlan.isconnected():
sleep(1)
print('Conectado!')
print(wlan.ifconfig())
def leer_temperatura_interna():
lectura = sensor_interno.read_u16()
voltaje = lectura * (3.3 / 65535)
temp_interna = 27 - (voltaje - 0.706) / 0.001721
return temp_interna
def leer_dht11():
try:
sensor_dht.measure()
temp = sensor_dht.temperature()
hum = sensor_dht.humidity()
return temp, hum
except Exception as e:
print("Error leyendo DHT11:", e)
return None, None
def mostrar_logo():
oled.fill(0)
oled.blit(fb_logo, 54, 15)
oled.text("DBSC", 54, 37)
oled.show()
time.sleep(2)
oled.fill(0)
oled.text("laboratorio 8", 0, 0)
oled.text("DBSC", 0, 10)
oled.text("Dylan Beyli", 0, 20)
oled.text("Sinani Catacora", 0, 30)
oled.show()
time.sleep(3)
oled.fill(0)
oled.show()
def escala_porcentaje(valor, min_val, max_val):
"""Convierte valor entre min_val y max_val a porcentaje (0 a 100)."""
if valor is None:
return 0
if valor < min_val:
return 0
if valor > max_val:
return 100
return int((valor - min_val) / (max_val - min_val) * 100)
def mostrar_datos_oled(temp_interna, temp_dht, hum_dht):
oled.fill(0)
oled.text("Temp Interna:", 0, 0)
oled.text("{:.1f} C".format(temp_interna), 90, 0)
pct_temp_interna = escala_porcentaje(temp_interna, 0, 50)
ancho_barra_interna = int(pct_temp_interna * 50 / 100)
oled.rect(0, 10, 52, 8, 1)
oled.fill_rect(1, 11, ancho_barra_interna, 6, 1)
if temp_dht is not None:
oled.text("Temp DHT11:", 0, 22)
oled.text("{:.1f} C".format(temp_dht), 90, 22)
pct_temp_dht = escala_porcentaje(temp_dht, 0, 50)
ancho_barra_dht = int(pct_temp_dht * 50 / 100)
oled.rect(0, 32, 52, 8, 1)
oled.fill_rect(1, 33, ancho_barra_dht, 6, 1)
else:
oled.text("Temp DHT11:", 0, 22)
oled.text("Err", 90, 22)
if hum_dht is not None:
oled.text("Humedad:", 0, 44)
oled.text("{} %".format(hum_dht), 90, 44)
ancho_barra_hum = int((hum_dht / 100) * 50)
oled.rect(0, 54, 52, 8, 1)
oled.fill_rect(1, 55, ancho_barra_hum, 6, 1)
else:
oled.text("Humedad:", 0, 44)
oled.text("Err", 90, 44)
oled.show()
def web_page(temp_interna, temp_dht, hum_dht):
temp_dht = temp_dht if temp_dht is not None else 0
hum_dht = hum_dht if hum_dht is not None else 0
pct_temp_interna = escala_porcentaje(temp_interna, 0, 50)
pct_temp_dht = escala_porcentaje(temp_dht, 0, 50)
pct_humedad = hum_dht
html = f"""<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<title>Temperaturas</title>
<style>
body {{
font-family: Arial, sans-serif;
background: linear-gradient(to right, #0f2027, #203a43, #2c5364);
color: #ffffff;
text-align: center;
padding: 20px;
}}
h1 {{
font-size: 32px;
color: #00ffff;
margin-bottom: 10px;
}}
.logo {{
width: 120px;
height: auto;
margin-bottom: 10px;
}}
.sensor {{
margin: 20px auto;
width: 80%;
background-color: #444;
border-radius: 10px;
padding: 15px;
box-shadow: 0 4px 8px rgba(0,0,0,0.4);
}}
.barra {{
width: 100%;
height: 20px;
background: #222;
border-radius: 10px;
margin-top: 10px;
overflow: hidden;
}}
.relleno-temp-interna {{
height: 100%;
background: #00ff00;
width: {pct_temp_interna}%;
border-radius: 10px;
transition: width 0.5s ease;
}}
.relleno-temp-dht {{
height: 100%;
background: #ffff00;
width: {pct_temp_dht}%;
border-radius: 10px;
transition: width 0.5s ease;
}}
.relleno-humedad {{
height: 100%;
background: #00ffff;
width: {pct_humedad}%;
border-radius: 10px;
transition: width 0.5s ease;
}}
.footer {{
margin-top: 40px;
font-size: 16px;
color: #cccccc;
font-family: "Times New Roman", serif;
}}
</style>
</head>
<body>
<img class="logo" src="https://ingenieriamecanica.umsa.bo/wp-content/uploads/elementor/thumbs/LOGO-MEC-2020-BLANCO-Copy-qpznm7iahpr8nwh1p1zgqjt0b8bbc84pdklg5ncr9c.png" alt="Logo UMSA">
<h1>Lectura de Temperatura y Humedad</h1>
<div class="sensor">
<div>Temperatura Interna: {temp_interna:.1f} °C</div>
<div class="barra">
<div class="relleno-temp-interna"></div>
</div>
</div>
<div class="sensor">
<div>Temperatura DHT11: {temp_dht:.1f} °C</div>
<div class="barra">
<div class="relleno-temp-dht"></div>
</div>
</div>
<div class="sensor">
<div>Humedad DHT11: {hum_dht} %</div>
<div class="barra">
<div class="relleno-humedad"></div>
</div>
</div>
<div class="footer">Univ. Sinani Catacora Dylan Beyli</div>
<div class="footer">laboratorio 8</div>
</body>
</html>
"""
return html
mostrar_logo()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 80))
s.listen(5)
while True:
temp_interna = leer_temperatura_interna()
temp_dht, hum_dht = leer_dht11()
mostrar_datos_oled(temp_interna, temp_dht, hum_dht)
conn, addr = s.accept()
print('Conexión de %s' % str(addr))
request = conn.recv(1024)
response = web_page(temp_interna, temp_dht, hum_dht)
conn.send("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n")
conn.send(response)
conn.close()