add ondemandwebportal example

This commit is contained in:
Shawn A
2017-09-18 12:58:31 -05:00
parent 2aef1344b4
commit b151c19b3c
2 changed files with 72 additions and 15 deletions
+15 -15
View File
@@ -95,7 +95,6 @@ WiFiManager::WiFiManager() {
WiFiManager::~WiFiManager() {
if(_userpersistent) WiFi.persistent(true); // reenable persistent
WiFi.mode(_usermode);
DEBUG_WM(_usermode);
DEBUG_WM(F("unloading"));
}
@@ -160,6 +159,19 @@ bool WiFiManager::startAP(){
return ret;
}
void WiFiManager::startWebPortal() {
if(configPortalActive || webPortalActive) return;
setupConfigPortal();
webPortalActive = true;
}
void WiFiManager::stopWebPortal() {
if(!configPortalActive && !webPortalActive) return;
DEBUG_WM("Stopping Web Portal");
webPortalActive = false;
stopConfigPortal();
}
boolean WiFiManager::configPortalHasTimeout(){
if(_configPortalTimeout == 0 || wifi_softap_get_station_num() > 0){
_configPortalStart = millis(); // kludge, bump configportal start time to skew timeouts
@@ -194,19 +206,6 @@ void WiFiManager::setupConfigPortal() {
DEBUG_WM(F("HTTP server started"));
}
void WiFiManager::startWebPortal() {
if(configPortalActive || webPortalActive) return;
setupConfigPortal();
webPortalActive = true;
}
void WiFiManager::stopWebPortal() {
if(!configPortalActive && !webPortalActive) return;
DEBUG_WM("Stopping Web Portal");
webPortalActive = false;
stopConfigPortal();
}
boolean WiFiManager::startConfigPortal() {
String ssid = "ESP" + String(ESP.getChipId());
return startConfigPortal(ssid.c_str(), NULL);
@@ -321,7 +320,8 @@ boolean WiFiManager::stopConfigPortal(){
//HTTP handler
server->handleClient();
// @todo does this free memory?
// @todo what is the proper way to shutdown and free the server up
server->stop();
server.reset();
dnsServer.reset();
@@ -0,0 +1,57 @@
/**
* OnDemandWebPortal.ino
* example of running the webportal manually, independantly from the captiveportal
* trigger pin will start a webportal for 120 seconds then turn it off.
*
*/
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
//needed for library
#include <ESP8266WebServer.h>
#include <DNSServer.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
// select which pin will trigger the configuration portal when set to LOW
// ESP-01 users please note: the only pins available (0 and 2), are shared
// with the bootloader, so always set them HIGH at power-up
#define TRIGGER_PIN 0
// set your ap credentials
const char *ssid = "********";
const char *password = "********";
int timeout = 120; // seconds
WiFiManager wifiManager;
bool webportalrunning = false;
int start = millis();
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("\n Starting");
pinMode(TRIGGER_PIN, INPUT_PULLUP);
}
void loop() {
if(webportalrunning){
wifiManager.process();
if(millis()-start > timeout*1000){
Serial.println("webportaltimeout");
webportalrunning = false;
wifiManager.stopWebPortal(); // auto off
}
}
// is configuration portal requested?
if ( digitalRead(TRIGGER_PIN) == LOW && !webportalrunning) {
wifiManager.startWebPortal();
webportalrunning = true;
start = millis();
}
// put your main code here, to run repeatedly:
}