from machine import Pin
import wifi 
import time
from umqtt.simple import MQTTClient
import urequests as requests

# LCD
from machine import Pin, SoftI2C
from lcd_api import LcdApi
from i2c_lcd import I2cLcd

# Setup LCD
I2C_ADDR = 0x27
totalRows = 2
totalColumns = 16
i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=10000) 
lcd = I2cLcd(i2c, I2C_ADDR, totalRows, totalColumns)
lcd.putstr("Open weather")

#setup Pin of led
led = Pin(12, Pin.OUT)

#link to get current time
api_time = "https://timeapi.io/api/Time/current/zone?timeZone=Asia/Bangkok"
my_city = "HCMC"

# function to handle callback that receive from Broker
def handle_callback(topic, msg):
  print("Topic:", topic)
  if(topic == b'IoT_MQTT_Temperature'):
    t = float(msg)
    print(t)
    if t > 30:
      led.on()
    else:
      led.off()

  # Get current time in HCMC
  response = requests.get(url=api_time).json()
  hour = response['hour']
  minute = response['minute']

  # Reset LCD
  lcd.clear()   
  lcd.move_to(0, 0)
  lcd.putstr(f"{my_city}, {hour}:{minute}")
  lcd.move_to(0,1)
  lcd.putstr(f"Temp: {t}")
  
#connect wifi
wifi.connect_ap()

# connect broker
client_id = "8fabccea0b2a0678dcd9ea1b94f8bc47"
server = "broker.hivemq.com"
client = MQTTClient(client_id, server)
client.set_callback(handle_callback)
client.connect()

# Subcribe topics 
client.subscribe(b"IoT_MQTT_Temperature")
#client.subscribe(b"IoT_MQTT_Humidity")

def loop():
  while True:
    client.wait_msg()
    
if __name__ == "__main__":
  loop()


$abcdeabcde151015202530fghijfghij