import network
import espnow
from machine import Pin, ADC, SoftI2C
import time
# --- CONFIGURACIÓN MANUAL (CAMBIA ESTO SEGÚN LA PLACA) ---
# En la placa de la PANTALLA pon: MODO_RECEPTOR = True
# En la placa del SENSOR pon: MODO_RECEPTOR = False
MODO_RECEPTOR = True
# Pausa para que el simulador no se bloquee
time.sleep(2)
# Configuración de Radio
sta = network.WLAN(network.STA_IF)
sta.active(True)
e = espnow.ESPNow()
e.active(True)
if MODO_RECEPTOR:
print("--- INICIANDO MODO RECEPTOR ---")
import ssd1306
# Usamos los pines 17 y 18 que tienes en el JSON
i2c = SoftI2C(scl=Pin(17), sda=Pin(18), freq=100000)
try:
oled = ssd1306.SSD1306_I2C(128, 64, i2c, addr=0x3c)
oled.fill(0)
oled.text("SISTEMA LISTO", 10, 30)
oled.show()
except Exception as err:
print("Error al conectar con la pantalla:", err)
while True:
host, msg = e.recv()
if msg:
try:
# El emisor envía "valor|estado"
dato = msg.decode('utf-8')
valor, estado = dato.split('|')
oled.fill(0)
oled.text("NIVEL OZONO", 20, 0)
oled.text("Valor: " + valor, 0, 30)
oled.text("Estado: " + estado, 0, 50)
oled.show()
print("Recibido: " + valor)
except:
pass
time.sleep(0.1)
else:
print("--- INICIANDO MODO EMISOR ---")
sensor = ADC(Pin(4))
sensor.atten(ADC.ATTN_11DB)
rele = Pin(5, Pin.OUT)
peer = b'\xff\xff\xff\xff\xff\xff'
e.add_peer(peer)
while True:
val = sensor.read()
alerta = "PELIGRO" if val > 2000 else "NORMAL"
rele.value(1 if alerta == "PELIGRO" else 0)
mensaje = "{0}|{1}".format(val, alerta)
e.send(peer, mensaje)
print("Enviando: " + mensaje)
time.sleep(1)Loading
esp32-s3-devkitc-1
esp32-s3-devkitc-1
Loading
esp32-s3-devkitc-1
esp32-s3-devkitc-1