wifimgr.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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[10] = "12345";
  17. char UDP_IP[16] = "239.0.0.57";
  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. // UNCOMMENT TO FORMAT CONFIG FILE, UPLOAD, EXECUTE ONCE AND REUPLOAD WITH THIS COMMENTEND
  35. // Serial.println("Please wait 30 secs for SPIFFS to be formatted");
  36. // SPIFFS.format();
  37. // Serial.println("Spiffs formatted");
  38. Serial.println("mounted file system");
  39. if (SPIFFS.exists("/config.json")) {
  40. //file exists, reading and loading
  41. Serial.println("reading config file");
  42. File configFile = SPIFFS.open("/config.json", "r");
  43. if (configFile) {
  44. Serial.println("opened config file");
  45. size_t size = configFile.size();
  46. // Allocate a buffer to store contents of the file.
  47. std::unique_ptr<char[]> buf(new char[size]);
  48. configFile.readBytes(buf.get(), size);
  49. DynamicJsonBuffer jsonBuffer;
  50. JsonObject& json = jsonBuffer.parseObject(buf.get());
  51. json.printTo(Serial);
  52. if (json.success()) {
  53. Serial.println("\nparsed json");
  54. strcpy(ESP_NAME, json["ESP_NAME"]);
  55. strcpy(UDP_PORT, json["UDP_PORT"]);
  56. strcpy(UDP_IP, json["UDP_IP"]);
  57. }
  58. else {
  59. Serial.println("failed to load json config");
  60. }
  61. }
  62. }
  63. } else {
  64. Serial.println("failed to mount FS");
  65. }
  66. //end read
  67. }
  68. void WriteConfig() {
  69. //save the custom parameters to FS
  70. if (flag_SaveConfig) {
  71. flag_SaveConfig = false ;
  72. Serial.println("saving config");
  73. DynamicJsonBuffer jsonBuffer;
  74. JsonObject& json = jsonBuffer.createObject();
  75. json["ESP_NAME"] = ESP_NAME;
  76. json["UDP_PORT"] = UDP_PORT;
  77. json["UDP_IP"] = UDP_IP;
  78. File configFile = SPIFFS.open("/config.json", "w");
  79. if (!configFile) {
  80. Serial.println("failed to open config file for writing");
  81. }
  82. json.printTo(Serial);
  83. json.printTo(configFile);
  84. configFile.close();
  85. Serial.println();
  86. //end save
  87. }
  88. }
  89. void StartConfigAP(){
  90. /* stops all tickers, start config portal and waits for new configuration
  91. * if new connection, saves the new configuration values and goes back to loop()
  92. add a check if disconnected ?
  93. */
  94. if (flag_ConfigPortal) { //or disconnected from wifi
  95. flag_ConfigPortal = false; //reset flag
  96. // detach all tickers (redLed, blueLed, OTA, wifimgr, UDP)
  97. redLedState (1, 500);
  98. blueLedState (1,500);
  99. //detachOTA();
  100. tkConfig.detach();
  101. delay (500);
  102. //ReadConfig() ; //read config.json from FS
  103. //WiFiManager
  104. // The extra parameters to be configured (can be either global or just in the setup)
  105. // After connecting, parameter.getValue() will get you the configured value
  106. // id/name placeholder/prompt default length
  107. WiFiManagerParameter custom_ESP_NAME("name", "ESP NAME", ESP_NAME, 40);
  108. WiFiManagerParameter custom_UDP_PORT("port", " UDP port", UDP_PORT, 10);
  109. WiFiManagerParameter custom_UDP_IP("ip", "UDP IP", UDP_IP, 32);
  110. //Local intialization. Once its business is done, there is no need to keep it around
  111. WiFiManager wifiManager;
  112. //wifiManager.resetSettings();//reset settings - for testing
  113. wifiManager.setSaveConfigCallback(saveConfigCallback);//set config save notify callback
  114. //add all your parameters here
  115. wifiManager.addParameter(&custom_ESP_NAME);
  116. wifiManager.addParameter(&custom_UDP_PORT);
  117. wifiManager.addParameter(&custom_UDP_IP);
  118. //it starts an access point with the specified name
  119. //here "AutoConnectAP"
  120. //and goes into a blocking loop awaiting configuration
  121. delay(1000);
  122. //Modify below to restart config portal
  123. // bool flag_connected =false ;
  124. //while (!flag_connected) {
  125. redLedState (1, 100);
  126. blueLedState (-1, 100);
  127. if (!wifiManager.startConfigPortal(ESP_NAME)) {
  128. Serial.println("failed to connect, restarting config portal");
  129. delay(2000);
  130. //reset and try again
  131. redLedState (-1, 100);
  132. blueLedState (-1, 100);
  133. ESP.reset();
  134. delay(3000);
  135. }
  136. // else {flag_connected = true;}
  137. //}
  138. //if you get here you have connected to the WiFi
  139. Serial.println("connected to wifi");
  140. blueLedState(0,500);
  141. //read updated parameters
  142. strcpy(ESP_NAME, custom_ESP_NAME.getValue());
  143. strcpy(UDP_PORT, custom_UDP_PORT.getValue());
  144. strcpy(UDP_IP, custom_UDP_IP.getValue());
  145. WriteConfig();
  146. Serial.println("Restarting");
  147. delay(500);
  148. ESP.reset();
  149. delay(3000);
  150. }
  151. }
  152. void setupWifi() {
  153. pinMode(TRIGGER_PIN, INPUT_PULLUP);
  154. blueLedState (-1, 300);
  155. ReadConfig();
  156. Serial.print("Connecting");
  157. WiFi.mode(WIFI_STA); // force remove AP
  158. WiFi.begin("sniperAP","razorshark");
  159. //WiFi.begin();
  160. int start_time = millis();
  161. while(WiFi.status()!=3){
  162. Serial.print(".");
  163. delay(1000);
  164. if (millis()-start_time > timeout) {
  165. Serial.println("timeout, starting config portal");
  166. flag_ConfigPortal = true;
  167. StartConfigAP();
  168. }
  169. }
  170. Serial.println();
  171. Serial.print("connected to "); //add wifi ssid
  172. Serial.println(WiFi.SSID());
  173. blueLedState(1,500);
  174. //ajout attente connection + leds
  175. Serial.print("IP address: ");
  176. Serial.println(WiFi.localIP());// ou vérifier si IP = 0.0.0.0, lancer config portal
  177. tkConfig.attach(5, CheckTriggerPin); // check TRIGGER_PIN state periodically
  178. //delay(10000);
  179. // while(WiFi.status()<3) {
  180. // Serial.print
  181. //clean FS, for testing
  182. //SPIFFS.format();
  183. //}
  184. }