summaryrefslogtreecommitdiffstats
path: root/sensor_firmware/sensor.py
blob: 3b39338bc3752747cada529821e5b47bd92c6278 (plain)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#
# Abstract sensor
# Copyright (c) 2020 Michael Büsch <m@bues.ch>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

import bme280
import dht
import micropython
import time
from airhum import relhum2abshum
from curve import Curve
from micropython import const
from movingavg import MovingAvg
from util import lim

class SensorError(Exception):
    pass

class SensorWrapper(object):
    TYPE_TH     = const(0)
    TYPE_THP    = const(1)

    def __init__(self, *args, **kwargs):
        self.args = args
        self.kwargs = kwargs
        self.dev = None

    def close(self):
        self.dev = None

class SensorWrapperDHT(SensorWrapper):
    type        = SensorWrapper.TYPE_TH
    sleep       = 2.0

    def setup(self):
        if not self.dev:
            try:
                dev = dht.DHT22(*self.args, **self.kwargs)
                dev.measure()
                self.dev = dev
            except Exception as e:
                return False
        return True

    def read(self):
        dev = self.dev
        try:
            dev.measure()
            t = lim(dev.temperature(), -40.0, 80.0)
            h = lim(dev.humidity() * 1e-2, 0.0, 1.0)
        except Exception as e:
            raise SensorError(str(e))
        return t, h

class SensorWrapperBME(SensorWrapper):
    type        = SensorWrapper.TYPE_THP
    sleep       = 0.0

    def setup(self):
        if not self.dev:
            try:
                self.dev = bme280.BME280(*self.args, **self.kwargs)
            except bme280.BME280Error as e:
                return False
        return True

    def close(self):
        if self.dev:
            self.dev.close()
        super().close()

    def read(self):
        dev = self.dev
        try:
            return dev.readForced(filter=bme280.FILTER_2,
                          tempOversampling=bme280.OVSMPL_4,
                          humidityOversampling=bme280.OVSMPL_4,
                          pressureOversampling=bme280.OVSMPL_4)
        except bme280.BME280Error as e:
            raise SensorError(str(e))

class Sensor(object):
    nullCurve = Curve()

    def __init__(self,
                 name,
                 wrapper,
                 avgSize,
                 tCurve=None,
                 hCurve=None,
                 pCurve=None):
        self.name = name
        self.wrapper = wrapper
        count = 2 + 1 if wrapper.type == SensorWrapper.TYPE_TH else 3 + 1
        self.__movingAvgs = [ MovingAvg(avgSize) for _ in range(count) ]
        self.__tCurve = tCurve or self.nullCurve
        self.__hCurve = hCurve or self.nullCurve
        if wrapper.type == SensorWrapper.TYPE_THP:
            self.__pCurve = pCurve or self.nullCurve

    def close(self):
        self.wrapper.close()

    def run(self):
        wrapper = self.wrapper
        if not wrapper.setup():
            return None

        if wrapper.sleep:
            time.sleep(wrapper.sleep)

        values = wrapper.read()
        values = tuple(self.__movingAvgs[i](value)
                       for i, value in enumerate(values))

        p = None
        if wrapper.type == SensorWrapper.TYPE_TH:
            t, h = values
        else:
            t, h, p = values

        t = self.__tCurve.intp(t, lambda x, y: x)
        h = self.__hCurve.intp(h, lambda x, y: x)
        if wrapper.type == SensorWrapper.TYPE_THP:
            p = self.__pCurve.intp(p, lambda x, y: x)

        a = self.__movingAvgs[-1](relhum2abshum(h, t))

        return t, h, a, p

# vim: ts=4 sw=4 expandtab
bues.ch cgit interface