From be6fb4bc3c084e819d2d9118489dc0b418bbf9f5 Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Thu, 9 Jun 2022 07:56:39 +0100 Subject: [PATCH] Write simple thermostat --- rpiserver/data.py | 4 ++++ rpiserver/main.py | 55 +++++++++++++++++++++++++++++++++++++++++++++ rpiserver/relays.py | 2 +- 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 rpiserver/data.py create mode 100644 rpiserver/main.py diff --git a/rpiserver/data.py b/rpiserver/data.py new file mode 100644 index 0000000..7432dd5 --- /dev/null +++ b/rpiserver/data.py @@ -0,0 +1,4 @@ +PROBES = { + '0218404f8bff': 'Rad', + '0218405068ff': 'Tub', +} diff --git a/rpiserver/main.py b/rpiserver/main.py new file mode 100644 index 0000000..5e4581c --- /dev/null +++ b/rpiserver/main.py @@ -0,0 +1,55 @@ +import os +import logging +logging.basicConfig( + format='[%(asctime)s] %(levelname)s %(module)s/%(funcName)s - %(message)s', + level=logging.DEBUG if os.environ.get('DEBUG') else logging.INFO) +#logging.getLogger('aiohttp').setLevel(logging.DEBUG if os.environ.get('DEBUG') else logging.WARNING) + +import asyncio +from statistics import mean + +import w1therm +import relays +import data + +HI_SETPOINT = 40.0 +LO_SETPOINT = 30.0 + +FAN_RELAY = relays.RELAY3 + +async def main(): + relays.init_relays() + + while True: + try: + temps = await w1therm.get_temperatures() + if len(temps) != len(data.PROBES): + raise + except: + logging.error('Problem reading temperature probes! Setting fan on and sleeping.') + relays.set_relay(FAN_RELAY, relays.RELAY_ON) + await asyncio.sleep(60) + continue + + temperature_log = [] + for id_, temp in temps.items(): + temperature_log.append('{}: {} C'.format(data.PROBES[id_], temp)) + logging.info('Temperature probe ' + ', '.join(temperature_log)) + + temperature_list = list(temps.values()) + temperature_mean = mean(temperature_list) + + if temperature_mean > HI_SETPOINT: + logging.info('Turning fan on') + relays.set_relay(FAN_RELAY, relays.RELAY_ON) + elif temperature_mean < LO_SETPOINT: + logging.info('Turning fan off') + relays.set_relay(FAN_RELAY, relays.RELAY_OFF) + + await asyncio.sleep(10) + +if __name__ == '__main__': + loop = asyncio.get_event_loop() + loop.run_until_complete(main()) + loop.close() + diff --git a/rpiserver/relays.py b/rpiserver/relays.py index 45c9f46..78dc5bc 100644 --- a/rpiserver/relays.py +++ b/rpiserver/relays.py @@ -31,7 +31,7 @@ async def test(): for r in RELAYS: set_relay(r, RELAY_ON) await asyncio.sleep(0.5) - set_relay(r, RELAY_OFF) + #set_relay(r, RELAY_OFF) if __name__ == '__main__': init_relays()