Write simple thermostat

This commit is contained in:
Tanner Collin 2022-06-09 07:56:39 +01:00
parent ae30c39954
commit be6fb4bc3c
3 changed files with 60 additions and 1 deletions

4
rpiserver/data.py Normal file
View File

@ -0,0 +1,4 @@
PROBES = {
'0218404f8bff': 'Rad',
'0218405068ff': 'Tub',
}

55
rpiserver/main.py Normal file
View File

@ -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()

View File

@ -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()