import network, time, urequests
from machine import Pin
from utelegram import Bot
import privado
# Token de telegram
TOKEN = privado.token
# Objetos
bot = Bot(TOKEN)
sensor = Pin(19, Pin.IN, Pin.PULL_DOWN)
rele = Pin(22, Pin.OUT)
# Conexión WiFi
def conectaWiFi(red, password):
global miRed
miRed = network.WLAN(network.STA_IF)
if not miRed.isconnected():
miRed.active(True)
miRed.connect(red, password)
print('Conectando a la red', red + ".........")
timeout = time.ticks_ms() # ✅ ticks_ms
while not miRed.isconnected():
if time.ticks_diff(time.ticks_ms(), timeout) > 10000: # ✅ 10 segundos
return False
return True
# BOT decoradores — ✅ fuera del if, al nivel raíz
@bot.add_message_handler("Menu")
def menu(update):
update.reply("Activar sistema: On Desactivar sistema: Off")
print("Menú enviado")
@bot.add_message_handler("On")
def activar(update):
update.reply("Sistema activado")
print("Sistema activado")
medicion = sensor.value()
if medicion == 1: # PIR da 1 cuando detecta
rele.value(1)
update.reply("¡Alerta! Se detectó movimiento")
print("Movimiento detectado")
else:
rele.value(0)
update.reply("Todo normal")
@bot.add_message_handler("Off") # ✅ corregido "botd" → "bot"
def desactivar(update):
rele.value(0)
update.reply("Sistema desactivado")
print("Sistema desactivado")
# Arranque
if conectaWiFi("Wokwi-GUEST", ""):
print("Conexión exitosa!")
print('Datos de red:', miRed.ifconfig())
bot.start_loop()
else:
print("Imposible conectar")
miRed.active(False)