Skip to content

Commit

Permalink
Merge pull request #3 from r-downing/dev
Browse files Browse the repository at this point in the history
version 3.0
  • Loading branch information
r-downing authored Nov 29, 2017
2 parents 1534900 + 6f30549 commit c7c1e48
Show file tree
Hide file tree
Showing 15 changed files with 361 additions and 22 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
PersWiFiManager.ino
PersWiFiManager.ino
data/*
!data/wifi*
73 changes: 58 additions & 15 deletions PersWiFiManager.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* PersWiFiManager
* version 2.0.2
* https://r-downing.github.io/PersWiFiManager/
*/
version 3.0.0
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,53 @@ 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
}

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)))) {
startApMode();
_connectStartTime = 0; //reset connect start time
}

} //handleWiFi

void PersWiFiManager::startApMode(){
//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());
return false;//} //if
//moved dns start from here
} //attemptConnection
if (_apHandler) _apHandler();
}//startApMode

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 @@ -85,6 +114,11 @@ void PersWiFiManager::setupWiFiHandlers() {
attemptConnection(_server->arg("n"), _server->arg("p"));
}); //_server->on /wifi/connect

_server->on("/wifi/ap", [&](){
_server->send(200, "text/html", "access point: "+getApSsid());
startApMode();
}); //_server->on /wifi/ap

_server->on("/wifi/rst", [&]() {
_server->send(200, "text/html", "Rebooting...");
delay(100);
Expand Down Expand Up @@ -113,3 +147,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::onAp(WiFiChangeHandlerFunction fn) {
_apHandler = fn;
}


22 changes: 20 additions & 2 deletions PersWiFiManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
class PersWiFiManager {

public:
//constructor - takes inputs for ESP8266WebServer and DNSServer, optional ap ssid

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

PersWiFiManager(ESP8266WebServer& s, DNSServer& d);

bool attemptConnection(const String& ssid = "", const String& pass = "");
Expand All @@ -23,12 +25,28 @@ class PersWiFiManager {

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

void setConnectNonBlock(bool b);

void handleWiFi();

void startApMode();

void onConnect(WiFiChangeHandlerFunction fn);

void onAp(WiFiChangeHandlerFunction fn);

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

bool _connectNonBlock;
unsigned long _connectStartTime;

WiFiChangeHandlerFunction _connectHandler;
WiFiChangeHandlerFunction _apHandler;

};//class

#endif


4 changes: 3 additions & 1 deletion data/wifi.htm
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,11 @@
<br>
<button onclick="p('Start WPS?','/wifi/wps')">WPS Setup</button>
<br>
<button onclick="p('Start AP mode?','/wifi/ap')">AP Mode</button>
<br>
<button onclick="p('Reboot device?','/wifi/rst')">Reboot</button>
<br>
<a href="javascript:history.back()">Back</a> |
<a href="javascript:history.back()">Back</a> |
<a href="/">Home</a>
</div>
</body>
Expand Down
2 changes: 1 addition & 1 deletion data/wifi.min.htm
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!DOCTYPE html><html><head><meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"/><title>ESP WiFi</title><script>function g(i){return document.getElementById(i);};function p(t,l){if(confirm(t)) window.location=l;};function E(s){return document.createElement(s)};var S="setAttribute",A="appendChild",H="innerHTML",X,wl;function scan(){if(X) return;X=new XMLHttpRequest(),wl=document.getElementById('wl');wl[H]="Scanning...";X.onreadystatechange=function(){if (this.readyState==4&&this.status==200){X=0;wl[H]="";this.responseText.split("\n").forEach(function (e){let t=e.split(","), s=t.slice(2).join(',');var d=E('div'),i=E('a'),c=E('a');i[S]('class','s'); c[S]('class','q');i.onclick=function(){g('s').value=s;g('p').focus();};i[A](document.createTextNode(s));c[H]=t[0]+"%"+(parseInt(t[1])?"\uD83D\uDD12":"\u26A0");wl[A](i); wl[A](c);wl[A](document.createElement('br'));});}};X.open("GET","wifi/list",true);X.send();};</script><style>input{padding:5px;font-size:1em;width:95%;filter:invert(100%);}body{text-align:center;font-family:verdana;background-color:black;color:white;}a{color:#1fa3ec;}button{border:0;border-radius:0.3em;background-color:#1fa3ec;color:#fff;line-height:2.4em;font-size:1.2em;width:100%;display:block;}.q{float:right;}.s{display:inline-block;width:14em;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;}#wl{line-height:1.5em;}</style></head><body><div style='text-align:left;display:inline-block;width:320px;padding:5px'><button onclick="scan()">&#x21bb; Scan</button><p id='wl'></p><form method='post' action='/wifi/connect'><input id='s' name='n' length=32 placeholder='SSID'><br><input id='p' name='p' length=64 type='password' placeholder='password'><br><br><button type='submit'>Connect</button></form><br><br><button onclick="p('Start WPS?','/wifi/wps')">WPS Setup</button><br><button onclick="p('Reboot device?','/wifi/rst')">Reboot</button><br><a href="javascript:history.back()">Back</a> |<a href="/">Home</a></div></body></html>
<!DOCTYPE html><html><head><meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"/><title>ESP WiFi</title><script>function g(i){return document.getElementById(i);};function p(t,l){if(confirm(t)) window.location=l;};function E(s){return document.createElement(s)};var S="setAttribute",A="appendChild",H="innerHTML",X,wl;function scan(){if(X) return;X=new XMLHttpRequest(),wl=document.getElementById('wl');wl[H]="Scanning...";X.onreadystatechange=function(){if (this.readyState==4&&this.status==200){X=0;wl[H]="";this.responseText.split("\n").forEach(function (e){let t=e.split(","), s=t.slice(2).join(',');var d=E('div'),i=E('a'),c=E('a');i[S]('class','s'); c[S]('class','q');i.onclick=function(){g('s').value=s;g('p').focus();};i[A](document.createTextNode(s));c[H]=t[0]+"%"+(parseInt(t[1])?"\uD83D\uDD12":"\u26A0");wl[A](i); wl[A](c);wl[A](document.createElement('br'));});}};X.open("GET","wifi/list",true);X.send();};</script><style>input{padding:5px;font-size:1em;width:95%;filter:invert(100%);}body{text-align:center;font-family:verdana;background-color:black;color:white;}a{color:#1fa3ec;}button{border:0;border-radius:0.3em;background-color:#1fa3ec;color:#fff;line-height:2.4em;font-size:1.2em;width:100%;display:block;}.q{float:right;}.s{display:inline-block;width:14em;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;}#wl{line-height:1.5em;}</style></head><body><div style='text-align:left;display:inline-block;width:320px;padding:5px'><button onclick="scan()">&#x21bb; Scan</button><p id='wl'></p><form method='post' action='/wifi/connect'><input id='s' name='n' length=32 placeholder='SSID'><br><input id='p' name='p' length=64 type='password' placeholder='password'><br><br><button type='submit'>Connect</button></form><br><br><button onclick="p('Start WPS?','/wifi/wps')">WPS Setup</button><br><button onclick="p('Start AP mode?','/wifi/ap')">AP Mode</button><br><button onclick="p('Reboot device?','/wifi/rst')">Reboot</button><br><a href="javascript:history.back()">Back</a> | <a href="/">Home</a></div></body></html>
Binary file modified data/wifi.min.htm.gz
Binary file not shown.
Binary file modified examples/basic_rest_api/data/wifi.min.htm.gz
Binary file not shown.
Binary file modified examples/spiffs_rest_api/data/wifi.min.htm.gz
Binary file not shown.
52 changes: 52 additions & 0 deletions examples/spiffs_rest_api_nonblocking/data/index.htm
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<h3>Current Data</h3>
x:
<span id="x"></span>
<br/> y:
<span id="y"></span>

<h6>Last updated
<span id="ut"></span> seconds ago.
<span id="status"></span>
</h6>

<h3>Update Data</h3>
<form id="xform" onsubmit="return transmit(this)">
x:
<input type="number" name="x" />
<input type="submit" />
</form>
<form id="yform" onsubmit="return transmit(this)">
y:
<input type="text" name="y" />
<input type="submit" />
</form>

<a href="wifi.htm">WiFi settings</a>

<script>
function g(i) { return document.getElementById(i) };
var xhttp, updateTime;

function transmit(f) {
if (!xhttp) { //prevent simultaneous requests
g("status").innerHTML = "updating...";
xhttp = new XMLHttpRequest();
xhttp.open("POST", "/api");
xhttp.send(f ? (new FormData(f)) : "");
xhttp.onreadystatechange = function () {
if (xhttp.readyState === XMLHttpRequest.DONE && xhttp.status === 200) {
var data = JSON.parse(xhttp.responseText);
g("x").innerHTML = data.x;
g("y").innerHTML = data.y;
xhttp = null;
g("status").innerHTML = "";
updateTime = 0;
}
}
}
return false; //prevent form redirect
}
transmit();
setInterval(function () { g("ut").innerHTML = ++updateTime; }, 1000);
setInterval(transmit, 5000); //autoupdate display every 5s
</script>
108 changes: 108 additions & 0 deletions examples/spiffs_rest_api_nonblocking/data/wifi.htm
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<!DOCTYPE html>
<html>

<head>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
<title>ESP WiFi</title>
<script>
function g(i){return document.getElementById(i);};
function p(t,l){if(confirm(t)) window.location=l;};
function E(s){return document.createElement(s)};
var S="setAttribute",A="appendChild",H="innerHTML",X,wl;
function scan(){
if(X) return;
X=new XMLHttpRequest(),wl=document.getElementById('wl');
wl[H]="Scanning...";
X.onreadystatechange=function(){
if (this.readyState==4&&this.status==200) {
X=0;
wl[H]="";
this.responseText.split("\n").forEach(function (e) {
let t=e.split(","), s=t.slice(2).join(',');
var d=E('div'),i=E('a'),c=E('a');
i[S]('class','s'); c[S]('class','q');
i.onclick=function(){g('s').value=s;g('p').focus();};
i[A](document.createTextNode(s));
c[H]=t[0]+"%"+(parseInt(t[1])?"\uD83D\uDD12":"\u26A0");
wl[A](i); wl[A](c);
wl[A](document.createElement('br'));
});
}
};
X.open("GET","wifi/list",true);
X.send();
};
</script>
<style>
input {
padding:5px;
font-size:1em;
width:95%;
filter:invert(100%);
}

body {
text-align:center;
font-family:verdana;
background-color:black;
color:white;
}

a {
color:#1fa3ec;
}

button {
border:0;
border-radius:0.3em;
background-color:#1fa3ec;
color:#fff;
line-height:2.4em;
font-size:1.2em;
width:100%;
display:block;
}

.q {
float:right;
}

.s {
display:inline-block;
width:14em;
overflow:hidden;
text-overflow:ellipsis;
white-space:nowrap;
}
#wl{
line-height:1.5em;
}
</style>
</head>

<body>
<div style='text-align:left;display:inline-block;width:320px;padding:5px'>
<button onclick="scan()">&#x21bb; Scan</button>
<p id='wl'></p>
<form method='post' action='/wifi/connect'>
<input id='s' name='n' length=32 placeholder='SSID'>
<br>
<input id='p' name='p' length=64 type='password' placeholder='password'>
<br>
<br>
<button type='submit'>Connect</button>
</form>
<br>
<br>
<button onclick="p('Start WPS?','/wifi/wps')">WPS Setup</button>
<br>
<button onclick="p('Start AP mode?','/wifi/ap')">AP Mode</button>
<br>
<button onclick="p('Reboot device?','/wifi/rst')">Reboot</button>
<br>
<a href="javascript:history.back()">Back</a> |
<a href="/">Home</a>
</div>
</body>

</html>
1 change: 1 addition & 0 deletions examples/spiffs_rest_api_nonblocking/data/wifi.min.htm
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!DOCTYPE html><html><head><meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"/><title>ESP WiFi</title><script>function g(i){return document.getElementById(i);};function p(t,l){if(confirm(t)) window.location=l;};function E(s){return document.createElement(s)};var S="setAttribute",A="appendChild",H="innerHTML",X,wl;function scan(){if(X) return;X=new XMLHttpRequest(),wl=document.getElementById('wl');wl[H]="Scanning...";X.onreadystatechange=function(){if (this.readyState==4&&this.status==200){X=0;wl[H]="";this.responseText.split("\n").forEach(function (e){let t=e.split(","), s=t.slice(2).join(',');var d=E('div'),i=E('a'),c=E('a');i[S]('class','s'); c[S]('class','q');i.onclick=function(){g('s').value=s;g('p').focus();};i[A](document.createTextNode(s));c[H]=t[0]+"%"+(parseInt(t[1])?"\uD83D\uDD12":"\u26A0");wl[A](i); wl[A](c);wl[A](document.createElement('br'));});}};X.open("GET","wifi/list",true);X.send();};</script><style>input{padding:5px;font-size:1em;width:95%;filter:invert(100%);}body{text-align:center;font-family:verdana;background-color:black;color:white;}a{color:#1fa3ec;}button{border:0;border-radius:0.3em;background-color:#1fa3ec;color:#fff;line-height:2.4em;font-size:1.2em;width:100%;display:block;}.q{float:right;}.s{display:inline-block;width:14em;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;}#wl{line-height:1.5em;}</style></head><body><div style='text-align:left;display:inline-block;width:320px;padding:5px'><button onclick="scan()">&#x21bb; Scan</button><p id='wl'></p><form method='post' action='/wifi/connect'><input id='s' name='n' length=32 placeholder='SSID'><br><input id='p' name='p' length=64 type='password' placeholder='password'><br><br><button type='submit'>Connect</button></form><br><br><button onclick="p('Start WPS?','/wifi/wps')">WPS Setup</button><br><button onclick="p('Start AP mode?','/wifi/ap')">AP Mode</button><br><button onclick="p('Reboot device?','/wifi/rst')">Reboot</button><br><a href="javascript:history.back()">Back</a> | <a href="/">Home</a></div></body></html>
Binary file not shown.
Loading

0 comments on commit c7c1e48

Please sign in to comment.