config.ino 30 KB

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