Browse Source

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
dev
Dmitry 4 months ago
committed by GitHub
parent
commit
1e765d7e68
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 52 deletions
  1. +1
    -0
      code/espurna/button.cpp
  2. +45
    -51
      code/espurna/thermostat.cpp
  3. +4
    -0
      code/espurna/thermostat.h
  4. +1
    -1
      code/platformio.ini

+ 1
- 0
code/espurna/button.cpp View File

@ -20,6 +20,7 @@ Copyright (C) 2019-2021 by Maxim Prokhorov <prokhorov dot max at outlook dot com
#include "mqtt.h"
#include "relay.h"
#include "system.h"
#include "thermostat.h"
#if WEB_SUPPORT
#include "ws.h"


+ 45
- 51
code/espurna/thermostat.cpp View File

@ -78,7 +78,7 @@ temp_range_t _temp_range;
thermostat_t _thermostat;
enum thermostat_cycle_type {cooling, heating};
unsigned int _thermostat_cycle = heating;
unsigned int _thermostat_cycle = _thermostat_mode_cooler ? heating : cooling;
String thermostat_remote_sensor_topic;
//------------------------------------------------------------------------------
@ -311,11 +311,10 @@ void setThermostatState(bool state) {
}
//------------------------------------------------------------------------------
void debugPrintSwitch(bool state, double temp) {
char tmp_str[16];
dtostrf(temp, 1, 1, tmp_str);
DEBUG_MSG_P(PSTR("[THERMOSTAT] switch %s, temp: %s, min: %d, max: %d, mode: %s, relay: %s, last switch %d\n"),
state ? "ON" : "OFF", tmp_str, _temp_range.min, _temp_range.max, _thermostat_mode_cooler ? "COOLER" : "HEATER", relayStatus(THERMOSTAT_RELAY) ? "ON" : "OFF", millis() - _thermostat.last_switch);
void debugPrintSwitch(bool state, const char* tmp_str, const char* reason) {
DEBUG_MSG_P(PSTR("[THERMOSTAT] switch %s, temp: %s, min: %d, max: %d, mode: %s, relay: %s, last switch %d, reason: %s\n"),
state ? "ON" : "OFF", tmp_str, _temp_range.min, _temp_range.max, _thermostat_mode_cooler ? "COOLER" : "HEATER",
relayStatus(THERMOSTAT_RELAY) ? "ON" : "OFF", millis() - _thermostat.last_switch, reason);
}
//------------------------------------------------------------------------------
@ -324,53 +323,48 @@ inline bool lastSwitchEarlierThan(unsigned int comparing_time) {
}
//------------------------------------------------------------------------------
inline void switchThermostat(bool state, double temp) {
debugPrintSwitch(state, temp);
inline void switchThermostat(bool state, const char* tmp_str, const char* reason) {
debugPrintSwitch(state, tmp_str, reason);
setThermostatState(state);
}
//------------------------------------------------------------------------------
//----------- Main function that make decision ---------------------------------
//----------- Main function that make decision and relay controling ------------
//------------------------------------------------------------------------------
void checkTempAndAdjustRelay(double temp) {
if (_thermostat_mode_cooler == false) { // Main operation mode. Thermostat is HEATER.
// if thermostat switched ON and t > 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;
}


+ 4
- 0
code/espurna/thermostat.h View File

@ -66,3 +66,7 @@ void thermostatModeCooler(bool cooler);
bool thermostatModeCooler();
void thermostatSetup();
void displayOn();
void displaySetup();
void displayLoop();

+ 1
- 1
code/platformio.ini View File

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


Loading…
Cancel
Save