# # Main loop # Copyright (c) 2020 Michael Büsch # # 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