From b05dff34b7a7385d78b9d36ba392703d4191ecb5 Mon Sep 17 00:00:00 2001 From: Alex Tudorache Date: Fri, 8 Jan 2016 21:04:37 +0200 Subject: [PATCH] added example for on demand config portal cleanup --- WiFiManager.cpp | 21 +++--- WiFiManager.h | 6 +- .../OnDemandConfigPortal.ino | 67 +++++++++++++++++++ 3 files changed, 81 insertions(+), 13 deletions(-) create mode 100644 examples/OnDemandConfigPortal/OnDemandConfigPortal.ino diff --git a/WiFiManager.cpp b/WiFiManager.cpp index f9747dd..dca01eb 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -49,23 +49,24 @@ void WiFiManager::addParameter(WiFiManagerParameter *p) { DEBUG_WM(p->getID()); } -void WiFiManager::setupConfigPortal(char const *apName, char const *apPasswd) { +void WiFiManager::setupConfigPortal(char const *apName, char const *apPassword) { dnsServer.reset(new DNSServer()); server.reset(new ESP8266WebServer(80)); DEBUG_WM(F("")); _apName = apName; - _apPasswd = apPasswd; + _apPassword = apPassword; start = millis(); DEBUG_WM(F("Configuring access point... ")); DEBUG_WM(_apName); - if (_apPasswd != NULL) { - if (strlen(_apPasswd) < 8 || strlen(_apPasswd) > 63) { + if (_apPassword != NULL) { + if (strlen(_apPassword) < 8 || strlen(_apPassword) > 63) { // fail passphrase to short or long! - DEBUG_WM(F("Invalid AccessPoint password")); + DEBUG_WM(F("Invalid AccessPoint password. Ignoring")); + _apPassword = NULL; } - DEBUG_WM(_apPasswd); + DEBUG_WM(_apPassword); } //optional soft ip config @@ -74,8 +75,8 @@ void WiFiManager::setupConfigPortal(char const *apName, char const *apPasswd) { WiFi.softAPConfig(_ip, _gw, _sn); } - if (_apPasswd != NULL) { - WiFi.softAP(_apName, _apPasswd);//password option + if (_apPassword != NULL) { + WiFi.softAP(_apName, _apPassword);//password option } else { WiFi.softAP(_apName); } @@ -136,7 +137,7 @@ boolean WiFiManager::startConfigPortal(char const *apName, char const *apPasswo connect = false; setupConfigPortal(apName, apPassword); - + DEBUG_WM("start loop"); while (timeout == 0 || millis() < start + timeout) { //DNS dnsServer->processNextRequest(); @@ -168,7 +169,7 @@ boolean WiFiManager::startConfigPortal(char const *apName, char const *apPasswo server.reset(); dnsServer.reset(); - + DEBUG_WM("end loop"); return WiFi.status() == WL_CONNECTED; } diff --git a/WiFiManager.h b/WiFiManager.h index 42ee500..7460082 100644 --- a/WiFiManager.h +++ b/WiFiManager.h @@ -59,7 +59,7 @@ class WiFiManager WiFiManager(); boolean autoConnect(); - boolean autoConnect(char const *apName, char const *apPasswd = NULL); + boolean autoConnect(char const *apName, char const *apPassword = NULL); //if you want to always start the config portal, without trying to connect first boolean startConfigPortal(char const *apName, char const *apPassword = NULL); @@ -96,11 +96,11 @@ class WiFiManager const String HTTP_HEAD = "{v}"; - void setupConfigPortal(char const *apName, char const *apPasswd); + void setupConfigPortal(char const *apName, char const *apPassword); int _eepromStart; const char* _apName = "no-net"; - const char* _apPasswd = NULL; + const char* _apPassword = NULL; String _ssid = ""; String _pass = ""; unsigned long timeout = 0; diff --git a/examples/OnDemandConfigPortal/OnDemandConfigPortal.ino b/examples/OnDemandConfigPortal/OnDemandConfigPortal.ino new file mode 100644 index 0000000..06b7677 --- /dev/null +++ b/examples/OnDemandConfigPortal/OnDemandConfigPortal.ino @@ -0,0 +1,67 @@ +#include //https://github.com/esp8266/Arduino + +//needed for library +#include +#include +#include //https://github.com/tzapu/WiFiManager + + +//callback notifying us of the need to save config +void saveConfigCallback () { + Serial.println("Should save config"); +} + + +void setup() { + // put your setup code here, to run once: + Serial.begin(115200); + Serial.println("\n Starting"); + + //read gpio 4 state on startup + pinMode(4, INPUT); + int pinState = digitalRead(4); + + //if gpio 4 is high startup config mode + if (pinState == LOW) { + //WiFiManager + //Local intialization. Once its business is done, there is no need to keep it around + WiFiManager wifiManager; + + //set config save notify callback + wifiManager.setSaveConfigCallback(saveConfigCallback); + + //reset settings - for testing + //wifiManager.resetSettings(); + + //sets timeout until configuration portal gets turned off + //useful to make it all retry or go to sleep + //in seconds + //wifiManager.setTimeout(120); + + //it starts an access point with the specified name + //here "AutoConnectAP" + //and goes into a blocking loop awaiting configuration + + //WITHOUT THIS THE AP DOES NOT SEEM TO WORK PROPERLY + WiFi.mode(WIFI_STA); + + if (!wifiManager.startConfigPortal("OnDemandAP")) { + Serial.println("failed to connect and hit timeout"); + delay(3000); + //reset and try again, or maybe put it to deep sleep + ESP.reset(); + delay(5000); + } + + //if you get here you have connected to the WiFi + Serial.println("connected...yeey :)"); + } + + +} + +void loop() { + // put your main code here, to run repeatedly: + + +}