wifimgr.h 5.8 KB

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