-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathunicornmini_print_temp.py
63 lines (45 loc) · 1.91 KB
/
unicornmini_print_temp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import time
import sys
from colorsys import hsv_to_rgb
from PIL import Image, ImageDraw, ImageFont
from unicornhatmini import UnicornHATMini
import requests
settings = {
'api_key':'your_key_here',
'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}"
# The text we want to display. You should probably keep this line and replace it below
# That way you'll have a guide as to what characters are supported!
text = "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789 #@&!?{}<>[]();:.,'%*=+-=$_\\/ :-)"
unicornhatmini = UnicornHATMini()
rotation = 0
if len(sys.argv) > 1:
try:
rotation = int(sys.argv[1])
except ValueError:
print("Usage: {} <rotation>".format(sys.argv[0]))
sys.exit(1)
unicornhatmini.set_rotation(rotation)
display_width, display_height = unicornhatmini.get_shape()
print("{}x{}".format(display_width, display_height))
unicornhatmini.set_brightness(0.1)
font = ImageFont.truetype("5x7.ttf", 8)
text_width, text_height = font.getsize(text)
image = Image.new('P', (text_width + display_width + display_width, display_height), 0)
draw = ImageDraw.Draw(image)
# Draw the text into the image
draw.text((0, 0), text, font=font, fill=255)
while True:
final_url = BASE_URL.format(settings["api_key"],settings["zip_code"],settings["country_code"],settings["temp_unit"])
weather_data = requests.get(final_url).json()
temperature = weather_data["main"]["temp"]
print(temperature)
text=str(temperature)
image = Image.new('P', (text_width + display_width + display_width, display_height), 0)
draw = ImageDraw.Draw(image)
draw.text((0, 0), text, font=font, fill=255)
unicornhatmini.set_image(image, 0, wrap=True)
unicornhatmini.show()
time.sleep(60) #get new data every 60 seconds