import time
import network
import urequests as requests
#import libuary of 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) #initializing the I2C method for ESP32
lcd = I2cLcd(i2c, I2C_ADDR, totalRows, totalColumns)
lcd.putstr("Hello")
#Connect to the Network
#Step 1: Create the request to the Openweather website
#Step 2: Catch the response => handle JSON
#Step 3: Print in the console
ssid = 'Wokwi-GUEST'
password = ''
print("Connecting to WiFi", end="")
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect(ssid, password)
while not sta_if.isconnected():
print(".", end="")
time.sleep(0.1)
print(" Connected!")
my_id = "2cfea83a122baaa50ca3f7b0fa4b3b11"
my_url = f"https://api.openweathermap.org/data/2.5/weather?id=1566083&appid={my_id}&lang=vi&units=metric"
my_city = "HaNoi"
api_url = "https://timeapi.io/api/Time/current/zone?timeZone=Asia/Bangkok"
while True:
#get current time
response = requests.get(url=api_url).json()
hour = response ['hour']
minute = response ['minute']
lcd.move_to(0,0)
lcd.putstr(f"{my_city}, {hour}:{minute}")
#each 30 minutes the lcd will update the weather
if(minute % 1 == 0): #when the current minute divide out 1 the
res = requests.get(url=my_url).json()
print("Current weather: ", res['weather'][0]["description"])
# Temperature
temperature = res['main']['temp']
#print("Temperature:", temperature)
# Humidity
humidity = res['main']['humidity']
#print("Humidity:", humidity)
lcd.move_to(0,1)
lcd.putstr(f"T:{temperature}, H:{humidity} ")
time.sleep(10)