import network
import time
import urequests
from machine import Pin, SPI
import framebuf
from epaper2in9 import EPD
# ================= USER CONFIG =================
WIFI_SSID = "Wokwi-GUEST"
WIFI_PASS = ""
API_KEY = "2ec18a416ab34d06bde00532260301"
CITY = "Warszawa"
COUNTRY = "Polska"
UPDATE_INTERVAL = 1800 # 30 minutes
# ==============================================
# ================= WIFI ========================
def connect_wifi():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
wlan.connect(WIFI_SSID, WIFI_PASS)
while not wlan.isconnected():
time.sleep(0.5)
# ==============================================
# ================= WEATHER =====================
def get_weather():
url = (
"https://api.weatherapi.com/v1/current.json?key=2ec18a416ab34d06bde00532260301&q=Warszawa"
)
r = urequests.get(url)
data = r.json()
r.close()
return {
"temp": int(data["current"]["temp_c"]),
"feels": int(data["current"]["feelslike_c"]),
"humidity": data["current"]["humidity"],
"pressure": data["current"]["pressure_mb"],
"wind": data["current"]["wind_kph"],
"desc": data["current"]["condition"]["text"],
}
# ==============================================
# ================= DISPLAY =====================
spi = SPI(
2,
baudrate=2000000,
polarity=0,
phase=0,
sck=Pin(18),
mosi=Pin(23),
miso=Pin(19),
)
epd = EPD(
spi,
cs=Pin(5),
dc=Pin(17),
rst=Pin(16),
busy=Pin(4),
)
WIDTH = 296
HEIGHT = 128
buf = bytearray(WIDTH * HEIGHT // 8)
fb = framebuf.FrameBuffer(buf, WIDTH, HEIGHT, framebuf.MONO_HLSB)
def draw_big(text, x, y, scale=2):
"""Simple text scaling using built-in font"""
for dx in range(scale):
for dy in range(scale):
fb.text(text, x + dx, y + dy, 0)
def draw_weather(w):
fb.fill(1)
# Header
fb.text(CITY.upper(), 4, 2, 0)
fb.hline(0, 14, WIDTH, 0)
# Big temperature
draw_big(f"{w['temp']}C", 5, 25, 3)
# Condition
fb.text(w["desc"], 5, 70, 0)
fb.text(f"Feels {w['feels']}C", 5, 85, 0)
# Right side stats
fb.text(f"Hum {w['humidity']}%", 170, 30, 0)
fb.text(f"Pres {w['pressure']}hPa", 170, 45, 0)
fb.text(f"Wind {w['wind']}m/s", 170, 60, 0)
# Footer time
t = time.localtime()
fb.text(
"{:02}:{:02} {:02}/{:02}".format(
t[3], t[4], t[2], t[1]
),
170,
100,
0,
)
epd.init()
epd.buffer = buf
epd.display_frame()
epd.sleep()
# ==============================================
# ================= MAIN ========================
connect_wifi()
while True:
try:
weather = get_weather()
draw_weather(weather)
except Exception as e:
print("Error:", e)
time.sleep(UPDATE_INTERVAL)
# ==============================================
Loading
epaper-2in9
epaper-2in9