config.ino 26 KB

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