forked from BaReinhard/Hacktoberfest-Raspberry-Pi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweather_conditions
More file actions
66 lines (52 loc) · 1.77 KB
/
weather_conditions
File metadata and controls
66 lines (52 loc) · 1.77 KB
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
64
65
66
#Get weather conditions on your pi using API darksky https://darksky.net/dev/docs you need to register and get the API key (it's free)
from urllib2 import urlopen
import json
import time
apikey="yourapikey"
lati ="" #find your latitude and longitude from google maps.
longi = ""
try:
#get the data from the api website
url="https://api.forecast.io/forecast/"+apikey+"/"+lati+","+longi+"?units=si"
oldTemp = 0
#in case the Internet is not working: try it but then use the oldTemp just in case
try:
meteo=urlopen(url).read()
meteo = meteo.decode('utf-8')
weather = json.loads(meteo)
currentTemp = weather['currently']['temperature']
condition = weather['currently']['icon']
wind = weather['currently']['windSpeed']
except IOError:
currentTemp = oldTemp
oldTemp = currentTemp #set oldTemp to last known temperature
print currentTemp
print condition
print wind
#Weather conditions will be written on a file txt
mytemp=str(condition) +" "+ str(currentTemp)+" "+str(wind)
file = open('temp.txt','w')
file.write(mytemp)
#use different colour LED to display temperature (e.g. red if temperature over 25C, yellow if temperature over 17C etc..."
if 0 < currentTemp < 10:
GPIO.output(27,GPIO.HIGH)
GPIO.output(23,GPIO.LOW)
GPIO.output(22,GPIO.LOW)
GPIO.output(24,GPIO.LOW)
if 10 < currentTemp < 17:
GPIO.output(24,GPIO.HIGH)
GPIO.output(23,GPIO.LOW)
GPIO.output(22,GPIO.LOW)
GPIO.output(27,GPIO.LOW)
if 17 < currentTemp < 25:
GPIO.output(23,GPIO.HIGH)
GPIO.output(24,GPIO.LOW)
GPIO.output(22,GPIO.LOW)
GPIO.output(27,GPIO.LOW)
if currentTemp > 25:
GPIO.output(22,GPIO.HIGH)
GPIO.output(23,GPIO.LOW)
GPIO.output(24,GPIO.LOW)
GPIO.output(27,GPIO.LOW)
except KeyboardInterrupt:
print("Exit")