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 Image , ImageDraw , time , urllib , json
2019
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'
20+ # API_KEY = "YOUR_API_KEY"
2721
28- # Fetch weather data from Yahoo!, parse resulting XML
29- dom = parseString (urllib .urlopen (
30- 'http://weather.yahooapis.com/forecastrss?w=' + WOEID ).read ())
22+ LAT = "40.726019"
23+ LONG = "-74.00536"
24+
25+ # Fetch weather data from DarkSky, parse resulting JSON
26+ url = "https://api.darksky.net/forecast/" + API_KEY + "/" + LAT + "," + LONG + "?exclude=[alerts,minutely,hourly,flags]&units=us"
27+ response = urllib .urlopen (url )
28+ data = json .loads (response .read ())
3129
3230# 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' )
31+
32+ temperature = int (data ['currently' ]['temperature' ])
33+ humidity = int (data ['currently' ]['humidity' ] * 100 );
34+ windSpeed = int (data ['currently' ]['windSpeed' ])
35+ windDir = str (data ['currently' ]['windBearing' ])
36+ windUnits = "mph"
37+
38+ # print(temperature)
39+ # print(humidity)
40+ # print(windSpeed)
41+ # print(windDir)
42+ # print(windUnits)
4343
4444# Although the Python Imaging Library does have nice font support,
4545# I opted here to use a raster bitmap for all of the glyphs instead.
6262
6363# Generate a list of sub-image glyphs cropped from the symbols image
6464def 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
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
7070
7171# Crop glyph lists (digits, days of week, etc.)
7272TimeDigit = croplist (TimeDigitWidth , 0 , 0 , 44 )
@@ -91,20 +91,20 @@ def croplist(widths, x, y, height):
9191
9292# Paste a series of glyphs (mostly numbers) from string to img
9393def 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
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
9999
100100# Determine total width of a series of glyphs in string
101101def 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
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
108108
109109# Render current time (always 24 hour XX:XX format)
110110t = time .localtime ()
@@ -134,12 +134,13 @@ def numWidth(str, list):
134134s2 = str (windSpeed )
135135winDirNum = 0 # Wind direction glyph number
136136if windSpeed > 0 :
137- for winDirNum in range (len (DirAngle ) - 1 ):
138- if windDir < DirAngle [winDirNum ]: break
137+ for winDirNum in range (len (DirAngle ) - 1 ):
138+ if windDir < DirAngle [winDirNum ]: break
139+ winDirNum += 1
139140w = Humidity .size [0 ] + 5 + numWidth (s , HumiDigit )
140141w2 = Wind .size [0 ] + 5 + numWidth (s2 , HumiDigit )
141142if windSpeed > 0 :
142- w2 += 3 + Dir [winDirNum ].size [0 ]
143+ w2 += 3 + Dir [winDirNum ].size [0 ]
143144if windUnits == 'kph' : w2 += 3 + Kph .size [0 ]
144145else : w2 += 3 + Mph .size [0 ]
145146if w2 > w : w = w2
@@ -154,14 +155,15 @@ def numWidth(str, list):
154155y += 23 # And advance to next line
155156img .paste (Wind , (x , y ))
156157x += Wind .size [0 ] + 5
158+
157159if windSpeed > 0 :
158- img .paste (Dir [winDirNum ], (x , y ))
159- x += Dir [winDirNum ].size [0 ] + 3
160+ img .paste (Dir [winDirNum ], (x , y ))
161+ x += Dir [winDirNum ].size [0 ] + 3
160162x = drawNums (s2 , x , y , HumiDigit ) + 3
161163if windUnits == 'kph' : img .paste (Kph , (x , y ))
162164else : img .paste (Mph , (x , y ))
163165
164166# Open connection to printer and print image
165167printer = Adafruit_Thermal ("/dev/ttyAMA0" , 19200 , timeout = 5 )
166168printer .printImage (img , True )
167- printer .feed (3 )
169+ printer .feed (3 )
0 commit comments