non blocking examples

This commit is contained in:
Shawn A
2018-09-07 15:43:43 -05:00
parent c52186079a
commit ed80635e32
6 changed files with 65 additions and 47 deletions
@@ -0,0 +1,26 @@
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
WiFiManager wm;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
//reset settings - wipe credentials for testing
//wm.resetSettings();
wm.setConfigPortalBlocking(false);
//automatically connect using saved credentials if they exist
//If connection fails it starts an access point with the specified name
if(wm.autoConnect("AutoConnectAP")){
Serial.println("connected...yeey :)");
}
else {
Serial.println("Configportal running");
}
}
void loop() {
wm.process();
// put your main code here, to run repeatedly:
}
@@ -0,0 +1,61 @@
/**
* OnDemandWebPortal.ino
* example of running the webportal or configportal manually and no blocking
* trigger pin will start a webportal for 120 seconds then turn it off.
* startCP = true will start the configportal AP and webportal
*/
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
// select which pin will trigger the configuration portal when set to LOW
#define TRIGGER_PIN 0
WiFiManager wm;
unsigned int timeout = 120; // seconds to run for
unsigned int startTime = millis();
bool portalRunning = false;
bool startCP = false; // start AP and webserver if true, else start only webserver
void setup() {
// put your setup code here, to run once
Serial.begin(115200);
Serial.println("\n Starting");
pinMode(TRIGGER_PIN, INPUT_PULLUP);
}
void loop() {
doWiFiManager();
// put your main code here, to run repeatedly:
}
void doWiFiManager(){
// is auto timeout portal running
if(portalRunning){
wm.process();
if((millis()-startTime) > (timeout*1000)){
Serial.println("portaltimeout");
portalRunning = false;
if(startCP){
wm.stopConfigPortal();
}
else{
wm.stopWebPortal();
}
}
}
// is configuration portal requested?
if(digitalRead(TRIGGER_PIN) == LOW && (!portalRunning)) {
Serial.println("Button Pressed, Starting Portal");
if(startCP){
wm.startConfigPortal();
}
else{
wm.startWebPortal();
}
portalRunning = true;
startTime = millis();
}
}