config.ino 11 KB

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