config.ino 23 KB

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