wifimgr.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //flag for saving data
  2. bool shouldSaveConfig = false;
  3. #define TRIGGER_PIN 12
  4. Ticker tkConfig ;
  5. bool StartConfig = false;
  6. //callback notifying us of the need to save config
  7. void saveConfigCallback () {
  8. Serial.println("Should save config");
  9. shouldSaveConfig = true;
  10. }
  11. void ConfigAPMode () {
  12. Serial.println("Config check");
  13. if ( digitalRead(TRIGGER_PIN) == LOW) {
  14. StartConfig = true;
  15. }
  16. }
  17. void StartConfigAP(){
  18. if (StartConfig) {
  19. // detach all tickers (redLed, blueLed, OTA, wifimgr, UKI_UDP)
  20. // redLedState (1, 500);
  21. // blueLedState (1,500);
  22. // detachOTA();
  23. tkConfig.detach();
  24. delay (500);
  25. //WiFiManager
  26. //Local intialization. Once its business is done, there is no need to keep it around
  27. WiFiManager wifiManager;
  28. //reset settings - for testing
  29. //wifiManager.resetSettings();
  30. //sets timeout until configuration portal gets turned off
  31. //useful to make it all retry or go to sleep
  32. //in seconds
  33. //wifiManager.setTimeout(120);
  34. //set config save notify callback
  35. wifiManager.setSaveConfigCallback(saveConfigCallback);
  36. //it starts an access point with the specified name
  37. //here "AutoConnectAP"
  38. //and goes into a blocking loop awaiting configuration
  39. //WITHOUT THIS THE AP DOES NOT SEEM TO WORK PROPERLY WITH SDK 1.5 , update to at least 1.5.1
  40. //WiFi.mode(WIFI_STA);
  41. //redLedState (0, 500);
  42. //blueLedState (-1, 100);
  43. delay(1000);
  44. if (!wifiManager.startConfigPortal("UKI_AP")) {
  45. Serial.println("failed to connect and hit timeout");
  46. delay(3000);
  47. //reset and try again, or maybe put it to deep sleep
  48. ESP.reset();
  49. delay(5000);
  50. }
  51. //if you get here you have connected to the WiFi
  52. Serial.println("connected to UKI wifi");
  53. // blueLedState(1,500);
  54. //save the custom parameters to FS
  55. if (shouldSaveConfig) {
  56. Serial.print("should save config");
  57. }
  58. }
  59. }
  60. void setupWifi() {
  61. pinMode(TRIGGER_PIN, INPUT);
  62. tkConfig.attach(5, ConfigAPMode); // check TRIGGER_PIN state periodically
  63. }