config.ino 26 KB

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