import network
import ntptime
import urequests
import ssd1306
import utime
from machine import Pin, I2C
# =========================
# OLED SETUP
# =========================
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
# =========================
# WIFI
# =========================
SSID = "Wokwi-GUEST"
PASSWORD = ""
# =========================
# REGION SETTINGS
# =========================
CITY = "Nha Trang"
COUNTRY = "VN"
# Vietnam UTC+7
TIMEZONE_OFFSET = 7 * 3600
# =========================
# WEATHER API
# =========================
API_KEY = "53f0dd346797402af28fbd24210c874e"
last_weather_time = 0
weather_temp = 0
# =========================
# CONNECT WIFI
# =========================
def connect_wifi():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, PASSWORD)
oled.fill(0)
oled.text("Connecting WiFi", 0, 25)
oled.show()
while not wlan.isconnected():
utime.sleep(1)
oled.fill(0)
oled.text("WiFi Connected", 0, 20)
oled.text(wlan.ifconfig()[0], 0, 40)
oled.show()
utime.sleep(2)
# =========================
# TIME SYNC
# =========================
def sync_time():
try:
ntptime.settime()
except:
print("NTP Failed")
# =========================
# LOCAL TIME
# =========================
def get_time():
return utime.localtime(
utime.time() + TIMEZONE_OFFSET
)
# =========================
# WEATHER
# =========================
def get_weather():
try:
city_encoded = CITY.replace(" ", "%20")
url = (
"https://api.openweathermap.org/data/2.5/weather?q={},{}&appid={}&units=metric"
.format(
city_encoded,
COUNTRY,
API_KEY
)
)
print("Getting Weather...")
print(url)
r = urequests.get(url)
print("Status:", r.status_code)
data = r.json()
r.close()
temp = data["main"]["temp"]
return temp
except Exception as e:
print("Weather Error:", e)
return 0
# =========================
# STARTUP SCREEN
# =========================
def startup_screen():
oled.fill(0)
oled.rect(10, 10, 30, 40, 1)
oled.rect(15, 15, 20, 30, 1)
oled.text("MicroPython", 50, 10)
oled.text("Smart Watch", 40, 25)
oled.text(CITY, 50, 40)
oled.show()
utime.sleep(3)
# =========================
# MAIN DISPLAY
# =========================
def show_watch():
global last_weather_time
global weather_temp
lt = get_time()
hour = "{:02d}".format(lt[3])
minute = "{:02d}".format(lt[4])
second = "{:02d}".format(lt[5])
day = "{:02d}".format(lt[2])
month = "{:02d}".format(lt[1])
year = str(lt[0])
oled.fill(0)
# Border
oled.rect(0, 0, 128, 64, 1)
# Title
oled.text("SMART WATCH", 15, 5)
# Time
oled.text(
hour + ":" + minute + ":" + second,
18,
22
)
# Date
oled.text(
day + "/" + month + "/" + year,
18,
38
)
# Update weather every 10 mins
if utime.time() - last_weather_time >= 600:
weather_temp = get_weather()
last_weather_time = utime.time()
# Weather
oled.text(
str(int(weather_temp)) + " C",
42,
52
)
oled.show()
# =========================
# START PROGRAM
# =========================
connect_wifi()
startup_screen()
sync_time()
# First weather update
weather_temp = get_weather()
# =========================
# LOOP
# =========================
while True:
show_watch()
utime.sleep(1)