@ -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 : cool ing ;
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 , te mp ) ;
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 ;
}