config.ino 22 KB

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