from wifi_lib import conecta 
import urequests
from umqttsimple import MQTTClient
import time
#from defs import *
from machine import Pin
from servo import Servo

mqtt_server = "broker.mqttdashboard.com"
mqtt_port = 1883
mqtt_user = ""
mqtt_password = ""
mqtt_client_id = "clientId-82CitkHpk59"

LED = Pin(5, Pin.OUT)
braco = Servo(4)

def recebi(topico, msg):
    braco.set_angle(int(msg.decode())) 

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

print("Conectando...")
station = conecta("Wokwi-GUEST","")
if not station.isconnected():
    print("Falha na conexão")
else:
    print("Conectado!")   
    print("Conectando broker MQTT HIVE")
    print("Conectado!") 

    client = MQTTClient(mqtt_client_id,
        mqtt_server,
        mqtt_port,
        mqtt_user,
        mqtt_password)
    client.connect()
    client.set_callback(recebi)
    client.subscribe("pucpr/iot/botao/bruna")

    #for i in range(3*60):
    #    client.publish("pucpr/iotmc/msg_node_red",f"{i}")
    #    client.sleep(1)

    client.disconnect()
    station.disconnect()