from machine import ADC, Pin, I2C
import onewire, ds18x20, time
import ssd1306
# --- CONFIGURACIÓN OLED ---
i2c = I2C(0, scl=Pin(5), sda=Pin(4)) # GP5 = SCL, GP4 = SDA
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
# --- SENSOR DE TEMPERATURA INTERNA RP2040 ---
sensor_temp = ADC(4)
CONVERSION = 3.3 / 65535
OFFSET = 0.706
COEF = 0.001721
def leer_temp_interna():
lectura = sensor_temp.read_u16()
voltaje = lectura * CONVERSION
temp_c = 27 - (voltaje - OFFSET) / COEF
return temp_c
# --- SENSOR EXTERNO DS18B20 ---
pin_ds = Pin(26) # GP26 físico 31
ds = ds18x20.DS18X20(onewire.OneWire(pin_ds))
roms = ds.scan()
if not roms:
print("No se detectaron sensores DS18B20.")
while True:
oled.fill(0)
oled.text("ERROR: Sin", 0, 20)
oled.text("DS18B20", 0, 35)
oled.show()
time.sleep(2)
# --- UMBRAL DE TEMPERATURA ---
UMBRAL = 30.0
# --- BUCLE PRINCIPAL ---
while True:
temp_int = leer_temp_interna()
ds.convert_temp()
time.sleep_ms(750)
temp_ext = ds.read_temp(roms[0])
print(f"Interna: {temp_int:.2f}C | Externa: {temp_ext:.2f}C")
oled.fill(0)
if temp_int > UMBRAL or temp_ext > UMBRAL:
oled.text("!ALERTA!", 20, 20)
oled.text("Alta temperatura", 0, 40)
else:
oled.text("Temperatura", 20, 20)
oled.text("normal", 40, 40)
oled.show()
time.sleep(2)