AutoConnectWithFSParametersAndCustomIP.ino 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #include <FS.h> //this needs to be first, or it all crashes and burns...
  2. #if defined(ESP8266)
  3. #include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
  4. #else
  5. #include <WiFi.h>
  6. #endif
  7. //needed for library
  8. #include <ESPAsyncDNSServer.h> //https://github.com/devyte/ESPAsyncDNSServer
  9. //https://github.com/me-no-dev/ESPAsyncUDP
  10. #include <ESPAsyncWebServer.h>
  11. #include <ESPAsyncWiFiManager.h> //https://github.com/tzapu/WiFiManager
  12. #include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson
  13. //define your default values here, if there are different values in config.json, they are overwritten.
  14. //length should be max size + 1
  15. char mqtt_server[40];
  16. char mqtt_port[6] = "8080";
  17. char blynk_token[33] = "YOUR_BLYNK_TOKEN";
  18. //default custom static IP
  19. char static_ip[16] = "10.0.1.56";
  20. char static_gw[16] = "10.0.1.1";
  21. char static_sn[16] = "255.255.255.0";
  22. //flag for saving data
  23. bool shouldSaveConfig = false;
  24. //callback notifying us of the need to save config
  25. void saveConfigCallback () {
  26. Serial.println("Should save config");
  27. shouldSaveConfig = true;
  28. }
  29. AsyncWebServer server(80);
  30. AsyncDNSServer dns;
  31. void setup() {
  32. // put your setup code here, to run once:
  33. Serial.begin(115200);
  34. Serial.println();
  35. //clean FS, for testing
  36. //SPIFFS.format();
  37. //read configuration from FS json
  38. Serial.println("mounting FS...");
  39. if (SPIFFS.begin()) {
  40. Serial.println("mounted file system");
  41. if (SPIFFS.exists("/config.json")) {
  42. //file exists, reading and loading
  43. Serial.println("reading config file");
  44. File configFile = SPIFFS.open("/config.json", "r");
  45. if (configFile) {
  46. Serial.println("opened config file");
  47. size_t size = configFile.size();
  48. // Allocate a buffer to store contents of the file.
  49. std::unique_ptr<char[]> buf(new char[size]);
  50. configFile.readBytes(buf.get(), size);
  51. DynamicJsonBuffer jsonBuffer;
  52. JsonObject& json = jsonBuffer.parseObject(buf.get());
  53. json.printTo(Serial);
  54. if (json.success()) {
  55. Serial.println("\nparsed json");
  56. strcpy(mqtt_server, json["mqtt_server"]);
  57. strcpy(mqtt_port, json["mqtt_port"]);
  58. strcpy(blynk_token, json["blynk_token"]);
  59. if(json["ip"]) {
  60. Serial.println("setting custom ip from config");
  61. //static_ip = json["ip"];
  62. strcpy(static_ip, json["ip"]);
  63. strcpy(static_gw, json["gateway"]);
  64. strcpy(static_sn, json["subnet"]);
  65. //strcat(static_ip, json["ip"]);
  66. //static_gw = json["gateway"];
  67. //static_sn = json["subnet"];
  68. Serial.println(static_ip);
  69. /* Serial.println("converting ip");
  70. IPAddress ip = ipFromCharArray(static_ip);
  71. Serial.println(ip);*/
  72. } else {
  73. Serial.println("no custom ip in config");
  74. }
  75. } else {
  76. Serial.println("failed to load json config");
  77. }
  78. }
  79. }
  80. } else {
  81. Serial.println("failed to mount FS");
  82. }
  83. //end read
  84. Serial.println(static_ip);
  85. Serial.println(blynk_token);
  86. Serial.println(mqtt_server);
  87. // The extra parameters to be configured (can be either global or just in the setup)
  88. // After connecting, parameter.getValue() will get you the configured value
  89. // id/name placeholder/prompt default length
  90. AsyncWiFiManagerParameter custom_mqtt_server("server", "mqtt server", mqtt_server, 40);
  91. AsyncWiFiManagerParameter custom_mqtt_port("port", "mqtt port", mqtt_port, 5);
  92. AsyncWiFiManagerParameter custom_blynk_token("blynk", "blynk token", blynk_token, 34);
  93. //WiFiManager
  94. //Local intialization. Once its business is done, there is no need to keep it around
  95. AsyncWiFiManager wifiManager(&server,&dns);
  96. //set config save notify callback
  97. wifiManager.setSaveConfigCallback(saveConfigCallback);
  98. //set static ip
  99. IPAddress _ip,_gw,_sn;
  100. _ip.fromString(static_ip);
  101. _gw.fromString(static_gw);
  102. _sn.fromString(static_sn);
  103. wifiManager.setSTAStaticIPConfig(_ip, _gw, _sn);
  104. //add all your parameters here
  105. wifiManager.addParameter(&custom_mqtt_server);
  106. wifiManager.addParameter(&custom_mqtt_port);
  107. wifiManager.addParameter(&custom_blynk_token);
  108. //reset settings - for testing
  109. //wifiManager.resetSettings();
  110. //set minimu quality of signal so it ignores AP's under that quality
  111. //defaults to 8%
  112. wifiManager.setMinimumSignalQuality();
  113. //sets timeout until configuration portal gets turned off
  114. //useful to make it all retry or go to sleep
  115. //in seconds
  116. //wifiManager.setTimeout(120);
  117. //fetches ssid and pass and tries to connect
  118. //if it does not connect it starts an access point with the specified name
  119. //here "AutoConnectAP"
  120. //and goes into a blocking loop awaiting configuration
  121. if (!wifiManager.autoConnect("AutoConnectAP", "password")) {
  122. Serial.println("failed to connect and hit timeout");
  123. delay(3000);
  124. //reset and try again, or maybe put it to deep sleep
  125. ESP.reset();
  126. delay(5000);
  127. }
  128. //if you get here you have connected to the WiFi
  129. Serial.println("connected...yeey :)");
  130. //read updated parameters
  131. strcpy(mqtt_server, custom_mqtt_server.getValue());
  132. strcpy(mqtt_port, custom_mqtt_port.getValue());
  133. strcpy(blynk_token, custom_blynk_token.getValue());
  134. //save the custom parameters to FS
  135. if (shouldSaveConfig) {
  136. Serial.println("saving config");
  137. DynamicJsonBuffer jsonBuffer;
  138. JsonObject& json = jsonBuffer.createObject();
  139. json["mqtt_server"] = mqtt_server;
  140. json["mqtt_port"] = mqtt_port;
  141. json["blynk_token"] = blynk_token;
  142. json["ip"] = WiFi.localIP().toString();
  143. json["gateway"] = WiFi.gatewayIP().toString();
  144. json["subnet"] = WiFi.subnetMask().toString();
  145. File configFile = SPIFFS.open("/config.json", "w");
  146. if (!configFile) {
  147. Serial.println("failed to open config file for writing");
  148. }
  149. json.prettyPrintTo(Serial);
  150. json.printTo(configFile);
  151. configFile.close();
  152. //end save
  153. }
  154. Serial.println("local ip");
  155. Serial.println(WiFi.localIP());
  156. Serial.println(WiFi.gatewayIP());
  157. Serial.println(WiFi.subnetMask());
  158. }
  159. void loop() {
  160. // put your main code here, to run repeatedly:
  161. }