Всё управление скомпоновал в одном методе. Выдаём управляющие команды каждый цикл, чтобы не было эффектов залипания

This commit is contained in:
ftc
2025-12-20 17:08:56 +03:00
parent 476a327e20
commit 09eceab708
2 changed files with 30 additions and 17 deletions
+2 -2
View File
@@ -25,10 +25,10 @@ class Contour:
return self.current_value return self.current_value
return None return None
def check_state(self, state_value): def check_state(self, state_value, force = False):
if self.state == state_value: if self.state == state_value:
return return
if (self.state is None) or (time.time() - self.state_change_time > self.CHANGE_FREQ): if (self.state is None) or (time.time() - self.state_change_time > self.CHANGE_FREQ) or force:
self.state = state_value self.state = state_value
self.logger.info("Contour " + self.sensor_topic + " changed state to " + str(self.state)) self.logger.info("Contour " + self.sensor_topic + " changed state to " + str(self.state))
+28 -15
View File
@@ -105,10 +105,13 @@ def on_message(client, userdata, msg):
any_opened_start = 0 any_opened_start = 0
any_opened = False any_opened = False
boiler_out = 0
pump_out = 0
def check_temperatures(): def check_temperatures():
"""Check temperatures against target and control GPIO""" """Check temperatures against target and control GPIO"""
global any_opened_start, any_opened global any_opened_start, any_opened, boiler_out, pump_out
# check boiler exhaust, turn off if too big # check boiler exhaust, turn off if too big
if (boiler_exhaust_t > BOILER_EXHAUST_LIMIT): if (boiler_exhaust_t > BOILER_EXHAUST_LIMIT):
logger.error("Boiler exhaust overheat") logger.error("Boiler exhaust overheat")
@@ -116,9 +119,9 @@ def check_temperatures():
# turn on the pump # turn on the pump
# and turn off the boiler of course # and turn off the boiler of course
for c in contours: for c in contours:
mqtt_client.publish(c.out_topic, "1", retain=True) c.check_state(True, True)
mqtt_client.publish(boiler_control, "0", retain=True) boiler_out = 0
mqtt_client.publish(pump_control, "1", retain=True) pump_out = 1
return return
# #
sum_t = 0 sum_t = 0
@@ -132,9 +135,10 @@ def check_temperatures():
if count_t == 0: if count_t == 0:
logger.error("No temperatures available, switch to full-on mode") logger.error("No temperatures available, switch to full-on mode")
for c in contours: for c in contours:
mqtt_client.publish(c.out_topic, "1", retain=True) c.check_state(True, True)
# turn on boiler # turn on boiler
mqtt_client.publish(boiler_control, "1", retain=True) boiler_out = 1
pump_out = 1
return return
for c in contours: for c in contours:
@@ -152,11 +156,6 @@ def check_temperatures():
if contour_t > target_t + hysteresis: if contour_t > target_t + hysteresis:
# should turn off contour # should turn off contour
c.check_state(False) c.check_state(False)
if c.state:
# 0 means "closed"
mqtt_client.publish(c.out_topic, "1", retain=True)
else:
mqtt_client.publish(c.out_topic, "0", retain=True)
if any_opened: if any_opened:
any_opened = False any_opened = False
@@ -171,17 +170,31 @@ def check_temperatures():
logger.debug("Any opened: " + str(any_opened) + " aos " + str(any_opened_start) + " time " + str(time.time())) logger.debug("Any opened: " + str(any_opened) + " aos " + str(any_opened_start) + " time " + str(time.time()))
if any_opened and time.time() - any_opened_start > BOILER_TURN_ON_AFTER: if any_opened and time.time() - any_opened_start > BOILER_TURN_ON_AFTER:
mqtt_client.publish(boiler_control, "1", retain=True) logger.info("Starting up pump and boiler, contours should be opened now")
mqtt_client.publish(pump_control, "1", retain=True) boiler_out = 1
pump_out = 1
else: else:
mqtt_client.publish(boiler_control, "0", retain=True) logger.info("All contours are closed, shutting down boiler and pump")
mqtt_client.publish(pump_control, "0", retain=True) boiler_out = 0
pump_out = 0
def issue_commands():
for c in contours:
if c.state:
# 0 means "closed"
mqtt_client.publish(c.out_topic, "1", retain=True)
else:
mqtt_client.publish(c.out_topic, "0", retain=True)
mqtt_client.publish(boiler_control, str(boiler_out), retain=True)
mqtt_client.publish(pump_control, str(pump_out), retain=True)
def main_cycle(): def main_cycle():
"""Main 5-second cycle""" """Main 5-second cycle"""
while True: while True:
try: try:
check_temperatures() check_temperatures()
issue_commands()
except Exception as e: except Exception as e:
logger.error(f"Error in main cycle: {e}") logger.error(f"Error in main cycle: {e}")
# Wait 5 seconds # Wait 5 seconds