config.ino 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. }
  14. else if ( strcmp(param, "apiToken") == 0 ) {
  15. strlcpy(http_token, value, 31);
  16. }
  17. else if ( strcmp(param, "httpUA") == 0 ) {
  18. strlcpy(http_user, value, 31);
  19. }
  20. else if ( strcmp(param, "httpPA") == 0 ) {
  21. strlcpy(http_pass, value, 31);
  22. }
  23. else if ( strcmp(param, "httpAuth") == 0 ) {
  24. if (atoi(value) == 1) http_user_auth = true;
  25. else http_user_auth = false;
  26. }
  27. else if ( strcmp(param, "httpU1") == 0 ) {
  28. strlcpy(http_user1, value, 31);
  29. }
  30. else if ( strcmp(param, "httpP1") == 0 ) {
  31. strlcpy(http_pass1, value, 31);
  32. }
  33. else if ( strcmp(param, "httpU2") == 0 ) {
  34. strlcpy(http_user2, value, 31);
  35. }
  36. else if ( strcmp(param, "httpP2") == 0 ) {
  37. strlcpy(http_pass2, value, 31);
  38. }
  39. else if ( strcmp(param, "mqttEnable") == 0 ) {
  40. if (atoi(value) == 1) mqtt_enable = true;
  41. else mqtt_enable = false;
  42. }
  43. else if ( strcmp(param, "mqttHost") == 0 ) {
  44. strlcpy(mqtt_server, value, 41);
  45. //mqtt_server[40] = '\0';
  46. }
  47. else if ( strcmp(param, "mqttPort") == 0 ) {
  48. mqtt_port = atoi(value);
  49. }
  50. else if ( strcmp(param, "mqttUser") == 0 ) {
  51. strlcpy(mqtt_user, value, 31);
  52. }
  53. else if ( strcmp(param, "mqttPass") == 0 ) {
  54. strlcpy(mqtt_pass, value, 31);
  55. }
  56. else if ( strcmp(param, "inTop") == 0 ) {
  57. strlcpy(mqtt_topic_in, value, 51);
  58. }
  59. else if ( strcmp(param, "outTop") == 0 ) {
  60. strlcpy(mqtt_topic_out, value, 51);
  61. }
  62. else if ( strcmp(param, "outRet") == 0 ) {
  63. //Serial.print("outRet DEBG: '"); Serial.print(value); Serial.print("' -> ");
  64. if (atoi(value) == 1) mqtt_outRetain = true;
  65. else mqtt_outRetain = false;
  66. //Serial.println(mqtt_outRetain);
  67. }
  68. else if ( strcmp(param, "willTop") == 0 ) {
  69. strlcpy(mqtt_willTopic, value, 51);
  70. }
  71. else if ( strcmp(param, "willQos") == 0 ) {
  72. int tmpval = atoi(value);
  73. if (tmpval >= 0 && tmpval <= 2) mqtt_willQos = tmpval;
  74. }
  75. else if ( strcmp(param, "willRet") == 0 ) {
  76. if (atoi(value) == 1) mqtt_willRetain = true;
  77. else mqtt_willRetain = false;
  78. }
  79. else if ( strcmp(param, "willMsg") == 0 ) {
  80. strlcpy(mqtt_willMsg, value, 31);
  81. }
  82. else if ( strcmp(param, "connMsg") == 0 ) {
  83. strlcpy(mqtt_connMsg, value, 31);
  84. }
  85. else if ( strcmp(param, "hbEnable") == 0 ) {
  86. if (atoi(value) == 1) mqtt_enable_heartbeat = true;
  87. else mqtt_enable_heartbeat = false;
  88. }
  89. else if ( strcmp(param, "hbReconn") == 0 ) {
  90. int tmpval = atoi(value);
  91. if (tmpval == 0) mqtt_heartbeat_maxage_reconnect = 0;
  92. else if (tmpval >= 2 && tmpval <= 10) mqtt_heartbeat_maxage_reconnect = tmpval * 60000;
  93. }
  94. else if ( strcmp(param, "hbReboot") == 0 ) {
  95. int tmpval = atoi(value);
  96. if (tmpval == 0) mqtt_heartbeat_maxage_reboot = 0;
  97. else if (tmpval >= 10 && tmpval <= 120) mqtt_heartbeat_maxage_reboot = tmpval * 60000;
  98. }
  99. }
  100. void printConfigWeb() {
  101. // prints current config vars to serial
  102. Serial.println("\nconfDataWeb:");
  103. Serial.print("devName: ");
  104. Serial.println(deviceName);
  105. Serial.print("apiToken: ");
  106. Serial.println(http_token);
  107. Serial.print("httpUA: ");
  108. Serial.println(http_user);
  109. Serial.print("httpPA: ");
  110. Serial.println(http_pass);
  111. Serial.print("httpAuth: ");
  112. Serial.println(http_user_auth);
  113. Serial.print("httpU1: ");
  114. Serial.println(http_user1);
  115. Serial.print("httpP1: ");
  116. Serial.println(http_pass1);
  117. Serial.print("httpU2: ");
  118. Serial.println(http_user2);
  119. Serial.print("httpP2: ");
  120. Serial.println(http_pass2);
  121. }
  122. void printConfigMqtt() {
  123. // prints current config vars to serial
  124. Serial.println("\nconfDataMqtt:");
  125. Serial.print("mqttEnable: ");
  126. Serial.println(mqtt_enable);
  127. Serial.print("mqttHost: ");
  128. Serial.println(mqtt_server);
  129. Serial.print("mqttPort: ");
  130. Serial.println(mqtt_port);
  131. Serial.print("mqttUser: ");
  132. Serial.println(mqtt_user);
  133. Serial.print("mqttPass: ");
  134. Serial.println(mqtt_pass);
  135. Serial.print("inTop: ");
  136. Serial.println(mqtt_topic_in);
  137. Serial.print("outTop: ");
  138. Serial.println(mqtt_topic_out);
  139. Serial.print("outRet: ");
  140. Serial.println(mqtt_outRetain);
  141. Serial.print("willTop: ");
  142. Serial.println(mqtt_willTopic);
  143. Serial.print("willQos: ");
  144. Serial.println(mqtt_willQos);
  145. Serial.print("willRet: ");
  146. Serial.println(mqtt_willRetain);
  147. Serial.print("willMsg: ");
  148. Serial.println(mqtt_willMsg);
  149. Serial.print("connMsg: ");
  150. Serial.println(mqtt_connMsg);
  151. Serial.print("hbEnable: ");
  152. Serial.println(mqtt_enable_heartbeat);
  153. Serial.print("hbReconn: ");
  154. Serial.println(mqtt_heartbeat_maxage_reconnect);
  155. Serial.print("hbReboot: ");
  156. Serial.println(mqtt_heartbeat_maxage_reboot);
  157. }
  158. bool loadConfigWeb() { // loadConfigWeb
  159. if (SPIFFS.exists("/confWeb.json")) {
  160. File configFile = SPIFFS.open("/confWeb.json", "r");
  161. if (!configFile) {
  162. Serial.println("ERR: Failed to open file /confWeb.json");
  163. return false;
  164. }
  165. else {
  166. Serial.println("file /confWeb.json opened");
  167. size_t size = configFile.size();
  168. Serial.print("file size: ");
  169. Serial.println(size);
  170. // Serial.println("file content:");
  171. //
  172. // while (configFile.available()) {
  173. // //Lets read line by line from the file
  174. // String line = configFile.readStringUntil('\n');
  175. // Serial.println(line);
  176. // }
  177. // Serial.println();
  178. if (size > 1000) {
  179. Serial.println("Config file size is too large");
  180. return false;
  181. }
  182. Serial.println("allocate buffer");
  183. // Allocate a buffer to store contents of the file.
  184. std::unique_ptr<char[]> buf(new char[size]);
  185. Serial.println("read file bytes to buffer");
  186. // We don't use String here because ArduinoJson library requires the input
  187. // buffer to be mutable. If you don't use ArduinoJson, you may as well
  188. // use configFile.readString instead.
  189. configFile.readBytes(buf.get(), size);
  190. StaticJsonBuffer<1050> jsonBuffer;
  191. JsonObject& json = jsonBuffer.parseObject(buf.get());
  192. if (!json.success()) {
  193. Serial.println("Failed to parse config file");
  194. return false;
  195. }
  196. strlcpy(deviceName, json["devName"] | "", 31);
  197. strlcpy(http_token, json["apiToken"] | "", 31);
  198. strlcpy(http_user, json["httpUA"] | "", 31);
  199. strlcpy(http_pass, json["httpPA"] | "", 31);
  200. if (atoi(json["httpAuth"] | "0") == 1) http_user_auth = true;
  201. else http_user_auth = false;
  202. strlcpy(http_user1, json["httpU1"] | "", 31);
  203. strlcpy(http_pass1, json["httpP1"] | "", 31);
  204. strlcpy(http_user2, json["httpU2"] | "", 31);
  205. strlcpy(http_pass2, json["httpP2"] | "", 31);
  206. Serial.println("Loaded config values:");
  207. printConfigWeb();
  208. return true;
  209. }
  210. configFile.close();
  211. }
  212. else {
  213. Serial.println("file /confWeb.json file does not exist");
  214. return false;
  215. }
  216. } // loadConfigWeb
  217. bool loadConfigMqtt() { // loadConfigMqtt
  218. if (SPIFFS.exists("/confMqtt.json")) {
  219. File configFile = SPIFFS.open("/confMqtt.json", "r");
  220. if (!configFile) {
  221. Serial.println("ERR: Failed to open file /confMqtt.json");
  222. return false;
  223. }
  224. else {
  225. Serial.println("file /confMqtt.json opened");
  226. size_t size = configFile.size();
  227. Serial.print("file size: ");
  228. Serial.println(size);
  229. // Serial.println("file content:");
  230. //
  231. // while (configFile.available()) {
  232. // //Lets read line by line from the file
  233. // String line = configFile.readStringUntil('\n');
  234. // Serial.println(line);
  235. // }
  236. // Serial.println();
  237. if (size > 1000) {
  238. Serial.println("Config file size is too large");
  239. return false;
  240. }
  241. Serial.println("allocate buffer");
  242. // Allocate a buffer to store contents of the file.
  243. std::unique_ptr<char[]> buf(new char[size]);
  244. Serial.println("read file bytes to buffer");
  245. // We don't use String here because ArduinoJson library requires the input
  246. // buffer to be mutable. If you don't use ArduinoJson, you may as well
  247. // use configFile.readString instead.
  248. configFile.readBytes(buf.get(), size);
  249. StaticJsonBuffer<1050> jsonBuffer;
  250. JsonObject& json = jsonBuffer.parseObject(buf.get());
  251. if (!json.success()) {
  252. Serial.println("Failed to parse config file");
  253. return false;
  254. }
  255. if (atoi(json["mqttEnable"] | "0") == 1) mqtt_enable = true;
  256. else mqtt_enable = false;
  257. strlcpy(mqtt_server, json["mqttHost"] | "", 41);
  258. mqtt_port = atoi(json["mqttPort"] | "1883");
  259. strlcpy(mqtt_user, json["mqttUser"] | "", 31);
  260. strlcpy(mqtt_pass, json["mqttPass"] | "", 31);
  261. strlcpy(mqtt_topic_in, json["inTop"] | "", 51);
  262. strlcpy(mqtt_topic_out, json["outTop"] | "", 51);
  263. //char outrettmp[8];
  264. //strcpy(outrettmp, json["outRet"]);
  265. //Serial.print("outRet='"); Serial.print(outrettmp); Serial.print("', atoi(outRet)='"); Serial.print(atoi(outrettmp));Serial.println("'");
  266. if (atoi(json["outRet"] | "0") == 1) mqtt_outRetain = true;
  267. else mqtt_outRetain = false;
  268. strlcpy(mqtt_willTopic, json["willTop"] | "", 51);
  269. mqtt_willQos = atoi(json["willQos"] | "0");
  270. if (atoi(json["willRet"] | "0") == 1) mqtt_willRetain = true;
  271. else mqtt_willRetain = false;
  272. strlcpy(mqtt_willMsg, json["willMsg"] | "", 31);
  273. strlcpy(mqtt_connMsg, json["connMsg"] | "", 31);
  274. if (atoi(json["hbEnable"] | "0") == 1) mqtt_enable_heartbeat = true;
  275. else mqtt_enable_heartbeat = false;
  276. unsigned int tmpval;
  277. tmpval = atoi(json["hbReconn"] | "0");
  278. if (tmpval == 0) mqtt_heartbeat_maxage_reconnect = 0;
  279. else if (tmpval >= 2 && tmpval <= 10) mqtt_heartbeat_maxage_reconnect = tmpval * 60000;
  280. tmpval = atoi(json["hbReboot"] | "0");
  281. if (tmpval == 0) mqtt_heartbeat_maxage_reboot = 0;
  282. else if (tmpval >= 2 && tmpval <= 10) mqtt_heartbeat_maxage_reboot = tmpval * 60000;
  283. Serial.println("Loaded config values:");
  284. printConfigMqtt();
  285. return true;
  286. }
  287. configFile.close();
  288. }
  289. else {
  290. Serial.println("file /confMqtt.json file does not exist");
  291. return false;
  292. }
  293. } // loadConfigMqtt
  294. bool saveConfigWeb() { // safeConfig
  295. StaticJsonBuffer<1050> jsonBuffer;
  296. JsonObject& json = jsonBuffer.createObject();
  297. json["devName"] = deviceName;
  298. json["apiToken"] = http_token;
  299. json["httpUA"] = http_user;
  300. json["httpPA"] = http_pass;
  301. if(http_user_auth) json["httpAuth"] = 1;
  302. else json["httpAuth"] = 0;
  303. json["httpU1"] = http_user1;
  304. json["httpP1"] = http_pass1;
  305. json["httpU2"] = http_user2;
  306. json["httpP2"] = http_pass2;
  307. yield();
  308. File configFile = SPIFFS.open("/confWeb.json", "w");
  309. if (!configFile) {
  310. Serial.println("Failed to open file confWeb.json for writing");
  311. return false;
  312. }
  313. json.printTo(configFile);
  314. return true;
  315. } // safeConfig
  316. bool saveConfigMqtt() { // safeConfig
  317. StaticJsonBuffer<1050> jsonBuffer;
  318. JsonObject& json = jsonBuffer.createObject();
  319. if(mqtt_enable) json["mqttEnable"] = 1;
  320. else json["mqttEnable"] = 0;
  321. json["mqttHost"] = mqtt_server;
  322. json["mqttPort"] = mqtt_port;
  323. json["mqttUser"] = mqtt_user;
  324. json["mqttPass"] = mqtt_pass;
  325. json["inTop"] = mqtt_topic_in;
  326. json["outTop"] = mqtt_topic_out;
  327. if(mqtt_outRetain) json["outRet"] = 1;
  328. else json["outRet"] = 0;
  329. json["willTop"] = mqtt_willTopic;
  330. json["willQos"] = mqtt_willQos;
  331. if(mqtt_willRetain) json["willRet"] = 1;
  332. else json["willRet"] = 0;
  333. json["willMsg"] = mqtt_willMsg;
  334. json["connMsg"] = mqtt_connMsg;
  335. if(mqtt_enable_heartbeat) json["hbEnable"] = 1;
  336. else json["hbEnable"] = 0;
  337. json["hbReconn"] = mqtt_heartbeat_maxage_reconnect / 60;
  338. json["hbReboot"] = mqtt_heartbeat_maxage_reboot / 60;
  339. yield();
  340. File configFile = SPIFFS.open("/confMqtt.json", "w");
  341. if (!configFile) {
  342. Serial.println("Failed to open file confMqtt.json for writing");
  343. return false;
  344. }
  345. json.printTo(configFile);
  346. return true;
  347. } // safeConfig
  348. void checkSaveConfigTriggered() {
  349. if (saveConfigWebToFlash) {
  350. saveConfigWebToFlash = false;
  351. saveConfigWeb();
  352. }
  353. if (saveConfigMqttToFlash) {
  354. saveConfigMqttToFlash = false;
  355. saveConfigMqtt();
  356. }
  357. // if (saveConfigHwToFlash) {
  358. // saveConfigHwToFlash = false;
  359. // saveConfigHw();
  360. // }
  361. }
  362. void deleteConfig() {
  363. Serial.println("deleting configuration");
  364. if (SPIFFS.remove("/formatted")) {
  365. Serial.println("config will be deleted after reboot");
  366. delay(100);
  367. ESP.restart();
  368. }
  369. }