From 09eceab7089f6b00b9f36abe3574d4df27c7ab9f Mon Sep 17 00:00:00 2001 From: Denis Denisov Date: Sat, 20 Dec 2025 17:08:56 +0300 Subject: [PATCH] =?UTF-8?q?=D0=92=D1=81=D1=91=20=D1=83=D0=BF=D1=80=D0=B0?= =?UTF-8?q?=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D1=81=D0=BA=D0=BE=D0=BC?= =?UTF-8?q?=D0=BF=D0=BE=D0=BD=D0=BE=D0=B2=D0=B0=D0=BB=20=D0=B2=20=D0=BE?= =?UTF-8?q?=D0=B4=D0=BD=D0=BE=D0=BC=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D0=B5?= =?UTF-8?q?.=20=D0=92=D1=8B=D0=B4=D0=B0=D1=91=D0=BC=20=D1=83=D0=BF=D1=80?= =?UTF-8?q?=D0=B0=D0=B2=D0=BB=D1=8F=D1=8E=D1=89=D0=B8=D0=B5=20=D0=BA=D0=BE?= =?UTF-8?q?=D0=BC=D0=B0=D0=BD=D0=B4=D1=8B=20=D0=BA=D0=B0=D0=B6=D0=B4=D1=8B?= =?UTF-8?q?=D0=B9=20=D1=86=D0=B8=D0=BA=D0=BB,=20=D1=87=D1=82=D0=BE=D0=B1?= =?UTF-8?q?=D1=8B=20=D0=BD=D0=B5=20=D0=B1=D1=8B=D0=BB=D0=BE=20=D1=8D=D1=84?= =?UTF-8?q?=D1=84=D0=B5=D0=BA=D1=82=D0=BE=D0=B2=20=D0=B7=D0=B0=D0=BB=D0=B8?= =?UTF-8?q?=D0=BF=D0=B0=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contour.py | 4 ++-- heat_control.py | 43 ++++++++++++++++++++++++++++--------------- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/contour.py b/contour.py index 1121e69..7f2fe95 100644 --- a/contour.py +++ b/contour.py @@ -25,10 +25,10 @@ class Contour: return self.current_value return None - def check_state(self, state_value): + def check_state(self, state_value, force = False): if self.state == state_value: 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.logger.info("Contour " + self.sensor_topic + " changed state to " + str(self.state)) diff --git a/heat_control.py b/heat_control.py index 918e97e..8391219 100755 --- a/heat_control.py +++ b/heat_control.py @@ -105,10 +105,13 @@ def on_message(client, userdata, msg): any_opened_start = 0 any_opened = False +boiler_out = 0 +pump_out = 0 + def check_temperatures(): """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 if (boiler_exhaust_t > BOILER_EXHAUST_LIMIT): logger.error("Boiler exhaust overheat") @@ -116,9 +119,9 @@ def check_temperatures(): # turn on the pump # and turn off the boiler of course for c in contours: - mqtt_client.publish(c.out_topic, "1", retain=True) - mqtt_client.publish(boiler_control, "0", retain=True) - mqtt_client.publish(pump_control, "1", retain=True) + c.check_state(True, True) + boiler_out = 0 + pump_out = 1 return # sum_t = 0 @@ -132,9 +135,10 @@ def check_temperatures(): if count_t == 0: logger.error("No temperatures available, switch to full-on mode") for c in contours: - mqtt_client.publish(c.out_topic, "1", retain=True) + c.check_state(True, True) # turn on boiler - mqtt_client.publish(boiler_control, "1", retain=True) + boiler_out = 1 + pump_out = 1 return for c in contours: @@ -152,11 +156,6 @@ def check_temperatures(): if contour_t > target_t + hysteresis: # should turn off contour 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: 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())) if any_opened and time.time() - any_opened_start > BOILER_TURN_ON_AFTER: - mqtt_client.publish(boiler_control, "1", retain=True) - mqtt_client.publish(pump_control, "1", retain=True) + logger.info("Starting up pump and boiler, contours should be opened now") + boiler_out = 1 + pump_out = 1 else: - mqtt_client.publish(boiler_control, "0", retain=True) - mqtt_client.publish(pump_control, "0", retain=True) + logger.info("All contours are closed, shutting down boiler and pump") + 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(): """Main 5-second cycle""" while True: try: check_temperatures() + issue_commands() except Exception as e: logger.error(f"Error in main cycle: {e}") # Wait 5 seconds