2020-12-21 01:23:52 +01:00
|
|
|
import logging
|
2020-09-12 20:15:46 +02:00
|
|
|
import time
|
|
|
|
|
2020-12-21 01:23:52 +01:00
|
|
|
try:
|
|
|
|
from config import APP_NAME as ROOT_LOGGER_NAME
|
|
|
|
except ImportError:
|
|
|
|
ROOT_LOGGER_NAME = 'root'
|
|
|
|
|
|
|
|
logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
|
|
|
|
|
|
|
|
from . import background_task
|
|
|
|
|
2020-09-18 16:32:14 +02:00
|
|
|
try:
|
2020-09-17 19:21:45 +02:00
|
|
|
import adafruit_dht
|
2020-09-18 16:32:14 +02:00
|
|
|
except ImportError:
|
|
|
|
logger.warning("Could not import adafruit_dht. DEBUG set to True")
|
|
|
|
DEBUG = True
|
|
|
|
else:
|
|
|
|
from . import DEBUG
|
2020-09-17 19:21:45 +02:00
|
|
|
|
|
|
|
class dht_22(background_task):
|
|
|
|
RUN_SLEEP_TIME = 2.0
|
|
|
|
MIN_REFRESH_RATE = RUN_SLEEP_TIME
|
2020-09-12 20:15:46 +02:00
|
|
|
KEY_TEMPERATURE = 'temperature'
|
|
|
|
KEY_HUMIDITY = 'humidity'
|
|
|
|
KEY_TIME = 'time'
|
|
|
|
|
|
|
|
def __init__(self, gpio, data_callback=None):
|
|
|
|
self.__data_callback__ = data_callback
|
2020-12-21 01:23:52 +01:00
|
|
|
self.__monitor__ = dht_22_monitor(300)
|
2020-09-12 20:15:46 +02:00
|
|
|
# Initial the dht device, with data pin connected to:
|
2020-09-17 19:21:45 +02:00
|
|
|
if not DEBUG:
|
2020-12-21 01:23:52 +01:00
|
|
|
self.__dht_device__ = adafruit_dht.DHT22(gpio, use_pulseio=False)
|
2020-09-17 19:21:45 +02:00
|
|
|
# Initialise background_task
|
|
|
|
background_task.__init__(self)
|
|
|
|
|
|
|
|
def __run__(self):
|
|
|
|
data = self.__dht_data_transmission__()
|
|
|
|
if data is not None:
|
|
|
|
logger.debug('DHT-Communication: Successfully: %s', repr(data))
|
2020-12-21 01:23:52 +01:00
|
|
|
self.__monitor__.process(data)
|
2020-09-17 19:21:45 +02:00
|
|
|
if self.__data_callback__ is not None:
|
|
|
|
self.__data_callback__(**data)
|
2020-12-21 01:23:52 +01:00
|
|
|
if self.__monitor__.status():
|
|
|
|
logger.warning("DHT measurement stopped caused by too many identical values!")
|
|
|
|
self.close()
|
|
|
|
|
2020-09-12 20:15:46 +02:00
|
|
|
|
|
|
|
def __dht_data_transmission__(self):
|
2020-09-17 19:21:45 +02:00
|
|
|
if not DEBUG:
|
|
|
|
while self.__active__:
|
|
|
|
try:
|
|
|
|
# Store the values
|
|
|
|
data = {}
|
|
|
|
data[self.KEY_TEMPERATURE] = self.__dht_device__.temperature
|
|
|
|
data[self.KEY_HUMIDITY] = self.__dht_device__.humidity
|
|
|
|
data[self.KEY_TIME] = int(time.time())
|
|
|
|
if data[self.KEY_TEMPERATURE] is not None and data[self.KEY_HUMIDITY] is not None:
|
|
|
|
return data
|
2020-12-21 01:23:52 +01:00
|
|
|
time.sleep(self.RUN_SLEEP_TIME)
|
2020-09-17 19:21:45 +02:00
|
|
|
except RuntimeError as error:
|
|
|
|
# Errors happen fairly often, DHT's are hard to read, just keep going
|
|
|
|
logger.info('DHT-Communication: ' + error.args[0])
|
2020-12-21 01:23:52 +01:00
|
|
|
time.sleep(2.0)
|
2020-09-17 19:21:45 +02:00
|
|
|
except Exception as error:
|
|
|
|
self.__dht_device__.exit()
|
|
|
|
raise error
|
|
|
|
else:
|
|
|
|
return {
|
|
|
|
self.KEY_TEMPERATURE: -17.17,
|
|
|
|
self.KEY_HUMIDITY: 1.7,
|
|
|
|
self.KEY_TIME: int(time.time()),
|
|
|
|
}
|
2020-12-21 01:23:52 +01:00
|
|
|
|
|
|
|
|
|
|
|
class dht_22_monitor(object):
|
|
|
|
def __init__(self, max_const_treshold=250):
|
|
|
|
self.__max_const_treshold__ = max_const_treshold
|
|
|
|
self.__fail__ = False
|
|
|
|
self.__max_const_measurements__ = 0
|
|
|
|
self.__init_statevars__()
|
|
|
|
|
|
|
|
def __init_statevars__(self):
|
|
|
|
self.__current_const_measurements__ = 0
|
|
|
|
self.__last_measurements__ = None
|
|
|
|
|
|
|
|
def process(self, data):
|
|
|
|
if self.__last_measurements__ is None:
|
|
|
|
self.__last_measurements__ = data
|
|
|
|
else:
|
|
|
|
data_is_equal = True
|
|
|
|
for key in [dht_22.KEY_HUMIDITY, dht_22.KEY_TEMPERATURE]:
|
|
|
|
if data[key] != self.__last_measurements__[key]:
|
|
|
|
data_is_equal = False
|
|
|
|
if data_is_equal:
|
|
|
|
self.__current_const_measurements__ += 1
|
|
|
|
if self.__current_const_measurements__ > self.__max_const_measurements__:
|
|
|
|
self.__max_const_measurements__ = self.__current_const_measurements__
|
|
|
|
else:
|
|
|
|
if self.__current_const_measurements__ >= self.__max_const_measurements__:
|
|
|
|
logger.warning('%d identical data measurements identified!', self.__max_const_measurements__)
|
|
|
|
self.__init_statevars__()
|
|
|
|
if self.__max_const_measurements__ > self.__max_const_treshold__:
|
|
|
|
if not self.__fail__:
|
|
|
|
logger.warning("DHT measurement values are suspicious constant!")
|
|
|
|
self.__fail__ = True
|
|
|
|
|
|
|
|
def status(self):
|
|
|
|
if self.__fail__:
|
|
|
|
self.__fail__ = False
|
|
|
|
self.__max_const_measurements__ = 0
|
|
|
|
return True
|
|
|
|
return False
|