]> defiant.homedns.org Git - pyshared.git/blob - humidity.py
Bootloader: Allow to set i2c addr
[pyshared.git] / humidity.py
1 #!/usr/bin/env python
2 # -*- coding: iso-8859-15 -*-
3
4 RS = 8314.3 # J/(kmol*K) universelle Gaskonstante
5 MW = 18.016 # kg/kmol Molekulargewicht des Wasserdampfes
6
7 # Openweathermap
8 STATION=2911298
9 API_ID="41ff32c10636d35b4c7a46000cb1a6a1"
10
11 import urllib
12 import json
13
14 # Sättigungsdampfdruck in hPa
15 def calc_saturation_vapor_pressure(temp_c):
16         if temp_c >= 0:
17                 a = 7.5
18                 b = 237.3
19         else:
20                 a = 7.6
21                 b = 240.7 
22         
23         return 6.1078 * 10**((a*temp_c)/(b+temp_c))
24
25
26 # https://www.wetterochs.de/wetter/feuchte.html
27 def calc_humidity_abs(temp_c, humdity_rel):
28         temp_k = temp_c + 273.15
29
30         # Dampfdruck in hPa
31         SDD = calc_saturation_vapor_pressure(temp_c)
32         DD = humdity_rel/100.0 * SDD
33         humidity_abs = 10**5 * MW/RS * DD/temp_k
34         return humidity_abs
35
36
37 def calc_humidity_rel(temp_c, humidity_abs):
38         temp_k = temp_c + 273.15
39         SDD = calc_saturation_vapor_pressure(temp_c)
40         DD = humidity_abs*temp_k * RS / (10**5*MW)
41         
42         humdity_rel = DD/SDD*100
43         return humdity_rel
44
45 def get_weather_current():
46         query = urllib.urlencode({'id': STATION, 'APPID': API_ID, 'lang': 'de'})
47         url = "http://api.openweathermap.org/data/2.5/weather?units=metric&%s" % query
48         response = urllib.urlopen(url)
49         results = response.read()
50         return json.loads(results)
51
52 def check_ventilate(temp_indoor, humdity_rel_indoor):
53         dWeatherCurrent = get_weather_current()
54         temp_outdoor = dWeatherCurrent["main"]["temp"]
55         humdity_rel_outdoor = dWeatherCurrent["main"]["humidity"]
56
57         humdity_abs_indoor = calc_humidity_abs(temp_indoor, humdity_rel_indoor)
58         humdity_abs_outdoor = calc_humidity_abs(temp_outdoor, humdity_rel_outdoor)
59
60         print "Wassergehalt Innen: %.2f, Außen: %.2f" % (humdity_abs_indoor, humdity_abs_outdoor)
61         return humdity_abs_indoor/humdity_abs_outdoor
62
63
64 if __name__ == "__main__":
65         check_ventilate(21, 50)