AutoConnectWithFSParameters.ino 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. char mqtt_server[40];
  15. char mqtt_port[6] = "8080";
  16. char blynk_token[34] = "YOUR_BLYNK_TOKEN";
  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. AsyncDNSServer 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. } else {
  55. Serial.println("failed to load json config");
  56. }
  57. }
  58. }
  59. } else {
  60. Serial.println("failed to mount FS");
  61. }
  62. //end read
  63. // The extra parameters to be configured (can be either global or just in the setup)
  64. // After connecting, parameter.getValue() will get you the configured value
  65. // id/name placeholder/prompt default length
  66. AsyncWiFiManagerParameter custom_mqtt_server("server", "mqtt server", mqtt_server, 40);
  67. AsyncWiFiManagerParameter custom_mqtt_port("port", "mqtt port", mqtt_port, 5);
  68. AsyncWiFiManagerParameter custom_blynk_token("blynk", "blynk token", blynk_token, 32);
  69. //WiFiManager
  70. //Local intialization. Once its business is done, there is no need to keep it around
  71. AsyncWiFiManager wifiManager(&server,&dns);
  72. //set config save notify callback
  73. wifiManager.setSaveConfigCallback(saveConfigCallback);
  74. //set static ip
  75. wifiManager.setSTAStaticIPConfig(IPAddress(10,0,1,99), IPAddress(10,0,1,1), IPAddress(255,255,255,0));
  76. //add all your parameters here
  77. wifiManager.addParameter(&custom_mqtt_server);
  78. wifiManager.addParameter(&custom_mqtt_port);
  79. wifiManager.addParameter(&custom_blynk_token);
  80. //reset settings - for testing
  81. //wifiManager.resetSettings();
  82. //set minimu quality of signal so it ignores AP's under that quality
  83. //defaults to 8%
  84. //wifiManager.setMinimumSignalQuality();
  85. //sets timeout until configuration portal gets turned off
  86. //useful to make it all retry or go to sleep
  87. //in seconds
  88. //wifiManager.setTimeout(120);
  89. //fetches ssid and pass and tries to connect
  90. //if it does not connect it starts an access point with the specified name
  91. //here "AutoConnectAP"
  92. //and goes into a blocking loop awaiting configuration
  93. if (!wifiManager.autoConnect("AutoConnectAP", "password")) {
  94. Serial.println("failed to connect and hit timeout");
  95. delay(3000);
  96. //reset and try again, or maybe put it to deep sleep
  97. ESP.reset();
  98. delay(5000);
  99. }
  100. //if you get here you have connected to the WiFi
  101. Serial.println("connected...yeey :)");
  102. //read updated parameters
  103. strcpy(mqtt_server, custom_mqtt_server.getValue());
  104. strcpy(mqtt_port, custom_mqtt_port.getValue());
  105. strcpy(blynk_token, custom_blynk_token.getValue());
  106. //save the custom parameters to FS
  107. if (shouldSaveConfig) {
  108. Serial.println("saving config");
  109. DynamicJsonBuffer jsonBuffer;
  110. JsonObject& json = jsonBuffer.createObject();
  111. json["mqtt_server"] = mqtt_server;
  112. json["mqtt_port"] = mqtt_port;
  113. json["blynk_token"] = blynk_token;
  114. File configFile = SPIFFS.open("/config.json", "w");
  115. if (!configFile) {
  116. Serial.println("failed to open config file for writing");
  117. }
  118. json.printTo(Serial);
  119. json.printTo(configFile);
  120. configFile.close();
  121. //end save
  122. }
  123. Serial.println("local ip");
  124. Serial.println(WiFi.localIP());
  125. }
  126. void loop() {
  127. // put your main code here, to run repeatedly:
  128. }