Skip to content

Commit cd0ca27

Browse files
authored
Merge pull request adafruit#43 from Nathanllee1/openweathermap
Migrate from DarkSky to OpenWeather
2 parents 3e7be0b + 655d2ba commit cd0ca27

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed
15.2 KB
Binary file not shown.

timetemp.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,31 @@
1515

1616
from __future__ import print_function
1717
from Adafruit_Thermal import *
18-
import time, urllib, json
18+
import time, urllib.request, json
1919
from PIL import Image, ImageDraw
2020

21-
API_KEY = "YOUR_API_KEY"
21+
API_KEY = "YOUR_OPEN_WEATHER_API_KEY"
2222

23-
LAT = "40.726019"
24-
LONG = "-74.00536"
23+
cityName = "YOUR_CITY_NAME"
2524

2625
# 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)
26+
url = f"http://api.openweathermap.org/data/2.5/weather?q={cityName}&appid={API_KEY}"
27+
response = urllib.request.urlopen(url)
2928
data = json.loads(response.read())
30-
29+
print(data)
3130
# Extract values relating to current temperature, humidity, wind
3231

33-
temperature = int(data['currently']['temperature'])
34-
humidity = int(data['currently']['humidity'] * 100);
35-
windSpeed = int(data['currently']['windSpeed'])
36-
windDir = data['currently']['windBearing']
32+
temperature = (int(data['main']['temp']) - 273.15) * 9/5 + 32
33+
humidity = int(data['main']['humidity'] * 100);
34+
windSpeed = int(data['wind']['speed'])
35+
windDir = data['wind']['deg']
3736
windUnits = "mph"
3837

3938
# print(temperature)
40-
# print(humidity)
41-
# print(windSpeed)
42-
# print(windDir)
43-
# print(windUnits)
39+
# print(humidity)
40+
# print(windSpeed)
41+
# print(windDir)
42+
# print(windUnits)
4443

4544
# Although the Python Imaging Library does have nice font support,
4645
# I opted here to use a raster bitmap for all of the glyphs instead.
@@ -137,7 +136,7 @@ def numWidth(str, list):
137136
if windSpeed > 0:
138137
for winDirNum in range(len(DirAngle) - 1):
139138
if windDir < DirAngle[winDirNum]: break
140-
winDirNum+=1
139+
winDirNum+=1
141140
w = Humidity.size[0] + 5 + numWidth(s, HumiDigit)
142141
w2 = Wind.size[0] + 5 + numWidth(s2, HumiDigit)
143142
if windSpeed > 0:

0 commit comments

Comments
 (0)