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
|
#
# Main loop
# 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 machine
import micropython
import time
from average import Average
from btsensors import BtSensors
from getconfig import config_get_sensors, CONF_BT_ENABLED, CONF_MAINLOOP_SLEEP
from logging import printInfo, printError
from micropython import const
from sensor import SensorError
from util import pa2mbar, mbar2pa
class Mainloop(object):
def __init__(self, watchdog):
self.sensors = config_get_sensors()
if CONF_BT_ENABLED:
self.bt = BtSensors()
self.avg = Average("f", 4)
def run(self):
if CONF_BT_ENABLED:
self.avg.reset()
for sensor in self.sensors:
try:
result = sensor.run()
if not result:
continue
temp, hum, ahum, pres = result
if CONF_BT_ENABLED:
self.avg.add(0, temp)
self.avg.add(1, hum)
self.avg.add(2, ahum)
if pres is None:
presStr = ""
else:
presStr = " P %.1f mBar" % pa2mbar(pres)
if CONF_BT_ENABLED:
self.avg.add(3, pres)
printInfo("%s T %.1f °C RH %.1f %% AH %.1f g/m3%s" % (
sensor.name, temp, hum * 1e2, ahum, presStr))
except SensorError as e:
printError("%s communication error: %s" % (sensor.name, str(e)))
sensor.close()
if CONF_BT_ENABLED:
self.bt.setValues(temp=self.avg.get(0, -1000.0),
relHum=self.avg.get(1),
absHum=self.avg.get(2),
pres=self.avg.get(3))
time.sleep(CONF_MAINLOOP_SLEEP)
printInfo("")
# vim: ts=4 sw=4 expandtab
|