AutoConnectWithFSParametersAndCustomIP.ino 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP
  68. // put your setup code here, to run once:
  69. Serial.begin(115200);
  70. Serial.println();
  71. setupSpiffs();
  72. // WiFiManager, Local intialization. Once its business is done, there is no need to keep it around
  73. WiFiManager wm;
  74. //set config save notify callback
  75. wm.setSaveConfigCallback(saveConfigCallback);
  76. // setup custom parameters
  77. //
  78. // The extra parameters to be configured (can be either global or just in the setup)
  79. // After connecting, parameter.getValue() will get you the configured value
  80. // id/name placeholder/prompt default length
  81. WiFiManagerParameter custom_mqtt_server("server", "mqtt server", mqtt_server, 40);
  82. WiFiManagerParameter custom_mqtt_port("port", "mqtt port", mqtt_port, 6);
  83. WiFiManagerParameter custom_api_token("api", "api token", "", 32);
  84. //add all your parameters here
  85. wm.addParameter(&custom_mqtt_server);
  86. wm.addParameter(&custom_mqtt_port);
  87. wm.addParameter(&custom_api_token);
  88. // set static ip
  89. IPAddress _ip,_gw,_sn;
  90. _ip.fromString(static_ip);
  91. _gw.fromString(static_gw);
  92. _sn.fromString(static_sn);
  93. wm.setSTAStaticIPConfig(_ip, _gw, _sn);
  94. //reset settings - wipe credentials for testing
  95. //wm.resetSettings();
  96. //automatically connect using saved credentials if they exist
  97. //If connection fails it starts an access point with the specified name
  98. //here "AutoConnectAP" if empty will auto generate basedcon chipid, if password is blank it will be anonymous
  99. //and goes into a blocking loop awaiting configuration
  100. if (!wm.autoConnect("AutoConnectAP", "password")) {
  101. Serial.println("failed to connect and hit timeout");
  102. delay(3000);
  103. // if we still have not connected restart and try all over again
  104. ESP.restart();
  105. delay(5000);
  106. }
  107. //if you get here you have connected to the WiFi
  108. Serial.println("connected...yeey :)");
  109. //read updated parameters
  110. strcpy(mqtt_server, custom_mqtt_server.getValue());
  111. strcpy(mqtt_port, custom_mqtt_port.getValue());
  112. strcpy(api_token, custom_api_token.getValue());
  113. //save the custom parameters to FS
  114. if (shouldSaveConfig) {
  115. Serial.println("saving config");
  116. DynamicJsonBuffer jsonBuffer;
  117. JsonObject& json = jsonBuffer.createObject();
  118. json["mqtt_server"] = mqtt_server;
  119. json["mqtt_port"] = mqtt_port;
  120. json["api_token"] = api_token;
  121. json["ip"] = WiFi.localIP().toString();
  122. json["gateway"] = WiFi.gatewayIP().toString();
  123. json["subnet"] = WiFi.subnetMask().toString();
  124. File configFile = SPIFFS.open("/config.json", "w");
  125. if (!configFile) {
  126. Serial.println("failed to open config file for writing");
  127. }
  128. json.prettyPrintTo(Serial);
  129. json.printTo(configFile);
  130. configFile.close();
  131. //end save
  132. shouldSaveConfig = false;
  133. }
  134. Serial.println("local ip");
  135. Serial.println(WiFi.localIP());
  136. Serial.println(WiFi.gatewayIP());
  137. Serial.println(WiFi.subnetMask());
  138. }
  139. void loop() {
  140. // put your main code here, to run repeatedly:
  141. }