forked from theyosh/TerrariumPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
terrariumAnalogSensor.py
60 lines (45 loc) · 1.92 KB
/
terrariumAnalogSensor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# -*- coding: utf-8 -*-
import terrariumLogging
logger = terrariumLogging.logging.getLogger(__name__)
import time
from gpiozero import MCP3008
from terrariumUtils import terrariumUtils
class terrariumAnalogSensor(object):
hardwaretype = None
def __init__(self, datapin, device = 0):
self.__datapin = datapin
self.__device = 0 if device is None else device
self.__value = None
logger.debug('Initializing sensor type \'%s\' with Analog address %s,%s' % (self.__class__.__name__,self.__datapin,self.__device))
self.__sensor = MCP3008(channel=int(self.__datapin), device=int(self.__device))
def __enter__(self):
"""used to enable python's with statement support"""
return self
def __exit__(self, type, value, traceback):
"""with support"""
self.close()
def __get_raw_data(self):
# Read 5 samples of data and get an average of it
values = []
for counter in range(5):
value = self.__sensor.value
if terrariumUtils.is_float(value):
values.append(float(value))
time.sleep(0.2)
# sort values from low to high
values.sort()
# Calculate average. Exclude the min and max value. And therefore devide by 3
self.__value = round((sum(values[1:-1]) / (len(values)-2)),5)
def get_current(self):
self.__get_raw_data()
return None if not terrariumUtils.is_float(self.__value) else float(self.__value)
def close(self):
self.__sensor = None
logger.debug('Closed sensor type \'%s\' with address %s,%s' % (self.__class__.__name__,self.__datapin,self.__device))
class terrariumSKUSEN0161Sensor(terrariumAnalogSensor):
hardwaretype = 'sku-sen0161'
def get_ph(self):
value = self.get_current()
# https://github.com/theyosh/TerrariumPI/issues/108
# We measure the values in volts already, so no deviding by 1000 as original script does
return None if not terrariumUtils.is_float(value) else ((float(value) * ( 5000.0 / 1024.0)) * 3.3 + 0.1614)