From 1e10ffa49a0b3009eb76fac7c49fc7fb2c67f2aa Mon Sep 17 00:00:00 2001 From: Shawn A Date: Fri, 7 Sep 2018 16:16:44 -0500 Subject: [PATCH] reworkgin examples --- examples/Advanced/Advanced.ino | 44 +++++++++------- .../onDemandNonBlocking.ino | 9 ++-- .../OnDemandConfigPortal.ino | 0 .../OnDemandWebPortal/onDemandWebPortal.ino | 50 +++++++++++++++++++ 4 files changed, 80 insertions(+), 23 deletions(-) rename examples/{ => OnDemand}/OnDemandConfigPortal/OnDemandConfigPortal.ino (100%) create mode 100644 examples/OnDemand/OnDemandWebPortal/onDemandWebPortal.ino diff --git a/examples/Advanced/Advanced.ino b/examples/Advanced/Advanced.ino index 7bc3795..700ab6c 100644 --- a/examples/Advanced/Advanced.ino +++ b/examples/Advanced/Advanced.ino @@ -58,6 +58,7 @@ void setup() { if(!res) { Serial.println("Failed to connect or hit timeout"); + // ESP.restart(); } else { //if you get here you have connected to the WiFi @@ -65,33 +66,38 @@ void setup() { } } -void loop() { +void checkButton(){ // check for button press if ( digitalRead(TRIGGER_PIN) == LOW ) { - // poor mans debounce, not recommended for production code + // poor mans debounce/press-hold, not recommended for production code delay(50); if( digitalRead(TRIGGER_PIN) == LOW ){ - delay(3000); // reset timer - if( digitalRead(TRIGGER_PIN) == LOW ){ - Serial.println("Erasing Config"); // holding button 3000 ms, reset settings - wm.resetSettings(); - ESP.restart(); - } - - Serial.println("Starting config portal"); - wm.setConfigPortalTimeout(120); - - if (!wm.startConfigPortal("OnDemandAP")) { - Serial.println("failed to connect or hit timeout"); - delay(3000); - } else { - //if you get here you have connected to the WiFi - Serial.println("connected...yeey :)"); + delay(3000); // reset delay hold + if( digitalRead(TRIGGER_PIN) == LOW ){ + Serial.println("Erasing Config"); + wm.resetSettings(); + ESP.restart(); + } + + // start portal w delay + Serial.println("Starting config portal"); + wm.setConfigPortalTimeout(120); + + if (!wm.startConfigPortal("OnDemandAP")) { + Serial.println("failed to connect or hit timeout"); + delay(3000); + // ESP.restart(); + } else { + //if you get here you have connected to the WiFi + Serial.println("connected...yeey :)"); + } } } - } +} +void loop() { + checkButton(); // put your main code here, to run repeatedly: } diff --git a/examples/NonBlocking/OnDemandNonBlocking/onDemandNonBlocking.ino b/examples/NonBlocking/OnDemandNonBlocking/onDemandNonBlocking.ino index c250038..9d283ec 100644 --- a/examples/NonBlocking/OnDemandNonBlocking/onDemandNonBlocking.ino +++ b/examples/NonBlocking/OnDemandNonBlocking/onDemandNonBlocking.ino @@ -1,8 +1,8 @@ /** - * OnDemandWebPortal.ino - * example of running the webportal or configportal manually and no blocking + * OnDemandNonBlocking.ino + * example of running the webportal or configportal manually and non blocking * trigger pin will start a webportal for 120 seconds then turn it off. - * startCP = true will start the configportal AP and webportal + * startCP = true will start both the configportal AP and webportal */ #include // https://github.com/tzapu/WiFiManager @@ -46,11 +46,12 @@ void doWiFiManager(){ // is configuration portal requested? if(digitalRead(TRIGGER_PIN) == LOW && (!portalRunning)) { - Serial.println("Button Pressed, Starting Portal"); if(startCP){ + Serial.println("Button Pressed, Starting Config Portal"); wm.startConfigPortal(); } else{ + Serial.println("Button Pressed, Starting Web Portal"); wm.startWebPortal(); } portalRunning = true; diff --git a/examples/OnDemandConfigPortal/OnDemandConfigPortal.ino b/examples/OnDemand/OnDemandConfigPortal/OnDemandConfigPortal.ino similarity index 100% rename from examples/OnDemandConfigPortal/OnDemandConfigPortal.ino rename to examples/OnDemand/OnDemandConfigPortal/OnDemandConfigPortal.ino diff --git a/examples/OnDemand/OnDemandWebPortal/onDemandWebPortal.ino b/examples/OnDemand/OnDemandWebPortal/onDemandWebPortal.ino new file mode 100644 index 0000000..5850e40 --- /dev/null +++ b/examples/OnDemand/OnDemandWebPortal/onDemandWebPortal.ino @@ -0,0 +1,50 @@ +/** + * OnDemandWebPortal.ino + * example of running the webportal (always NON blocking) + */ +#include // https://github.com/tzapu/WiFiManager + +// select which pin will trigger the configuration portal when set to LOW +#define TRIGGER_PIN 0 + +WiFiManager wm; + +bool portalRunning = false; + +void setup() { + // put your setup code here, to run once + Serial.begin(115200); + Serial.println("\n Starting"); + pinMode(TRIGGER_PIN, INPUT_PULLUP); +} + +void loop() { + checkButton(); + // put your main code here, to run repeatedly: +} + +void checkButton(){ + // is auto timeout portal running + if(portalRunning){ + wm.process(); + } + + // is configuration portal requested? + if(digitalRead(TRIGGER_PIN) == LOW) { + delay(50) + if(digitalRead(TRIGGER_PIN) == LOW) { + if(!portalRunning){ + Serial.println("Button Pressed, Starting Portal"); + wm.startWebPortal(); + portalRunning = true; + } + else{ + Serial.println("Button Pressed, Stopping Portal"); + wm.startWebPortal(); + portalRunning = false; + } + } + } +} + +