AutoConnectWithFSParametersAndCustomIP.ino 6.0 KB

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