config.ino 29 KB

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