import network
import urequests
import time
from machine import Pin
# Configuração dos LEDs
led1 = Pin(10, Pin.OUT) # L1
led2 = Pin(11, Pin.OUT) # L2
# Configuração WiFi
SSID = "Wokwi-GUEST"
PASSWORD = ""
# Endpoint
URL = "https://ishikawamarketing.pythonanywhere.com/micro"
def conectar_wifi():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, PASSWORD)
print("Conectando ao WiFi", end="")
while not wlan.isconnected():
print(".", end="")
time.sleep(0.5)
print()
print("Conectado! IP:", wlan.ifconfig()[0])
def controlar_leds(dispositivos):
for item in dispositivos:
for equip, status in item.items():
if equip == "L1":
if status == 1:
led1.value(1)
print("L1: LIGADA → LED ON")
else:
led1.value(0)
print("L1: DESLIGADA → LED OFF")
elif equip == "L2":
if status == 1:
led2.value(1)
print("L2: LIGADA → LED ON")
else:
led2.value(0)
print("L2: DESLIGADA → LED OFF")
# Conecta ao WiFi
conectar_wifi()
# Loop principal
while True:
try:
resposta = urequests.get(URL)
print(resposta.text)
dados = resposta.json()
resposta.close()
dispositivos = dados.get("dispositivos", [])
controlar_leds(dispositivos)
except Exception as e:
print("Erro na requisição:", e)
time.sleep(1)