wifimgr.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #include <FS.h> //this needs to be first, or it all crashes and burns...
  2. #include <ESP8266WiFi.h>
  3. //web config portal
  4. #include <ESP8266WebServer.h>
  5. #include <DNSServer.h>
  6. #include <WiFiManager.h>
  7. //Configuration saving
  8. #include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson
  9. //ticker flag for saving data
  10. bool flag_SaveConfig = false;
  11. #define TRIGGER_PIN 12 //start onDemand config portal when set to LOW
  12. Ticker tkConfig ;
  13. bool flag_ConfigPortal = false;
  14. int timeout = 30000;
  15. char ESP_NAME[40];
  16. char UDP_PORT[6] = "9000";
  17. char UDP_IP[16] = "192.168.10.100";
  18. //callback notifying the need to save config
  19. void saveConfigCallback () {
  20. Serial.println("Should save config");
  21. flag_SaveConfig = true;
  22. }
  23. // Ticker flag to go to config mode
  24. void CheckTriggerPin () {
  25. //Serial.println("Config check");
  26. if ( digitalRead(TRIGGER_PIN) == LOW) {
  27. flag_ConfigPortal = true;
  28. }
  29. }
  30. void ReadConfig() {
  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(ESP_NAME, json["ESP_NAME"]);
  51. strcpy(UDP_PORT, json["UDP_PORT"]);
  52. strcpy(UDP_IP, json["UDP_IP"]);
  53. }
  54. else {
  55. Serial.println("failed to load json config");
  56. }
  57. }
  58. }
  59. } else {
  60. Serial.println("failed to mount FS");
  61. }
  62. //end read
  63. }
  64. void WriteConfig() {
  65. //save the custom parameters to FS
  66. if (flag_SaveConfig) {
  67. flag_SaveConfig = false ;
  68. Serial.println("saving config");
  69. DynamicJsonBuffer jsonBuffer;
  70. JsonObject& json = jsonBuffer.createObject();
  71. json["ESP_NAME"] = ESP_NAME;
  72. json["UDP_PORT"] = UDP_PORT;
  73. json["UDP_IP"] = UDP_IP;
  74. File configFile = SPIFFS.open("/config.json", "w");
  75. if (!configFile) {
  76. Serial.println("failed to open config file for writing");
  77. }
  78. json.printTo(Serial);
  79. json.printTo(configFile);
  80. configFile.close();
  81. Serial.println();
  82. //end save
  83. }
  84. }
  85. void StartConfigAP(){
  86. /* stops all tickers, start config portal and waits for new configuration
  87. * if new connection, saves the new configuration values and goes back to loop()
  88. add a check if disconnected ?
  89. */
  90. if (flag_ConfigPortal) { //or disconnected from wifi
  91. flag_ConfigPortal = false; //reset flag
  92. // detach all tickers (redLed, blueLed, OTA, wifimgr, UDP)
  93. redLedState (1, 500);
  94. blueLedState (1,500);
  95. detachOTA();
  96. tkConfig.detach();
  97. delay (500);
  98. //ReadConfig() ; //read config.json from FS
  99. //WiFiManager
  100. // The extra parameters to be configured (can be either global or just in the setup)
  101. // After connecting, parameter.getValue() will get you the configured value
  102. // id/name placeholder/prompt default length
  103. WiFiManagerParameter custom_ESP_NAME("name", "ESP NAME", ESP_NAME, 40);
  104. WiFiManagerParameter custom_UDP_PORT("port", " UDP port", UDP_PORT, 5);
  105. WiFiManagerParameter custom_UDP_IP("ip", "UDP IP", UDP_IP, 32);
  106. //Local intialization. Once its business is done, there is no need to keep it around
  107. WiFiManager wifiManager;
  108. //wifiManager.resetSettings();//reset settings - for testing
  109. wifiManager.setSaveConfigCallback(saveConfigCallback);//set config save notify callback
  110. //add all your parameters here
  111. wifiManager.addParameter(&custom_ESP_NAME);
  112. wifiManager.addParameter(&custom_UDP_PORT);
  113. wifiManager.addParameter(&custom_UDP_IP);
  114. //it starts an access point with the specified name
  115. //here "AutoConnectAP"
  116. //and goes into a blocking loop awaiting configuration
  117. delay(1000);
  118. //Modify below to restart config portal
  119. // bool flag_connected =false ;
  120. //while (!flag_connected) {
  121. redLedState (1, 100);
  122. blueLedState (-1, 100);
  123. if (!wifiManager.startConfigPortal(ESP_NAME)) {
  124. Serial.println("failed to connect, restarting config portal");
  125. delay(2000);
  126. //reset and try again
  127. redLedState (-1, 100);
  128. blueLedState (-1, 100);
  129. ESP.reset();
  130. delay(3000);
  131. }
  132. // else {flag_connected = true;}
  133. //}
  134. //if you get here you have connected to the WiFi
  135. Serial.println("connected to wifi");
  136. blueLedState(0,500);
  137. //read updated parameters
  138. strcpy(ESP_NAME, custom_ESP_NAME.getValue());
  139. strcpy(UDP_PORT, custom_UDP_PORT.getValue());
  140. strcpy(UDP_IP, custom_UDP_IP.getValue());
  141. WriteConfig();
  142. Serial.println("Restarting");
  143. delay(500);
  144. ESP.reset();
  145. delay(3000);
  146. }
  147. }
  148. void setupWifi() {
  149. pinMode(TRIGGER_PIN, INPUT);
  150. blueLedState (-1, 300);
  151. ReadConfig();
  152. Serial.print("Connecting");
  153. WiFi.begin();
  154. int start_time = millis();
  155. while(WiFi.status()!=3){
  156. Serial.print(".");
  157. delay(1000);
  158. if (millis()-start_time > timeout) {
  159. Serial.println("timeout, starting config portal");
  160. flag_ConfigPortal = true;
  161. StartConfigAP();
  162. }
  163. }
  164. Serial.println();
  165. Serial.print("connected to "); //add wifi ssid
  166. Serial.println(WiFi.SSID());
  167. blueLedState(1,500);
  168. //ajout attente connection + leds
  169. Serial.print("IP address: ");
  170. Serial.println(WiFi.localIP());// ou vérifier si IP = 0.0.0.0, lancer config portal
  171. tkConfig.attach(5, CheckTriggerPin); // check TRIGGER_PIN state periodically
  172. //delay(10000);
  173. // while(WiFi.status()<3) {
  174. // Serial.print
  175. //clean FS, for testing
  176. //SPIFFS.format();
  177. //}
  178. }