from machine import Pin
import wifi
import time
from umqtt.simple import MQTTClient
led = Pin(12, Pin.OUT)
def handle_callback(topic, msg):
print("Topic:", topic)
if(topic == b'py_iot/nhiet_do'):
t = float(msg)
print(t)
if t > 30:
led.on()
else:
led.off()
wifi.connect_ap()
# Ket noi den broker
client_id = "7d2f41ca73d2d1fff80548af5c19c7ed"
server = "broker.hivemq.com"
client = MQTTClient(client_id, server)
client.set_callback(handle_callback)
client.connect()
client.subscribe(b"py_iot/nhiet_do")
client.subscribe(b"py_iot/do_am")
# Subcribe các topic
# Đợi các tin nhắn từ broker gửi về
# Cần tạo callback => xử lý
def loop():
while True:
client.wait_msg()
if __name__ == "__main__":
loop()