import urequests
import ujson
from machine import Pin, I2C
import ssd1306
import network
import time
# Kết nối Wi-Fi
ssid = 'Wokwi-GUEST'
password = ''
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect(ssid, password)
while not wifi.isconnected():
print("Đang kết nối Wi-Fi...")
time.sleep(1)
print("Kết nối thành công:", wifi.ifconfig())
# Cài đặt OLED
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
# Gọi API OpenWeatherMap
API_KEY = 'b9b2d6d7c7e6e62e0c5c328c24a98870'
lat = '9.916551724013'
lon = '105.14427879556978'
url = f'https://api.openweathermap.org/data/2.5/weather?lat=9.916551724013&lon=105.14427879556978&appid=b9b2d6d7c7e6e62e0c5c328c24a98870&units=metric'
try:
response = urequests.get(url)
data = response.json()
temp = data['main']['temp']
weather = data['weather'][0]['description']
name = data['name']
# Hiển thị trên OLED
oled.fill(0) # Xóa màn hình
oled.text('Thoi tiet:', 0, 0)
oled.text(f'Temp: {temp}C', 0, 16)
oled.text(weather, 0, 32)
oled.text(f'Name: {name}', 0, 48)
oled.show()
response.close()
except Exception as e:
oled.fill(0)
oled.text('API Error!', 0, 0)
oled.show()
print("Lỗi:", e)