11#!/usr/bin/python
22
33# Current time and temperature display for Raspberry Pi w/Adafruit Mini
4- # Thermal Printer. Retrieves data from Yahoo! weather , prints current
4+ # Thermal Printer. Retrieves data from DarkSky.net's API , prints current
55# conditions and time using large, friendly graphics.
66# See forecast.py for a different weather example that's all text-based.
77# Written by Adafruit Industries. MIT license.
1515
1616from __future__ import print_function
1717from Adafruit_Thermal import *
18- from xml . dom . minidom import parseString
19- import Image , ImageDraw , time , urllib
18+ import time , urllib , json
19+ from PIL import Image , ImageDraw
2020
21- # WOEID indicates the geographic location for the forecast. It is
22- # not a ZIP code or other common indicator. Instead, it can be found
23- # by 'manually' visiting http://weather.yahoo.com, entering a location
24- # and requesting a forecast, then copy the number from the end of the
25- # current URL string and paste it here.
26- WOEID = '2459115'
21+ API_KEY = "YOUR_API_KEY"
2722
28- # Fetch weather data from Yahoo!, parse resulting XML
29- dom = parseString (urllib .urlopen (
30- 'http://weather.yahooapis.com/forecastrss?w=' + WOEID ).read ())
23+ LAT = "40.726019"
24+ LONG = "-74.00536"
25+
26+ # Fetch weather data from DarkSky, parse resulting JSON
27+ url = "https://api.darksky.net/forecast/" + API_KEY + "/" + LAT + "," + LONG + "?exclude=[alerts,minutely,hourly,flags]&units=us"
28+ response = urllib .urlopen (url )
29+ data = json .loads (response .read ())
3130
3231# Extract values relating to current temperature, humidity, wind
33- temperature = int (dom .getElementsByTagName (
34- 'yweather:condition' )[0 ].getAttribute ('temp' ))
35- humidity = int (dom .getElementsByTagName (
36- 'yweather:atmosphere' )[0 ].getAttribute ('humidity' ))
37- windSpeed = int (dom .getElementsByTagName (
38- 'yweather:wind' )[0 ].getAttribute ('speed' ))
39- windDir = int (dom .getElementsByTagName (
40- 'yweather:wind' )[0 ].getAttribute ('direction' ))
41- windUnits = dom .getElementsByTagName (
42- 'yweather:units' )[0 ].getAttribute ('speed' )
32+
33+ temperature = int (data ['currently' ]['temperature' ])
34+ humidity = int (data ['currently' ]['humidity' ] * 100 );
35+ windSpeed = int (data ['currently' ]['windSpeed' ])
36+ windDir = data ['currently' ]['windBearing' ]
37+ windUnits = "mph"
38+
39+ # print(temperature)
40+ # print(humidity)
41+ # print(windSpeed)
42+ # print(windDir)
43+ # print(windUnits)
4344
4445# Although the Python Imaging Library does have nice font support,
4546# I opted here to use a raster bitmap for all of the glyphs instead.
6263
6364# Generate a list of sub-image glyphs cropped from the symbols image
6465def croplist (widths , x , y , height ):
65- list = []
66- for i in range (len (widths )):
67- list .append (symbols .crop (
68- [x , y + i * height , x + widths [i ], y + (i + 1 )* height ]))
69- return list
66+ list = []
67+ for i in range (len (widths )):
68+ list .append (symbols .crop (
69+ [x , y + i * height , x + widths [i ], y + (i + 1 )* height ]))
70+ return list
7071
7172# Crop glyph lists (digits, days of week, etc.)
7273TimeDigit = croplist (TimeDigitWidth , 0 , 0 , 44 )
@@ -91,20 +92,20 @@ def croplist(widths, x, y, height):
9192
9293# Paste a series of glyphs (mostly numbers) from string to img
9394def drawNums (str , x , y , list ):
94- for i in range (len (str )):
95- d = ord (str [i ]) - ord ('0' )
96- img .paste (list [d ], (x , y ))
97- x += list [d ].size [0 ] + 1
98- return x
95+ for i in range (len (str )):
96+ d = ord (str [i ]) - ord ('0' )
97+ img .paste (list [d ], (x , y ))
98+ x += list [d ].size [0 ] + 1
99+ return x
99100
100101# Determine total width of a series of glyphs in string
101102def numWidth (str , list ):
102- w = 0 # Cumulative width
103- for i in range (len (str )):
104- d = ord (str [i ]) - ord ('0' )
105- if i > 0 : w += 1 # Space between digits
106- w += list [d ].size [0 ] # Digit width
107- return w
103+ w = 0 # Cumulative width
104+ for i in range (len (str )):
105+ d = ord (str [i ]) - ord ('0' )
106+ if i > 0 : w += 1 # Space between digits
107+ w += list [d ].size [0 ] # Digit width
108+ return w
108109
109110# Render current time (always 24 hour XX:XX format)
110111t = time .localtime ()
@@ -134,12 +135,13 @@ def numWidth(str, list):
134135s2 = str (windSpeed )
135136winDirNum = 0 # Wind direction glyph number
136137if windSpeed > 0 :
137- for winDirNum in range (len (DirAngle ) - 1 ):
138- if windDir < DirAngle [winDirNum ]: break
138+ for winDirNum in range (len (DirAngle ) - 1 ):
139+ if windDir < DirAngle [winDirNum ]: break
140+ winDirNum += 1
139141w = Humidity .size [0 ] + 5 + numWidth (s , HumiDigit )
140142w2 = Wind .size [0 ] + 5 + numWidth (s2 , HumiDigit )
141143if windSpeed > 0 :
142- w2 += 3 + Dir [winDirNum ].size [0 ]
144+ w2 += 3 + Dir [winDirNum ].size [0 ]
143145if windUnits == 'kph' : w2 += 3 + Kph .size [0 ]
144146else : w2 += 3 + Mph .size [0 ]
145147if w2 > w : w = w2
@@ -154,14 +156,15 @@ def numWidth(str, list):
154156y += 23 # And advance to next line
155157img .paste (Wind , (x , y ))
156158x += Wind .size [0 ] + 5
159+
157160if windSpeed > 0 :
158- img .paste (Dir [winDirNum ], (x , y ))
159- x += Dir [winDirNum ].size [0 ] + 3
161+ img .paste (Dir [winDirNum ], (x , y ))
162+ x += Dir [winDirNum ].size [0 ] + 3
160163x = drawNums (s2 , x , y , HumiDigit ) + 3
161164if windUnits == 'kph' : img .paste (Kph , (x , y ))
162165else : img .paste (Mph , (x , y ))
163166
164167# Open connection to printer and print image
165- printer = Adafruit_Thermal ("/dev/ttyAMA0 " , 19200 , timeout = 5 )
168+ printer = Adafruit_Thermal ("/dev/serial0 " , 19200 , timeout = 5 )
166169printer .printImage (img , True )
167170printer .feed (3 )
0 commit comments