wifimgr.h 5.6 KB

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