Skip to content

Instantly share code, notes, and snippets.

@vwillcox
Created February 5, 2023 12:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vwillcox/28922a62ee4bf28cee599b9e44d29b3d to your computer and use it in GitHub Desktop.
Save vwillcox/28922a62ee4bf28cee599b9e44d29b3d to your computer and use it in GitHub Desktop.
Galactic Unicorn Current Weather Display
try:
import urequests as requests
except:
import requests
try:
import ujson as json
except:
import json
import network
import secrets
from picographics import PicoGraphics, DISPLAY_GALACTIC_UNICORN as DISPLAY
from galactic import GalacticUnicorn
import jpegdec, math, ntptime, time
galactic = GalacticUnicorn()
graphics = PicoGraphics(DISPLAY)
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 = 'YOURTOWN'
country_code = 'YOUR COUNTRY'
#example
#city = 'Lahore'
#country_code = 'PAK'
width = GalacticUnicorn.WIDTH
height = GalacticUnicorn.HEIGHT
open_weather_map_api_key = '<YOUR_KEY_PLEASE>'
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_display_if_reqd():
#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)) + '°C'
raw_min = weather_data.json().get('main').get('temp_min')-273.17
mintemp = str(round(raw_min))+'°C'
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+'/'+mintemp, 16, 2)
galactic.set_brightness(0.5)
# Display the result
while True:
redraw_display_if_reqd()
galactic.update(graphics)
#graphics.clear()
time.sleep(120)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment