-
-
Save edwork/a39c8b076901ac1b8ff42d542982b515 to your computer and use it in GitHub Desktop.
A threaded weather and clock display for the #Pimoroni #GlacticUnicorn
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import urequests as requests | |
| import json | |
| import network | |
| import secrets | |
| from picographics import PicoGraphics, DISPLAY_GALACTIC_UNICORN as DISPLAY | |
| from galactic import GalacticUnicorn | |
| import jpegdec, math, ntptime, time | |
| import _thread | |
| rtc = machine.RTC() | |
| year, month, day, wd, hour, minute, second, _ = rtc.datetime() | |
| galactic = GalacticUnicorn() | |
| graphics = PicoGraphics(DISPLAY) | |
| #Timer global variables | |
| last_time_sync = time.time() | |
| last_cheerlight_check = time.time() | |
| utc_offset = 0 | |
| def sync_time(): | |
| global wlan | |
| if not wifi_available: | |
| return | |
| if not wlan.isconnected(): | |
| wifi_connect() | |
| try: | |
| ntptime.settime() | |
| #print("Time set") | |
| except OSError as e: | |
| print(e) | |
| pass | |
| def sync_time_if_reqd(): | |
| global last_time_sync | |
| if time.time() - last_time_sync > 86400: #Sync once per day | |
| #print ("Running Time Sync") | |
| sync_time() | |
| last_time_sync = time.time() | |
| WHITE = graphics.create_pen(255, 255, 255) | |
| BLACK = graphics.create_pen(0, 0, 0) | |
| BACKGROUND_COLOUR = (10, 0, 96) # Blue | |
| OUTLINE_COLOUR = (0, 0, 0) | |
| MESSAGE_COLOUR = (255, 255, 255) | |
| city = 'SOMEPLACE' | |
| country_code = 'SOMECOUNTRY' | |
| #example | |
| #city = 'Lahore' | |
| #country_code = 'PAK' | |
| width = GalacticUnicorn.WIDTH | |
| height = GalacticUnicorn.HEIGHT | |
| open_weather_map_api_key = '<YOUR API KEY>' | |
| station = network.WLAN(network.STA_IF) | |
| station.active(True) | |
| station.connect(secrets.WIFI_SSID, secrets.WIFI_PASSWORD) | |
| while station.isconnected() == False: | |
| pass | |
| def outline_text(text, x, y): | |
| graphics.set_pen(BLACK) | |
| graphics.text(text, x - 1, y - 1, -1, 1) | |
| graphics.text(text, x, y - 1, -1, 1) | |
| graphics.text(text, x + 1, y - 1, -1, 1) | |
| graphics.text(text, x - 1, y, -1, 1) | |
| graphics.text(text, x + 1, y, -1, 1) | |
| graphics.text(text, x - 1, y + 1, -1, 1) | |
| graphics.text(text, x, y + 1, -1, 1) | |
| graphics.text(text, x + 1, y + 1, -1, 1) | |
| graphics.set_pen(WHITE) | |
| graphics.text(text, x, y, -1, 1) | |
| def set_background(): | |
| # global cheercolor | |
| graphics.set_pen(graphics.create_pen(10,0,16)) | |
| for x in range(14,width): | |
| for y in range(0,height): | |
| graphics.pixel(x, y) | |
| print('Connection successful') | |
| def redraw_weather(): | |
| while True: | |
| #set your unique OpenWeatherMap.org URL | |
| open_weather_map_url = 'http://api.openweathermap.org/data/2.5/weather?q=' + city + ',' + country_code + '&APPID=' + open_weather_map_api_key | |
| weather_data = requests.get(open_weather_map_url) | |
| # Location (City and Country code) | |
| location = weather_data.json().get('name') | |
| # Weather Description | |
| description = weather_data.json().get('weather')[0].get('main') | |
| print(description) | |
| if (description == "Clouds"): | |
| icon="greycloud.jpeg" | |
| else: | |
| icon="sunny.jpeg" | |
| #icon="thunder.jpg" | |
| raw_temperature = weather_data.json().get('main').get('temp')-273.15 | |
| temperature = str(round(raw_temperature)) + '°' | |
| raw_min = weather_data.json().get('main').get('temp_min')-273.17 | |
| mintemp = str(round(raw_min))+'°' | |
| print(temperature) | |
| set_background() | |
| j = jpegdec.JPEG(graphics) | |
| j.open_file(icon) | |
| j.decode(0, 0, jpegdec.JPEG_SCALE_FULL) | |
| graphics.set_font("bitmap8") | |
| graphics.set_pen(WHITE) | |
| outline_text(temperature,16, 2) | |
| galactic.update(graphics) | |
| time.sleep(120) | |
| def redraw_time(): | |
| while True: | |
| global year, month, day, wd, hour, minute, second, last_second | |
| # Display the result | |
| year, month, day, wd, hour, minute, second, _ = rtc.datetime() | |
| clock = "{:02}:{:02}".format(hour, minute) | |
| # set the font | |
| #graphics.set_font("bitmap8") | |
| # calculate text position so that it is centred | |
| w = graphics.measure_text(clock, 1) | |
| #x = int(width / 2 - w / 2 + 1) | |
| x = 10 | |
| y = 2 | |
| outline_text(clock, 32, y) | |
| galactic.update(graphics) | |
| time.sleep(0.01) | |
| while True: | |
| second_thread = _thread.start_new_thread(redraw_time, ()) | |
| redraw_weather() | |
| #galactic.update(graphics) | |
| #graphics.clear() | |
| time.sleep(30) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment