From 1e765d7e6826dbd2f7499adad609fa96fb20c2ca Mon Sep 17 00:00:00 2001 From: Dmitry <8344919+ElderJoy@users.noreply.github.com> Date: Wed, 20 Dec 2023 18:39:45 +0200 Subject: [PATCH] thermostat: clean-up and build fixes (#2601) - update library esp8266-oled-ssd1306 to the latest version - fix thermostat build issues - simplify and clarify thermostat control logic - add logs to thermostat control logic --- code/espurna/button.cpp | 1 + code/espurna/thermostat.cpp | 96 +++++++++++++++++-------------------- code/espurna/thermostat.h | 4 ++ code/platformio.ini | 2 +- 4 files changed, 51 insertions(+), 52 deletions(-) diff --git a/code/espurna/button.cpp b/code/espurna/button.cpp index 39f57448..87fdc986 100644 --- a/code/espurna/button.cpp +++ b/code/espurna/button.cpp @@ -20,6 +20,7 @@ Copyright (C) 2019-2021 by Maxim Prokhorov max - switch it OFF and start cooling - if (relayStatus(THERMOSTAT_RELAY) && temp > _temp_range.max) { - _thermostat_cycle = cooling; - switchThermostat(false, temp); - // if thermostat switched ON for max time - switch it OFF for rest - } else if (relayStatus(THERMOSTAT_RELAY) && lastSwitchEarlierThan(_thermostat_max_on_time)) { - switchThermostat(false, temp); - // if t < min and thermostat switched OFF for at least minimum time - switch it ON and start - } else if (!relayStatus(THERMOSTAT_RELAY) && temp < _temp_range.min - && (_thermostat.last_switch == 0 || lastSwitchEarlierThan(_thermostat_min_off_time))) { - _thermostat_cycle = heating; - switchThermostat(true, temp); - // if heating cycle and thermostat switchaed OFF for more than min time - switch it ON - // continue heating cycle - } else if (!relayStatus(THERMOSTAT_RELAY) && _thermostat_cycle == heating - && lastSwitchEarlierThan(_thermostat_min_off_time)) { - switchThermostat(true, temp); - } - } else { // Thermostat is COOLER. Inverse logic. - // if thermostat switched ON and t < min - switch it OFF and start heating - if (relayStatus(THERMOSTAT_RELAY) && temp < _temp_range.min) { - _thermostat_cycle = heating; - switchThermostat(false, temp); - // if thermostat switched ON for max time - switch it OFF for rest - } else if (relayStatus(THERMOSTAT_RELAY) && lastSwitchEarlierThan(_thermostat_max_on_time)) { - switchThermostat(false, temp); - // if t > max and thermostat switched OFF for at least minimum time - switch it ON and start - } else if (!relayStatus(THERMOSTAT_RELAY) && temp > _temp_range.max - && (_thermostat.last_switch == 0 || lastSwitchEarlierThan(_thermostat_min_off_time))) { - _thermostat_cycle = cooling; - switchThermostat(true, temp); - // if cooling cycle and thermostat switchaed OFF for more than min time - switch it ON - // continue cooling cycle - } else if (!relayStatus(THERMOSTAT_RELAY) && _thermostat_cycle == cooling - && lastSwitchEarlierThan(_thermostat_min_off_time)) { - switchThermostat(true, temp); + char tmp_str[16]; + dtostrf(temp, 1, 1, tmp_str); + // if t < min - start heating cycle + if (_thermostat_cycle == cooling && temp < _temp_range.min) { + DEBUG_MSG_P(PSTR("[THERMOSTAT] starting HEATING cycle because the temperature %s dropped below minimum %d\n"), tmp_str, _temp_range.min); + _thermostat_cycle = heating; + // if t > max - start cooling cycle + } else if (_thermostat_cycle == heating && temp > _temp_range.max) { + DEBUG_MSG_P(PSTR("[THERMOSTAT] starting COOLING cycle because the temperature %s has risen above the maximum %d\n"), tmp_str, _temp_range.max); + _thermostat_cycle = cooling; + } + + // active cycle + if ((_thermostat_mode_cooler && _thermostat_cycle == cooling) || + (!_thermostat_mode_cooler && _thermostat_cycle == heating)) { + if (!relayStatus(THERMOSTAT_RELAY)) { + // if relay is OFF switch it ON if min_off_time passed by + if (_thermostat.last_switch == 0 || lastSwitchEarlierThan(_thermostat_min_off_time)) { + switchThermostat(true, tmp_str, _thermostat_mode_cooler ? "start/continue cooling" : "start/continue heating"); + } else { + int rest_mins_left = (_thermostat_min_off_time - (millis() - _thermostat.last_switch))/MILLIS_IN_MIN + 1; + DEBUG_MSG_P(PSTR("[THERMOSTAT] thermostat is in rest state for %d min\n"), rest_mins_left); + } + // if thermostat works more than max_on_time it need rest + } else if (lastSwitchEarlierThan(_thermostat_max_on_time)) { + switchThermostat(false, tmp_str, "thermostat switch OFF for 10 min to give rest for heater/cooler"); + } else { + DEBUG_MSG_P(PSTR("[THERMOSTAT] thermostat is active.\n")); } + // pasive cycle + } else if (relayStatus(THERMOSTAT_RELAY)) { + // if relay is ON - switch it OFF + switchThermostat(false, tmp_str, _thermostat_mode_cooler ? "start heating cycle" : "start cooling cycle"); } } @@ -526,28 +520,28 @@ void resetBurnCounters() { #define wifi_on_width 16 #define wifi_on_height 16 -const char wifi_on_bits[] PROGMEM = { +const uint8_t wifi_on_bits[] PROGMEM = { 0x00, 0x00, 0x0E, 0x00, 0x7E, 0x00, 0xFE, 0x01, 0xE0, 0x03, 0x80, 0x07, 0x02, 0x0F, 0x1E, 0x1E, 0x3E, 0x1C, 0x78, 0x38, 0xE0, 0x38, 0xC0, 0x31, 0xC6, 0x71, 0x8E, 0x71, 0x8E, 0x73, 0x00, 0x00, }; #define mqtt_width 16 #define mqtt_height 16 -const char mqtt_bits[] PROGMEM = { +const uint8_t mqtt_bits[] PROGMEM = { 0x00, 0x00, 0x00, 0x08, 0x00, 0x18, 0x00, 0x38, 0xEA, 0x7F, 0xEA, 0x7F, 0x00, 0x38, 0x10, 0x18, 0x18, 0x08, 0x1C, 0x00, 0xFE, 0x57, 0xFE, 0x57, 0x1C, 0x00, 0x18, 0x00, 0x10, 0x00, 0x00, 0x00, }; #define remote_temp_width 16 #define remote_temp_height 16 -const char remote_temp_bits[] PROGMEM = { +const uint8_t remote_temp_bits[] PROGMEM = { 0x00, 0x00, 0xE0, 0x18, 0x10, 0x25, 0x10, 0x25, 0x90, 0x19, 0x50, 0x01, 0x50, 0x01, 0xD0, 0x01, 0x50, 0x01, 0x50, 0x01, 0xD0, 0x01, 0x50, 0x01, 0xE0, 0x00, 0xE0, 0x00, 0xE0, 0x00, 0x00, 0x00, }; #define server_width 16 #define server_height 16 -const char server_bits[] PROGMEM = { +const uint8_t server_bits[] PROGMEM = { 0x00, 0x00, 0xF8, 0x1F, 0xFC, 0x3F, 0x0C, 0x30, 0x0C, 0x30, 0x0C, 0x30, 0x0C, 0x30, 0x0C, 0x30, 0x0C, 0x30, 0xF8, 0x1F, 0xFC, 0x3F, 0xFE, 0x7F, 0x1E, 0x78, 0xFE, 0x7F, 0xFC, 0x3F, 0x00, 0x00, }; @@ -570,7 +564,7 @@ bool _display_need_refresh = true; bool _temp_range_need_update = true; //------------------------------------------------------------------------------ -void drawIco(int16_t x, int16_t y, const char *ico, bool on = true) { +void drawIco(int16_t x, int16_t y, const uint8_t *ico, bool on = true) { display.drawIco16x16(x, y, ico, !on); _display_need_refresh = true; } diff --git a/code/espurna/thermostat.h b/code/espurna/thermostat.h index 4d1812d0..029ad403 100644 --- a/code/espurna/thermostat.h +++ b/code/espurna/thermostat.h @@ -66,3 +66,7 @@ void thermostatModeCooler(bool cooler); bool thermostatModeCooler(); void thermostatSetup(); + +void displayOn(); +void displaySetup(); +void displayLoop(); diff --git a/code/platformio.ini b/code/platformio.ini index 3a7c6a21..46bc9a8c 100644 --- a/code/platformio.ini +++ b/code/platformio.ini @@ -159,7 +159,7 @@ shared_lib_deps = sparkfun/SparkFun VEML6075 Arduino Library@^1.1.4 pololu/VL53L1X@^1.0.1 https://github.com/mcleng/MAX6675-Library#2.0.1 - https://github.com/ThingPulse/esp8266-oled-ssd1306#3398c97 + https://github.com/ThingPulse/esp8266-oled-ssd1306#9be7b01 adafruit/Adafruit BusIO@^1.11.5 adafruit/Adafruit SI1145 Library@^1.2.0 https://github.com/BoschSensortec/BSEC-Arduino-library.git#v1.6.1480