wifimgr.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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[34] = "192.168.10.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. //end save
  72. }
  73. }
  74. void StartConfigAP(){
  75. /* stops all tickers, start config portal and waits for new configuration
  76. * if new connection, saves the new configuration values and goes back to loop()
  77. add a check if disconnected ?
  78. */
  79. if (flag_ConfigPortal) { //or disconnected from wifi
  80. flag_ConfigPortal = false; //reset flag
  81. // detach all tickers (redLed, blueLed, OTA, wifimgr, UKI_UDP)
  82. redLedState (1, 500);
  83. blueLedState (1,500);
  84. detachOTA();
  85. tkConfig.detach();
  86. delay (500);
  87. ReadConfig() ; //read config.json from FS
  88. //WiFiManager
  89. // The extra parameters to be configured (can be either global or just in the setup)
  90. // After connecting, parameter.getValue() will get you the configured value
  91. // id/name placeholder/prompt default length
  92. WiFiManagerParameter custom_UKI_NAME("name", "UKI NAME", UKI_NAME, 40);
  93. WiFiManagerParameter custom_UKI_UDP_PORT("port", "UKI udp port", UKI_UDP_PORT, 5);
  94. WiFiManagerParameter custom_UKI_UDP_IP("ip", "UKI udp IP", UKI_UDP_IP, 32);
  95. //Local intialization. Once its business is done, there is no need to keep it around
  96. WiFiManager wifiManager;
  97. //wifiManager.resetSettings();//reset settings - for testing
  98. wifiManager.setSaveConfigCallback(saveConfigCallback);//set config save notify callback
  99. //add all your parameters here
  100. wifiManager.addParameter(&custom_UKI_NAME);
  101. wifiManager.addParameter(&custom_UKI_UDP_PORT);
  102. wifiManager.addParameter(&custom_UKI_UDP_IP);
  103. //it starts an access point with the specified name
  104. //here "AutoConnectAP"
  105. //and goes into a blocking loop awaiting configuration
  106. redLedState (0, 500);
  107. blueLedState (-1, 100);
  108. delay(1000);
  109. if (!wifiManager.startConfigPortal("ESP_UKI_AP")) {
  110. Serial.println("failed to connect, restarting");
  111. //reset and try again
  112. redLedState (-1, 100);
  113. blueLedState (-1, 100);
  114. ESP.reset();
  115. delay(3000);
  116. }
  117. //if you get here you have connected to the WiFi
  118. Serial.println("connected to UKI wifi");
  119. blueLedState(1,500);
  120. //read updated parameters
  121. strcpy(UKI_NAME, custom_UKI_NAME.getValue());
  122. strcpy(UKI_UDP_PORT, custom_UKI_UDP_PORT.getValue());
  123. strcpy(UKI_UDP_IP, custom_UKI_UDP_IP.getValue());
  124. WriteConfig();
  125. // attach() tickers back now that it's working
  126. }
  127. }
  128. void setupWifi() {
  129. pinMode(TRIGGER_PIN, INPUT);
  130. tkConfig.attach(5, CheckTriggerPin); // check TRIGGER_PIN state periodically
  131. WiFi.begin();
  132. // while(WiFi.status()<3) {
  133. // Serial.print
  134. //clean FS, for testing
  135. //SPIFFS.format();
  136. //}
  137. }