]> defiant.homedns.org Git - pyshared.git/commitdiff
added i2c/humidity
authorErik Andresen <erik@vontaene.de>
Sun, 25 Dec 2016 10:23:11 +0000 (11:23 +0100)
committerErik Andresen <erik@vontaene.de>
Sun, 25 Dec 2016 10:23:11 +0000 (11:23 +0100)
humidity.py [new file with mode: 0755]
i2c.py [new file with mode: 0755]

diff --git a/humidity.py b/humidity.py
new file mode 100755 (executable)
index 0000000..9e4d439
--- /dev/null
@@ -0,0 +1,65 @@
+#!/usr/bin/env python
+# -*- coding: iso-8859-15 -*-
+
+RS = 8314.3 # J/(kmol*K) universelle Gaskonstante
+MW = 18.016 # kg/kmol Molekulargewicht des Wasserdampfes
+
+# Openweathermap
+STATION=2911298
+API_ID="41ff32c10636d35b4c7a46000cb1a6a1"
+
+import urllib
+import json
+
+# Sättigungsdampfdruck in hPa
+def calc_saturation_vapor_pressure(temp_c):
+       if temp_c >= 0:
+               a = 7.5
+               b = 237.3
+       else:
+               a = 7.6
+               b = 240.7 
+       
+       return 6.1078 * 10**((a*temp_c)/(b+temp_c))
+
+
+# https://www.wetterochs.de/wetter/feuchte.html
+def calc_humidity_abs(temp_c, humdity_rel):
+       temp_k = temp_c + 273.15
+
+       # Dampfdruck in hPa
+       SDD = calc_saturation_vapor_pressure(temp_c)
+       DD = humdity_rel/100.0 * SDD
+       humidity_abs = 10**5 * MW/RS * DD/temp_k
+       return humidity_abs
+
+
+def calc_humidity_rel(temp_c, humidity_abs):
+       temp_k = temp_c + 273.15
+       SDD = calc_saturation_vapor_pressure(temp_c)
+       DD = humidity_abs*temp_k * RS / (10**5*MW)
+       
+       humdity_rel = DD/SDD*100
+       return humdity_rel
+
+def get_weather_current():
+       query = urllib.urlencode({'id': STATION, 'APPID': API_ID, 'lang': 'de'})
+       url = "http://api.openweathermap.org/data/2.5/weather?units=metric&%s" % query
+       response = urllib.urlopen(url)
+       results = response.read()
+       return json.loads(results)
+
+def check_ventilate(temp_indoor, humdity_rel_indoor):
+       dWeatherCurrent = get_weather_current()
+       temp_outdoor = dWeatherCurrent["main"]["temp"]
+       humdity_rel_outdoor = dWeatherCurrent["main"]["humidity"]
+
+       humdity_abs_indoor = calc_humidity_abs(temp_indoor, humdity_rel_indoor)
+       humdity_abs_outdoor = calc_humidity_abs(temp_outdoor, humdity_rel_outdoor)
+
+       print "Wassergehalt Innen: %.2f, Außen: %.2f" % (humdity_abs_indoor, humdity_abs_outdoor)
+       return humdity_abs_indoor - humdity_abs_outdoor
+
+
+if __name__ == "__main__":
+       check_ventilate(21, 50) 
diff --git a/i2c.py b/i2c.py
new file mode 100755 (executable)
index 0000000..2e9a109
--- /dev/null
+++ b/i2c.py
@@ -0,0 +1,72 @@
+#!/usr/bin/env python
+# -*- coding: iso-8859-15 -*-
+
+import threading
+import inspect
+import os
+import logging
+from ctypes import *
+from time import sleep
+
+DEBUG=0
+logger = logging.getLogger(__name__)
+
+class i2c:
+       libc = CDLL("libc.so.6")
+       I2C_SLAVE = 0x0703  # Use this slave address
+       __single = None
+       __lock = threading.Lock()
+       __parent_owner = None
+
+       def __init__(self, addr):
+               with i2c.__lock:
+                       count = 0
+                       while(i2c.__single):
+                               parent = inspect.stack()[1][3]
+                               count += 1
+                               sleep(0.001)
+                       if DEBUG:
+                               if count > 10:
+                                       parent_owner = "%s (%d), %s()" % (self.__parent_owner[1], self.__parent_owner[2], self.__parent_owner[3])
+                                       logger.warning("Error: (%s) I2C blocked %fs by %s!", parent, count*0.001, parent_owner)
+                               i2c.__parent_owner = inspect.stack()[1]
+                       i2c.__single = True
+               self.dev = i2c.libc.open("/dev/i2c-2", os.O_RDWR)
+               if self.dev < 0:
+                       raise IOError("open")
+               err = i2c.libc.ioctl(self.dev, i2c.I2C_SLAVE, addr>>1)
+               if err < 0:
+                       raise IOError("ioctl")
+
+       def write(self, s):
+               num_write = i2c.libc.write(self.dev, s, len(s))
+               if num_write != len(s):
+                       self.close()
+                       raise IOError("write: %d" % (num_write))
+       
+       def read(self, num):
+               buf = create_string_buffer(num)
+               num_read = i2c.libc.read(self.dev, buf, num)
+               if num_read != num:
+                       self.close()
+                       raise IOError("read: %d" % (num_read))
+               return buf.raw
+
+       def close(self):
+               if self.dev:
+                       i2c.libc.close(self.dev)
+                       self.dev = None
+                       #i2c.__parent_owner = None
+                       i2c.__single = None
+
+       def __del__(self):
+               self.close()
+
+if __name__ == "__main__":
+       import struct
+       import sys
+
+       dev = i2c(0x50)
+       s = struct.pack(">Bh", int(sys.argv[1]), int(sys.argv[2]))
+       dev.write(s)
+       dev.close()