meigrafd

weather

Oct 14th, 2015
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.82 KB | None | 0 0
  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"
  8. updateEvery = 300 #sec
  9. pir_pin = 18
  10.  
  11. #------------------------------------------------------------------------
  12.  
  13. GPIO.setmode(GPIO.BCM)
  14. GPIO.setup(pir_pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
  15.  
  16. # define dictionary http://www.tutorialspoint.com/python/python_dictionary.htm
  17. dictionary = {}
  18.  
  19. def getForecast(city):
  20.     print("getForecast:", city)
  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)
  30.         dictionary['forecast'] = getForecast(city)
  31.         timedDataUpdate = threading.Timer( period, updateWeatherData, [period] )
  32.         timedDataUpdate.start()
  33.  
  34. def displayWeather(channel):
  35.     print("displayWeather:", dictionary['forecast']['list'])
  36.     for day in dictionary['forecast']['list']:
  37.         wetter_id=day['weather'][0]['id']
  38.         humidity=day['humidity']
  39.         temp_min=day['temp']['min']
  40.         temp_max=day['temp']['max']
  41.         temp_morning=day['temp']['morn']
  42.         temp_evening=day['temp']['eve']
  43.         temp=(temp_min + temp_max + temp_morning + temp_evening) / 4.0
  44.         wind_speed=day['speed']
  45.     #calculating the felt temperature
  46.     if (temp >= 26.7) and (humidity >= 40):
  47.         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))
  48.     elif temp <=10.0:
  49.         temp=float(13.12 + (0.6215 * temp) - (11.37 * wind_speed ** .16) + (.3965 * temp * wind_speed ** .16))
  50.     #bad weather colors
  51.     if wetter_id >= 600 and wetter_id<= 622:
  52.         os.system("irsend SEND_ONCE Led WHITE")#Snow
  53.     elif (wetter_id >= 300 and wetter_id<= 321) or (wetter_id >=200 and wetter_id <=232) or (wetter_id >=500 and wetter_id<=531):
  54.         os.system("irsend SEND_ONCE Led VIOLETT")#Rain
  55.     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:
  56.         os.system("irsend SEND_ONCE Led STROBE")#heavy Storm, alarm function
  57.     #temperature colors
  58.     #improvements:
  59.     #fix the temperature scale to get better results in human felt temperature, like green. 17 degrees can be a bit cold and 26 hot
  60.     #maybe i could use 12V WS2801 Modules and show the results like a clock, like 3:00, 6:00, 9:00, 12:00.
  61.     #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
  62.     elif temp <=-5:
  63.         os.system("irsend SEND_ONCE Led BLUE_5")#darker blue
  64.     elif temp >=-5 and temp <=0:
  65.         os.system("irsend SEND_ONCE Led BLUE_4")
  66.     elif temp >=0 and temp <=5:
  67.         os.system("irsend SEND_ONCE Led BLUE_3")
  68.     elif temp >=5 and temp <=10:
  69.         os.system("irsend SEND_ONCE Led BLUE_2")
  70.     elif temp >=10 and temp <=12.5:
  71.         os.system("irsend SEND_ONCE Led BLUE_1")
  72.     elif temp >=12.5 and temp <=17:
  73.         os.system("irsend SEND_ONCE Led GREEN_1")
  74.     elif temp >=17 and temp <=23:
  75.         os.system("irsend SEND_ONCE Led GREEN_2")
  76.     elif temp >=23 and temp <=26:
  77.         os.system("irsend SEND_ONCE Led RED_1")#yellow
  78.     elif temp >=26 and temp <=29:
  79.         os.system("irsend SEND_ONCE Led RED_2")#orange
  80.     elif temp >=29 and temp <=32:
  81.         os.system("irsend SEND_ONCE Led RED_2")#orange
  82.     elif temp >=32 and temp <=35:
  83.         os.system("irsend SEND_ONCE Led RED_2")#orange
  84.     elif temp >=35 and temp <=38:
  85.         os.system("irsend SEND_ONCE Led RED_2")#red
  86.     else:
  87.         os.system("irsend SEND_ONCE Led PINK")#violet
  88.     time.sleep(120)
  89.     os.system("irsend SEND_ONCE Led OFF")
  90.  
  91. def main():
  92.     dictionary['update'] = True
  93.     dictionary['forecast'] = getForecast(city)
  94.     timedDataUpdate = threading.Timer( float(updateEvery), updateWeatherData, [float(updateEvery)] )
  95.     timedDataUpdate.start()
  96.     #call displayWeather if pir_pin is rising and dont recognize new GPIO changes for 5000ms
  97.     GPIO.add_event_detect(pir_pin, GPIO.RISING, callback=displayWeather, bouncetime=5000)
  98.     while True:
  99.        time.sleep(1000)
  100.        
  101.  
  102. if __name__ == '__main__':
  103.     try:
  104.         main()
  105.     except (KeyboardInterrupt, SystemExit):
  106.         print("\nQuit\n")
  107.         dictionary['update'] = False
  108.         GPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment