config.ino 24 KB

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