config.ino 30 KB

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