#Importo las librerias necesarias
import network
import urequests
import ujson
import time
from machine import Pin
# --- Configuración WiFi ---
WIFI_SSID = "Wokwi-GUEST"
WIFI_PASS = ""
# --- Configuración Bot de Telegram ---
TOKEN = "8308037183:AAFL15M3le80ICxW8ZqCmElXOo5HTPq_H4o" # pega aquí el token de BotFather
CHAT_ID = "1617651795" # tu chat id de Telegram
URL = "https://api.telegram.org/bot" + TOKEN
# --- Configuración LED ---
led = Pin(2, Pin.OUT)
# --- Conexión WiFi ---
print("Conectando a WiFi...")
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect(WIFI_SSID, WIFI_PASS)
while not wifi.isconnected():
time.sleep(0.5)
print("Conectado:", wifi.ifconfig())
# --- Obtener actualizaciones de Telegram ---
offset = 0
while True:
try:
res = urequests.get(URL + "/getUpdates?offset=" + str(offset))
datos = res.json()
res.close()
for r in datos["result"]:
offset = r["update_id"] + 1
mensaje = r["message"]["text"]
print("Mensaje recibido:", mensaje)
if mensaje.upper() == "ON":
led.value(1)
urequests.get(URL + f"/sendMessage?chat_id={CHAT_ID}&text=LED Encendido")
elif mensaje.upper() == "OFF":
led.value(0)
urequests.get(URL + f"/sendMessage?chat_id={CHAT_ID}&text=LED Apagado")
except Exception as e:
print("Error:", e)
time.sleep(2) # espera 2 segundos antes de pedir nuevos mensajes