from time import sleep
import time
import network
import urequests
from ds1307 import DS1307
from machine import Pin, I2C, Timer
from rotary_irq_rp2 import RotaryIRQ
from ssd1306_setup import HEIGHT, WIDTH, setup
from writer import Writer
import courier20
def refresh_weather(t):
api_r = urequests.get("http://api.open-meteo.com/v1/forecast?latitude=44.38055097790471&longitude=-73.22704831282502¤t=temperature_2m,apparent_temperature&daily=temperature_2m_max,temperature_2m_min,sunrise,sunset,precipitation_probability_max&timezone=auto")
# Lat/long is for Hack Club HQ. Change to your location manually.
weather = api_r.json()
update()
def update():
page = r.value()
oled.fill(0)
if page == 1:
Writer.set_textpos(oled, 12, 0)
time_str = ""
if rtc.hour > 12:
time_str = time_str + str(rtc.hour-12)
else:
time_str = time_str + str(rtc.hour)
time_str = time_str + ":"
if rtc.minute <= 10:
time_str = time_str + "0" + str(rtc.minute)
else:
time_str = time_str + str(rtc.minute)
oled.text("[NOW] " + time_str, 2, 2)
cour.printstring(str(weather["current"]["temperature_2m"]) + "C")
cour.printstring("\nFL " + str(weather["current"]["apparent_temperature"]) + "C")
if page == 2:
Writer.set_textpos(oled, 12, 0)
oled.text("[TODAY]", 2, 2)
cour.printstring("L: " + str(weather["daily"]["temperature_2m_min"][0]) + "C")
cour.printstring("\nH: " + str(weather["daily"]["temperature_2m_max"][0]) + "C")
oled.text("MAX RAIN: " + str(weather["daily"]["precipitation_probability_max"][0]) + "%", 2, 52)
if page == 3:
Writer.set_textpos(oled, 12, 0)
oled.text("[TOMORROW]", 2, 2)
cour.printstring("L: " + str(weather["daily"]["temperature_2m_min"][1]) + "C")
cour.printstring("\nH: " + str(weather["daily"]["temperature_2m_max"][1]) + "C")
oled.text("MAX RAIN: " + str(weather["daily"]["precipitation_probability_max"][1]) + "%", 2, 52)
if page == 4:
Writer.set_textpos(oled, 12, 0)
oled.text("[IN 2 DAYS]", 2, 2)
cour.printstring("L: " + str(weather["daily"]["temperature_2m_min"][2]) + "C")
cour.printstring("\nH: " + str(weather["daily"]["temperature_2m_max"][2]) + "C")
oled.text("MAX RAIN: " + str(weather["daily"]["precipitation_probability_max"][2]) + "%", 2, 52)
if page == 5:
Writer.set_textpos(oled, 12, 0)
oled.text("[IN 3 DAYS]", 2, 2)
cour.printstring("L: " + str(weather["daily"]["temperature_2m_min"][3]) + "C")
cour.printstring("\nH: " + str(weather["daily"]["temperature_2m_max"][3]) + "C")
oled.text("MAX RAIN: " + str(weather["daily"]["precipitation_probability_max"][3]) + "%", 2, 52)
if page == 5:
Writer.set_textpos(oled, 12, 0)
oled.text("[IN 4 DAYS]", 2, 2)
cour.printstring("L: " + str(weather["daily"]["temperature_2m_min"][4]) + "C")
cour.printstring("\nH: " + str(weather["daily"]["temperature_2m_max"][4]) + "C")
oled.text("MAX RAIN: " + str(weather["daily"]["precipitation_probability_max"][4]) + "%", 2, 52)
if page == 6:
Writer.set_textpos(oled, 12, 0)
oled.text("[IN 5 DAYS]", 2, 2)
cour.printstring("L: " + str(weather["daily"]["temperature_2m_min"][5]) + "C")
cour.printstring("\nH: " + str(weather["daily"]["temperature_2m_max"][5]) + "C")
oled.text("MAX RAIN: " + str(weather["daily"]["precipitation_probability_max"][5]) + "%", 2, 52)
if page == 7:
Writer.set_textpos(oled, 12, 0)
oled.text("[IN 6 DAYS]", 2, 2)
cour.printstring("L: " + str(weather["daily"]["temperature_2m_min"][6]) + "C")
cour.printstring("\nH: " + str(weather["daily"]["temperature_2m_max"][6]) + "C")
oled.text("MAX RAIN: " + str(weather["daily"]["precipitation_probability_max"][6]) + "%", 2, 52)
oled.show()
print("Connecting to Wokwi-GUEST", end="")
net = network.WLAN(network.STA_IF)
net.active(True)
net.connect('Wokwi-GUEST', '')
while not net.isconnected():
print(".", end="")
sleep(0.3)
print("Wi-Fi connected!")
print(net.ifconfig())
r = RotaryIRQ(pin_num_clk=12,
pin_num_dt=13,
min_val=1,
max_val=7,
reverse=False,
range_mode=RotaryIRQ.RANGE_WRAP)
val_old = r.value()
api_r = urequests.get("http://api.open-meteo.com/v1/forecast?latitude=44.38055097790471&longitude=-73.22704831282502¤t=temperature_2m,apparent_temperature&daily=temperature_2m_max,temperature_2m_min,sunrise,sunset,precipitation_probability_max&timezone=auto")
# Lat/long is for Hack Club HQ. Change to your location manually.
weather = api_r.json()
oled = setup(False)
rtc_i2c = I2C(0, scl=Pin(5), sda=Pin(4), freq=200000)
rtc = DS1307(addr=0x68, i2c=rtc_i2c)
print("RTC connected")
r.set(value=1)
r.add_listener(update)
cour = Writer(oled, courier20)
print("Setup done!")
print(r.value())
print(rtc.datetime)
update()
update_t = Timer()
update_t.init(mode=Timer.PERIODIC, period=60000, callback=refresh_weather)
while True:
sleep(10)
Loading
pi-pico-w
pi-pico-w
Loading
ssd1306
ssd1306