AutoConnectWithFSParametersAndCustomIP.ino 6.0 KB

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