config.ino 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  1. bool setConfig(char* param, char* value) {
  2. // sets the corresponding config variable for 'param' to new value
  3. // does not trigger saving to flash
  4. // does not distinguish between config and config2 as this is only split on flash and web-interface
  5. if (debug) {
  6. Serial.print("setConfig - '");
  7. Serial.print(param);
  8. Serial.print("' to '");
  9. Serial.print(value);
  10. Serial.println("'");
  11. }
  12. // values
  13. if ( strcmp(param, "temp") == 0 ) {
  14. float valueFloat = round(atof(value) * 2.0) / 2.0;
  15. #ifdef DEBUG_VERBOSE
  16. Serial.print(valueFloat);
  17. #endif
  18. setTempTo(valueFloat);
  19. }
  20. else if ( strcmp(param, "tempLow") == 0 ) {
  21. float valueFloat = round(atof(value) * 2.0) / 2.0;
  22. #ifdef DEBUG_VERBOSE
  23. Serial.print(valueFloat);
  24. #endif
  25. setTempLowTo(valueFloat);
  26. }
  27. else if ( strcmp(param, "mode") == 0 ) {
  28. setHeatingmodeTo(atoi(value));
  29. }
  30. //confdata
  31. else if ( strcmp(param, "devName") == 0 ) {
  32. strlcpy(deviceName, value, 31);
  33. }
  34. else if ( strcmp(param, "httpUser") == 0 ) {
  35. strlcpy(http_user, value, 31);
  36. }
  37. else if ( strcmp(param, "httpPass") == 0 ) {
  38. strlcpy(http_pass, value, 31);
  39. }
  40. else if ( strcmp(param, "mqttHost") == 0 ) {
  41. strlcpy(mqtt_server, value, 41);
  42. }
  43. else if ( strcmp(param, "mqttPort") == 0 ) {
  44. mqtt_port = atoi(value);
  45. }
  46. else if ( strcmp(param, "mqttUser") == 0 ) {
  47. strlcpy(mqtt_user, value, 31);
  48. }
  49. else if ( strcmp(param, "mqttPass") == 0 ) {
  50. strlcpy(mqtt_pass, value, 31);
  51. }
  52. else if ( strcmp(param, "inTop") == 0 ) {
  53. strlcpy(mqtt_topic_in, value, 51);
  54. }
  55. else if ( strcmp(param, "outTop") == 0 ) {
  56. strlcpy(mqtt_topic_out, value, 51);
  57. }
  58. else if ( strcmp(param, "outRet") == 0 ) {
  59. if (atoi(value) == 1) mqtt_outRetain = true;
  60. else mqtt_outRetain = false;
  61. }
  62. else if ( strcmp(param, "willTop") == 0 ) {
  63. strlcpy(mqtt_willTopic, value, 51);
  64. }
  65. else if ( strcmp(param, "willQos") == 0 ) {
  66. int tmpval = atoi(value);
  67. if (tmpval >= 0 && tmpval <= 2) mqtt_willQos = tmpval;
  68. }
  69. else if ( strcmp(param, "willRet") == 0 ) {
  70. if (atoi(value) == 1) mqtt_willRetain = true;
  71. else mqtt_willRetain = false;
  72. }
  73. else if ( strcmp(param, "willMsg") == 0 ) {
  74. strlcpy(mqtt_willMsg, value, 31);
  75. }
  76. else if ( strcmp(param, "domOutTop") == 0 ) {
  77. strlcpy(domoticz_out_topic, value, 51);
  78. }
  79. //confdata2
  80. else if ( strcmp(param, "domIdxTherm") == 0 ) {
  81. domoticzIdx_Thermostat = atoi(value);
  82. }
  83. else if ( strcmp(param, "domIdxMode") == 0 ) {
  84. domoticzIdx_ThermostatMode = atoi(value);
  85. }
  86. else if ( strcmp(param, "domIdxTempHum") == 0 ) {
  87. domoticzIdx_TempHumSensor = atoi(value);
  88. }
  89. else if ( strcmp(param, "domIdxPIR") == 0 ) {
  90. domoticzIdx_PIR = atoi(value);
  91. }
  92. else if ( strcmp(param, "outTempTop") == 0 ) {
  93. strlcpy(outTemp_topic_in, value, 51);
  94. }
  95. else if ( strcmp(param, "outHumTop") == 0 ) {
  96. strlcpy(outHum_topic_in, value, 51);
  97. }
  98. else if ( strcmp(param, "autoSaveTemp") == 0 ) {
  99. if (atoi(value) == 1) autoSaveSetTemp = true;
  100. else autoSaveSetTemp = false;
  101. }
  102. else if ( strcmp(param, "autoSaveMode") == 0 ) {
  103. if (atoi(value) == 1) autoSaveHeatingMode = true;
  104. else autoSaveHeatingMode = false;
  105. }
  106. else if ( strcmp(param, "minOffTime") == 0 ) {
  107. heatingMinOffTime = atoi(value);
  108. }
  109. else if ( strcmp(param, "tempMin") == 0 ) {
  110. float valueFloat = round(atof(value) * 2.0) / 2.0;
  111. #ifdef DEBUG_VERBOSE
  112. Serial.print(valueFloat);
  113. #endif
  114. if (valueFloat >= 10 && valueFloat <= 16) {
  115. setTempMin = valueFloat;
  116. }
  117. }
  118. else if ( strcmp(param, "tempMax") == 0 ) {
  119. float valueFloat = round(atof(value) * 2.0) / 2.0;
  120. #ifdef DEBUG_VERBOSE
  121. Serial.print(valueFloat);
  122. #endif
  123. if (valueFloat >= 18 && valueFloat <= 30) {
  124. setTempMax = valueFloat;
  125. }
  126. }
  127. else if ( strcmp(param, "hyst") == 0 ) {
  128. float valueFloat = atof(value);
  129. #ifdef DEBUG_VERBOSE
  130. Serial.print(valueFloat);
  131. #endif
  132. if (valueFloat >= 0.1 && valueFloat <= 4.0) {
  133. hysteresis = valueFloat;
  134. }
  135. }
  136. else if ( strcmp(param, "tempCorr") == 0 ) {
  137. float valueFloat = atof(value);
  138. if (valueFloat >= -5.0 && valueFloat <= 5.0) {
  139. tempCorrVal = valueFloat;
  140. }
  141. }
  142. else if ( strcmp(param, "humCorr") == 0 ) {
  143. int valueInt = atoi(value);
  144. if (valueInt >= -40 && valueInt <= 40) {
  145. humCorrVal = valueInt;
  146. }
  147. }
  148. else if ( strcmp(param, "measInt") == 0 ) {
  149. int valueInt = atoi(value);
  150. if (valueInt >= 5 && valueInt <= 120) {
  151. measureInterval = valueInt;
  152. }
  153. }
  154. else if ( strcmp(param, "dispInt") == 0 ) {
  155. int valueInt = atoi(value);
  156. if (valueInt >= 2 && valueInt <= 120) {
  157. displayInterval = valueInt;
  158. }
  159. }
  160. else if ( strcmp(param, "dispTout") == 0 ) {
  161. int valueInt = atoi(value);
  162. if (valueInt >= 2 && valueInt <= 1200) {
  163. displayTimeout = valueInt;
  164. }
  165. }
  166. }
  167. void getConfig(char* param) {
  168. // gets and prints the corresponding config variable for 'param'
  169. if (debug) {
  170. Serial.print("getConfig - '");
  171. Serial.print(param);
  172. Serial.println("'");
  173. }
  174. char buf[101];
  175. // values
  176. if ( strcmp(param, "temp") == 0 ) {
  177. char buf2[11];
  178. dtostrf(setTemp, 2, 1, buf2);
  179. sprintf(buf, "setTemp: '%s'", buf2);
  180. sendStatus(buf);
  181. }
  182. else if ( strcmp(param, "tempLow") == 0 ) {
  183. char buf2[11];
  184. dtostrf(setTempLow, 2, 1, buf2);
  185. sprintf(buf, "setTempLow: '%s'", buf2);
  186. sendStatus(buf);
  187. }
  188. else if ( strcmp(param, "mode") == 0 ) {
  189. sprintf(buf, "heatingMode: '%d'", heatingMode);
  190. sendStatus(buf);
  191. }
  192. //confdata
  193. else if ( strcmp(param, "devName") == 0 ) {
  194. sprintf(buf, "devName: '%s'", deviceName);
  195. sendStatus(buf);
  196. }
  197. else if ( strcmp(param, "httpUser") == 0 ) {
  198. sprintf(buf, "httpUser: '%s'", http_user);
  199. sendStatus(buf);
  200. }
  201. else if ( strcmp(param, "httpPass") == 0 ) {
  202. sprintf(buf, "httpPass: '%s'", http_pass);
  203. sendStatus(buf);
  204. }
  205. else if ( strcmp(param, "mqttHost") == 0 ) {
  206. sprintf(buf, "mqttHost: '%s'", mqtt_server);
  207. sendStatus(buf);
  208. }
  209. else if ( strcmp(param, "mqttPort") == 0 ) {
  210. sprintf(buf, "mqttPort: '%s'", mqtt_port);
  211. sendStatus(buf);
  212. }
  213. else if ( strcmp(param, "mqttUser") == 0 ) {
  214. sprintf(buf, "mqttUser: '%s'", mqtt_user);
  215. sendStatus(buf);
  216. }
  217. else if ( strcmp(param, "mqttPass") == 0 ) {
  218. sprintf(buf, "mqttPass: '%s'", mqtt_pass);
  219. sendStatus(buf);
  220. }
  221. else if ( strcmp(param, "inTop") == 0 ) {
  222. sprintf(buf, "inTop: '%s'", mqtt_topic_in);
  223. sendStatus(buf);
  224. }
  225. else if ( strcmp(param, "outTop") == 0 ) {
  226. sprintf(buf, "outTop: '%s'", mqtt_topic_out);
  227. sendStatus(buf);
  228. }
  229. else if ( strcmp(param, "outRet") == 0 ) {
  230. char buf2[11];
  231. if (mqtt_outRetain) strcpy(buf2, "1");
  232. else strcpy(buf2, "0");
  233. sprintf(buf, "outRet: '%s'", buf2);
  234. sendStatus(buf);
  235. }
  236. else if ( strcmp(param, "willTop") == 0 ) {
  237. sprintf(buf, "willTop: '%s'", mqtt_willTopic);
  238. sendStatus(buf);
  239. }
  240. else if ( strcmp(param, "willQos") == 0 ) {
  241. sprintf(buf, "willQos: '%d'", mqtt_willQos);
  242. sendStatus(buf);
  243. }
  244. else if ( strcmp(param, "willRet") == 0 ) {
  245. char buf2[11];
  246. if (mqtt_willRetain) strcpy(buf2, "1");
  247. else strcpy(buf2, "0");
  248. sprintf(buf, "willRet: '%s'", buf2);
  249. sendStatus(buf);
  250. }
  251. else if ( strcmp(param, "willMsg") == 0 ) {
  252. sprintf(buf, "willMsg: '%s'", mqtt_willMsg);
  253. sendStatus(buf);
  254. }
  255. else if ( strcmp(param, "domOutTop") == 0 ) {
  256. sprintf(buf, "domOutTop: '%s'", domoticz_out_topic);
  257. sendStatus(buf);
  258. }
  259. //confdata2
  260. else if ( strcmp(param, "domIdxTherm") == 0 ) {
  261. sprintf(buf, "domIdxTherm: '%d'", domoticzIdx_Thermostat);
  262. sendStatus(buf);
  263. }
  264. else if ( strcmp(param, "domIdxMode") == 0 ) {
  265. sprintf(buf, "domIdxMode: '%d'", domoticzIdx_ThermostatMode);
  266. sendStatus(buf);
  267. }
  268. else if ( strcmp(param, "domIdxTempHum") == 0 ) {
  269. sprintf(buf, "domIdxTempHum: '%d'", domoticzIdx_TempHumSensor);
  270. sendStatus(buf);
  271. }
  272. else if ( strcmp(param, "domIdxPIR") == 0 ) {
  273. sprintf(buf, "domIdxPIR: '%d'", domoticzIdx_PIR);
  274. sendStatus(buf);
  275. }
  276. else if ( strcmp(param, "outTempTop") == 0 ) {
  277. sprintf(buf, "outTempTop: '%s'", outTemp_topic_in);
  278. sendStatus(buf);
  279. }
  280. else if ( strcmp(param, "outHumTop") == 0 ) {
  281. sprintf(buf, "outHumTop: '%s'", outHum_topic_in);
  282. sendStatus(buf);
  283. }
  284. else if ( strcmp(param, "autoSaveTemp") == 0 ) {
  285. char buf2[11];
  286. if (autoSaveSetTemp) strcpy(buf2, "1");
  287. else strcpy(buf2, "0");
  288. sprintf(buf, "autoSaveTemp: '%s'", buf2);
  289. sendStatus(buf);
  290. }
  291. else if ( strcmp(param, "autoSaveMode") == 0 ) {
  292. char buf2[11];
  293. if (autoSaveHeatingMode) strcpy(buf2, "1");
  294. else strcpy(buf2, "0");
  295. sprintf(buf, "autoSaveMode: '%s'", buf2);
  296. sendStatus(buf);
  297. }
  298. else if ( strcmp(param, "minOffTime") == 0 ) {
  299. sprintf(buf, "minOffTime: '%d'", heatingMinOffTime);
  300. sendStatus(buf);
  301. }
  302. else if ( strcmp(param, "tempMin") == 0 ) {
  303. char buf2[11];
  304. dtostrf(setTempMin, 2, 1, buf2);
  305. sprintf(buf, "tempMin: '%s'", buf2);
  306. sendStatus(buf);
  307. }
  308. else if ( strcmp(param, "tempMax") == 0 ) {
  309. char buf2[11];
  310. dtostrf(setTempMax, 2, 1, buf2);
  311. sprintf(buf, "tempMax: '%s'", buf2);
  312. sendStatus(buf);
  313. }
  314. else if ( strcmp(param, "hyst") == 0 ) {
  315. char buf2[11];
  316. dtostrf(hysteresis, 2, 1, buf2);
  317. sprintf(buf, "hyst: '%s'", buf2);
  318. sendStatus(buf);
  319. }
  320. else if ( strcmp(param, "tempCorr") == 0 ) {
  321. char buf2[11];
  322. dtostrf(tempCorrVal, 2, 1, buf2);
  323. sprintf(buf, "tempCorr: '%s'", buf2);
  324. sendStatus(buf);
  325. }
  326. else if ( strcmp(param, "humCorr") == 0 ) {
  327. sprintf(buf, "humCorr: '%d'", humCorrVal);
  328. sendStatus(buf);
  329. }
  330. else if ( strcmp(param, "measInt") == 0 ) {
  331. sprintf(buf, "measInt: '%d'", measureInterval);
  332. sendStatus(buf);
  333. }
  334. else if ( strcmp(param, "dispInt") == 0 ) {
  335. sprintf(buf, "dispInt: '%d'", displayInterval);
  336. sendStatus(buf);
  337. }
  338. else if ( strcmp(param, "dispTout") == 0 ) {
  339. sprintf(buf, "dispTout: '%d'", displayTimeout);
  340. sendStatus(buf);
  341. }
  342. }
  343. void printConfig() {
  344. // prints current config vars to serial
  345. Serial.println("\nconfdata:");
  346. Serial.print("devName: ");
  347. Serial.println(deviceName);
  348. Serial.print("httpUser: ");
  349. Serial.println(http_user);
  350. Serial.print("httpPass: ");
  351. Serial.println(http_pass);
  352. Serial.print("mqttHost: ");
  353. Serial.println(mqtt_server);
  354. Serial.print("mqttPort: ");
  355. Serial.println(mqtt_port);
  356. Serial.print("mqttUser: ");
  357. Serial.println(mqtt_user);
  358. Serial.print("mqttPass: ");
  359. Serial.println(mqtt_pass);
  360. Serial.print("inTop: ");
  361. Serial.println(mqtt_topic_in);
  362. Serial.print("outTop: ");
  363. Serial.println(mqtt_topic_out);
  364. Serial.print("outRet: ");
  365. Serial.println(mqtt_outRetain);
  366. Serial.print("willTop: ");
  367. Serial.println(mqtt_willTopic);
  368. Serial.print("willQos: ");
  369. Serial.println(mqtt_willQos);
  370. Serial.print("willRet: ");
  371. Serial.println(mqtt_willRetain);
  372. Serial.print("willMsg: ");
  373. Serial.println(mqtt_willMsg);
  374. Serial.print("domOutTop: ");
  375. Serial.println(domoticz_out_topic);
  376. Serial.println();
  377. }
  378. void printConfig2() {
  379. Serial.println("\nconfdata2:");
  380. Serial.print("domIdxTherm: ");
  381. Serial.println(domoticzIdx_Thermostat);
  382. Serial.print("domIdxMode: ");
  383. Serial.println(domoticzIdx_ThermostatMode);
  384. Serial.print("domIdxTempHum: ");
  385. Serial.println(domoticzIdx_TempHumSensor);
  386. Serial.print("domIdxPIR: ");
  387. Serial.println(domoticzIdx_PIR);
  388. Serial.print("outTempTop: ");
  389. Serial.println(outTemp_topic_in);
  390. Serial.print("outHumTop: ");
  391. Serial.println(outHum_topic_in);
  392. Serial.print("autoSaveTemp: ");
  393. Serial.println(autoSaveSetTemp);
  394. Serial.print("autoSaveMode: ");
  395. Serial.println(autoSaveHeatingMode);
  396. Serial.print("minOffTime: ");
  397. Serial.println(heatingMinOffTime);
  398. Serial.print("tempMin: ");
  399. Serial.println(setTempMin);
  400. Serial.print("tempMax: ");
  401. Serial.println(setTempMax);
  402. Serial.print("tempLow: ");
  403. Serial.print(setTempLow);
  404. Serial.print("hyst: ");
  405. Serial.println(hysteresis);
  406. Serial.print("tempCorr: ");
  407. Serial.println(tempCorrVal);
  408. Serial.print("humCorr: ");
  409. Serial.println(humCorrVal);
  410. Serial.print("measInt: ");
  411. Serial.println(measureInterval);
  412. Serial.print("dispInt: ");
  413. Serial.println(displayInterval);
  414. Serial.print("dispTout: ");
  415. Serial.println(displayTimeout);
  416. Serial.println();
  417. }
  418. bool loadConfig() { // loadConfig 1
  419. if (SPIFFS.exists("/conf.json")) {
  420. File configFile = SPIFFS.open("/conf.json", "r");
  421. if (!configFile) {
  422. Serial.println("ERR: Failed to open file /conf.json");
  423. return false;
  424. }
  425. else {
  426. Serial.println("file /conf.json opened");
  427. size_t size = configFile.size();
  428. Serial.print("file size: ");
  429. Serial.println(size);
  430. // Serial.println("file content:");
  431. //
  432. // while (configFile.available()) {
  433. // //Lets read line by line from the file
  434. // String line = configFile.readStringUntil('\n');
  435. // Serial.println(line);
  436. // }
  437. // Serial.println();
  438. if (size > 1000) {
  439. Serial.println("Config file size is too large");
  440. return false;
  441. }
  442. Serial.println("allocate buffer");
  443. // Allocate a buffer to store contents of the file.
  444. std::unique_ptr<char[]> buf(new char[size]);
  445. Serial.println("read file bytes to buffer");
  446. // We don't use String here because ArduinoJson library requires the input
  447. // buffer to be mutable. If you don't use ArduinoJson, you may as well
  448. // use configFile.readString instead.
  449. configFile.readBytes(buf.get(), size);
  450. StaticJsonBuffer<1050> jsonBuffer;
  451. JsonObject& json = jsonBuffer.parseObject(buf.get());
  452. if (!json.success()) {
  453. Serial.println("Failed to parse config file");
  454. return false;
  455. }
  456. strlcpy(deviceName, json["devName"] | "", 31);
  457. strlcpy(http_user, json["httpUser"] | "", 31);
  458. strlcpy(http_pass, json["httpPass"] | "", 31);
  459. strlcpy(mqtt_server, json["mqttHost"] | "", 41);
  460. mqtt_port = atoi(json["mqttPort"] | "");
  461. strlcpy(mqtt_user, json["mqttUser"] | "", 31);
  462. strlcpy(mqtt_pass, json["mqttPass"] | "", 31);
  463. strlcpy(mqtt_topic_in, json["inTop"] | "", 51);
  464. strlcpy(mqtt_topic_out, json["outTop"] | "", 51);
  465. if (atoi(json["outRet"] | "") == 1) mqtt_outRetain = true;
  466. else mqtt_outRetain = false;
  467. strlcpy(mqtt_willTopic, json["willTop"] | "", 51);
  468. mqtt_willQos = atoi(json["willQos"] | "0");
  469. if (atoi(json["willRet"] | "") == 1) mqtt_willRetain = true;
  470. else mqtt_willRetain = false;
  471. strlcpy(mqtt_willMsg, json["willMsg"] | "", 31);
  472. strlcpy(domoticz_out_topic, json["domOutTop"] | "", 51);
  473. Serial.println("Loaded config values:");
  474. printConfig();
  475. return true;
  476. }
  477. configFile.close();
  478. }
  479. else {
  480. Serial.println("file /config.json file does not exist");
  481. return false;
  482. }
  483. } // loadConfig 1
  484. bool loadConfig2() {
  485. if (SPIFFS.exists("/conf2.json")) {
  486. File configFile = SPIFFS.open("/conf2.json", "r");
  487. if (!configFile) {
  488. Serial.println("ERR: Failed to open file /conf2.json");
  489. return false;
  490. }
  491. else {
  492. Serial.println("file /conf2.json opened");
  493. size_t size = configFile.size();
  494. Serial.print("file size: ");
  495. Serial.println(size);
  496. if (size > 1000) {
  497. Serial.println("Config file size is too large");
  498. return false;
  499. }
  500. // Allocate a buffer to store contents of the file.
  501. std::unique_ptr<char[]> buf(new char[size]);
  502. // We don't use String here because ArduinoJson library requires the input
  503. // buffer to be mutable. If you don't use ArduinoJson, you may as well
  504. // use configFile.readString instead.
  505. configFile.readBytes(buf.get(), size);
  506. StaticJsonBuffer<1050> jsonBuffer;
  507. JsonObject& json = jsonBuffer.parseObject(buf.get());
  508. if (!json.success()) {
  509. Serial.println("Failed to parse config file");
  510. return false;
  511. }
  512. domoticzIdx_Thermostat = atoi(json["domIdxTherm"] | "");
  513. domoticzIdx_ThermostatMode = atoi(json["domIdxMode"] | "");
  514. domoticzIdx_TempHumSensor = atoi(json["domIdxTempHum"] | "");
  515. domoticzIdx_PIR = atoi(json["domIdxPIR"] | "");
  516. strlcpy(outTemp_topic_in, json["outTempTop"] | "", 51);
  517. strlcpy(outHum_topic_in, json["outHumTop"] | "", 51);
  518. if (atoi(json["autoSaveTemp"] | "") == 1) autoSaveSetTemp = true;
  519. else autoSaveSetTemp = false;
  520. if (atoi(json["autoSaveMode"] | "") == 1) autoSaveHeatingMode = true;
  521. else autoSaveHeatingMode = false;
  522. heatingMinOffTime = atoi(json["minOffTime"] | "");
  523. setTempMin = atof(json["tempMin"] | "");
  524. setTempMax = atof(json["tempMax"] | "");
  525. setTempLow = atof(json["tempLow"] | "");
  526. hysteresis = atof(json["hyst"] | "");
  527. tempCorrVal = atof(json["tempCorr"] | "");
  528. humCorrVal = atoi(json["humCorr"] | "");
  529. measureInterval = atoi(json["measInt"] | "");
  530. displayInterval = atoi(json["dispInt"] | "");
  531. displayTimeout = atoi(json["dispTout"] | "");
  532. Serial.println("Loaded config values:");
  533. printConfig2();
  534. return true;
  535. }
  536. }
  537. else {
  538. Serial.println("file /conf2.json file does not exist");
  539. return false;
  540. }
  541. } //loadConfig2
  542. bool loadSetTemp() { // loadSetTemp
  543. File configFile = SPIFFS.open("/setTemp", "r");
  544. if (!configFile) {
  545. Serial.println("ERR: Failed to open file /setTemp");
  546. return false;
  547. }
  548. String s = configFile.readStringUntil('\n');
  549. configFile.close();
  550. float tmpSetTemp = s.toFloat();
  551. if ( tmpSetTemp >= setTempMin && tmpSetTemp <= setTempMax ) {
  552. setTemp = tmpSetTemp;
  553. return true;
  554. }
  555. else return false;
  556. } // loadSetTemp
  557. bool loadHeatingMode() { // loadHeatingMode
  558. File configFile = SPIFFS.open("/heatingMode", "r");
  559. if (!configFile) {
  560. Serial.println("ERR: Failed to open file /heatingMode");
  561. return false;
  562. }
  563. String s = configFile.readStringUntil('\n');
  564. configFile.close();
  565. int tmpHeatingMode = s.toInt();
  566. if ( tmpHeatingMode >= 0 && tmpHeatingMode <= 2 ) {
  567. heatingMode = tmpHeatingMode;
  568. return true;
  569. }
  570. else return false;
  571. } // loadHeatingMode
  572. bool saveConfig() { // safeConfig
  573. StaticJsonBuffer<1050> jsonBuffer;
  574. JsonObject& json = jsonBuffer.createObject();
  575. json["devName"] = deviceName;
  576. json["httpUser"] = http_user;
  577. json["httpPass"] = http_pass;
  578. json["mqttHost"] = mqtt_server;
  579. json["mqttPort"] = mqtt_port;
  580. json["mqttUser"] = mqtt_user;
  581. json["mqttPass"] = mqtt_pass;
  582. json["inTop"] = mqtt_topic_in;
  583. json["outTop"] = mqtt_topic_out;
  584. if(mqtt_outRetain) json["outRet"] = 1;
  585. else json["outRet"] = 0;
  586. json["willTop"] = mqtt_willTopic;
  587. json["willQos"] = mqtt_willQos;
  588. if(mqtt_willRetain) json["willRet"] = 1;
  589. else json["willRet"] = 0;
  590. json["willMsg"] = mqtt_willMsg;
  591. json["domOutTop"] = domoticz_out_topic;
  592. yield();
  593. File configFile = SPIFFS.open("/conf.json", "w");
  594. if (!configFile) {
  595. Serial.println("Failed to open conf file for writing");
  596. return false;
  597. }
  598. json.printTo(configFile);
  599. configFile.close();
  600. return true;
  601. } // safeConfig
  602. bool saveConfig2() { // safeConfig2
  603. StaticJsonBuffer<1050> jsonBuffer;
  604. JsonObject& json = jsonBuffer.createObject();
  605. json["domIdxTherm"] = domoticzIdx_Thermostat;
  606. json["domIdxMode"] = domoticzIdx_ThermostatMode;
  607. json["domIdxTempHum"] = domoticzIdx_TempHumSensor;
  608. json["domIdxPIR"] = domoticzIdx_PIR;
  609. json["outTempTop"] = outTemp_topic_in;
  610. json["outHumTop"] = outHum_topic_in;
  611. if(autoSaveSetTemp) json["autoSaveTemp"] = 1;
  612. else json["autoSaveTemp"] = 0;
  613. if(autoSaveHeatingMode) json["autoSaveMode"] = 1;
  614. else json["autoSaveMode"] = 0;
  615. json["minOffTime"] = heatingMinOffTime;
  616. json["tempMin"] = setTempMin;
  617. json["tempMax"] = setTempMax;
  618. json["tempLow"] = setTempLow;
  619. json["hyst"] = hysteresis;
  620. json["tempCorr"] = tempCorrVal;
  621. json["humCorr"] = humCorrVal;
  622. json["measInt"] = measureInterval;
  623. json["dispInt"] = displayInterval;
  624. json["dispTout"] = displayTimeout;
  625. yield();
  626. File configFile = SPIFFS.open("/conf2.json", "w");
  627. if (!configFile) {
  628. Serial.println("Failed to open conf2 file for writing");
  629. return false;
  630. }
  631. json.printTo(configFile);
  632. configFile.close();
  633. return true;
  634. } // safeConfig2
  635. bool saveSetTemp() { // saveSetTemp
  636. File configFile = SPIFFS.open("/setTemp", "w");
  637. if (!configFile) {
  638. Serial.println("Failed to open setTemp file for writing");
  639. return false;
  640. }
  641. configFile.println(setTemp);
  642. configFile.close();
  643. setTempSaved = setTemp;
  644. return true;
  645. } // saveSetTemp
  646. bool saveHeatingMode() { // saveHeatingMode
  647. File configFile = SPIFFS.open("/heatingMode", "w");
  648. if (!configFile) {
  649. Serial.println("Failed to open heatingMode file for writing");
  650. return false;
  651. }
  652. configFile.println(heatingMode);
  653. configFile.close();
  654. heatingModeSaved = heatingMode;
  655. return true;
  656. } // saveHeatingMode
  657. void checkSaveConfigTriggered() {
  658. if (saveConfigToFlash) {
  659. saveConfigToFlash = false;
  660. saveConfig();
  661. }
  662. if (saveConfig2ToFlash) {
  663. saveConfig2ToFlash = false;
  664. saveConfig2();
  665. }
  666. // if (saveSetTempToFlash) {
  667. // saveSetTempToFlash = false;
  668. // saveSetTemp();
  669. // }
  670. // if (saveHeatingModeToFlash) {
  671. // saveHeatingModeToFlash = false;
  672. // saveHeatingMode();
  673. // }
  674. }
  675. void deleteConfig() {
  676. Serial.println("deleting configuration");
  677. if (SPIFFS.remove("/formatComplete.txt")) {
  678. Serial.println("config will be deleted after reboot");
  679. delay(100);
  680. ESP.restart();
  681. }
  682. }