AutoConnectWithFSParameters.ino 4.7 KB

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