AutoConnectWithFSParameters.ino 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #include <FS.h> // this needs to be first, or it all crashes and burns...
  2. #include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
  3. #include <ArduinoJson.h> // https://github.com/bblanchon/ArduinoJson
  4. #ifdef ESP32
  5. #include <SPIFFS.h>
  6. #endif
  7. //define your default values here, if there are different values in config.json, they are overwritten.
  8. char mqtt_server[40];
  9. char mqtt_port[6] = "8080";
  10. char api_token[32] = "YOUR_API_TOKEN";
  11. //default custom static IP
  12. char static_ip[16] = "10.0.1.56";
  13. char static_gw[16] = "10.0.1.1";
  14. char static_sn[16] = "255.255.255.0";
  15. //flag for saving data
  16. bool shouldSaveConfig = false;
  17. //callback notifying us of the need to save config
  18. void saveConfigCallback () {
  19. Serial.println("Should save config");
  20. shouldSaveConfig = true;
  21. }
  22. void setupSpiffs(){
  23. //clean FS, for testing
  24. // SPIFFS.format();
  25. //read configuration from FS json
  26. Serial.println("mounting FS...");
  27. if (SPIFFS.begin()) {
  28. Serial.println("mounted file system");
  29. if (SPIFFS.exists("/config.json")) {
  30. //file exists, reading and loading
  31. Serial.println("reading config file");
  32. File configFile = SPIFFS.open("/config.json", "r");
  33. if (configFile) {
  34. Serial.println("opened config file");
  35. size_t size = configFile.size();
  36. // Allocate a buffer to store contents of the file.
  37. std::unique_ptr<char[]> buf(new char[size]);
  38. configFile.readBytes(buf.get(), size);
  39. DynamicJsonBuffer jsonBuffer;
  40. JsonObject& json = jsonBuffer.parseObject(buf.get());
  41. json.printTo(Serial);
  42. if (json.success()) {
  43. Serial.println("\nparsed json");
  44. strcpy(mqtt_server, json["mqtt_server"]);
  45. strcpy(mqtt_port, json["mqtt_port"]);
  46. strcpy(api_token, json["api_token"]);
  47. // if(json["ip"]) {
  48. // Serial.println("setting custom ip from config");
  49. // strcpy(static_ip, json["ip"]);
  50. // strcpy(static_gw, json["gateway"]);
  51. // strcpy(static_sn, json["subnet"]);
  52. // Serial.println(static_ip);
  53. // } else {
  54. // Serial.println("no custom ip in config");
  55. // }
  56. } else {
  57. Serial.println("failed to load json config");
  58. }
  59. }
  60. }
  61. } else {
  62. Serial.println("failed to mount FS");
  63. }
  64. //end read
  65. }
  66. void setup() {
  67. // put your setup code here, to run once:
  68. Serial.begin(115200);
  69. Serial.println();
  70. setupSpiffs();
  71. // WiFiManager, Local intialization. Once its business is done, there is no need to keep it around
  72. WiFiManager wm;
  73. //set config save notify callback
  74. wm.setSaveConfigCallback(saveConfigCallback);
  75. // setup custom parameters
  76. //
  77. // The extra parameters to be configured (can be either global or just in the setup)
  78. // After connecting, parameter.getValue() will get you the configured value
  79. // id/name placeholder/prompt default length
  80. WiFiManagerParameter custom_mqtt_server("server", "mqtt server", mqtt_server, 40);
  81. WiFiManagerParameter custom_mqtt_port("port", "mqtt port", mqtt_port, 6);
  82. WiFiManagerParameter custom_api_token("api", "api token", "", 32);
  83. //add all your parameters here
  84. wm.addParameter(&custom_mqtt_server);
  85. wm.addParameter(&custom_mqtt_port);
  86. wm.addParameter(&custom_api_token);
  87. // set static ip
  88. // IPAddress _ip,_gw,_sn;
  89. // _ip.fromString(static_ip);
  90. // _gw.fromString(static_gw);
  91. // _sn.fromString(static_sn);
  92. // wm.setSTAStaticIPConfig(_ip, _gw, _sn);
  93. //reset settings - wipe credentials for testing
  94. //wm.resetSettings();
  95. //automatically connect using saved credentials if they exist
  96. //If connection fails it starts an access point with the specified name
  97. //here "AutoConnectAP" if empty will auto generate basedcon chipid, if password is blank it will be anonymous
  98. //and goes into a blocking loop awaiting configuration
  99. if (!wm.autoConnect("AutoConnectAP", "password")) {
  100. Serial.println("failed to connect and hit timeout");
  101. delay(3000);
  102. // if we still have not connected restart and try all over again
  103. ESP.restart();
  104. delay(5000);
  105. }
  106. // always start configportal for a little while
  107. // wm.setConfigPortalTimeout(60);
  108. // wm.startConfigPortal("AutoConnectAP","password");
  109. //if you get here you have connected to the WiFi
  110. Serial.println("connected...yeey :)");
  111. //read updated parameters
  112. strcpy(mqtt_server, custom_mqtt_server.getValue());
  113. strcpy(mqtt_port, custom_mqtt_port.getValue());
  114. strcpy(api_token, custom_api_token.getValue());
  115. //save the custom parameters to FS
  116. if (shouldSaveConfig) {
  117. Serial.println("saving config");
  118. DynamicJsonBuffer jsonBuffer;
  119. JsonObject& json = jsonBuffer.createObject();
  120. json["mqtt_server"] = mqtt_server;
  121. json["mqtt_port"] = mqtt_port;
  122. json["api_token"] = api_token;
  123. // json["ip"] = WiFi.localIP().toString();
  124. // json["gateway"] = WiFi.gatewayIP().toString();
  125. // json["subnet"] = WiFi.subnetMask().toString();
  126. File configFile = SPIFFS.open("/config.json", "w");
  127. if (!configFile) {
  128. Serial.println("failed to open config file for writing");
  129. }
  130. json.prettyPrintTo(Serial);
  131. json.printTo(configFile);
  132. configFile.close();
  133. //end save
  134. shouldSaveConfig = false;
  135. }
  136. Serial.println("local ip");
  137. Serial.println(WiFi.localIP());
  138. Serial.println(WiFi.gatewayIP());
  139. Serial.println(WiFi.subnetMask());
  140. }
  141. void loop() {
  142. // put your main code here, to run repeatedly:
  143. }