Last active
April 29, 2021 01:20
-
-
Save extrasleepy/df4c5b4a0e17f4bf305bae06fa30e63b to your computer and use it in GitHub Desktop.
This file contains 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 time | |
from requests import get | |
import requests | |
import json | |
import os | |
import subprocess | |
import digitalio | |
import board | |
from PIL import Image, ImageDraw, ImageFont | |
import adafruit_rgb_display.st7789 as st7789 | |
settings = { | |
'api_key':'YOUR_KEY', | |
'zip_code':'94112', | |
'country_code':'us', | |
'temp_unit':'imperial'} #unit can be metric, imperial, or kelvin | |
BASE_URL = "http://api.openweathermap.org/data/2.5/weather?appid={0}&zip={1},{2}&units={3}" | |
cs_pin = digitalio.DigitalInOut(board.CE0) | |
dc_pin = digitalio.DigitalInOut(board.D25) | |
reset_pin = None | |
# Config for display baudrate (default max is 24mhz): | |
BAUDRATE = 64000000 | |
# Setup SPI bus using hardware SPI: | |
spi = board.SPI() | |
# Create the ST7789 display: | |
disp = st7789.ST7789( | |
spi, | |
cs=cs_pin, | |
dc=dc_pin, | |
rst=reset_pin, | |
baudrate=BAUDRATE, | |
width=240, | |
height=240, | |
x_offset=0, | |
y_offset=80, | |
) | |
# Create blank image for drawing. | |
# Make sure to create image with mode 'RGB' for full color. | |
height = disp.width # we swap height/width to rotate it to landscape! | |
width = disp.height | |
image = Image.new("RGB", (width, height)) | |
rotation = 180 | |
# Get drawing object to draw on image. | |
draw = ImageDraw.Draw(image) | |
# First define some constants to allow easy resizing of shapes. | |
padding = 0 | |
top = padding | |
bottom = height - padding | |
# Move left to right keeping track of the current x position for drawing shapes. | |
x = 0 | |
# Alternatively load a TTF font. Make sure the .ttf font file is in the | |
# same directory as the python script! | |
# Some other nice fonts to try: http://www.dafont.com/bitmap.php | |
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 48) | |
# Turn on the backlight | |
backlight = digitalio.DigitalInOut(board.D22) | |
backlight.switch_to_output() | |
backlight.value = True | |
while True: | |
y=top | |
# Draw a black filled box to clear the image. | |
draw.rectangle((0, 0, width, height), outline=0, fill=0) | |
final_url = BASE_URL.format(settings["api_key"],settings["zip_code"],settings["country_code"],settings["temp_unit"]) | |
weather_data = get(final_url).json()['weather'][0]['icon'] | |
image_url = "http://openweathermap.org/img/wn/"+ weather_data+"@2x.png" | |
img_data = requests.get(image_url).content | |
with open(weather_data+'.png', 'wb') as handler: | |
handler.write(img_data) | |
image = Image.open(weather_data+'.png') | |
image_ratio = image.width / image.height | |
screen_ratio = width / height | |
if screen_ratio < image_ratio: | |
scaled_width = image.width * height // image.height | |
scaled_height = height | |
else: | |
scaled_width = width | |
scaled_height = image.height * width // image.width | |
image = image.resize((scaled_width, scaled_height), Image.BICUBIC) | |
# Crop and center the image | |
x = scaled_width // 2 - width // 2 | |
y = scaled_height // 2 - height // 2 | |
image = image.crop((x, y, x + width, y + height)) | |
# Get drawing object to draw on image. | |
draw = ImageDraw.Draw(image) | |
disp.image(image, rotation) | |
time.sleep(10) #get new data every 10 seconds | |
y=top | |
# Draw a black filled box to clear the image. | |
draw.rectangle((0, 0, width, height), outline=0, fill=0) | |
final_url = BASE_URL.format(settings["api_key"],settings["zip_code"],settings["country_code"],settings["temp_unit"]) | |
weather_data = get(final_url).json()['weather'][0]['main'] | |
temp_data = requests.get(final_url).json() | |
temperature = temp_data["main"]["temp"] | |
#print the date for 5 sec | |
draw.text((x, y),weather_data, font=font, fill="#FFFFFF") | |
y += font.getsize(weather_data)[1] | |
draw.text((x, 50),str(temperature), font=font, fill="#FFFFFF") | |
# Display image. | |
disp.image(image, rotation) | |
time.sleep(10) #get new data every 10 seconds |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment