Skip to content

Commit

Permalink
new trial version, nonblock + handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
r-downing committed Nov 19, 2017
1 parent 1534900 commit d5ba651
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 18 deletions.
4 changes: 3 additions & 1 deletion .gitignore
@@ -1 +1,3 @@
PersWiFiManager.ino
PersWiFiManager.ino
data/*
!data/wifi*
68 changes: 51 additions & 17 deletions PersWiFiManager.cpp
@@ -1,7 +1,7 @@
/* PersWiFiManager
* version 2.0.2
* https://r-downing.github.io/PersWiFiManager/
*/
version 2.0.2 - nonblock mod
https://r-downing.github.io/PersWiFiManager/
*/

#include "PersWiFiManager.h"

Expand All @@ -16,11 +16,6 @@ PersWiFiManager::PersWiFiManager(ESP8266WebServer& s, DNSServer& d) {
} //PersWiFiManager

bool PersWiFiManager::attemptConnection(const String& ssid, const String& pass) {
IPAddress apIP(192, 168, 1, 1);
//moved dns start to here
_dnsServer->setErrorReplyCode(DNSReplyCode::NoError);
_dnsServer->start((byte)53, "*", apIP); //used for captive portal in AP mode

//attempt to connect to wifi
WiFi.mode(WIFI_STA);
if (ssid.length()) {
Expand All @@ -29,19 +24,49 @@ bool PersWiFiManager::attemptConnection(const String& ssid, const String& pass)
} else {
WiFi.begin();
}
unsigned long connectTime = millis();
//while ((millis() - connectTime) < 1000 * WIFI_CONNECT_TIMEOUT && WiFi.status() != WL_CONNECTED)
while (WiFi.status() != WL_CONNECT_FAILED && WiFi.status() != WL_CONNECTED && (millis() - connectTime) < 1000 * WIFI_CONNECT_TIMEOUT)

//if in nonblock mode, skip this loop
_connectStartTime = millis() + 1;
while (!_connectNonBlock && _connectStartTime) {
handleWiFi();
delay(10);
if (WiFi.status() == WL_CONNECTED) return true;// { //if timed out, switch to AP mode
WiFi.mode(WIFI_AP);
WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
_apPass.length() ? WiFi.softAP(getApSsid().c_str(), _apPass.c_str()) : WiFi.softAP(getApSsid().c_str());
return false;//} //if
//moved dns start from here
}

return (WiFi.status() == WL_CONNECTED);

} //attemptConnection

void PersWiFiManager::handleWiFi() {
if (!_connectStartTime) return;

if (WiFi.status() == WL_CONNECTED) {
_connectStartTime = 0;
if (_connectHandler) _connectHandler();
return;
}

//if failed or not connected and time is up
if ((WiFi.status() == WL_CONNECT_FAILED) || (WiFi.status() != WL_CONNECTED && (millis() - _connectStartTime) > 1000 * WIFI_CONNECT_TIMEOUT)) {
//start AP mode
IPAddress apIP(192, 168, 1, 1);
WiFi.mode(WIFI_AP);
WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
_apPass.length() ? WiFi.softAP(getApSsid().c_str(), _apPass.c_str()) : WiFi.softAP(getApSsid().c_str());
_connectStartTime = 0; //reset connect start time
if (_failHandler) _failHandler();
}

} //handleWiFi

void PersWiFiManager::setConnectNonBlock(bool b) {
_connectNonBlock = b;
} //setConnectNonBlock

void PersWiFiManager::setupWiFiHandlers() {
IPAddress apIP(192, 168, 1, 1);
_dnsServer->setErrorReplyCode(DNSReplyCode::NoError);
_dnsServer->start((byte)53, "*", apIP); //used for captive portal in AP mode

_server->on("/wifi/list", [&] () {
//scan for wifi networks
int n = WiFi.scanNetworks();
Expand Down Expand Up @@ -113,3 +138,12 @@ void PersWiFiManager::setApCredentials(const String& apSsid, const String& apPas
if (apPass.length() >= 8) _apPass = apPass;
} //setApCredentials

void PersWiFiManager::onConnect(WiFiChangeHandlerFunction fn) {
_connectHandler = fn;
}

void PersWiFiManager::onFail(WiFiChangeHandlerFunction fn) {
_failHandler = fn;
}


17 changes: 17 additions & 0 deletions PersWiFiManager.h
Expand Up @@ -10,6 +10,9 @@
class PersWiFiManager {

public:

typedef std::function<void(void)> WiFiChangeHandlerFunction;

//constructor - takes inputs for ESP8266WebServer and DNSServer, optional ap ssid
PersWiFiManager(ESP8266WebServer& s, DNSServer& d);

Expand All @@ -23,11 +26,25 @@ class PersWiFiManager {

void setApCredentials(const String& apSsid, const String& apPass = "");

void setConnectNonBlock(bool b);

void handleWiFi();

void onConnect(WiFiChangeHandlerFunction fn);

void onFail(WiFiChangeHandlerFunction fn);

private:
ESP8266WebServer * _server;
DNSServer * _dnsServer;
String _apSsid, _apPass;

bool _connectNonBlock;
unsigned long _connectStartTime;

WiFiChangeHandlerFunction _connectHandler;
WiFiChangeHandlerFunction _failHandler;

};//class

#endif
Expand Down
2 changes: 2 additions & 0 deletions keywords.txt
Expand Up @@ -17,6 +17,8 @@ setupWiFiHandlers KEYWORD2
begin KEYWORD2
getApSsid KEYWORD2
setApCredentials KEYWORD2
handleWiFi KEYWORD2
setConnectNonBlock KEYWORD2

#######################################
# Constants (LITERAL1)
Expand Down

0 comments on commit d5ba651

Please sign in to comment.