soakforjesus/rpiserver/main.py

118 lines
3.0 KiB
Python
Raw Normal View History

2022-06-09 06:56:39 +00:00
import os
import logging
2022-06-17 04:09:07 +00:00
DEBUG = os.environ.get('DEBUG', False)
2022-06-09 06:56:39 +00:00
logging.basicConfig(
format='[%(asctime)s] %(levelname)s %(module)s/%(funcName)s - %(message)s',
2022-06-17 04:09:07 +00:00
level=logging.DEBUG if DEBUG else logging.INFO)
2022-06-09 06:56:39 +00:00
import asyncio
from statistics import mean
2022-06-17 04:09:07 +00:00
from signal import *
import time
2022-06-09 06:56:39 +00:00
import w1therm
import relays
import data
2022-06-17 04:09:07 +00:00
import requests
2022-06-09 06:56:39 +00:00
HI_SETPOINT = 40.0
2022-06-17 04:09:07 +00:00
LO_SETPOINT = HI_SETPOINT - 5.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 04:09:07 +00:00
def controller_message(message):
payload = dict(misc=message)
r = requests.post('https://tbot.tannercollin.com/message', data=payload, timeout=10)
if r.status_code == 200:
return True
else:
logging.exception('Unable to communicate with controller! Message: ' + message)
return False
def set_fan_on():
relays.set_relay(FAN_RELAY, relays.RELAY_ON)
def set_fan_off():
relays.set_relay(FAN_RELAY, relays.RELAY_OFF)
def set_pump_on():
relays.set_relay(PUMP_RELAY, relays.RELAY_ON)
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-17 04:09:07 +00:00
async def run_thermostat():
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')
2022-06-17 04:09:07 +00:00
set_fan_on()
2022-06-09 06:56:39 +00:00
elif temperature_mean < LO_SETPOINT:
logging.info('Turning fan off')
2022-06-17 04:09:07 +00:00
set_fan_off()
2022-06-09 06:56:39 +00:00
await asyncio.sleep(10)
2022-06-17 04:09:07 +00:00
def run_on_exit(*args):
logging.info('Cryptosoak exiting, turning pump and fan on.')
controller_message('Cryptosoak exiting.')
set_pump_on()
set_fan_on()
exit()
async def feed_watchdog():
while True:
with open('/dev/watchdog', 'w') as wdt:
wdt.write('1')
await asyncio.sleep(5)
def init():
relays.init_relays()
set_pump_on()
for sig in (SIGABRT, SIGILL, SIGINT, SIGSEGV, SIGTERM):
signal(sig, run_on_exit)
logging.info('Signals initialized')
async def main():
controller_message('Cryptosoak booting up...')
logging.info('Initialization complete')
await run_thermostat()
2022-06-09 06:56:39 +00:00
if __name__ == '__main__':
2022-06-17 04:09:07 +00:00
init()
if not DEBUG:
logging.info('Waiting 60 seconds to boot...')
time.sleep(60)
2022-06-09 06:56:39 +00:00
loop = asyncio.get_event_loop()
2022-06-17 04:09:07 +00:00
loop.run_until_complete(main()).add_done_callback(run_on_exit)
if not DEBUG:
loop.run_until_complete(feed_watchdog())
2022-06-09 06:56:39 +00:00
loop.close()