Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

830 lines
33 KiB

  1. /*
  2. THERMOSTAT MODULE
  3. Copyright (C) 2017 by Dmitry Blinov <dblinov76 at gmail dot com>
  4. */
  5. #if THERMOSTAT_SUPPORT
  6. #include <ArduinoJson.h>
  7. #include <float.h>
  8. #include "relay.h"
  9. #include "ws.h"
  10. bool _thermostat_enabled = true;
  11. bool _thermostat_mode_cooler = false;
  12. const char* NAME_THERMOSTAT_ENABLED = "thermostatEnabled";
  13. const char* NAME_THERMOSTAT_MODE = "thermostatMode";
  14. const char* NAME_TEMP_RANGE_MIN = "tempRangeMin";
  15. const char* NAME_TEMP_RANGE_MAX = "tempRangeMax";
  16. const char* NAME_REMOTE_SENSOR_NAME = "remoteSensorName";
  17. const char* NAME_REMOTE_TEMP_MAX_WAIT = "remoteTempMaxWait";
  18. const char* NAME_ALONE_ON_TIME = "aloneOnTime";
  19. const char* NAME_ALONE_OFF_TIME = "aloneOffTime";
  20. const char* NAME_MAX_ON_TIME = "maxOnTime";
  21. const char* NAME_MIN_OFF_TIME = "minOffTime";
  22. const char* NAME_BURN_TOTAL = "burnTotal";
  23. const char* NAME_BURN_TODAY = "burnToday";
  24. const char* NAME_BURN_YESTERDAY = "burnYesterday";
  25. const char* NAME_BURN_THIS_MONTH = "burnThisMonth";
  26. const char* NAME_BURN_PREV_MONTH = "burnPrevMonth";
  27. const char* NAME_BURN_DAY = "burnDay";
  28. const char* NAME_BURN_MONTH = "burnMonth";
  29. const char* NAME_OPERATION_MODE = "thermostatOperationMode";
  30. #define ASK_TEMP_RANGE_INTERVAL_INITIAL 15000 // ask initially once per every 15 seconds
  31. #define ASK_TEMP_RANGE_INTERVAL_REGULAR 60000 // ask every minute to be sure
  32. #define MILLIS_IN_SEC 1000
  33. #define MILLIS_IN_MIN 60000
  34. #define THERMOSTAT_STATE_UPDATE_INTERVAL 60000 // 1 min
  35. #define THERMOSTAT_RELAY 0 // use relay 0
  36. #define THERMOSTAT_TEMP_RANGE_MIN 10 // grad. Celsius
  37. #define THERMOSTAT_TEMP_RANGE_MIN_MIN 3 // grad. Celsius
  38. #define THERMOSTAT_TEMP_RANGE_MIN_MAX 30 // grad. Celsius
  39. #define THERMOSTAT_TEMP_RANGE_MAX 20 // grad. Celsius
  40. #define THERMOSTAT_TEMP_RANGE_MAX_MIN 8 // grad. Celsius
  41. #define THERMOSTAT_TEMP_RANGE_MAX_MAX 35 // grad. Celsius
  42. #define THERMOSTAT_ALONE_ON_TIME 5 // 5 min
  43. #define THERMOSTAT_ALONE_OFF_TIME 55 // 55 min
  44. #define THERMOSTAT_MAX_ON_TIME 30 // 30 min
  45. #define THERMOSTAT_MIN_OFF_TIME 10 // 10 min
  46. unsigned long _thermostat_remote_temp_max_wait = THERMOSTAT_REMOTE_TEMP_MAX_WAIT * MILLIS_IN_SEC;
  47. unsigned long _thermostat_alone_on_time = THERMOSTAT_ALONE_ON_TIME * MILLIS_IN_MIN;
  48. unsigned long _thermostat_alone_off_time = THERMOSTAT_ALONE_OFF_TIME * MILLIS_IN_MIN;
  49. unsigned long _thermostat_max_on_time = THERMOSTAT_MAX_ON_TIME * MILLIS_IN_MIN;
  50. unsigned long _thermostat_min_off_time = THERMOSTAT_MIN_OFF_TIME * MILLIS_IN_MIN;
  51. unsigned int _thermostat_on_time_for_day = 0;
  52. unsigned int _thermostat_burn_total = 0;
  53. unsigned int _thermostat_burn_today = 0;
  54. unsigned int _thermostat_burn_yesterday = 0;
  55. unsigned int _thermostat_burn_this_month = 0;
  56. unsigned int _thermostat_burn_prev_month = 0;
  57. unsigned int _thermostat_burn_day = 0;
  58. unsigned int _thermostat_burn_month = 0;
  59. struct temp_t {
  60. float temp;
  61. unsigned long last_update = 0;
  62. bool need_display_update = false;
  63. };
  64. temp_t _remote_temp;
  65. struct temp_range_t {
  66. int min = THERMOSTAT_TEMP_RANGE_MIN;
  67. int max = THERMOSTAT_TEMP_RANGE_MAX;
  68. unsigned long last_update = 0;
  69. unsigned long ask_time = 0;
  70. unsigned int ask_interval = 0;
  71. bool need_display_update = true;
  72. };
  73. temp_range_t _temp_range;
  74. enum temperature_source_t {temp_none, temp_local, temp_remote};
  75. struct thermostat_t {
  76. unsigned long last_update = 0;
  77. unsigned long last_switch = 0;
  78. String remote_sensor_name;
  79. unsigned int temperature_source = temp_none;
  80. };
  81. thermostat_t _thermostat;
  82. enum thermostat_cycle_type {cooling, heating};
  83. unsigned int _thermostat_cycle = heating;
  84. String thermostat_remote_sensor_topic;
  85. //------------------------------------------------------------------------------
  86. void thermostatEnabled(bool enabled) {
  87. _thermostat_enabled = enabled;
  88. }
  89. //------------------------------------------------------------------------------
  90. bool thermostatEnabled() {
  91. return _thermostat_enabled;
  92. }
  93. //------------------------------------------------------------------------------
  94. void thermostatModeCooler(bool cooler) {
  95. _thermostat_mode_cooler = cooler;
  96. }
  97. //------------------------------------------------------------------------------
  98. bool thermostatModeCooler() {
  99. return _thermostat_mode_cooler;
  100. }
  101. //------------------------------------------------------------------------------
  102. std::vector<thermostat_callback_f> _thermostat_callbacks;
  103. void thermostatRegister(thermostat_callback_f callback) {
  104. _thermostat_callbacks.push_back(callback);
  105. }
  106. //------------------------------------------------------------------------------
  107. void updateOperationMode() {
  108. #if WEB_SUPPORT
  109. String message;
  110. if (_thermostat.temperature_source == temp_remote) {
  111. message = "{\"thermostatVisible\": 1, \"thermostatOperationMode\": \"remote temperature\"}";
  112. updateRemoteTemp(true);
  113. } else if (_thermostat.temperature_source == temp_local) {
  114. message = "{\"thermostatVisible\": 1, \"thermostatOperationMode\": \"local temperature\"}";
  115. updateRemoteTemp(false);
  116. } else {
  117. message = "{\"thermostatVisible\": 1, \"thermostatOperationMode\": \"autonomous\"}";
  118. updateRemoteTemp(false);
  119. }
  120. wsSend(message.c_str());
  121. #endif
  122. }
  123. //------------------------------------------------------------------------------
  124. void updateRemoteTemp(bool remote_temp_actual) {
  125. #if WEB_SUPPORT
  126. char tmp_str[16];
  127. if (remote_temp_actual) {
  128. dtostrf(_remote_temp.temp, 1, 1, tmp_str);
  129. } else {
  130. strcpy(tmp_str, "\"?\"");
  131. }
  132. char buffer[128];
  133. snprintf_P(buffer, sizeof(buffer), PSTR("{\"thermostatVisible\": 1, \"remoteTmp\": %s}"), tmp_str);
  134. wsSend(buffer);
  135. #endif
  136. }
  137. //------------------------------------------------------------------------------
  138. // MQTT
  139. //------------------------------------------------------------------------------
  140. void thermostatMQTTCallback(unsigned int type, const char * topic, const char * payload) {
  141. if (type == MQTT_CONNECT_EVENT) {
  142. mqttSubscribeRaw(thermostat_remote_sensor_topic.c_str());
  143. mqttSubscribe(MQTT_TOPIC_HOLD_TEMP);
  144. _temp_range.ask_interval = ASK_TEMP_RANGE_INTERVAL_INITIAL;
  145. _temp_range.ask_time = millis();
  146. }
  147. if (type == MQTT_MESSAGE_EVENT) {
  148. // Match topic
  149. String t = mqttMagnitude((char *) topic);
  150. if (strcmp(topic, thermostat_remote_sensor_topic.c_str()) != 0
  151. && !t.equals(MQTT_TOPIC_HOLD_TEMP))
  152. return;
  153. // Parse JSON input
  154. DynamicJsonBuffer jsonBuffer;
  155. JsonObject& root = jsonBuffer.parseObject(payload);
  156. if (!root.success()) {
  157. DEBUG_MSG_P(PSTR("[THERMOSTAT] Error parsing data\n"));
  158. return;
  159. }
  160. // Check rempte sensor temperature
  161. if (strcmp(topic, thermostat_remote_sensor_topic.c_str()) == 0) {
  162. if (root.containsKey(magnitudeTopic(MAGNITUDE_TEMPERATURE))) {
  163. String remote_temp = root[magnitudeTopic(MAGNITUDE_TEMPERATURE)];
  164. _remote_temp.temp = remote_temp.toFloat();
  165. _remote_temp.last_update = millis();
  166. _remote_temp.need_display_update = true;
  167. DEBUG_MSG_P(PSTR("[THERMOSTAT] Remote sensor temperature: %s\n"), remote_temp.c_str());
  168. updateRemoteTemp(true);
  169. }
  170. }
  171. // Check temperature range change
  172. if (t.equals(MQTT_TOPIC_HOLD_TEMP)) {
  173. if (root.containsKey(MQTT_TOPIC_HOLD_TEMP_MIN)) {
  174. int t_min = root[MQTT_TOPIC_HOLD_TEMP_MIN];
  175. int t_max = root[MQTT_TOPIC_HOLD_TEMP_MAX];
  176. if (t_min < THERMOSTAT_TEMP_RANGE_MIN_MIN || t_min > THERMOSTAT_TEMP_RANGE_MIN_MAX ||
  177. t_max < THERMOSTAT_TEMP_RANGE_MAX_MIN || t_max > THERMOSTAT_TEMP_RANGE_MAX_MAX) {
  178. DEBUG_MSG_P(PSTR("[THERMOSTAT] Hold temperature range error\n"));
  179. return;
  180. }
  181. _temp_range.min = root[MQTT_TOPIC_HOLD_TEMP_MIN];
  182. _temp_range.max = root[MQTT_TOPIC_HOLD_TEMP_MAX];
  183. setSetting(NAME_TEMP_RANGE_MIN, _temp_range.min);
  184. setSetting(NAME_TEMP_RANGE_MAX, _temp_range.max);
  185. saveSettings();
  186. _temp_range.ask_interval = ASK_TEMP_RANGE_INTERVAL_REGULAR;
  187. _temp_range.last_update = millis();
  188. _temp_range.need_display_update = true;
  189. DEBUG_MSG_P(PSTR("[THERMOSTAT] Hold temperature range: (%d - %d)\n"), _temp_range.min, _temp_range.max);
  190. // Update websocket clients
  191. #if WEB_SUPPORT
  192. char buffer[100];
  193. snprintf_P(buffer, sizeof(buffer), PSTR("{\"thermostatVisible\": 1, \"tempRangeMin\": %d, \"tempRangeMax\": %d}"), _temp_range.min, _temp_range.max);
  194. wsSend(buffer);
  195. #endif
  196. } else {
  197. DEBUG_MSG_P(PSTR("[THERMOSTAT] Error temperature range data\n"));
  198. }
  199. }
  200. }
  201. }
  202. #if MQTT_SUPPORT
  203. //------------------------------------------------------------------------------
  204. void thermostatSetupMQTT() {
  205. mqttRegister(thermostatMQTTCallback);
  206. }
  207. #endif
  208. //------------------------------------------------------------------------------
  209. void notifyRangeChanged(bool min) {
  210. DEBUG_MSG_P(PSTR("[THERMOSTAT] notifyRangeChanged %s = %d\n"), min ? "MIN" : "MAX", min ? _temp_range.min : _temp_range.max);
  211. char tmp_str[6];
  212. sprintf(tmp_str, "%d", min ? _temp_range.min : _temp_range.max);
  213. mqttSend(min ? MQTT_TOPIC_NOTIFY_TEMP_RANGE_MIN : MQTT_TOPIC_NOTIFY_TEMP_RANGE_MAX, tmp_str, true);
  214. }
  215. //------------------------------------------------------------------------------
  216. // Setup
  217. //------------------------------------------------------------------------------
  218. void commonSetup() {
  219. _thermostat_enabled = getSetting(NAME_THERMOSTAT_ENABLED, false);
  220. DEBUG_MSG_P(PSTR("[THERMOSTAT] _thermostat_enabled = %d\n"), _thermostat_enabled);
  221. _thermostat_mode_cooler = getSetting(NAME_THERMOSTAT_MODE, false);
  222. DEBUG_MSG_P(PSTR("[THERMOSTAT] _thermostat_mode_cooler = %d\n"), _thermostat_mode_cooler);
  223. _temp_range.min = getSetting(NAME_TEMP_RANGE_MIN, THERMOSTAT_TEMP_RANGE_MIN);
  224. _temp_range.max = getSetting(NAME_TEMP_RANGE_MAX, THERMOSTAT_TEMP_RANGE_MAX);
  225. DEBUG_MSG_P(PSTR("[THERMOSTAT] _temp_range.min = %d\n"), _temp_range.min);
  226. DEBUG_MSG_P(PSTR("[THERMOSTAT] _temp_range.max = %d\n"), _temp_range.max);
  227. _thermostat.remote_sensor_name = getSetting(NAME_REMOTE_SENSOR_NAME);
  228. thermostat_remote_sensor_topic = _thermostat.remote_sensor_name + String("/") + String(MQTT_TOPIC_JSON);
  229. _thermostat_remote_temp_max_wait = getSetting(NAME_REMOTE_TEMP_MAX_WAIT, THERMOSTAT_REMOTE_TEMP_MAX_WAIT) * MILLIS_IN_SEC;
  230. _thermostat_alone_on_time = getSetting(NAME_ALONE_ON_TIME, THERMOSTAT_ALONE_ON_TIME) * MILLIS_IN_MIN;
  231. _thermostat_alone_off_time = getSetting(NAME_ALONE_OFF_TIME, THERMOSTAT_ALONE_OFF_TIME) * MILLIS_IN_MIN;
  232. _thermostat_max_on_time = getSetting(NAME_MAX_ON_TIME, THERMOSTAT_MAX_ON_TIME) * MILLIS_IN_MIN;
  233. _thermostat_min_off_time = getSetting(NAME_MIN_OFF_TIME, THERMOSTAT_MIN_OFF_TIME) * MILLIS_IN_MIN;
  234. }
  235. //------------------------------------------------------------------------------
  236. void thermostatConfigure() {
  237. commonSetup();
  238. _thermostat.temperature_source = temp_none;
  239. _thermostat_burn_total = getSetting(NAME_BURN_TOTAL, 0);
  240. _thermostat_burn_today = getSetting(NAME_BURN_TODAY, 0);
  241. _thermostat_burn_yesterday = getSetting(NAME_BURN_YESTERDAY, 0);
  242. _thermostat_burn_this_month = getSetting(NAME_BURN_THIS_MONTH, 0);
  243. _thermostat_burn_prev_month = getSetting(NAME_BURN_PREV_MONTH, 0);
  244. _thermostat_burn_day = getSetting(NAME_BURN_DAY, 0);
  245. _thermostat_burn_month = getSetting(NAME_BURN_MONTH, 0);
  246. }
  247. //------------------------------------------------------------------------------
  248. void _thermostatReload() {
  249. int prev_temp_range_min = _temp_range.min;
  250. int prev_temp_range_max = _temp_range.max;
  251. commonSetup();
  252. if (_temp_range.min != prev_temp_range_min)
  253. notifyRangeChanged(true);
  254. if (_temp_range.max != prev_temp_range_max)
  255. notifyRangeChanged(false);
  256. }
  257. #if WEB_SUPPORT
  258. //------------------------------------------------------------------------------
  259. void _thermostatWebSocketOnConnected(JsonObject& root) {
  260. root["thermostatEnabled"] = thermostatEnabled();
  261. root["thermostatMode"] = thermostatModeCooler();
  262. root["thermostatVisible"] = 1;
  263. root[NAME_TEMP_RANGE_MIN] = _temp_range.min;
  264. root[NAME_TEMP_RANGE_MAX] = _temp_range.max;
  265. root[NAME_REMOTE_SENSOR_NAME] = _thermostat.remote_sensor_name;
  266. root[NAME_REMOTE_TEMP_MAX_WAIT] = _thermostat_remote_temp_max_wait / MILLIS_IN_SEC;
  267. root[NAME_MAX_ON_TIME] = _thermostat_max_on_time / MILLIS_IN_MIN;
  268. root[NAME_MIN_OFF_TIME] = _thermostat_min_off_time / MILLIS_IN_MIN;
  269. root[NAME_ALONE_ON_TIME] = _thermostat_alone_on_time / MILLIS_IN_MIN;
  270. root[NAME_ALONE_OFF_TIME] = _thermostat_alone_off_time / MILLIS_IN_MIN;
  271. root[NAME_BURN_TODAY] = _thermostat_burn_today;
  272. root[NAME_BURN_YESTERDAY] = _thermostat_burn_yesterday;
  273. root[NAME_BURN_THIS_MONTH] = _thermostat_burn_this_month;
  274. root[NAME_BURN_PREV_MONTH] = _thermostat_burn_prev_month;
  275. root[NAME_BURN_TOTAL] = _thermostat_burn_total;
  276. if (_thermostat.temperature_source == temp_remote) {
  277. root[NAME_OPERATION_MODE] = "remote temperature";
  278. root["remoteTmp"] = _remote_temp.temp;
  279. } else if (_thermostat.temperature_source == temp_local) {
  280. root[NAME_OPERATION_MODE] = "local temperature";
  281. root["remoteTmp"] = "?";
  282. } else {
  283. root[NAME_OPERATION_MODE] = "autonomous";
  284. root["remoteTmp"] = "?";
  285. }
  286. }
  287. //------------------------------------------------------------------------------
  288. bool _thermostatWebSocketOnKeyCheck(const char * key, JsonVariant& value) {
  289. if (strncmp(key, NAME_THERMOSTAT_ENABLED, strlen(NAME_THERMOSTAT_ENABLED)) == 0) return true;
  290. if (strncmp(key, NAME_THERMOSTAT_MODE, strlen(NAME_THERMOSTAT_MODE)) == 0) return true;
  291. if (strncmp(key, NAME_TEMP_RANGE_MIN, strlen(NAME_TEMP_RANGE_MIN)) == 0) return true;
  292. if (strncmp(key, NAME_TEMP_RANGE_MAX, strlen(NAME_TEMP_RANGE_MAX)) == 0) return true;
  293. if (strncmp(key, NAME_REMOTE_SENSOR_NAME, strlen(NAME_REMOTE_SENSOR_NAME)) == 0) return true;
  294. if (strncmp(key, NAME_REMOTE_TEMP_MAX_WAIT, strlen(NAME_REMOTE_TEMP_MAX_WAIT)) == 0) return true;
  295. if (strncmp(key, NAME_MAX_ON_TIME, strlen(NAME_MAX_ON_TIME)) == 0) return true;
  296. if (strncmp(key, NAME_MIN_OFF_TIME, strlen(NAME_MIN_OFF_TIME)) == 0) return true;
  297. if (strncmp(key, NAME_ALONE_ON_TIME, strlen(NAME_ALONE_ON_TIME)) == 0) return true;
  298. if (strncmp(key, NAME_ALONE_OFF_TIME, strlen(NAME_ALONE_OFF_TIME)) == 0) return true;
  299. return false;
  300. }
  301. //------------------------------------------------------------------------------
  302. void _thermostatWebSocketOnAction(uint32_t client_id, const char * action, JsonObject& data) {
  303. if (strcmp(action, "thermostat_reset_counters") == 0) resetBurnCounters();
  304. }
  305. #endif
  306. //------------------------------------------------------------------------------
  307. void thermostatSetup() {
  308. thermostatConfigure();
  309. #if MQTT_SUPPORT
  310. thermostatSetupMQTT();
  311. #endif
  312. // Websockets
  313. #if WEB_SUPPORT
  314. wsRegister()
  315. .onConnected(_thermostatWebSocketOnConnected)
  316. .onKeyCheck(_thermostatWebSocketOnKeyCheck)
  317. .onAction(_thermostatWebSocketOnAction);
  318. #endif
  319. espurnaRegisterLoop(thermostatLoop);
  320. espurnaRegisterReload(_thermostatReload);
  321. }
  322. //------------------------------------------------------------------------------
  323. void sendTempRangeRequest() {
  324. DEBUG_MSG_P(PSTR("[THERMOSTAT] sendTempRangeRequest\n"));
  325. mqttSend(MQTT_TOPIC_ASK_TEMP_RANGE, "", true);
  326. }
  327. //------------------------------------------------------------------------------
  328. void setThermostatState(bool state) {
  329. DEBUG_MSG_P(PSTR("[THERMOSTAT] setThermostatState: %s\n"), state ? "ON" : "OFF");
  330. relayStatus(THERMOSTAT_RELAY, state, mqttForward(), false);
  331. _thermostat.last_switch = millis();
  332. // Send thermostat change state event to subscribers
  333. for (unsigned char i = 0; i < _thermostat_callbacks.size(); i++) {
  334. (_thermostat_callbacks[i])(state);
  335. }
  336. }
  337. //------------------------------------------------------------------------------
  338. void debugPrintSwitch(bool state, double temp) {
  339. char tmp_str[16];
  340. dtostrf(temp, 1, 1, tmp_str);
  341. DEBUG_MSG_P(PSTR("[THERMOSTAT] switch %s, temp: %s, min: %d, max: %d, mode: %s, relay: %s, last switch %d\n"),
  342. 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);
  343. }
  344. //------------------------------------------------------------------------------
  345. inline bool lastSwitchEarlierThan(unsigned int comparing_time) {
  346. return millis() - _thermostat.last_switch > comparing_time;
  347. }
  348. //------------------------------------------------------------------------------
  349. inline void switchThermostat(bool state, double temp) {
  350. debugPrintSwitch(state, temp);
  351. setThermostatState(state);
  352. }
  353. //------------------------------------------------------------------------------
  354. //----------- Main function that make decision ---------------------------------
  355. //------------------------------------------------------------------------------
  356. void checkTempAndAdjustRelay(double temp) {
  357. if (_thermostat_mode_cooler == false) { // Main operation mode. Thermostat is HEATER.
  358. // if thermostat switched ON and t > max - switch it OFF and start cooling
  359. if (relayStatus(THERMOSTAT_RELAY) && temp > _temp_range.max) {
  360. _thermostat_cycle = cooling;
  361. switchThermostat(false, temp);
  362. // if thermostat switched ON for max time - switch it OFF for rest
  363. } else if (relayStatus(THERMOSTAT_RELAY) && lastSwitchEarlierThan(_thermostat_max_on_time)) {
  364. switchThermostat(false, temp);
  365. // if t < min and thermostat switched OFF for at least minimum time - switch it ON and start
  366. } else if (!relayStatus(THERMOSTAT_RELAY) && temp < _temp_range.min
  367. && (_thermostat.last_switch == 0 || lastSwitchEarlierThan(_thermostat_min_off_time))) {
  368. _thermostat_cycle = heating;
  369. switchThermostat(true, temp);
  370. // if heating cycle and thermostat switchaed OFF for more than min time - switch it ON
  371. // continue heating cycle
  372. } else if (!relayStatus(THERMOSTAT_RELAY) && _thermostat_cycle == heating
  373. && lastSwitchEarlierThan(_thermostat_min_off_time)) {
  374. switchThermostat(true, temp);
  375. }
  376. } else { // Thermostat is COOLER. Inverse logic.
  377. // if thermostat switched ON and t < min - switch it OFF and start heating
  378. if (relayStatus(THERMOSTAT_RELAY) && temp < _temp_range.min) {
  379. _thermostat_cycle = heating;
  380. switchThermostat(false, temp);
  381. // if thermostat switched ON for max time - switch it OFF for rest
  382. } else if (relayStatus(THERMOSTAT_RELAY) && lastSwitchEarlierThan(_thermostat_max_on_time)) {
  383. switchThermostat(false, temp);
  384. // if t > max and thermostat switched OFF for at least minimum time - switch it ON and start
  385. } else if (!relayStatus(THERMOSTAT_RELAY) && temp > _temp_range.max
  386. && (_thermostat.last_switch == 0 || lastSwitchEarlierThan(_thermostat_min_off_time))) {
  387. _thermostat_cycle = cooling;
  388. switchThermostat(true, temp);
  389. // if cooling cycle and thermostat switchaed OFF for more than min time - switch it ON
  390. // continue cooling cycle
  391. } else if (!relayStatus(THERMOSTAT_RELAY) && _thermostat_cycle == cooling
  392. && lastSwitchEarlierThan(_thermostat_min_off_time)) {
  393. switchThermostat(true, temp);
  394. }
  395. }
  396. }
  397. //------------------------------------------------------------------------------
  398. void updateCounters() {
  399. if (relayStatus(THERMOSTAT_RELAY)) {
  400. setSetting(NAME_BURN_TOTAL, ++_thermostat_burn_total);
  401. setSetting(NAME_BURN_TODAY, ++_thermostat_burn_today);
  402. setSetting(NAME_BURN_THIS_MONTH, ++_thermostat_burn_this_month);
  403. }
  404. if (ntpSynced()) {
  405. String value = NTP.getDateStr();
  406. unsigned int day = value.substring(0, 2).toInt();
  407. unsigned int month = value.substring(3, 5).toInt();
  408. if (day != _thermostat_burn_day) {
  409. _thermostat_burn_yesterday = _thermostat_burn_today;
  410. _thermostat_burn_today = 0;
  411. _thermostat_burn_day = day;
  412. setSetting(NAME_BURN_YESTERDAY, _thermostat_burn_yesterday);
  413. setSetting(NAME_BURN_TODAY, _thermostat_burn_today);
  414. setSetting(NAME_BURN_DAY, _thermostat_burn_day);
  415. }
  416. if (month != _thermostat_burn_month) {
  417. _thermostat_burn_prev_month = _thermostat_burn_this_month;
  418. _thermostat_burn_this_month = 0;
  419. _thermostat_burn_month = month;
  420. setSetting(NAME_BURN_PREV_MONTH, _thermostat_burn_prev_month);
  421. setSetting(NAME_BURN_THIS_MONTH, _thermostat_burn_this_month);
  422. setSetting(NAME_BURN_MONTH, _thermostat_burn_month);
  423. }
  424. }
  425. }
  426. //------------------------------------------------------------------------------
  427. double getLocalTemperature() {
  428. #if SENSOR_SUPPORT
  429. for (byte i=0; i<magnitudeCount(); i++) {
  430. if (magnitudeType(i) == MAGNITUDE_TEMPERATURE) {
  431. double temp = magnitudeValue(i);
  432. char tmp_str[16];
  433. dtostrf(temp, 1, 1, tmp_str);
  434. DEBUG_MSG_P(PSTR("[THERMOSTAT] getLocalTemperature temp: %s\n"), tmp_str);
  435. return temp > -0.1 && temp < 0.1 ? DBL_MIN : temp;
  436. }
  437. }
  438. #endif
  439. return DBL_MIN;
  440. }
  441. //------------------------------------------------------------------------------
  442. double getLocalHumidity() {
  443. #if SENSOR_SUPPORT
  444. for (byte i=0; i<magnitudeCount(); i++) {
  445. if (magnitudeType(i) == MAGNITUDE_HUMIDITY) {
  446. double hum = magnitudeValue(i);
  447. char tmp_str[16];
  448. dtostrf(hum, 1, 0, tmp_str);
  449. DEBUG_MSG_P(PSTR("[THERMOSTAT] getLocalHumidity hum: %s\%\n"), tmp_str);
  450. return hum > -0.1 && hum < 0.1 ? DBL_MIN : hum;
  451. }
  452. }
  453. #endif
  454. return DBL_MIN;
  455. }
  456. //------------------------------------------------------------------------------
  457. // Loop
  458. //------------------------------------------------------------------------------
  459. void thermostatLoop(void) {
  460. if (!thermostatEnabled())
  461. return;
  462. // Update temperature range
  463. if (mqttConnected()) {
  464. if (millis() - _temp_range.ask_time > _temp_range.ask_interval) {
  465. _temp_range.ask_time = millis();
  466. sendTempRangeRequest();
  467. }
  468. }
  469. // Update thermostat state
  470. if (millis() - _thermostat.last_update > THERMOSTAT_STATE_UPDATE_INTERVAL) {
  471. _thermostat.last_update = millis();
  472. updateCounters();
  473. unsigned int last_temp_src = _thermostat.temperature_source;
  474. if (_remote_temp.last_update != 0 && millis() - _remote_temp.last_update < _thermostat_remote_temp_max_wait) {
  475. // we have remote temp
  476. _thermostat.temperature_source = temp_remote;
  477. DEBUG_MSG_P(PSTR("[THERMOSTAT] setup thermostat by remote temperature\n"));
  478. checkTempAndAdjustRelay(_remote_temp.temp);
  479. } else if (getLocalTemperature() != DBL_MIN) {
  480. // we have local temp
  481. _thermostat.temperature_source = temp_local;
  482. DEBUG_MSG_P(PSTR("[THERMOSTAT] setup thermostat by local temperature\n"));
  483. checkTempAndAdjustRelay(getLocalTemperature());
  484. // updateRemoteTemp(false);
  485. } else {
  486. // we don't have any temp - switch thermostat on for N minutes every hour
  487. _thermostat.temperature_source = temp_none;
  488. DEBUG_MSG_P(PSTR("[THERMOSTAT] setup thermostat by timeout\n"));
  489. if (relayStatus(THERMOSTAT_RELAY) && millis() - _thermostat.last_switch > _thermostat_alone_on_time) {
  490. setThermostatState(false);
  491. } else if (!relayStatus(THERMOSTAT_RELAY) && millis() - _thermostat.last_switch > _thermostat_alone_off_time) {
  492. setThermostatState(false);
  493. }
  494. }
  495. if (last_temp_src != _thermostat.temperature_source) {
  496. updateOperationMode();
  497. }
  498. }
  499. }
  500. //------------------------------------------------------------------------------
  501. String getBurnTimeStr(unsigned int burn_time) {
  502. char burnTimeStr[18] = { 0 };
  503. if (burn_time < 60) {
  504. sprintf(burnTimeStr, "%d мин.", burn_time);
  505. } else {
  506. sprintf(burnTimeStr, "%d ч. %d мин.", (int)floor(burn_time / 60), burn_time % 60);
  507. }
  508. return String(burnTimeStr);
  509. }
  510. //------------------------------------------------------------------------------
  511. void resetBurnCounters() {
  512. DEBUG_MSG_P(PSTR("[THERMOSTAT] resetBurnCounters\n"));
  513. setSetting(NAME_BURN_TOTAL, 0);
  514. setSetting(NAME_BURN_TODAY, 0);
  515. setSetting(NAME_BURN_YESTERDAY, 0);
  516. setSetting(NAME_BURN_THIS_MONTH, 0);
  517. setSetting(NAME_BURN_PREV_MONTH, 0);
  518. _thermostat_burn_total = 0;
  519. _thermostat_burn_today = 0;
  520. _thermostat_burn_yesterday = 0;
  521. _thermostat_burn_this_month = 0;
  522. _thermostat_burn_prev_month = 0;
  523. }
  524. #endif // THERMOSTAT_SUPPORT
  525. //#######################################################################
  526. // ___ _ _
  527. // | \ (_) ___ _ __ | | __ _ _ _
  528. // | |) || |(_-<| '_ \| |/ _` || || |
  529. // |___/ |_|/__/| .__/|_|\__,_| \_, |
  530. // |_| |__/
  531. //#######################################################################
  532. #if THERMOSTAT_DISPLAY_SUPPORT
  533. #include "SSD1306.h" // alias for `#include "SSD1306Wire.h"`
  534. #define wifi_on_width 16
  535. #define wifi_on_height 16
  536. const char wifi_on_bits[] PROGMEM = {
  537. 0x00, 0x00, 0x0E, 0x00, 0x7E, 0x00, 0xFE, 0x01, 0xE0, 0x03, 0x80, 0x07,
  538. 0x02, 0x0F, 0x1E, 0x1E, 0x3E, 0x1C, 0x78, 0x38, 0xE0, 0x38, 0xC0, 0x31,
  539. 0xC6, 0x71, 0x8E, 0x71, 0x8E, 0x73, 0x00, 0x00, };
  540. #define mqtt_width 16
  541. #define mqtt_height 16
  542. const char mqtt_bits[] PROGMEM = {
  543. 0x00, 0x00, 0x00, 0x08, 0x00, 0x18, 0x00, 0x38, 0xEA, 0x7F, 0xEA, 0x7F,
  544. 0x00, 0x38, 0x10, 0x18, 0x18, 0x08, 0x1C, 0x00, 0xFE, 0x57, 0xFE, 0x57,
  545. 0x1C, 0x00, 0x18, 0x00, 0x10, 0x00, 0x00, 0x00, };
  546. #define remote_temp_width 16
  547. #define remote_temp_height 16
  548. const char remote_temp_bits[] PROGMEM = {
  549. 0x00, 0x00, 0xE0, 0x18, 0x10, 0x25, 0x10, 0x25, 0x90, 0x19, 0x50, 0x01,
  550. 0x50, 0x01, 0xD0, 0x01, 0x50, 0x01, 0x50, 0x01, 0xD0, 0x01, 0x50, 0x01,
  551. 0xE0, 0x00, 0xE0, 0x00, 0xE0, 0x00, 0x00, 0x00, };
  552. #define server_width 16
  553. #define server_height 16
  554. const char server_bits[] PROGMEM = {
  555. 0x00, 0x00, 0xF8, 0x1F, 0xFC, 0x3F, 0x0C, 0x30, 0x0C, 0x30, 0x0C, 0x30,
  556. 0x0C, 0x30, 0x0C, 0x30, 0x0C, 0x30, 0xF8, 0x1F, 0xFC, 0x3F, 0xFE, 0x7F,
  557. 0x1E, 0x78, 0xFE, 0x7F, 0xFC, 0x3F, 0x00, 0x00, };
  558. #define LOCAL_TEMP_UPDATE_INTERVAL 60000
  559. #define LOCAL_HUM_UPDATE_INTERVAL 61000
  560. SSD1306 display(0x3c, 1, 3);
  561. unsigned long _local_temp_last_update = 0xFFFF;
  562. unsigned long _local_hum_last_update = 0xFFFF;
  563. bool _display_wifi_status = true;
  564. bool _display_mqtt_status = true;
  565. bool _display_server_status = true;
  566. bool _display_remote_temp_status = true;
  567. bool _display_need_refresh = false;
  568. bool _temp_range_need_update = true;
  569. //------------------------------------------------------------------------------
  570. void drawIco(int16_t x, int16_t y, const char *ico, bool on = true) {
  571. display.drawIco16x16(x, y, ico, !on);
  572. _display_need_refresh = true;
  573. }
  574. //------------------------------------------------------------------------------
  575. void display_wifi_status(bool on) {
  576. _display_wifi_status = on;
  577. drawIco(0, 0, wifi_on_bits, on);
  578. }
  579. //------------------------------------------------------------------------------
  580. void display_mqtt_status(bool on) {
  581. _display_mqtt_status = on;
  582. drawIco(17, 0, mqtt_bits, on);
  583. }
  584. //------------------------------------------------------------------------------
  585. void display_server_status(bool on) {
  586. _display_server_status = on;
  587. drawIco(34, 0, server_bits, on);
  588. }
  589. //------------------------------------------------------------------------------
  590. void display_remote_temp_status(bool on) {
  591. _display_remote_temp_status = on;
  592. drawIco(51, 0, remote_temp_bits, on);
  593. }
  594. //------------------------------------------------------------------------------
  595. void display_temp_range() {
  596. _temp_range.need_display_update = false;
  597. display.setColor(BLACK);
  598. display.fillRect(68, 0, 60, 16);
  599. display.setColor(WHITE);
  600. display.setTextAlignment(TEXT_ALIGN_RIGHT);
  601. display.setFont(ArialMT_Plain_16);
  602. String temp_range = String(_temp_range.min) + "°- " + String(_temp_range.max) + "°";
  603. display.drawString(128, 0, temp_range);
  604. _display_need_refresh = true;
  605. }
  606. //------------------------------------------------------------------------------
  607. void display_remote_temp() {
  608. _remote_temp.need_display_update = false;
  609. display.setColor(BLACK);
  610. display.fillRect(0, 16, 128, 16);
  611. display.setColor(WHITE);
  612. display.setFont(ArialMT_Plain_16);
  613. display.setTextAlignment(TEXT_ALIGN_LEFT);
  614. String temp_range_title = String("Remote t");
  615. display.drawString(0, 16, temp_range_title);
  616. String temp_range_vol = String("= ") + (_display_remote_temp_status ? String(_remote_temp.temp, 1) : String("?")) + "°";
  617. display.drawString(75, 16, temp_range_vol);
  618. _display_need_refresh = true;
  619. }
  620. //------------------------------------------------------------------------------
  621. void display_local_temp() {
  622. display.setColor(BLACK);
  623. display.fillRect(0, 32, 128, 16);
  624. display.setColor(WHITE);
  625. display.setFont(ArialMT_Plain_16);
  626. display.setTextAlignment(TEXT_ALIGN_LEFT);
  627. String local_temp_title = String("Local t");
  628. display.drawString(0, 32, local_temp_title);
  629. String local_temp_vol = String("= ") + (getLocalTemperature() != DBL_MIN ? String(getLocalTemperature(), 1) : String("?")) + "°";
  630. display.drawString(75, 32, local_temp_vol);
  631. _display_need_refresh = true;
  632. }
  633. //------------------------------------------------------------------------------
  634. void display_local_humidity() {
  635. display.setColor(BLACK);
  636. display.fillRect(0, 48, 128, 16);
  637. display.setColor(WHITE);
  638. display.setFont(ArialMT_Plain_16);
  639. display.setTextAlignment(TEXT_ALIGN_LEFT);
  640. String local_hum_title = String("Local h ");
  641. display.drawString(0, 48, local_hum_title);
  642. String local_hum_vol = String("= ") + (getLocalHumidity() != DBL_MIN ? String(getLocalHumidity(), 0) : String("?")) + "%";
  643. display.drawString(75, 48, local_hum_vol);
  644. _display_need_refresh = true;
  645. }
  646. //------------------------------------------------------------------------------
  647. // Setup
  648. //------------------------------------------------------------------------------
  649. void displaySetup() {
  650. display.init();
  651. display.flipScreenVertically();
  652. // display.setFont(ArialMT_Plain_24);
  653. // display.setTextAlignment(TEXT_ALIGN_CENTER);
  654. // display.drawString(64, 17, "Thermostat");
  655. espurnaRegisterLoop(displayLoop);
  656. }
  657. //------------------------------------------------------------------------------
  658. void displayLoop() {
  659. _display_need_refresh = false;
  660. //------------------------------------------------------------------------------
  661. // Indicators
  662. //------------------------------------------------------------------------------
  663. if (!_display_wifi_status) {
  664. if (wifiConnected() && WiFi.getMode() != WIFI_AP)
  665. display_wifi_status(true);
  666. } else if (!wifiConnected() || WiFi.getMode() == WIFI_AP) {
  667. display_wifi_status(false);
  668. }
  669. if (!_display_mqtt_status) {
  670. if (mqttConnected())
  671. display_mqtt_status(true);
  672. } else if (!mqttConnected()) {
  673. display_mqtt_status(false);
  674. }
  675. if (millis() - _temp_range.last_update < THERMOSTAT_SERVER_LOST_INTERVAL) {
  676. if (!_display_server_status)
  677. display_server_status(true);
  678. } else if (_display_server_status) {
  679. display_server_status(false);
  680. }
  681. if (millis() - _remote_temp.last_update < _thermostat_remote_temp_max_wait) {
  682. if (!_display_remote_temp_status)
  683. display_remote_temp_status(true);
  684. } else if (_display_remote_temp_status) {
  685. display_remote_temp_status(false);
  686. display_remote_temp();
  687. }
  688. //------------------------------------------------------------------------------
  689. // Temp range
  690. //------------------------------------------------------------------------------
  691. if (_temp_range.need_display_update) {
  692. display_temp_range();
  693. }
  694. //------------------------------------------------------------------------------
  695. // Remote temp
  696. //------------------------------------------------------------------------------
  697. if (_remote_temp.need_display_update) {
  698. display_remote_temp();
  699. }
  700. //------------------------------------------------------------------------------
  701. // Local temp
  702. //------------------------------------------------------------------------------
  703. if (millis() - _local_temp_last_update > LOCAL_TEMP_UPDATE_INTERVAL) {
  704. _local_temp_last_update = millis();
  705. display_local_temp();
  706. }
  707. //------------------------------------------------------------------------------
  708. // Local temp
  709. //------------------------------------------------------------------------------
  710. if (millis() - _local_hum_last_update > LOCAL_HUM_UPDATE_INTERVAL) {
  711. _local_hum_last_update = millis();
  712. display_local_humidity();
  713. }
  714. //------------------------------------------------------------------------------
  715. // Display update
  716. //------------------------------------------------------------------------------
  717. if (_display_need_refresh) {
  718. yield();
  719. display.display();
  720. }
  721. }
  722. #endif // THERMOSTAT_DISPLAY_SUPPORT