onDemandNonBlocking.ino 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /**
  2. * OnDemandNonBlocking.ino
  3. * example of running the webportal or configportal manually and non blocking
  4. * trigger pin will start a webportal for 120 seconds then turn it off.
  5. * startCP = true will start both the configportal AP and webportal
  6. */
  7. #include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
  8. // select which pin will trigger the configuration portal when set to LOW
  9. #define TRIGGER_PIN 0
  10. WiFiManager wm;
  11. unsigned int timeout = 120; // seconds to run for
  12. unsigned int startTime = millis();
  13. bool portalRunning = false;
  14. bool startAP = false; // start AP and webserver if true, else start only webserver
  15. void setup() {
  16. WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP
  17. // put your setup code here, to run once
  18. Serial.begin(115200);
  19. Serial.println("\n Starting");
  20. pinMode(TRIGGER_PIN, INPUT_PULLUP);
  21. }
  22. void loop() {
  23. doWiFiManager();
  24. // put your main code here, to run repeatedly:
  25. }
  26. void doWiFiManager(){
  27. // is auto timeout portal running
  28. if(portalRunning){
  29. wm.process();
  30. if((millis()-startTime) > (timeout*1000)){
  31. Serial.println("portaltimeout");
  32. portalRunning = false;
  33. if(startAP){
  34. wm.stopConfigPortal();
  35. }
  36. else{
  37. wm.stopWebPortal();
  38. }
  39. }
  40. }
  41. // is configuration portal requested?
  42. if(digitalRead(TRIGGER_PIN) == LOW && (!portalRunning)) {
  43. if(startAP){
  44. Serial.println("Button Pressed, Starting Config Portal");
  45. wm.setConfigPortalBlocking(false);
  46. wm.startConfigPortal();
  47. }
  48. else{
  49. Serial.println("Button Pressed, Starting Web Portal");
  50. wm.startWebPortal();
  51. }
  52. portalRunning = true;
  53. startTime = millis();
  54. }
  55. }