View difference between Paste ID: SRLS6bXM and 69kK4tiT
SHOW: | | - or go back to the newest paste.
1
#!/usr/bin/python3
2
import RPi.GPIO as GPIO
3
import time, os, threading, urllib.request, json
4
5
#------------------------------------------------------------------------
6
7-
city = "Duisburg&APPID=7064d39148beac195ec2a777d2598c58"
7+
city = "Duisburg&APPID=xxxxxxxxxxxxxxx"
8
updateEvery = 300 #sec
9
runTime = 120 #sec
10
pir_pin = 18
11
12
#------------------------------------------------------------------------
13
14
GPIO.setmode(GPIO.BCM)
15
GPIO.setup(pir_pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
16
17
# define dictionary http://www.tutorialspoint.com/python/python_dictionary.htm
18
dictionary = {}
19
20-
    print("getForecast:", city)
20+
21
    url = "http://api.openweathermap.org/data/2.5/forecast/daily?cnt=1&units=metric&mode=json&q="
22
    url = url + city
23
    req = urllib.request.Request(url)
24
    response=urllib.request.urlopen(req)
25
    return json.loads(response.read().decode("UTF-8"))
26
27
def updateWeatherData(period):
28
    if dictionary['update'] == True:
29-
        print("updateWeatherData:", period)
29+
30
        timedDataUpdate = threading.Timer( period, updateWeatherData, [period] )
31
        timedDataUpdate.start()
32
33
def displayWeather():
34-
def displayWeather(channel):
34+
35-
    print("displayWeather:", dictionary['forecast']['list'])
35+
36
        humidity=day['humidity']
37
        temp_min=day['temp']['min']
38
        temp_max=day['temp']['max']
39
        temp_morning=day['temp']['morn']
40
        temp_evening=day['temp']['eve']
41
        temp=(temp_min + temp_max + temp_morning + temp_evening) / 4.0
42
        wind_speed=day['speed']
43
    #calculating the felt temperature
44
    if (temp >= 26.7) and (humidity >= 40):
45
        temp=float(-8.784695+(1.61139411*temp)+(2.338549*humidity)+(-0.14611605*temp*humidity)+(-1.2308094e-2*temp*temp)+(-1.6424828e-2*humidity*humidity)+(2.211732e-3*temp*temp*humidity)+(7.2546e-4*temp*humidity*humidity)+ (-3.582e-6*temp*temp*humidity*humidity))
46
    elif temp <=10.0:
47
        temp=float(13.12 + (0.6215 * temp) - (11.37 * wind_speed ** .16) + (.3965 * temp * wind_speed ** .16))
48
    #bad weather colors
49
    if wetter_id >= 600 and wetter_id<= 622:
50
        os.system("irsend SEND_ONCE Led WHITE")#Snow
51
    elif (wetter_id >= 300 and wetter_id<= 321) or (wetter_id >=200 and wetter_id <=232) or (wetter_id >=500 and wetter_id<=531):
52
        os.system("irsend SEND_ONCE Led VIOLETT")#Rain
53
    elif wetter_id==232 or wetter_id==212 or wetter_id==202 or wetter_id==502 or wetter_id==503 or wetter_id==504 or wetter_id==602 or wetter_id==622 or wetter_id==771 or wetter_id==781 or wetter_id>=950:
54
        os.system("irsend SEND_ONCE Led STROBE")#heavy Storm, alarm function
55
    #temperature colors
56
    #improvements:
57
    #fix the temperature scale to get better results in human felt temperature, like green. 17 degrees can be a bit cold and 26 hot
58
    #maybe i could use 12V WS2801 Modules and show the results like a clock, like 3:00, 6:00, 9:00, 12:00.
59
    #my idea is to put it inside an small ribba case, but on the downside this thing maybe can't light a small room in the dark
60
    elif temp <=-5:
61
        os.system("irsend SEND_ONCE Led BLUE_5")#darker blue
62
    elif temp >=-5 and temp <=0:
63
        os.system("irsend SEND_ONCE Led BLUE_4")
64
    elif temp >=0 and temp <=5:
65
        os.system("irsend SEND_ONCE Led BLUE_3")
66
    elif temp >=5 and temp <=10:
67
        os.system("irsend SEND_ONCE Led BLUE_2")
68
    elif temp >=10 and temp <=12.5:
69
        os.system("irsend SEND_ONCE Led BLUE_1")
70
    elif temp >=12.5 and temp <=17:
71
        os.system("irsend SEND_ONCE Led GREEN_1")
72
    elif temp >=17 and temp <=23:
73
        os.system("irsend SEND_ONCE Led GREEN_2")
74
    elif temp >=23 and temp <=26:
75
        os.system("irsend SEND_ONCE Led RED_1")#yellow
76
    elif temp >=26 and temp <=29:
77
        os.system("irsend SEND_ONCE Led RED_2")#orange
78
    elif temp >=29 and temp <=32:
79
        os.system("irsend SEND_ONCE Led RED_2")#orange
80
    elif temp >=32 and temp <=35:
81
        os.system("irsend SEND_ONCE Led RED_2")#orange
82
    elif temp >=35 and temp <=38:
83
        os.system("irsend SEND_ONCE Led RED_2")#red
84
    else:
85
        os.system("irsend SEND_ONCE Led PINK")#violet
86
87
def interrupt_event(pin):
88-
    time.sleep(120)
88+
    if dictionary['running'] == False:
89-
    os.system("irsend SEND_ONCE Led OFF")
89+
        dictionary['running'] = True
90
        dictionary['startTime'] = time.time()
91
        displayWeather_thread = threading.Thread(target=displayWeather)
92
        displayWeather_thread.start()
93
    elif dictionary['running'] == True:
94
        dictionary['startTime'] = time.time()
95
96-
    #call displayWeather if pir_pin is rising and dont recognize new GPIO changes for 5000ms
96+
97-
    GPIO.add_event_detect(pir_pin, GPIO.RISING, callback=displayWeather, bouncetime=5000)
97+
98
    dictionary['update'] = True
99-
       time.sleep(1000)
99+
    dictionary['running'] = False
100
    dictionary['startTime'] = 0
101
    timedDataUpdate = threading.Timer( float(updateEvery), updateWeatherData, [float(updateEvery)] )
102
    timedDataUpdate.start()
103
    GPIO.add_event_detect(pir_pin, GPIO.RISING, callback=interrupt_event)
104
    while True:
105
        time.sleep(1)
106
        diff = time.time() - dictionary['startTime']
107
        if diff >= runTime:
108
            dictionary['running'] = False
109
            os.system("irsend SEND_ONCE Led OFF")
110
        
111
112
if __name__ == '__main__':
113
    try:
114
        main()
115
    except (KeyboardInterrupt, SystemExit):
116
        print("\nQuit\n")
117
        dictionary['update'] = False
118
        GPIO.cleanup()