AutoConnectWithFSParameters.ino 4.8 KB

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