Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- import RPi.GPIO as GPIO
- import time, os, threading, urllib.request, json
- #------------------------------------------------------------------------
- city = "Duisburg&APPID=xxxxxxxxxxxxxxx"
- updateEvery = 300 #sec
- runTime = 120 #sec
- pir_pin = 18
- #------------------------------------------------------------------------
- GPIO.setmode(GPIO.BCM)
- GPIO.setup(pir_pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
- # define dictionary http://www.tutorialspoint.com/python/python_dictionary.htm
- dictionary = {}
- def getForecast(city):
- url = "http://api.openweathermap.org/data/2.5/forecast/daily?cnt=1&units=metric&mode=json&q="
- url = url + city
- req = urllib.request.Request(url)
- response=urllib.request.urlopen(req)
- return json.loads(response.read().decode("UTF-8"))
- def updateWeatherData(period):
- if dictionary['update'] == True:
- dictionary['forecast'] = getForecast(city)
- timedDataUpdate = threading.Timer( period, updateWeatherData, [period] )
- timedDataUpdate.start()
- def displayWeather():
- for day in dictionary['forecast']['list']:
- wetter_id=day['weather'][0]['id']
- humidity=day['humidity']
- temp_min=day['temp']['min']
- temp_max=day['temp']['max']
- temp_morning=day['temp']['morn']
- temp_evening=day['temp']['eve']
- temp=(temp_min + temp_max + temp_morning + temp_evening) / 4.0
- wind_speed=day['speed']
- #calculating the felt temperature
- if (temp >= 26.7) and (humidity >= 40):
- 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))
- elif temp <=10.0:
- temp=float(13.12 + (0.6215 * temp) - (11.37 * wind_speed ** .16) + (.3965 * temp * wind_speed ** .16))
- #bad weather colors
- if wetter_id >= 600 and wetter_id<= 622:
- os.system("irsend SEND_ONCE Led WHITE")#Snow
- elif (wetter_id >= 300 and wetter_id<= 321) or (wetter_id >=200 and wetter_id <=232) or (wetter_id >=500 and wetter_id<=531):
- os.system("irsend SEND_ONCE Led VIOLETT")#Rain
- 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:
- os.system("irsend SEND_ONCE Led STROBE")#heavy Storm, alarm function
- #temperature colors
- #improvements:
- #fix the temperature scale to get better results in human felt temperature, like green. 17 degrees can be a bit cold and 26 hot
- #maybe i could use 12V WS2801 Modules and show the results like a clock, like 3:00, 6:00, 9:00, 12:00.
- #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
- elif temp <=-5:
- os.system("irsend SEND_ONCE Led BLUE_5")#darker blue
- elif temp >=-5 and temp <=0:
- os.system("irsend SEND_ONCE Led BLUE_4")
- elif temp >=0 and temp <=5:
- os.system("irsend SEND_ONCE Led BLUE_3")
- elif temp >=5 and temp <=10:
- os.system("irsend SEND_ONCE Led BLUE_2")
- elif temp >=10 and temp <=12.5:
- os.system("irsend SEND_ONCE Led BLUE_1")
- elif temp >=12.5 and temp <=17:
- os.system("irsend SEND_ONCE Led GREEN_1")
- elif temp >=17 and temp <=23:
- os.system("irsend SEND_ONCE Led GREEN_2")
- elif temp >=23 and temp <=26:
- os.system("irsend SEND_ONCE Led RED_1")#yellow
- elif temp >=26 and temp <=29:
- os.system("irsend SEND_ONCE Led RED_2")#orange
- elif temp >=29 and temp <=32:
- os.system("irsend SEND_ONCE Led RED_2")#orange
- elif temp >=32 and temp <=35:
- os.system("irsend SEND_ONCE Led RED_2")#orange
- elif temp >=35 and temp <=38:
- os.system("irsend SEND_ONCE Led RED_2")#red
- else:
- os.system("irsend SEND_ONCE Led PINK")#violet
- def interrupt_event(pin):
- if dictionary['running'] == False:
- dictionary['running'] = True
- dictionary['startTime'] = time.time()
- displayWeather_thread = threading.Thread(target=displayWeather)
- displayWeather_thread.start()
- elif dictionary['running'] == True:
- dictionary['startTime'] = time.time()
- def main():
- dictionary['forecast'] = getForecast(city)
- dictionary['update'] = True
- dictionary['running'] = False
- dictionary['startTime'] = 0
- timedDataUpdate = threading.Timer( float(updateEvery), updateWeatherData, [float(updateEvery)] )
- timedDataUpdate.start()
- GPIO.add_event_detect(pir_pin, GPIO.RISING, callback=interrupt_event)
- while True:
- time.sleep(1)
- diff = time.time() - dictionary['startTime']
- if diff >= runTime:
- dictionary['running'] = False
- os.system("irsend SEND_ONCE Led OFF")
- if __name__ == '__main__':
- try:
- main()
- except (KeyboardInterrupt, SystemExit):
- print("\nQuit\n")
- dictionary['update'] = False
- GPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement