AutoConnectWithFSParameters.ino 4.8 KB

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