WiFiThermostat.ino 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. // pre compiletime config
  2. //#define DEBUG_VERBOSE
  3. #define SPIFFS_DBG
  4. #define SPIFFS_USE_MAGIC
  5. #define FIRMWARE_NAME "WiFiThermostat"
  6. #define VERSION "0.3.0"
  7. // default values, can later be overridden via configuration (conf)
  8. #define DEVICE_NAME "WiFi-Thermostat-1"
  9. #define DEFAULT_HTTP_USER ""
  10. #define DEFAULT_HTTP_PASS ""
  11. #define HTTP_SET_TOKEN "grzbrz"
  12. #define MQTT_SERVER "10.1.1.11"
  13. #define MQTT_PORT 1883
  14. #define MQTT_USER ""
  15. #define MQTT_PASS ""
  16. #define MQTT_TOPIC_IN "Test/Thermostat"
  17. #define MQTT_TOPIC_OUT "Test/Thermostat/stat"
  18. #define MQTT_OUT_RETAIN false
  19. #define MQTT_WILLTOPIC "Test/Thermostat/availability"
  20. #define MQTT_WILLQOS 2
  21. #define MQTT_WILLRETAIN false
  22. #define MQTT_WILLMSG "offline"
  23. #define MQTT_CONNMSG "online"
  24. #define DOMOTICZ_OUT_TOPIC "domoticz/out"
  25. // default values, can later be overridden via configuration (conf2)
  26. #define DOMOTICZ_IDX_THERMOSTAT 0
  27. #define DOMOTICZ_IDX_THERMOSTATMODE 0
  28. #define DOMOTICZ_IDX_TEMPHUMSENSOR 0
  29. #define DOMOTICZ_IDX_HEATING 0
  30. #define DOMOTICZ_IDX_PIR 0
  31. #define OUTTEMP_TOPIC_IN ""
  32. #define OUTHUM_TOPIC_IN ""
  33. #define MQTT_TOPIC_PIR "" // extra publish topic for PIR sensor
  34. #define AUTOSAVE_SETTEMP true
  35. #define AUTOSAVE_SETMODE true
  36. #define DEFAULT_HEATING_MIN_OFFTIME 120 // minimal time the heating keeps turned off in s
  37. #define DEFAULT_SETTEMP_MIN 16.0 // minimal temperature that can be set
  38. #define DEFAULT_SETTEMP_MAX 25.0 // maximal temperature that can be set
  39. #define DEFAULT_SETTEMP_LOW 18.0 // set temperature in night/low mode
  40. #define DEFAULT_SETTEMP_LOW2 20.0 // set temperature in night/low mode
  41. #define DEFAULT_SETTEMP_HEATOFF 3.0 // set temperature in OFF mode (freezing guard > 0)
  42. #define DEFAULT_HYSTERESIS 0.1 // hysteresis, normally 0.1 - 0.5
  43. #define SETTEMP_DECREASE_VALUE 0.0 // decreases the set temp to overcome further temperature rise when the heating is already switched off
  44. #define TEMPSENSOR_CORRECTION_VALUE 0.0 // correction value for temperature sensor reading
  45. #define HUMSENSOR_CORRECTION_VALUE 0 // correction value for humidity sensor reading
  46. #define DEFAULT_MEASURE_INTERVAL 15 // interval for temp/hum measurement
  47. #define DEFAULT_DISPLAY_INTERVAL 5 // interval for display updates (if out-temp is active, display will toggle in this interval)
  48. #define DEFAULT_DISPLAY_TIMEOUT 30 // display timeout after keypress (illumination)
  49. #define DEFAULT_PIR_ENABLES_DISPLAY false
  50. #define MODE_NAME_0 "off"
  51. #define MODE_NAME_1 "heat"
  52. #define PRESET_NAME_0 "none"
  53. #define PRESET_NAME_1 "reduction1"
  54. #define PRESET_NAME_2 "reduction2"
  55. // default initial values
  56. #define DEFAULT_SETTEMP 21.5
  57. #define DEFAULT_HEATINGMODE 1
  58. #define DEFAULT_PRESET 1
  59. // default values that can only be configured at compile time / hardware configuration
  60. #define CLEARCONF_TOKEN "DOIT!" // Token used to reset configuration via http call on http://<IP>/delconf?token=<TOKEN> (use when password is forgotten)
  61. #define BUTTON_DEBOUNCE_TIME 120
  62. #define BUTTON_HOLD_TIME 750
  63. #define DOMOTICZ_IN_TOPIC "domoticz/in" // if Domoticz IDXes are configured, updates will be sent to this topic
  64. #define SETTEMP_LOW_MIN 14.0 // minimal configurable temperature for reduction mode
  65. #define SETTEMP_LOW_MAX 20.0 // maximal configurable temperature for reduction mode
  66. #define DOMOTICZ_DISMISSUPDATE_TIMEOUT 2500 // after a value was changed by data from domoticz/out, domoticz/out parsing for this device will be turned off for this time to prevent infinite loops
  67. #define DOMOTICZ_FORCEUPDATE_INTERVAL 15 // interval in min to force update of domoticz devices
  68. #define MQTT_HEARTBEAT_MAXAGE 180000 // interval for MQTT heartbeat message. only applicable if MQTT IN-topic is defined. after this timeout MQTT reconnect is forced
  69. // pin assignments and I2C addresses
  70. #define PIN_DHTSENSOR 13
  71. #define PIN_RELAIS 15 //16
  72. #define PIN_BUTTON_PLUS 2
  73. #define PIN_BUTTON_MINUS 0
  74. #define PIN_BUTTON_MODE 14
  75. #define PIN_PIRSENSOR 12
  76. #define DHTTYPE DHT22 // DHT sensor type
  77. #define LCDADDR 0x27 // I2C address LCD
  78. #define LCDCOLS 16
  79. #define LCDLINES 2
  80. // default logic levels
  81. #define RELAISONSTATE HIGH
  82. #define BUTTONONSTATE LOW
  83. #include <Button.h>
  84. #include <ButtonEventCallback.h>
  85. #include <PushButton.h>
  86. #include <Bounce2.h>
  87. PushButton buttonPlus = PushButton(PIN_BUTTON_PLUS, ENABLE_INTERNAL_PULLUP);
  88. PushButton buttonMinus = PushButton(PIN_BUTTON_MINUS, ENABLE_INTERNAL_PULLUP);
  89. PushButton buttonMode = PushButton(PIN_BUTTON_MODE, ENABLE_INTERNAL_PULLUP);
  90. PushButton pirSensor = PushButton(PIN_PIRSENSOR, PRESSED_WHEN_HIGH);
  91. #include <PersWiFiManager.h>
  92. #include <ArduinoJson.h>
  93. #include <ESP8266WiFi.h>
  94. #include <WiFiClient.h>
  95. #include <ESP8266WebServer.h>
  96. #include <ESP8266mDNS.h>
  97. #include <ESP8266HTTPUpdateServer.h>
  98. #include "PubSubClient.h"
  99. #include <DNSServer.h>
  100. #include <FS.h>
  101. #include <Wire.h>
  102. #include <LiquidCrystal_I2C.h>
  103. #include <DHT.h>
  104. #include <string.h>
  105. #ifndef MESSZ
  106. #define MESSZ 405 // Max number of characters in JSON message string (4 x DS18x20 sensors)
  107. #endif
  108. // Max message size calculated by PubSubClient is (MQTT_MAX_PACKET_SIZE < 5 + 2 + strlen(topic) + plength)
  109. #if (MQTT_MAX_PACKET_SIZE -TOPSZ -7) < MESSZ // If the max message size is too small, throw an error at compile time
  110. // See pubsubclient.c line 359
  111. #error "MQTT_MAX_PACKET_SIZE is too small in libraries/PubSubClient/src/PubSubClient.h, increase it to at least 512"
  112. #endif
  113. boolean serialdebug = true;
  114. boolean mqttdebug = true;
  115. // config variables - do not change here!
  116. //conf
  117. char deviceName[31]; // device name - just for web interface
  118. char http_user[31];
  119. char http_pass[31];
  120. char http_token[31];
  121. char mqtt_server[41];
  122. int mqtt_port = MQTT_PORT;
  123. char mqtt_user[31];
  124. char mqtt_pass[31];
  125. char mqtt_topic_in[51]; // MQTT in topic for commands
  126. char mqtt_topic_out[51]; // MQTT out base topic, will be extended by various value names
  127. boolean mqtt_outRetain = MQTT_OUT_RETAIN; // send MQTT out with retain flag
  128. char mqtt_willTopic[51]; // MQTT Last Will topic
  129. int mqtt_willQos = MQTT_WILLQOS; // MQTT Last Will topic QOS
  130. boolean mqtt_willRetain = MQTT_WILLRETAIN; // MQTT Last Will retain
  131. char mqtt_willMsg[31]; // MQTT Last Will payload
  132. char mqtt_connMsg[31];
  133. char domoticz_out_topic[55]; // domoticz out topic to subscribe to (only applicable if domoticzIdx_Thermostat and/or domoticzIdx_ThermostatMode is set to >0)
  134. char mqtt_topic_in_cmd[61];
  135. char mqtt_topic_in_setTemp[61];
  136. char mqtt_topic_in_setMode[61];
  137. char mqtt_topic_in_setPreset[61];
  138. //conf2
  139. int domoticzIdx_Thermostat = DOMOTICZ_IDX_THERMOSTAT;
  140. int domoticzIdx_ThermostatMode = DOMOTICZ_IDX_THERMOSTATMODE;
  141. int domoticzIdx_TempHumSensor = DOMOTICZ_IDX_TEMPHUMSENSOR;
  142. int domoticzIdx_Heating = DOMOTICZ_IDX_HEATING;
  143. int domoticzIdx_PIR = DOMOTICZ_IDX_PIR;
  144. char outTemp_topic_in[51];
  145. char outHum_topic_in[51];
  146. char mqtt_topic_pir[51];
  147. boolean autoSaveSetTemp = AUTOSAVE_SETTEMP;
  148. boolean autoSaveHeatingMode = AUTOSAVE_SETMODE;
  149. int heatingMinOffTime = DEFAULT_HEATING_MIN_OFFTIME; // minimal time the heating keeps turned off in s
  150. float setTempMin = DEFAULT_SETTEMP_MIN; // minimal temperature that can be set
  151. float setTempMax = DEFAULT_SETTEMP_MAX; // maximal temperature that can be set
  152. float setTempLow = DEFAULT_SETTEMP_LOW; // set temperature in night/low mode
  153. float setTempLow2 = DEFAULT_SETTEMP_LOW2; // set temperature in night/low mode
  154. float hysteresis = DEFAULT_HYSTERESIS; // hysteresis, normally 0.1 - 0.5
  155. float setTempDecreaseVal = SETTEMP_DECREASE_VALUE; // decreases the set temp to overcome further temperature rise when the heating is already switched off
  156. float tempCorrVal = TEMPSENSOR_CORRECTION_VALUE; // correction value for temperature sensor reading
  157. int humCorrVal = HUMSENSOR_CORRECTION_VALUE; // correction value for humidity sensor reading
  158. int measureInterval = DEFAULT_MEASURE_INTERVAL; // interval for temp/hum measurement
  159. int displayInterval = DEFAULT_DISPLAY_INTERVAL; // interval for display updates (if out-temp is active, display will toggle in this interval)
  160. int displayInterval_saved = DEFAULT_DISPLAY_INTERVAL;
  161. int displayTimeout = DEFAULT_DISPLAY_TIMEOUT; // display timeout after keypress (illumination)
  162. boolean PIR_enablesDisplay = DEFAULT_PIR_ENABLES_DISPLAY; // PIR sensor enables display illumination
  163. char modename0[15];
  164. char modename1[15];
  165. char psetname0[15];
  166. char psetname1[15];
  167. char psetname2[15];
  168. char offMessage[15];
  169. //set values
  170. float setTemp = DEFAULT_SETTEMP;
  171. float currSetTemp = DEFAULT_SETTEMP;
  172. byte heatingMode = DEFAULT_HEATINGMODE; // 0 = off, 1 = heat
  173. byte preset = DEFAULT_PRESET; // 0 = normal/day, 1 = night/reduction 1, 2 = reduction 2 (long term leave)
  174. float setTempSaved;
  175. byte heatingModeSaved; // 0 = off, 1 = heat/on
  176. byte presetSaved; // 0 = normal/day, 1 = night/reduction 1, 2 = reduction 2
  177. // not changeable via configuration
  178. float setTempLowMin = SETTEMP_LOW_MIN;
  179. float setTempLowMax = SETTEMP_LOW_MAX;
  180. boolean debug = true;
  181. int debounceTime = BUTTON_DEBOUNCE_TIME;
  182. int buttonHoldTime = BUTTON_HOLD_TIME;
  183. // global variables
  184. float currTemp; // last reading from DHT sensor
  185. float currTemp_raw; // last reading from DHT sensor
  186. int currHum; // last reading from DHT sensor
  187. int currHum_raw; // last reading from DHT sensor
  188. boolean turnHeatingOn = false; // true if heating is active (relais switched on)
  189. unsigned long heatingLastOnMillis; // last time heating was switched on
  190. unsigned long heatingLastOffMillis; // last time heating was switched off
  191. float outTemp; // outside temp (via MQTT if enabled and in-topic configured)
  192. int outHum; // outside temp (via MQTT if enabled and in-topic configured)
  193. long outTempHumLastUpdate; // last reading from out temp/hum source
  194. char outTemp_newValue[6];
  195. boolean outTemp_parseNewValue;
  196. char outHum_newValue[4];
  197. boolean outHum_parseNewValue;
  198. char currentModeName[15];
  199. char currentPresetName[15];
  200. byte whichTempToDisplay; // 1=temp inside (from DHT sensor), 2= temp outside (via MQTT) - if out temp/hum available this value and the displayed value pair toggles with every displayInterval
  201. unsigned long lastMeasure = 0; // millis of last temp/hum measurement
  202. unsigned long lastDisplayUpdate = 0; // millis of last display update
  203. unsigned long lastDisplayToggle = 0; // millis of last display toggle
  204. unsigned long lastTempUpdate = 0; // last update time of DHT reading
  205. char msg[50]; // buffer MQTT in payload
  206. char topic[50]; // buffer MQTT in topic
  207. boolean displayActive = false; // gets true when button is pressed. display light gets switched on until timeout. button actions are only performed while display is active
  208. boolean PIRSensorOn = false;
  209. unsigned long heatingOnTime, heatingOffTime;
  210. boolean useDomoticz = false; // will be set to true in setup() if idx-values other than 0 are configured
  211. boolean domoticzOutParseData = false; // indicates that domoticz/out json data is buffered, will then be parsed in next loop() run
  212. boolean domoticzOutParserBusy = false; // indicates that domoticz/out json data is currently processed - no futher data will be accepted until finished
  213. char domoticzOutPayload[450]; // buffer for domoticz/out data
  214. int dismissUpdateFromDomoticzTimeout = DOMOTICZ_DISMISSUPDATE_TIMEOUT; // after a value was changed by data from domoticz/out, domoticz/out parsing for this device will be turned off for this time to prevent infinite loops
  215. unsigned long lastUpdate_setTemp = 0; // set to millis() every time setTemp value is changed. next update from domoticz/out will be rejected for dismissUpdateFromDomoticzTimeout in this case
  216. unsigned long lastUpdate_heatingMode = 0; // set to millis() every time heatingMode value is changed. next update from domoticz/out will be rejected for dismissUpdateFromDomoticzTimeout in this case
  217. unsigned long lastUpdate_preset = 0; // set to millis() every time preset value is changed. next update from domoticz/out will be rejected for dismissUpdateFromDomoticzTimeout in this case
  218. boolean lastUpdateFromDomoticz_setTemp = false;
  219. boolean lastUpdateFromDomoticz_heatingMode = false;
  220. int domoticzUpdateInterval = DOMOTICZ_FORCEUPDATE_INTERVAL; // interval in min to force update of domoticz devices
  221. char cmdPayload[101]; // buffer for commands
  222. boolean cmdInQueue = false; // command is queued and will be processed next loop() run
  223. boolean saveConfigToFlash = false; // conf is saved in next loop() run
  224. boolean saveConfig2ToFlash = false; // conf2 is saved in next loop() run
  225. unsigned int saveValuesTimeout = 5000;
  226. unsigned long lastValueChange; // is set to millis() whenever setTemp value and/or heatingMode value is changed. used for autoSave function with hardcoded 5s timeout
  227. boolean setTempAlreadySaved = true; // only save if not yet done
  228. boolean heatingModeAlreadySaved = true; // only save if not yet done
  229. boolean presetAlreadySaved = true; // only save if not yet done
  230. unsigned long lastRun = 0;
  231. int count100ms = 0;
  232. int countSeconds = 0;
  233. int countMeasureInterval = 0;
  234. int countDisplayInterval = 0;
  235. int displayOverlayMsgTimeout = 2;
  236. boolean togglingTempHumAIDisplay = false;
  237. byte mqttMode = 0;
  238. unsigned long mqttLastReconnectAttempt = 0;
  239. int mqttReconnectAttempts = 0;
  240. int mqttReconnects = 0;
  241. boolean mqttConnected = false;
  242. unsigned long mqttLastHeartbeat;
  243. boolean mqttInTopicSubscribed = false;
  244. boolean pendingRestart = false;
  245. boolean doRestart = false;
  246. unsigned long pendingRestart_lastMillis = 0;
  247. boolean displayShowFullscreenMsg = false;
  248. //unsigned long displayShowFullscreenMsg_lastMillis = 0;
  249. boolean pendingPresetToggle = false;
  250. boolean updateDisplayImmediately = false;
  251. int pendingPreset;
  252. char pendingPresetName[15];
  253. //unsigned long pendingPreset_millis = 0;
  254. //int pendingPreset_timeout = 2000;
  255. boolean displayShowLine2OverlayMsg = false;
  256. DHT dht(PIN_DHTSENSOR, DHTTYPE);
  257. LiquidCrystal_I2C lcd(LCDADDR, LCDCOLS, LCDLINES); // set the LCD address to 0x27 for a 16 chars and 2 line display
  258. WiFiClient espClient;
  259. void mqttCallback(char* topic, byte* payload, unsigned int length);
  260. PubSubClient mqttclient(espClient);
  261. ESP8266WebServer httpServer(80);
  262. DNSServer dnsServer;
  263. PersWiFiManager persWM(httpServer, dnsServer);
  264. ESP8266HTTPUpdateServer httpUpdater;
  265. void setup() {
  266. Serial.begin(115200);
  267. delay(500);
  268. Serial.println();
  269. Serial.print(FIRMWARE_NAME);
  270. Serial.print(" v");
  271. Serial.print(VERSION);
  272. Serial.println("starting...");
  273. pinMode(PIN_RELAIS, OUTPUT);
  274. digitalWrite(PIN_RELAIS, !RELAISONSTATE);
  275. pinMode(PIN_PIRSENSOR, INPUT);
  276. buttonPlus.configureButton(configurePushButton);
  277. //buttonPlus.onPress(onButtonPressed); // When the button is first pressed, call the function onButtonPressed (further down the page)
  278. buttonPlus.onHoldRepeat(1000, 350, onButtonHeld); // Once the button has been held for 1 second (1000ms) call onButtonHeld. Call it again every 350ms until it is let go
  279. buttonPlus.onRelease(50, 500, onButtonReleased); // When the button is held >50ms and released after <500ms, call onButtonReleased
  280. buttonMinus.configureButton(configurePushButton);
  281. //buttonMinus.onPress(onButtonPressed); // When the button is first pressed, call the function onButtonPressed (further down the page)
  282. buttonMinus.onHoldRepeat(1000, 350, onButtonHeld); // Once the button has been held for 1 second (1000ms) call onButtonHeld. Call it again every 350ms until it is let go
  283. buttonMinus.onRelease(50, 500, onButtonReleased); // When the button is held >50ms and released after <500ms, call onButtonReleased
  284. buttonMode.configureButton(configurePushButton);
  285. //buttonMode.onPress(onButtonPressed); // When the button is first pressed, call the function onButtonPressed (further down the page)
  286. //buttonMode.onHold(1000, onButtonHeldNoRepeat); // Once the button has been held for 1 second (1000ms) call onButtonHeld
  287. buttonMode.onHoldRepeat(1000, 1000, onButtonHeld); // Once the button has been held for 1 second (1000ms) call onButtonHeld
  288. buttonMode.onRelease(50, 500, onButtonReleased); // When the button is held >50ms and released after <500ms, call onButtonReleased
  289. pirSensor.configureButton(configurePushButton);
  290. //pirSensor.onPress(onButtonPressed); // When the button is first pressed, call the function onButtonPressed (further down the page)
  291. pirSensor.onHold(500, onButtonHeldNoRepeat); // Once the button has been held for 1 second (1000ms) call onButtonHeld
  292. pirSensor.onRelease(500, onButtonReleased); // When the button is held >50ms and released after <500ms, call onButtonReleased
  293. //set conf default values (bool, int and float variables are set at declaration)
  294. strlcpy(deviceName, DEVICE_NAME, 31);
  295. strlcpy(http_user, DEFAULT_HTTP_USER, 31);
  296. strlcpy(http_pass, DEFAULT_HTTP_PASS, 31);
  297. strlcpy(http_token, HTTP_SET_TOKEN, 31);
  298. strlcpy(mqtt_server, MQTT_SERVER, 41);
  299. strlcpy(mqtt_user, MQTT_USER, 31);
  300. strlcpy(mqtt_pass, MQTT_PASS, 31);
  301. strlcpy(mqtt_topic_in, MQTT_TOPIC_IN, 51);
  302. strlcpy(mqtt_topic_out, MQTT_TOPIC_OUT, 51);
  303. strlcpy(mqtt_willTopic, MQTT_WILLTOPIC, 51);
  304. strlcpy(mqtt_willMsg, MQTT_WILLMSG, 31);
  305. strlcpy(mqtt_connMsg, MQTT_CONNMSG, 31);
  306. strlcpy(domoticz_out_topic, DOMOTICZ_OUT_TOPIC, 51); // changeable subscription topic, as domoticz supports different flat/hierarchical out-topics
  307. //set conf2 default values (bool, int and float variables are set at declaration)
  308. strlcpy(outTemp_topic_in, OUTTEMP_TOPIC_IN, 51);
  309. strlcpy(outHum_topic_in, OUTHUM_TOPIC_IN, 51);
  310. strlcpy(mqtt_topic_pir, MQTT_TOPIC_PIR, 51);
  311. strlcpy(modename0, MODE_NAME_0, 14);
  312. strlcpy(modename1, MODE_NAME_1, 14);
  313. strlcpy(psetname0, PRESET_NAME_0, 14);
  314. strlcpy(psetname1, PRESET_NAME_1, 14);
  315. strlcpy(psetname2, PRESET_NAME_2, 14);
  316. Serial.println("default config values loaded..");
  317. Serial.println("Mounting FS...");
  318. if (!SPIFFS.begin()) {
  319. Serial.println("Failed to mount file system");
  320. return;
  321. }
  322. //uncomment for initial SPIFFS format
  323. //SPIFFS.format();
  324. //Serial.print("Format SPIFFS complete.");
  325. if (!SPIFFS.exists("/formatComplete.txt")) {
  326. Serial.println("Please wait 30 secs for SPIFFS to be formatted");
  327. SPIFFS.format();
  328. Serial.println("Spiffs formatted");
  329. File f = SPIFFS.open("/formatComplete.txt", "w");
  330. if (!f) {
  331. Serial.println("file open failed");
  332. } else {
  333. f.println("Format Complete");
  334. }
  335. f.close();
  336. } else {
  337. Serial.println("SPIFFS is formatted. Moving along...");
  338. }
  339. // // load config from SPIFFS if files exist
  340. if (!loadConfig()) {
  341. Serial.println("Failed to load conf.json");
  342. } else {
  343. Serial.println("conf.json loaded");
  344. }
  345. if (!loadConfig2()) {
  346. Serial.println("Failed to load conf2.json");
  347. } else {
  348. Serial.println("conf2.json loaded");
  349. }
  350. if (!loadSetTemp()) {
  351. Serial.println("Failed to load file 'setTemp'");
  352. } else {
  353. Serial.println("file 'setTemp' loaded");
  354. }
  355. if (!loadHeatingMode()) {
  356. Serial.println("Failed to load file 'heatingMode'");
  357. } else {
  358. Serial.println("file 'heatingMode' loaded");
  359. }
  360. if (!loadPreset()) {
  361. Serial.println("Failed to load file 'preset'");
  362. } else {
  363. Serial.println("file 'preset' loaded");
  364. }
  365. setTempSaved = setTemp;
  366. heatingModeSaved = heatingMode;
  367. presetSaved = preset;
  368. updateCurrentHeatingModeName();
  369. updateCurrentPresetName();
  370. // initialize DHT11/22 temp/hum sensor
  371. dht.begin();
  372. mqttPrepareSubscribeTopics();
  373. checkUseDomoticz();
  374. delay(500);
  375. //optional code handlers to run everytime wifi is connected...
  376. persWM.onConnect([]() {
  377. Serial.println("wifi connected");
  378. Serial.println(WiFi.SSID());
  379. Serial.println(WiFi.localIP());
  380. displayShowWifiConnected();
  381. });
  382. //...or AP mode is started
  383. persWM.onAp([]() {
  384. Serial.println("AP MODE");
  385. Serial.println(persWM.getApSsid());
  386. displayShowWifiConnectionError();
  387. });
  388. //sets network name for AP mode
  389. persWM.setApCredentials(DEVICE_NAME);
  390. //persWM.setApCredentials(DEVICE_NAME, "password"); optional password
  391. //make connecting/disconnecting non-blocking
  392. persWM.setConnectNonBlock(true);
  393. //in non-blocking mode, program will continue past this point without waiting
  394. persWM.begin();
  395. delay(500);
  396. httpServerInit();
  397. mqttPrepareConnection();
  398. mqttClientInit();
  399. initDisplay();
  400. Serial.println("setup complete.");
  401. //delay(1000);
  402. } //void setup
  403. void loop() {
  404. checkMillis();
  405. persWM.handleWiFi(); //in non-blocking mode, handleWiFi must be called in the main loop
  406. yield();
  407. mqttHandleConnection();
  408. yield();
  409. outTempHum_updateOnNewValue();
  410. yield();
  411. dnsServer.processNextRequest();
  412. httpServer.handleClient();
  413. buttonPlus.update();
  414. buttonMinus.update();
  415. buttonMode.update();
  416. pirSensor.update();
  417. yield();
  418. evalCmd();
  419. yield();
  420. if ( domoticzOutParseData ) {
  421. parseDomoticzOut();
  422. yield();
  423. }
  424. if (Serial.available()) {
  425. serialEvent();
  426. yield();
  427. }
  428. if (updateDisplayImmediately) {
  429. updateDisplayImmediately = false;
  430. updateDisplay();
  431. }
  432. } //void loop