AutoConnectWithFSParameters.ino 4.8 KB

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