2022-06-09 06:56:39 +00:00
|
|
|
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
|
2022-06-17 02:41:09 +00:00
|
|
|
LO_SETPOINT = HI_SETPOINT - 10.0
|
2022-06-09 06:56:39 +00:00
|
|
|
|
2022-06-17 02:41:09 +00:00
|
|
|
PUMP_RELAY = relays.RELAY1
|
2022-06-09 06:56:39 +00:00
|
|
|
FAN_RELAY = relays.RELAY3
|
|
|
|
|
2022-06-17 02:41:09 +00:00
|
|
|
async def get_temp():
|
|
|
|
temps = await w1therm.get_temperatures()
|
|
|
|
if len(temps) != len(data.PROBES):
|
|
|
|
raise
|
|
|
|
|
|
|
|
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)
|
|
|
|
return temperature_mean
|
|
|
|
|
|
|
|
|
2022-06-09 06:56:39 +00:00
|
|
|
async def main():
|
|
|
|
relays.init_relays()
|
2022-06-17 02:41:09 +00:00
|
|
|
relays.set_relay(PUMP_RELAY, relays.RELAY_ON)
|
2022-06-09 06:56:39 +00:00
|
|
|
|
|
|
|
while True:
|
|
|
|
try:
|
2022-06-17 02:41:09 +00:00
|
|
|
temperature_mean = await get_temp()
|
2022-06-09 06:56:39 +00:00
|
|
|
except:
|
2022-06-17 02:41:09 +00:00
|
|
|
logging.error('Problem reading temperature probes! Turning fan on and sleeping 60s.')
|
2022-06-09 06:56:39 +00:00
|
|
|
relays.set_relay(FAN_RELAY, relays.RELAY_ON)
|
|
|
|
await asyncio.sleep(60)
|
|
|
|
continue
|
|
|
|
|
|
|
|
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()
|
|
|
|
|