Toggle Navigation
Hatchery
Eggs
weather2019
weather.py
Users
Badges
Login
Register
MCH2022 badge?
go to mch2022.badge.team
weather.py
raw
Content
import ugfx, gc, wifi, badge, deepsleep, easydraw, system, time from time import sleep, localtime import urequests as requests ugfx.init() # Make sure WiFi is connected try: wifi.init() ugfx.clear(ugfx.WHITE); ugfx.string(10,10,"Waiting for wifi...","Roboto_Regular12", 0) ugfx.flush() while not wifi.sta_if.isconnected(): sleep(0.1) except: wifi.connect() easydraw.messageCentered("Connecting...", True, "/media/wifi.png") if not wifi.wait(): easydraw.messageCentered("Could not connect!", True, "/media/alert.png") time.sleep(2) system.home() easydraw.messageCentered("Connected!", True, "/media/wifi.png") timepos = (10, 30) iconpos = (5, 60) temppos = (10, 105) icons = { "clear-day": "\uf00d", "clear-night": "\uf02e", "rain": "\uf019", "snow": "\uf01b", "sleet": "\uf0b5", "wind": "\uf050", "fog": "\uf021", "cloudy": "\uf041", "partly-cloudy-day": "\uf002", "partly-cloudy-night": "\uf086" } try: import consts _ = consts.INFO_FIRMWARE_NAME # Simple check if we're on the platform firmware... # The platform firwmare has a new weather font :-) icons = { "clear-day": 14, # Sun "clear-night": 47, # Moon "rain": 79, # Rain "snow": 28, # Cloud with snow "sleet": 24, # Cloud with spotty rain "wind": 81, # Wind "fog": 100, # Fog "cloudy": 66, # Cloud "partly-cloudy-day": 3, # Cloud with sun "partly-cloudy-night": 135 # Cloud with moon } except: print("Older firmware detected!!") pass def getDisplayName(time): #if time[3] == 0: # n = "Night" #if time[3] == 6: # n = "Morning" #if time[3] == 12: # n = "Noon" #if time[3] == 18: # n = "Evening" #return str(time[2]) + "." + str(time[1]) + ". " + n return str(time[2]) + "." + str(time[1]) + ". " + str(time[3]) + ":00" # loading screen ugfx.clear(ugfx.WHITE); ugfx.string(10,10,"Getting weather from Dark Sky ...","Roboto_Regular12", 0) ugfx.string(110,100,"https://darksky.net/poweredby/","Roboto_Regular12", 0) ugfx.flush() # fetching forecast data url = "https://badge.team/weather" r = requests.get(url) gc.collect() data = r.json() r.close() # aligning according to time hourly = data["hourly"]["data"] starttime = localtime(hourly[0]["time"]) index = starttime[3] if starttime[3] > 6: index = 12 - starttime[3] if starttime[3] > 12: index = 18 - starttime[3] if starttime[3] > 18: index = 24 - starttime[3] print(index) # displaying forecast ugfx.clear(); ugfx.line(0, 26, 296, 26, 0) ugfx.line(0, 44, 296, 44, 0) ugfx.string(5, 5, "Weather Forecast for Zehdenick, DE", "pixelade13", 0) dp = hourly[index] time = localtime(dp["time"]) ugfx.string(timepos[0], timepos[1], getDisplayName(time), "pixelade13", 0) ugfx.string(iconpos[0], iconpos[1], icons[dp["icon"]], "weather42", 0) ugfx.string(temppos[0], temppos[1], str(int(dp["temperature"]))+"°C", "DejaVuSans20", 0) print(dp["icon"]) dp = hourly[index+6] time = localtime(dp["time"]) ugfx.string(timepos[0] + 1*11 + 1*64, timepos[1], getDisplayName(time), "pixelade13", 0) ugfx.string(iconpos[0] + 1*11 + 1*64, iconpos[1], icons[dp["icon"]], "weather42", 0) ugfx.string(temppos[0] + 1*11 + 1*64, temppos[1], str(int(dp["temperature"]))+"°C", "DejaVuSans20", 0) print(dp["icon"]) dp = hourly[index+12] time = localtime(dp["time"]) ugfx.string(timepos[0] + 2*11 + 2*64, timepos[1], getDisplayName(time), "pixelade13", 0) ugfx.string(iconpos[0] + 2*11 + 2*64, iconpos[1], icons[dp["icon"]], "weather42", 0) ugfx.string(temppos[0] + 2*11 + 2*64, temppos[1], str(int(dp["temperature"]))+"°C", "DejaVuSans20", 0) print(dp["icon"]) dp = hourly[index+18] time = localtime(dp["time"]) ugfx.string(timepos[0] + 3*11 + 3*64, timepos[1], getDisplayName(time), "pixelade13", 0) ugfx.string(iconpos[0] + 3*11 + 3*64, iconpos[1], icons[dp["icon"]], "weather42", 0) ugfx.string(temppos[0] + 3*11 + 3*64, temppos[1], str(int(dp["temperature"]))+"°C", "DejaVuSans20", 0) print(dp["icon"]) ugfx.flush(ugfx.LUT_FULL); badge.eink_busy_wait() deepsleep.start_sleeping(60000)