from wifi_lib import conecta
from umqttsimple import MQTTClient
import time
from machine import Pin
import urequests

LED = Pin(5, Pin.OUT)  
PIR = Pin(14, Pin.IN)  

mqtt_server = "broker.hivemq.com"  
mqtt_port = 1883
mqtt_user = ""
mqtt_password = ""
mqtt_client_id = "clientId-OcGHWDGjS0-wokwi"


def recebi(topico, msg):
    print(topico.decode())
    print(msg.decode())
    if msg.decode() == "MOVIMENTO DETECTADO":
        LED.on()
    else:
        LED.off()


print("Conectando...")
station = conecta("Wokwi-GUEST", "")
if not station.isconnected():
    print("Falha na conexão")
else:
    print("Conectado Wi-Fi")
    print("Conectando Broker HiveMQTT...")
    client = MQTTClient(mqtt_client_id,
        mqtt_server,
        mqtt_port,
        mqtt_user,
        mqtt_password)
    client.connect()

    client.publish("pgp/iot/msg_wokwi", "teste wokwi")
    time.sleep(5)

    client.set_callback(recebi)
    client.subscribe("pgp/iot/msg_hive")
    client.sleep(60)

client.disconnect()
station.disconnect()