config.ino 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. bool setConfig(char* param, char* value) {
  2. // sets the corresponding config variable for 'param' to new value
  3. // does not trigger saving to flash
  4. // does not distinguish between config and config2 as this is only split on flash and web-interface
  5. Serial.print("setConfig - '");
  6. Serial.print(param);
  7. Serial.print("' to '");
  8. Serial.print(value);
  9. Serial.println("'");
  10. //confdata
  11. if ( strcmp(param, "devName") == 0 ) {
  12. strlcpy(deviceName, value, 31);
  13. // deviceName[30] = '\0';
  14. }
  15. else if ( strcmp(param, "httpUser") == 0 ) {
  16. strlcpy(http_user, value, 31);
  17. }
  18. else if ( strcmp(param, "httpPass") == 0 ) {
  19. strlcpy(http_pass, value, 31);
  20. }
  21. else if ( strcmp(param, "mqttHost") == 0 ) {
  22. strlcpy(mqtt_server, value, 41);
  23. //mqtt_server[40] = '\0';
  24. }
  25. else if ( strcmp(param, "mqttPort") == 0 ) {
  26. mqtt_port = atoi(value);
  27. }
  28. else if ( strcmp(param, "mqttUser") == 0 ) {
  29. strlcpy(mqtt_user, value, 31);
  30. }
  31. else if ( strcmp(param, "mqttPass") == 0 ) {
  32. strlcpy(mqtt_pass, value, 31);
  33. }
  34. else if ( strcmp(param, "inTop") == 0 ) {
  35. strlcpy(mqtt_topic_in, value, 51);
  36. }
  37. else if ( strcmp(param, "outTop") == 0 ) {
  38. strlcpy(mqtt_topic_out, value, 51);
  39. }
  40. else if ( strcmp(param, "outRet") == 0 ) {
  41. if (atoi(value) == 1) mqtt_outRetain = true;
  42. else mqtt_outRetain = false;
  43. }
  44. else if ( strcmp(param, "willTop") == 0 ) {
  45. strlcpy(mqtt_willTopic, value, 51);
  46. }
  47. else if ( strcmp(param, "willQos") == 0 ) {
  48. int tmpval = atoi(value);
  49. if (tmpval >= 0 && tmpval <= 2) mqtt_willQos = tmpval;
  50. }
  51. else if ( strcmp(param, "willRet") == 0 ) {
  52. if (atoi(value) == 1) mqtt_willRetain = true;
  53. else mqtt_willRetain = false;
  54. }
  55. else if ( strcmp(param, "willMsg") == 0 ) {
  56. strlcpy(mqtt_willMsg, value, 31);
  57. }
  58. else if ( strcmp(param, "domOutTop") == 0 ) {
  59. strlcpy(domoticz_out_topic, value, 51);
  60. }
  61. else if ( strcmp(param, "allOnOffTop") == 0 ) {
  62. strlcpy(mqtt_allOnOffTopic, value, 51);
  63. }
  64. else if ( strcmp(param, "sleep") == 0 ) {
  65. if (atoi(value) >= 0 && atoi(value) <= 250) {
  66. sleep = atoi(value);
  67. }
  68. }
  69. //confdata2
  70. else if ( strcmp(param, "btnRet") == 0 ) {
  71. if (atoi(value) == 1) mqtt_btnRetain = true;
  72. else mqtt_btnRetain = false;
  73. }
  74. else if ( strcmp(param, "top1") == 0 ) {
  75. strlcpy(mqtt_topic_out_1, value, 51);
  76. }
  77. else if ( strcmp(param, "top2") == 0 ) {
  78. strlcpy(mqtt_topic_out_2, value, 51);
  79. }
  80. else if ( strcmp(param, "top3") == 0 ) {
  81. strlcpy(mqtt_topic_out_3, value, 51);
  82. }
  83. else if ( strcmp(param, "topHld1") == 0 ) {
  84. strlcpy(mqtt_topic_out_hold_1, value, 51);
  85. }
  86. else if ( strcmp(param, "topHld2") == 0 ) {
  87. strlcpy(mqtt_topic_out_hold_2, value, 51);
  88. }
  89. else if ( strcmp(param, "topHld3") == 0 ) {
  90. strlcpy(mqtt_topic_out_hold_3, value, 51);
  91. }
  92. else if ( strcmp(param, "pld1") == 0 ) {
  93. strlcpy(mqtt_payload_out_1, value, 31);
  94. }
  95. else if ( strcmp(param, "pld2") == 0 ) {
  96. strlcpy(mqtt_payload_out_2, value, 31);
  97. }
  98. else if ( strcmp(param, "pld3") == 0 ) {
  99. strlcpy(mqtt_payload_out_3, value, 31);
  100. }
  101. else if ( strcmp(param, "pldHld1") == 0 ) {
  102. strlcpy(mqtt_payload_out_hold_1, value, 31);
  103. }
  104. else if ( strcmp(param, "pldHld2") == 0 ) {
  105. strlcpy(mqtt_payload_out_hold_2, value, 31);
  106. }
  107. else if ( strcmp(param, "pldHld3") == 0 ) {
  108. strlcpy(mqtt_payload_out_hold_3, value, 31);
  109. }
  110. else if ( strcmp(param, "domIdx1") == 0 ) {
  111. domoticzIdx[0] = atoi(value);
  112. }
  113. else if ( strcmp(param, "domIdx2") == 0 ) {
  114. domoticzIdx[1] = atoi(value);
  115. }
  116. else if ( strcmp(param, "domIdx3") == 0 ) {
  117. domoticzIdx[2] = atoi(value);
  118. }
  119. else if ( strcmp(param, "hld1ToRel") == 0 ) {
  120. hldToRel[0] = atoi(value);
  121. }
  122. else if ( strcmp(param, "hld2ToRel") == 0 ) {
  123. hldToRel[1] = atoi(value);
  124. }
  125. else if ( strcmp(param, "hld3ToRel") == 0 ) {
  126. hldToRel[2] = atoi(value);
  127. }
  128. // confdatahw
  129. else if ( strcmp(param, "debtime") == 0 ) {
  130. debounceTime = atoi(value);
  131. }
  132. else if ( strcmp(param, "hldtime") == 0 ) {
  133. buttonHoldTime = atoi(value);
  134. }
  135. else if ( strcmp(param, "ioRel1") == 0 ) {
  136. int io = atoi(value);
  137. if ( io == 0 || io == 2 || io == 4 || io == 5 || io == 12 || io == 13 || io == 14 ) relais_pins[0] = io;
  138. else Serial.println("ioRel1 - false IO number, not set");
  139. }
  140. else if ( strcmp(param, "ioRel2") == 0 ) {
  141. int io = atoi(value);
  142. if ( io == 0 || io == 2 || io == 4 || io == 5 || io == 12 || io == 13 || io == 14 ) relais_pins[1] = io;
  143. else Serial.println("ioRel2 - false IO number, not set");
  144. }
  145. else if ( strcmp(param, "ioRel3") == 0 ) {
  146. int io = atoi(value);
  147. if ( io == 0 || io == 2 || io == 4 || io == 5 || io == 12 || io == 13 || io == 14 ) relais_pins[2] = io;
  148. else Serial.println("ioRel3 - false IO number, not set");
  149. }
  150. else if ( strcmp(param, "ioBtn1") == 0 ) {
  151. int io = atoi(value);
  152. if ( io == 0 || io == 2 || io == 4 || io == 5 || io == 12 || io == 13 || io == 14 ) buttons_pins[0] = io;
  153. else Serial.println("ioBtn1 - false IO number, not set");
  154. }
  155. else if ( strcmp(param, "ioBtn2") == 0 ) {
  156. int io = atoi(value);
  157. if ( io == 0 || io == 2 || io == 4 || io == 5 || io == 12 || io == 13 || io == 14 ) buttons_pins[1] = io;
  158. else Serial.println("ioBtn2 - false IO number, not set");
  159. }
  160. else if ( strcmp(param, "ioBtn3") == 0 ) {
  161. int io = atoi(value);
  162. if ( io == 0 || io == 2 || io == 4 || io == 5 || io == 12 || io == 13 || io == 14 ) buttons_pins[2] = io;
  163. else Serial.println("ioBtn3 - false IO number, not set");
  164. }
  165. else if ( strcmp(param, "ioLed1") == 0 ) {
  166. int io = atoi(value);
  167. if ( io == 0 || io == 2 || io == 4 || io == 5 || io == 12 || io == 13 || io == 14 ) leds_pins[0] = io;
  168. else Serial.println("ioLed1 - false IO number, not set");
  169. }
  170. else if ( strcmp(param, "ioLed2") == 0 ) {
  171. int io = atoi(value);
  172. if ( io == 0 || io == 2 || io == 4 || io == 5 || io == 12 || io == 13 || io == 14 ) leds_pins[1] = io;
  173. else Serial.println("ioLed2 - false IO number, not set");
  174. }
  175. else if ( strcmp(param, "ioLed3") == 0 ) {
  176. int io = atoi(value);
  177. if ( io == 0 || io == 2 || io == 4 || io == 5 || io == 12 || io == 13 || io == 14 ) leds_pins[2] = io;
  178. else Serial.println("ioLed3 - false IO number, not set");
  179. }
  180. else if ( strcmp(param, "invRel1") == 0 ) {
  181. if (atoi(value) == 1) relais_invert[0] = true;
  182. else relais_invert[0] = false;
  183. }
  184. else if ( strcmp(param, "invRel2") == 0 ) {
  185. if (atoi(value) == 1) relais_invert[1] = true;
  186. else relais_invert[1] = false;
  187. }
  188. else if ( strcmp(param, "invRel3") == 0 ) {
  189. if (atoi(value) == 1) relais_invert[2] = true;
  190. else relais_invert[2] = false;
  191. }
  192. else if ( strcmp(param, "invBtn1") == 0 ) {
  193. if (atoi(value) == 1) button_invert[0] = true;
  194. else button_invert[0] = false;
  195. }
  196. else if ( strcmp(param, "invBtn2") == 0 ) {
  197. if (atoi(value) == 1) button_invert[1] = true;
  198. else button_invert[1] = false;
  199. }
  200. else if ( strcmp(param, "invBtn3") == 0 ) {
  201. if (atoi(value) == 1) button_invert[2] = true;
  202. else button_invert[2] = false;
  203. }
  204. else if ( strcmp(param, "invLed1") == 0 ) {
  205. if (atoi(value) == 1) led_invert[0] = true;
  206. else led_invert[0] = false;
  207. }
  208. else if ( strcmp(param, "invLed2") == 0 ) {
  209. if (atoi(value) == 1) led_invert[1] = true;
  210. else led_invert[1] = false;
  211. }
  212. else if ( strcmp(param, "invLed3") == 0 ) {
  213. if (atoi(value) == 1) led_invert[2] = true;
  214. else led_invert[2] = false;
  215. }
  216. else if ( strcmp(param, "puls1") == 0 ) {
  217. relais_impulse[0] = atoi(value);
  218. }
  219. else if ( strcmp(param, "puls2") == 0 ) {
  220. relais_impulse[1] = atoi(value);
  221. }
  222. else if ( strcmp(param, "puls3") == 0 ) {
  223. relais_impulse[2] = atoi(value);
  224. }
  225. else if ( strcmp(param, "enaRel1") == 0 ) {
  226. if (atoi(value) == 1) relais_enabled[0] = true;
  227. else relais_enabled[0] = false;
  228. }
  229. else if ( strcmp(param, "enaRel2") == 0 ) {
  230. if (atoi(value) == 1) relais_enabled[1] = true;
  231. else relais_enabled[1] = false;
  232. }
  233. else if ( strcmp(param, "enaRel3") == 0 ) {
  234. if (atoi(value) == 1) relais_enabled[2] = true;
  235. else relais_enabled[2] = false;
  236. }
  237. else if ( strcmp(param, "enaBtn1") == 0 ) {
  238. if (atoi(value) == 1) button_enabled[0] = true;
  239. else button_enabled[0] = false;
  240. }
  241. else if ( strcmp(param, "enaBtn2") == 0 ) {
  242. if (atoi(value) == 1) button_enabled[1] = true;
  243. else button_enabled[1] = false;
  244. }
  245. else if ( strcmp(param, "enaBtn3") == 0 ) {
  246. if (atoi(value) == 1) button_enabled[2] = true;
  247. else button_enabled[2] = false;
  248. }
  249. else if ( strcmp(param, "enaLed1") == 0 ) {
  250. if (atoi(value) == 1) led_enabled[0] = true;
  251. else led_enabled[0] = false;
  252. }
  253. else if ( strcmp(param, "enaLed2") == 0 ) {
  254. if (atoi(value) == 1) led_enabled[1] = true;
  255. else led_enabled[1] = false;
  256. }
  257. else if ( strcmp(param, "enaLed3") == 0 ) {
  258. if (atoi(value) == 1) led_enabled[2] = true;
  259. else led_enabled[2] = false;
  260. }
  261. }
  262. void printConfig() {
  263. // prints current config vars to serial
  264. Serial.println("\nconfdata:");
  265. Serial.print("devName: ");
  266. Serial.println(deviceName);
  267. Serial.print("httpUser: ");
  268. Serial.println(http_user);
  269. Serial.print("httpPass: ");
  270. Serial.println(http_pass);
  271. Serial.print("mqttHost: ");
  272. Serial.println(mqtt_server);
  273. Serial.print("mqttPort: ");
  274. Serial.println(mqtt_port);
  275. Serial.print("mqttUser: ");
  276. Serial.println(mqtt_user);
  277. Serial.print("mqttPass: ");
  278. Serial.println(mqtt_pass);
  279. Serial.print("inTop: ");
  280. Serial.println(mqtt_topic_in);
  281. Serial.print("outTop: ");
  282. Serial.println(mqtt_topic_out);
  283. Serial.print("outRet: ");
  284. Serial.println(mqtt_outRetain);
  285. Serial.print("willTop: ");
  286. Serial.println(mqtt_willTopic);
  287. Serial.print("willQos: ");
  288. Serial.println(mqtt_willQos);
  289. Serial.print("willRet: ");
  290. Serial.println(mqtt_willRetain);
  291. Serial.print("willMsg: ");
  292. Serial.println(mqtt_willMsg);
  293. Serial.print("domOutTop: ");
  294. Serial.println(domoticz_out_topic);
  295. Serial.print("allOnOffTop: ");
  296. Serial.println(mqtt_allOnOffTopic);
  297. Serial.print("sleep: ");
  298. Serial.println(sleep);
  299. Serial.println();
  300. }
  301. void printConfig2() {
  302. Serial.println("\nconfdata2:");
  303. Serial.print("btnRet: ");
  304. Serial.println(mqtt_btnRetain);
  305. Serial.print("top1: ");
  306. Serial.println(mqtt_topic_out_1);
  307. Serial.print("top2: ");
  308. Serial.println(mqtt_topic_out_2);
  309. Serial.print("top3: ");
  310. Serial.println(mqtt_topic_out_3);
  311. Serial.print("pld1: ");
  312. Serial.println(mqtt_payload_out_1);
  313. Serial.print("pld2: ");
  314. Serial.println(mqtt_payload_out_2);
  315. Serial.print("pld3: ");
  316. Serial.println(mqtt_payload_out_3);
  317. Serial.print("topHld1: ");
  318. Serial.println(mqtt_topic_out_hold_1);
  319. Serial.print("topHld2: ");
  320. Serial.println(mqtt_topic_out_hold_2);
  321. Serial.print("topHld3: ");
  322. Serial.println(mqtt_topic_out_hold_3);
  323. Serial.print("pldHld1: ");
  324. Serial.println(mqtt_payload_out_hold_1);
  325. Serial.print("pldHld2: ");
  326. Serial.println(mqtt_payload_out_hold_2);
  327. Serial.print("pldHld3: ");
  328. Serial.println(mqtt_payload_out_hold_3);
  329. Serial.print("domIdx1: ");
  330. Serial.println(domoticzIdx[0]);
  331. Serial.print("domIdx2: ");
  332. Serial.println(domoticzIdx[1]);
  333. Serial.print("domIdx3: ");
  334. Serial.println(domoticzIdx[2]);
  335. Serial.print("hld1ToRel: ");
  336. Serial.println(hldToRel[0]);
  337. Serial.print("hld2ToRel: ");
  338. Serial.println(hldToRel[1]);
  339. Serial.print("hld3ToRel: ");
  340. Serial.println(hldToRel[2]);
  341. Serial.println();
  342. }
  343. void printConfigHw() {
  344. Serial.println("\nconfdatahw:");
  345. Serial.print("debtime: ");
  346. Serial.println(debounceTime);
  347. Serial.print("holdtime: ");
  348. Serial.println(buttonHoldTime);
  349. Serial.print("enaRel1: ");
  350. Serial.println(relais_enabled[0]);
  351. Serial.print("enaRel2: ");
  352. Serial.println(relais_enabled[1]);
  353. Serial.print("enaRel3: ");
  354. Serial.println(relais_enabled[2]);
  355. Serial.print("enaBtn1: ");
  356. Serial.println(button_enabled[0]);
  357. Serial.print("enaBtn2: ");
  358. Serial.println(button_enabled[1]);
  359. Serial.print("enaBtn3: ");
  360. Serial.println(button_enabled[2]);
  361. Serial.print("enaLed1: ");
  362. Serial.println(led_enabled[0]);
  363. Serial.print("enaLed2: ");
  364. Serial.println(led_enabled[1]);
  365. Serial.print("enaLed3: ");
  366. Serial.println(led_enabled[2]);
  367. Serial.print("ioRel1: ");
  368. Serial.println(relais_pins[0]);
  369. Serial.print("ioRel2: ");
  370. Serial.println(relais_pins[1]);
  371. Serial.print("ioRel3: ");
  372. Serial.println(relais_pins[2]);
  373. Serial.print("ioBtn1: ");
  374. Serial.println(buttons_pins[0]);
  375. Serial.print("ioBtn2: ");
  376. Serial.println(buttons_pins[1]);
  377. Serial.print("ioBtn3: ");
  378. Serial.println(buttons_pins[2]);
  379. Serial.print("ioLed1: ");
  380. Serial.println(leds_pins[0]);
  381. Serial.print("ioLed2: ");
  382. Serial.println(leds_pins[1]);
  383. Serial.print("ioLed3: ");
  384. Serial.println(leds_pins[2]);
  385. Serial.print("invRel1: ");
  386. Serial.println(relais_invert[0]);
  387. Serial.print("invRel2: ");
  388. Serial.println(relais_invert[1]);
  389. Serial.print("invRel3: ");
  390. Serial.println(relais_invert[2]);
  391. Serial.print("invBtn1: ");
  392. Serial.println(button_invert[0]);
  393. Serial.print("invBtn2: ");
  394. Serial.println(button_invert[1]);
  395. Serial.print("invBtn3: ");
  396. Serial.println(button_invert[2]);
  397. Serial.print("invLed1: ");
  398. Serial.println(led_invert[0]);
  399. Serial.print("invLed2: ");
  400. Serial.println(led_invert[1]);
  401. Serial.print("invLed3: ");
  402. Serial.println(led_invert[2]);
  403. Serial.print("puls1: ");
  404. Serial.println(relais_impulse[0]);
  405. Serial.print("puls2: ");
  406. Serial.println(relais_impulse[1]);
  407. Serial.print("puls3: ");
  408. Serial.println(relais_impulse[2]);
  409. Serial.println();
  410. }
  411. bool loadConfig() { // loadConfig 1
  412. if (SPIFFS.exists("/conf.json")) {
  413. File configFile = SPIFFS.open("/conf.json", "r");
  414. if (!configFile) {
  415. Serial.println("ERR: Failed to open file /conf.json");
  416. return false;
  417. }
  418. else {
  419. Serial.println("file /conf.json opened");
  420. size_t size = configFile.size();
  421. Serial.print("file size: ");
  422. Serial.println(size);
  423. // Serial.println("file content:");
  424. //
  425. // while (configFile.available()) {
  426. // //Lets read line by line from the file
  427. // String line = configFile.readStringUntil('\n');
  428. // Serial.println(line);
  429. // }
  430. // Serial.println();
  431. if (size > 1000) {
  432. Serial.println("Config file size is too large");
  433. return false;
  434. }
  435. Serial.println("allocate buffer");
  436. // Allocate a buffer to store contents of the file.
  437. std::unique_ptr<char[]> buf(new char[size]);
  438. Serial.println("read file bytes to buffer");
  439. // We don't use String here because ArduinoJson library requires the input
  440. // buffer to be mutable. If you don't use ArduinoJson, you may as well
  441. // use configFile.readString instead.
  442. configFile.readBytes(buf.get(), size);
  443. StaticJsonBuffer<1050> jsonBuffer;
  444. JsonObject& json = jsonBuffer.parseObject(buf.get());
  445. if (!json.success()) {
  446. Serial.println("Failed to parse config file");
  447. return false;
  448. }
  449. strlcpy(deviceName, json["devName"] | "", 31);
  450. strlcpy(http_user, json["httpUser"] | "", 31);
  451. strlcpy(http_pass, json["httpPass"] | "", 31);
  452. strlcpy(mqtt_server, json["mqttHost"] | "", 41);
  453. mqtt_port = atoi(json["mqttPort"] | "");
  454. strlcpy(mqtt_user, json["mqttUser"] | "", 31);
  455. strlcpy(mqtt_pass, json["mqttPass"] | "", 31);
  456. strlcpy(mqtt_topic_in, json["inTop"] | "", 51);
  457. strlcpy(mqtt_topic_out, json["outTop"] | "", 51);
  458. if (atoi(json["outRet"] | "") == 1) mqtt_outRetain = true;
  459. else mqtt_outRetain = false;
  460. strlcpy(mqtt_willTopic, json["willTop"] | "", 51);
  461. mqtt_willQos = atoi(json["willQos"] | "0");
  462. if (atoi(json["willRet"] | "") == 1) mqtt_willRetain = true;
  463. else mqtt_willRetain = false;
  464. strlcpy(mqtt_willMsg, json["willMsg"] | "", 31);
  465. strlcpy(domoticz_out_topic, json["domOutTop"] | "", 51);
  466. strlcpy(mqtt_allOnOffTopic, json["allOnOffTop"] | "", 51);
  467. sleep = atoi(json["sleep"] | "");
  468. Serial.println("Loaded config values:");
  469. printConfig();
  470. return true;
  471. }
  472. configFile.close();
  473. }
  474. else {
  475. Serial.println("file /config.json file does not exist");
  476. return false;
  477. }
  478. } // loadConfig 1
  479. bool loadConfig2() {
  480. if (SPIFFS.exists("/conf2.json")) {
  481. File configFile = SPIFFS.open("/conf2.json", "r");
  482. if (!configFile) {
  483. Serial.println("ERR: Failed to open file /conf2.json");
  484. return false;
  485. }
  486. else {
  487. Serial.println("file /conf2.json opened");
  488. size_t size = configFile.size();
  489. Serial.print("file size: ");
  490. Serial.println(size);
  491. if (size > 1000) {
  492. Serial.println("Config file size is too large");
  493. return false;
  494. }
  495. // Allocate a buffer to store contents of the file.
  496. std::unique_ptr<char[]> buf(new char[size]);
  497. // We don't use String here because ArduinoJson library requires the input
  498. // buffer to be mutable. If you don't use ArduinoJson, you may as well
  499. // use configFile.readString instead.
  500. configFile.readBytes(buf.get(), size);
  501. StaticJsonBuffer<1050> jsonBuffer;
  502. JsonObject& json = jsonBuffer.parseObject(buf.get());
  503. if (!json.success()) {
  504. Serial.println("Failed to parse config file");
  505. return false;
  506. }
  507. if (atoi(json["btnRet"] | "") == 1) mqtt_btnRetain = true;
  508. else mqtt_btnRetain = false;
  509. strlcpy(mqtt_topic_out_1, json["top1"] | "", 51);
  510. strlcpy(mqtt_topic_out_2, json["top2"] | "", 51);
  511. strlcpy(mqtt_topic_out_3, json["top3"] | "", 51);
  512. strlcpy(mqtt_topic_out_hold_1, json["topHld1"] | "", 51);
  513. strlcpy(mqtt_topic_out_hold_2, json["topHld2"] | "", 51);
  514. strlcpy(mqtt_topic_out_hold_3, json["topHld3"] | "", 51);
  515. strlcpy(mqtt_payload_out_1, json["pld1"] | "", 31);
  516. strlcpy(mqtt_payload_out_2, json["pld2"] | "", 31);
  517. strlcpy(mqtt_payload_out_3, json["pld3"] | "", 31);
  518. strlcpy(mqtt_payload_out_hold_1, json["pldHld1"] | "", 31);
  519. strlcpy(mqtt_payload_out_hold_2, json["pldHld2"] | "", 31);
  520. strlcpy(mqtt_payload_out_hold_3, json["pldHld3"] | "", 31);
  521. domoticzIdx[0] = atoi(json["domIdx1"] | "");
  522. domoticzIdx[1] = atoi(json["domIdx2"] | "");
  523. domoticzIdx[2] = atoi(json["domIdx3"] | "");
  524. hldToRel[0] = atoi(json["hld1ToRel"] | "");
  525. hldToRel[1] = atoi(json["hld2ToRel"] | "");
  526. hldToRel[2] = atoi(json["hld3ToRel"] | "");
  527. Serial.println("Loaded config values:");
  528. printConfig2();
  529. return true;
  530. }
  531. }
  532. else {
  533. Serial.println("file /conf2.json file does not exist");
  534. return false;
  535. }
  536. } //loadConfig2
  537. bool loadConfigHw() {
  538. if (SPIFFS.exists("/confhw.json")) {
  539. File configFile = SPIFFS.open("/confhw.json", "r");
  540. if (!configFile) {
  541. Serial.println("ERR: Failed to open file /confhw.json");
  542. return false;
  543. }
  544. else {
  545. Serial.println("file /confhw.json opened");
  546. size_t size = configFile.size();
  547. Serial.print("file size: ");
  548. Serial.println(size);
  549. if (size > 1000) {
  550. Serial.println("Config file size is too large");
  551. return false;
  552. }
  553. // Allocate a buffer to store contents of the file.
  554. std::unique_ptr<char[]> buf(new char[size]);
  555. // We don't use String here because ArduinoJson library requires the input
  556. // buffer to be mutable. If you don't use ArduinoJson, you may as well
  557. // use configFile.readString instead.
  558. configFile.readBytes(buf.get(), size);
  559. StaticJsonBuffer<1050> jsonBuffer;
  560. JsonObject& json = jsonBuffer.parseObject(buf.get());
  561. if (!json.success()) {
  562. Serial.println("Failed to parse config file");
  563. return false;
  564. }
  565. debounceTime = atoi(json["debtime"] | "");
  566. buttonHoldTime = atoi(json["hldtime"] | "");
  567. relais_pins[0] = atoi(json["ioRel1"] | "");
  568. relais_pins[1] = atoi(json["ioRel2"] | "");
  569. relais_pins[2] = atoi(json["ioRel3"] | "");
  570. buttons_pins[0] = atoi(json["ioBtn1"] | "");
  571. buttons_pins[1] = atoi(json["ioBtn2"] | "");
  572. buttons_pins[2] = atoi(json["ioBtn3"] | "");
  573. leds_pins[0] = atoi(json["ioLed1"] | "");
  574. leds_pins[1] = atoi(json["ioLed2"] | "");
  575. leds_pins[2] = atoi(json["ioLed3"] | "");
  576. if (atoi(json["enaRel1"] | "") == 1) relais_enabled[0] = true;
  577. else relais_enabled[0] = false;
  578. if (atoi(json["enaRel2"] | "") == 1) relais_enabled[1] = true;
  579. else relais_enabled[1] = false;
  580. if (atoi(json["enaRel3"] | "") == 1) relais_enabled[2] = true;
  581. else relais_enabled[2] = false;
  582. if (atoi(json["enaBtn1"] | "") == 1) button_enabled[0] = true;
  583. else button_enabled[0] = false;
  584. if (atoi(json["enaBtn2"] | "") == 1) button_enabled[1] = true;
  585. else button_enabled[1] = false;
  586. if (atoi(json["enaBtn3"] | "") == 1) button_enabled[2] = true;
  587. else button_enabled[2] = false;
  588. if (atoi(json["enaLed1"] | "") == 1) led_enabled[0] = true;
  589. else led_enabled[0] = false;
  590. if (atoi(json["enaLed2"] | "") == 1) led_enabled[1] = true;
  591. else led_enabled[1] = false;
  592. if (atoi(json["enaLed3"] | "") == 1) led_enabled[2] = true;
  593. else led_enabled[2] = false;
  594. if (atoi(json["invRel1"] | "") == 1) relais_invert[0] = true;
  595. else relais_invert[0] = false;
  596. if (atoi(json["invRel2"] | "") == 1) relais_invert[1] = true;
  597. else relais_invert[1] = false;
  598. if (atoi(json["invRel3"] | "") == 1) relais_invert[2] = true;
  599. else relais_invert[2] = false;
  600. if (atoi(json["invBtn1"] | "") == 1) button_invert[0] = true;
  601. else button_invert[0] = false;
  602. if (atoi(json["invBtn2"] | "") == 1) button_invert[1] = true;
  603. else button_invert[1] = false;
  604. if (atoi(json["invBtn3"] | "") == 1) button_invert[2] = true;
  605. else button_invert[2] = false;
  606. if (atoi(json["invLed1"] | "") == 1) led_invert[0] = true;
  607. else led_invert[0] = false;
  608. if (atoi(json["invLed2"] | "") == 1) led_invert[1] = true;
  609. else led_invert[1] = false;
  610. if (atoi(json["invLed3"] | "") == 1) led_invert[2] = true;
  611. else led_invert[2] = false;
  612. relais_impulse[0] = atoi(json["puls1"] | "");
  613. relais_impulse[1] = atoi(json["puls2"] | "");
  614. relais_impulse[2] = atoi(json["puls3"] | "");
  615. Serial.println("Loaded config values:");
  616. printConfigHw();
  617. return true;
  618. }
  619. }
  620. else {
  621. Serial.println("file /confhw.json file does not exist");
  622. return false;
  623. }
  624. }//loadConfigHw
  625. bool saveConfig() { // safeConfig
  626. StaticJsonBuffer<1050> jsonBuffer;
  627. JsonObject& json = jsonBuffer.createObject();
  628. json["devName"] = deviceName;
  629. json["httpUser"] = http_user;
  630. json["httpPass"] = http_pass;
  631. json["mqttHost"] = mqtt_server;
  632. json["mqttPort"] = mqtt_port;
  633. json["mqttUser"] = mqtt_user;
  634. json["mqttPass"] = mqtt_pass;
  635. json["inTop"] = mqtt_topic_in;
  636. json["outTop"] = mqtt_topic_out;
  637. json["outRet"] = mqtt_outRetain;
  638. json["willTop"] = mqtt_willTopic;
  639. json["willQos"] = mqtt_willQos;
  640. json["willRet"] = mqtt_willRetain;
  641. json["willMsg"] = mqtt_willMsg;
  642. json["domOutTop"] = domoticz_out_topic;
  643. json["allOnOffTop"] = mqtt_allOnOffTopic;
  644. json["sleep"] = sleep;
  645. yield();
  646. File configFile = SPIFFS.open("/conf.json", "w");
  647. if (!configFile) {
  648. Serial.println("Failed to open conf file for writing");
  649. return false;
  650. }
  651. json.printTo(configFile);
  652. return true;
  653. } // safeConfig
  654. bool saveConfig2() { // safeConfig2
  655. StaticJsonBuffer<1050> jsonBuffer;
  656. JsonObject& json = jsonBuffer.createObject();
  657. json["btnRet"] = mqtt_btnRetain;
  658. json["top1"] = mqtt_topic_out_1;
  659. json["top2"] = mqtt_topic_out_2;
  660. json["top3"] = mqtt_topic_out_3;
  661. json["pld1"] = mqtt_payload_out_1;
  662. json["pld2"] = mqtt_payload_out_2;
  663. json["pld3"] = mqtt_payload_out_3;
  664. json["topHld1"] = mqtt_topic_out_hold_1;
  665. json["topHld2"] = mqtt_topic_out_hold_2;
  666. json["topHld3"] = mqtt_topic_out_hold_3;
  667. json["pldHld1"] = mqtt_payload_out_hold_1;
  668. json["pldHld2"] = mqtt_payload_out_hold_2;
  669. json["pldHld3"] = mqtt_payload_out_hold_3;
  670. json["domIdx1"] = domoticzIdx[0];
  671. json["domIdx2"] = domoticzIdx[1];
  672. json["domIdx3"] = domoticzIdx[2];
  673. json["hld1ToRel"] = hldToRel[0];
  674. json["hld2ToRel"] = hldToRel[1];
  675. json["hld3ToRel"] = hldToRel[2];
  676. yield();
  677. File configFile = SPIFFS.open("/conf2.json", "w");
  678. if (!configFile) {
  679. Serial.println("Failed to open conf2 file for writing");
  680. return false;
  681. }
  682. json.printTo(configFile);
  683. return true;
  684. } // safeConfig2
  685. bool saveConfigHw() { // safeConfigHw
  686. StaticJsonBuffer<1050> jsonBuffer;
  687. JsonObject& json = jsonBuffer.createObject();
  688. json["debtime"] = debounceTime;
  689. json["hldtime"] = buttonHoldTime;
  690. json["enaRel1"] = relais_enabled[0];
  691. json["enaRel2"] = relais_enabled[1];
  692. json["enaRel3"] = relais_enabled[2];
  693. json["enaBtn1"] = button_enabled[0];
  694. json["enaBtn2"] = button_enabled[1];
  695. json["enaBtn3"] = button_enabled[2];
  696. json["enaLed1"] = led_enabled[0];
  697. json["enaLed2"] = led_enabled[1];
  698. json["enaLed3"] = led_enabled[2];
  699. json["ioRel1"] = relais_pins[0];
  700. json["ioRel2"] = relais_pins[1];
  701. json["ioRel3"] = relais_pins[2];
  702. json["ioBtn1"] = buttons_pins[0];
  703. json["ioBtn2"] = buttons_pins[1];
  704. json["ioBtn3"] = buttons_pins[2];
  705. json["ioLed1"] = leds_pins[0];
  706. json["ioLed2"] = leds_pins[1];
  707. json["ioLed3"] = leds_pins[2];
  708. json["invRel1"] = relais_invert[0];
  709. json["invRel2"] = relais_invert[1];
  710. json["invRel3"] = relais_invert[2];
  711. json["invBtn1"] = button_invert[0];
  712. json["invBtn2"] = button_invert[1];
  713. json["invBtn3"] = button_invert[2];
  714. json["invLed1"] = led_invert[0];
  715. json["invLed2"] = led_invert[1];
  716. json["invLed3"] = led_invert[2];
  717. json["puls1"] = relais_impulse[0];
  718. json["puls2"] = relais_impulse[1];
  719. json["puls3"] = relais_impulse[2];
  720. yield();
  721. File configFile = SPIFFS.open("/confhw.json", "w");
  722. if (!configFile) {
  723. Serial.println("Failed to open confhw file for writing");
  724. return false;
  725. }
  726. json.printTo(configFile);
  727. return true;
  728. } // safeConfigHw
  729. void checkSaveConfigTriggered() {
  730. if (saveConfigToFlash) {
  731. saveConfigToFlash = false;
  732. saveConfig();
  733. }
  734. if (saveConfig2ToFlash) {
  735. saveConfig2ToFlash = false;
  736. saveConfig2();
  737. }
  738. if (saveConfigHwToFlash) {
  739. saveConfigHwToFlash = false;
  740. saveConfigHw();
  741. }
  742. }
  743. void deleteConfig() {
  744. Serial.println("deleting configuration");
  745. if (SPIFFS.remove("/formatComplete.txt")) {
  746. Serial.println("config will be deleted after reboot");
  747. delay(100);
  748. ESP.restart();
  749. }
  750. }