AutoConnectWithFSParametersAndCustomIP.ino 5.9 KB

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