import os import re import Adafruit_DHT from .base import Sensor, sensor @sensor class DS18B20(Sensor): sensor_class = 'pi-ds18b20' def __init__(self, **sensor_conf): super(DS18B20, self).__init__(**sensor_conf) self._sensor_id = None if 'hw_id' in sensor_conf: self._sensor_id = sensor_conf['hw_id'] else: self._find_sensor() def _find_sensor(self): basepath = "/sys/bus/w1/devices" for d in os.listdir(basepath): if 'master' not in d and '-' in d and \ os.path.exists(os.path.join(basepath, d, 'w1_slave')): self._sensor_id = d break else: raise ValueError("No sensor found in filesystem") def get_data(self): temp = None try: path = "/sys/bus/w1/devices/{}/w1_slave".format(self._sensor_id) with open(path) as f: data = f.read() m = re.search(r".* t=(?P\d+)$", data) if m: temp = int(m.group('temp')) / 1000.0 except IOError: # log? pass return dict(temp=temp) @sensor class DHT(Sensor): sensor_class = 'pi-dht' def __init__(self, **sensor_conf): super(DHT, self).__init__(**sensor_conf) if sensor_conf['dht_type'] == 'dht11': self._dht_type = Adafruit_DHT.DHT11 elif sensor_conf['dht_type'] == 'dht22': self._dht_type = Adafruit_DHT.DHT22 else: raise ValueError("Unknown DHT") def get_data(self): data = Adafruit_DHT.read_retry(self._dht_type, self.pin) return dict(humidity=data[0], temp=data[1])