from machine import Pin, I2C
import time
import framebuf
try:
from ssd1306 import SSD1306_I2C
except ImportError:
print("Error: ssd1306 no encontrada. Añade ssd1306.py estándar.")
while True:
time.sleep(1)
try:
import onewire
import ds18x20
print("Bibliotecas onewire y ds18x20 cargadas correctamente")
except ImportError as e:
print("Error: No se encontraron onewire o ds18x20. Añade onewire.py y ds18x20.py.")
while True:
time.sleep(1)
# Configuración I2C para OLED
try:
i2c = I2C(0, scl=Pin(1), sda=Pin(0), freq=200000)
print("Escaneando bus I2C...")
devices = i2c.scan()
if devices:
print("Dispositivos I2C encontrados:", [hex(device) for device in devices])
else:
print("¡No se encontraron dispositivos I2C! Revisa el esquema.")
while True:
time.sleep(1)
except Exception as e:
print("Error inicializando I2C:", e)
while True:
time.sleep(1)
# Inicializar OLED
WIDTH = 128
HEIGHT = 64
try:
oled = SSD1306_I2C(WIDTH, HEIGHT, i2c)
except Exception as e:
print("Error inicializando OLED:", e)
while True:
time.sleep(1)
# Configuración OneWire para DS18B20
try:
ow = onewire.OneWire(Pin(26))
ds = ds18x20.DS18X20(ow)
except Exception as e:
print("Error inicializando OneWire:", e)
oled.fill(0)
oled.text("Error OneWire", 0, 20)
oled.show()
while True:
time.sleep(1)
# Escanear sensores con reintentos
print("Escaneando bus OneWire...")
for _ in range(3):
try:
ow.reset()
roms = ds.scan()
if len(roms) >= 3:
print("Sensores DS18B20 encontrados:", [hex(r) for r in roms])
break
print("Intento fallido. Encontrados:", len(roms), "sensores")
time.sleep(1)
except Exception as e:
print("Error escaneando:", e)
time.sleep(1)
else:
print("¡Error! Se encontraron", len(roms), "sensores. Se esperan 3.")
oled.fill(0)
oled.text("Error: Sensores", 0, 20)
oled.text("faltantes", 0, 30)
oled.show()
while True:
time.sleep(1)
# Logo "VINK€$"
logo = bytearray([
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 8, 88, 0, 8, 64, 16, 16, 65, 4, 0, 0, 0, 0, 0,
2, 0, 0, 0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 0, 107, 224, 64, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64,
0, 3, 192, 240, 255, 225, 192, 120, 48, 14, 31, 239, 192, 255, 128, 0,
0, 1, 192, 240, 120, 225, 240, 112, 16, 58, 23, 252, 195, 229, 32, 0,
0, 3, 192, 240, 14, 1, 240, 56, 112, 252, 30, 0, 7, 128, 240, 16,
0, 3, 192, 112, 14, 1, 116, 120, 50, 240, 30, 0, 67, 128, 0, 0,
4, 3, 192, 240, 14, 1, 207, 120, 127, 128, 23, 254, 7, 255, 128, 0,
0, 1, 192, 176, 14, 33, 195, 120, 63, 128, 31, 252, 1, 255, 224, 8,
0, 3, 128, 240, 14, 1, 64, 184, 115, 192, 22, 0, 0, 0, 240, 0,
0, 1, 227, 226, 14, 1, 192, 120, 112, 248, 30, 8, 7, 16, 240, 0,
16, 0, 152, 64, 14, 1, 64, 48, 80, 52, 50, 0, 5, 129, 160, 0,
0, 0, 62, 0, 251, 161, 192, 120, 112, 31, 31, 127, 195, 255, 96, 0,
0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 10, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 64, 0, 64, 0, 0, 0, 1, 1, 0, 64, 0, 0, 16,
0, 0, 0, 0, 167, 243, 0, 0, 0, 0, 239, 224, 128, 0, 0, 0,
0, 0, 0, 0, 7, 243, 0, 0, 0, 0, 237, 224, 0, 0, 0, 0,
0, 0, 0, 32, 0, 3, 0, 0, 0, 0, 96, 0, 0, 128, 0, 0,
0, 0, 0, 0, 3, 243, 0, 0, 0, 0, 15, 64, 0, 2, 0, 0,
0, 0, 0, 64, 0, 3, 0, 0, 0, 0, 96, 8, 0, 0, 0, 64,
2, 0, 0, 0, 0, 0, 128, 0, 0, 1, 224, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 63, 245, 255, 223, 0, 0, 0, 128, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 12, 113, 198, 24, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 12, 113, 66, 8, 0, 0, 0, 0, 2, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 32, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 2, 0, 4, 0, 0,
0, 0, 0, 0, 0, 0, 0, 128, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0
])
# Crear buffer de imagen para el logo
try:
fb = framebuf.FrameBuffer(logo, 128, 64, framebuf.MONO_HLSB)
except Exception as e:
print("Error creando buffer de logo:", e)
oled.fill(0)
oled.text("Error logo", 0, 20)
oled.show()
while True:
time.sleep(1)
# Mostrar el logo
oled.fill(0)
oled.blit(fb, 0, 0)
oled.show()
time.sleep(3)
# Mostrar mensaje "Sistema listo"
oled.fill(0)
oled.text("Sistema listo", 15, 30)
oled.show()
time.sleep(2)
# Bucle para leer y mostrar temperaturas
while True:
try:
# Iniciar conversión de temperatura
ds.convert_temp()
time.sleep_ms(750)
# Leer temperaturas
temps = []
for rom in roms[:3]:
temp = ds.read_temp(rom)
temps.append(temp)
# Limpiar pantalla
oled.fill(0)
# Mostrar temperaturas
oled.text("Zona1: {:.1f}C".format(temps[0]), 0, 10)
oled.text("Zona2: {:.1f}C".format(temps[1]), 0, 30)
oled.text("Zona3: {:.1f}C".format(temps[2]), 0, 50)
# Actualizar pantalla
oled.show()
except Exception as e:
print("Error leyendo sensores:", e)
oled.fill(0)
oled.text("Error lectura", 0, 20)
oled.text("sensores", 0, 30)
oled.show()
time.sleep(10)