AutoConnectWithFSParametersAndCustomIP.ino 6.0 KB

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