mirror of
https://github.com/alexhopeoconnor/WiFiManager.git
synced 2026-10-14 15:11:32 +10:00
cleanup
moved ap creation logic to new function so it can be called on demand implemented changes and suggestions from @riccibitti , thanks Merge branch 'master' of https://github.com/riccibitti/WiFiManager # Conflicts: # WiFiManager.cpp # WiFiManager.h
This commit is contained in:
+19
-71
@@ -49,16 +49,7 @@ void WiFiManager::addParameter(WiFiManagerParameter *p) {
|
||||
DEBUG_WM(p->getID());
|
||||
}
|
||||
|
||||
|
||||
void WiFiManager::begin() {
|
||||
begin("NoNetESP");
|
||||
}
|
||||
|
||||
void WiFiManager::begin(char const *apName) {
|
||||
begin(apName, NULL);
|
||||
}
|
||||
|
||||
void WiFiManager::begin(char const *apName, char const *apPasswd) {
|
||||
void WiFiManager::setupConfigPortal(char const *apName, char const *apPasswd) {
|
||||
dnsServer.reset(new DNSServer());
|
||||
server.reset(new ESP8266WebServer(80));
|
||||
|
||||
@@ -114,42 +105,38 @@ boolean WiFiManager::autoConnect() {
|
||||
return autoConnect(ssid.c_str(), NULL);
|
||||
}
|
||||
|
||||
boolean WiFiManager::autoConnect(char const *apName) {
|
||||
return autoConnect(apName, NULL);
|
||||
}
|
||||
|
||||
boolean WiFiManager::autoConnect(char const *apName, char const *apPasswd) {
|
||||
boolean WiFiManager::autoConnect(char const *apName, char const *apPassword) {
|
||||
DEBUG_WM(F(""));
|
||||
DEBUG_WM(F("AutoConnect"));
|
||||
|
||||
// read eeprom for ssid and pass
|
||||
String ssid = getSSID();
|
||||
String pass = getPassword();
|
||||
//use SDK functions to get SSID and pass
|
||||
//String ssid = WiFi.SSID();
|
||||
//String pass = WiFi.psk();
|
||||
|
||||
// attempt to connect; should it fail, fall back to AP
|
||||
WiFi.mode(WIFI_STA);
|
||||
connectWifi(ssid, pass);
|
||||
int s = WiFi.status();
|
||||
if (s == WL_CONNECTED) {
|
||||
if (connectWifi(ssid, pass) == WL_CONNECTED) {
|
||||
DEBUG_WM(F("IP Address:"));
|
||||
DEBUG_WM(WiFi.localIP());
|
||||
//connected
|
||||
return true;
|
||||
}
|
||||
|
||||
//not connected
|
||||
return startConfigPortal(apName, apPassword);
|
||||
}
|
||||
|
||||
boolean WiFiManager::startConfigPortal(char const *apName, char const *apPassword) {
|
||||
//setup AP
|
||||
WiFi.mode(WIFI_AP);
|
||||
|
||||
//notify we entered AP mode
|
||||
if ( _apcallback != NULL) {
|
||||
_apcallback();
|
||||
}
|
||||
|
||||
connect = false;
|
||||
begin(apName, apPasswd);
|
||||
setupConfigPortal(apName, apPassword);
|
||||
|
||||
bool looping = true;
|
||||
while (timeout == 0 || millis() < start + timeout) {
|
||||
//DNS
|
||||
dnsServer->processNextRequest();
|
||||
@@ -161,16 +148,9 @@ boolean WiFiManager::autoConnect(char const *apName, char const *apPasswd) {
|
||||
delay(2000);
|
||||
DEBUG_WM(F("Connecting to new AP"));
|
||||
connect = false;
|
||||
//ssid = getSSID();
|
||||
//pass = getPassword();
|
||||
connectWifi(_ssid, _pass);
|
||||
int s = WiFi.status();
|
||||
if (s != WL_CONNECTED) {
|
||||
// using user-provided _ssid, _pass in place of system-stored ssid amd pass
|
||||
if (connectWifi(_ssid, _pass) != WL_CONNECTED) {
|
||||
DEBUG_WM(F("Failed to connect."));
|
||||
//not connected, should retry everything
|
||||
//ESP.reset();
|
||||
//delay(1000);
|
||||
//return false;
|
||||
} else {
|
||||
//connected
|
||||
WiFi.mode(WIFI_STA);
|
||||
@@ -188,24 +168,26 @@ boolean WiFiManager::autoConnect(char const *apName, char const *apPasswd) {
|
||||
|
||||
server.reset();
|
||||
dnsServer.reset();
|
||||
|
||||
return WiFi.status() == WL_CONNECTED;
|
||||
}
|
||||
|
||||
|
||||
void WiFiManager::connectWifi(String ssid, String pass) {
|
||||
int WiFiManager::connectWifi(String ssid, String pass) {
|
||||
DEBUG_WM(F("Connecting as wifi client..."));
|
||||
//WiFi.disconnect();
|
||||
WiFi.begin(ssid.c_str(), pass.c_str());
|
||||
int connRes = WiFi.waitForConnectResult();
|
||||
DEBUG_WM ("Connection result: ");
|
||||
DEBUG_WM ( connRes );
|
||||
return connRes;
|
||||
}
|
||||
|
||||
|
||||
String WiFiManager::getSSID() {
|
||||
if (_ssid == "") {
|
||||
DEBUG_WM(F("Reading SSID"));
|
||||
_ssid = WiFi.SSID();//getEEPROMString(0, 32);
|
||||
_ssid = WiFi.SSID();
|
||||
DEBUG_WM(F("SSID: "));
|
||||
DEBUG_WM(_ssid);
|
||||
}
|
||||
@@ -215,47 +197,13 @@ String WiFiManager::getSSID() {
|
||||
String WiFiManager::getPassword() {
|
||||
if (_pass == "") {
|
||||
DEBUG_WM(F("Reading Password"));
|
||||
_pass = WiFi.psk();//getEEPROMString(32, 64);
|
||||
_pass = WiFi.psk();
|
||||
DEBUG_WM("Password: " + _pass);
|
||||
//DEBUG_WM(_pass);
|
||||
}
|
||||
return _pass;
|
||||
}
|
||||
|
||||
/*
|
||||
String WiFiManager::getEEPROMString(int start, int len) {
|
||||
EEPROM.begin(512);
|
||||
delay(10);
|
||||
String string = "";
|
||||
for (int i = _eepromStart + start; i < _eepromStart + start + len; i++) {
|
||||
//DEBUG_WM(i);
|
||||
string += char(EEPROM.read(i));
|
||||
}
|
||||
EEPROM.end();
|
||||
return string;
|
||||
}
|
||||
*/
|
||||
/*
|
||||
void WiFiManager::setEEPROMString(int start, int len, String string) {
|
||||
EEPROM.begin(512);
|
||||
delay(10);
|
||||
int si = 0;
|
||||
for (int i = _eepromStart + start; i < _eepromStart + start + len; i++) {
|
||||
char c;
|
||||
if (si < string.length()) {
|
||||
c = string[si];
|
||||
//DEBUG_WM(F("Wrote: ");
|
||||
//DEBUG_WM(c);
|
||||
} else {
|
||||
c = 0;
|
||||
}
|
||||
EEPROM.write(i, c);
|
||||
si++;
|
||||
}
|
||||
EEPROM.end();
|
||||
DEBUG_WM(F("Wrote " + string);
|
||||
}*/
|
||||
|
||||
String WiFiManager::urldecode(const char *src)
|
||||
{
|
||||
String decoded = "";
|
||||
@@ -348,7 +296,7 @@ void WiFiManager::handleRoot() {
|
||||
}
|
||||
|
||||
/** Wifi config page handler */
|
||||
void WiFiManager::handleWifi(bool scan) {
|
||||
void WiFiManager::handleWifi(boolean scan) {
|
||||
server->sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
|
||||
server->sendHeader("Pragma", "no-cache");
|
||||
server->sendHeader("Expires", "-1");
|
||||
|
||||
+76
-92
@@ -1,21 +1,18 @@
|
||||
/**************************************************************
|
||||
* WiFiManager is a library for the ESP8266/Arduino platform
|
||||
* (https://github.com/esp8266/Arduino) to enable easy
|
||||
* configuration and reconfiguration of WiFi credentials and
|
||||
* store them in EEPROM.
|
||||
* inspired by http://www.esp8266.com/viewtopic.php?f=29&t=2520
|
||||
* https://github.com/chriscook8/esp-arduino-apboot
|
||||
* Built by AlexT https://github.com/tzapu
|
||||
* Licensed under MIT license
|
||||
WiFiManager is a library for the ESP8266/Arduino platform
|
||||
(https://github.com/esp8266/Arduino) to enable easy
|
||||
configuration and reconfiguration of WiFi credentials and
|
||||
store them in EEPROM.
|
||||
inspired by http://www.esp8266.com/viewtopic.php?f=29&t=2520
|
||||
https://github.com/chriscook8/esp-arduino-apboot
|
||||
Built by AlexT https://github.com/tzapu
|
||||
Licensed under MIT license
|
||||
**************************************************************/
|
||||
|
||||
#ifndef WiFiManager_h
|
||||
#define WiFiManager_h
|
||||
|
||||
#include <ESP8266WiFi.h>
|
||||
|
||||
#include <EEPROM.h>
|
||||
//#include <WiFiClient.h>
|
||||
#include <ESP8266WebServer.h>
|
||||
#include <DNSServer.h>
|
||||
#include <memory>
|
||||
@@ -39,125 +36,112 @@ const char HTTP_END[] PROGMEM = "</div></body></html>";
|
||||
#define WIFI_MANAGER_MAX_PARAMS 10
|
||||
|
||||
class WiFiManagerParameter {
|
||||
public:
|
||||
WiFiManagerParameter(const char *id, const char *placeholder, const char *defaultValue, int length);
|
||||
public:
|
||||
WiFiManagerParameter(const char *id, const char *placeholder, const char *defaultValue, int length);
|
||||
|
||||
const char* getID();
|
||||
const char* getValue();
|
||||
const char* getPlaceholder();
|
||||
int getValueLength();
|
||||
private:
|
||||
const char* _id;
|
||||
const char* _placeholder;
|
||||
char* _value;
|
||||
int _length;
|
||||
const char* getID();
|
||||
const char* getValue();
|
||||
const char* getPlaceholder();
|
||||
int getValueLength();
|
||||
private:
|
||||
const char* _id;
|
||||
const char* _placeholder;
|
||||
char* _value;
|
||||
int _length;
|
||||
|
||||
friend class WiFiManager;
|
||||
friend class WiFiManager;
|
||||
};
|
||||
|
||||
|
||||
class WiFiManager
|
||||
{
|
||||
public:
|
||||
public:
|
||||
WiFiManager();
|
||||
|
||||
boolean autoConnect();
|
||||
boolean autoConnect(char const *apName);
|
||||
boolean autoConnect(char const *apName, char const *apPasswd);
|
||||
|
||||
String getSSID();
|
||||
String getPassword();
|
||||
boolean autoConnect();
|
||||
boolean autoConnect(char const *apName, char const *apPasswd = NULL);
|
||||
|
||||
void resetSettings();
|
||||
//if you want to always start the config portal, without trying to connect first
|
||||
boolean startConfigPortal(char const *apName, char const *apPassword = NULL);
|
||||
|
||||
String getSSID();
|
||||
String getPassword();
|
||||
|
||||
void resetSettings();
|
||||
//for convenience
|
||||
String urldecode(const char*);
|
||||
String urldecode(const char*);
|
||||
|
||||
//sets timeout before webserver loop ends and exits even if there has been no setup.
|
||||
//sets timeout before webserver loop ends and exits even if there has been no setup.
|
||||
//usefully for devices that failed to connect at some point and got stuck in a webserver loop
|
||||
//in seconds
|
||||
void setTimeout(unsigned long seconds);
|
||||
void setDebugOutput(boolean debug);
|
||||
void setTimeout(unsigned long seconds);
|
||||
void setDebugOutput(boolean debug);
|
||||
|
||||
//sets a custom ip /gateway /subnet configuration
|
||||
void setAPConfig(IPAddress ip, IPAddress gw, IPAddress sn);
|
||||
void setAPConfig(IPAddress ip, IPAddress gw, IPAddress sn);
|
||||
//called when AP mode and config portal is started
|
||||
void setAPCallback( void (*func)(void) );
|
||||
//called when settings have been changed and connection was successful
|
||||
void setSaveConfigCallback( void (*func)(void) );
|
||||
void setAPCallback( void (*func)(void) );
|
||||
//called when settings have been changed and connection was successful
|
||||
void setSaveConfigCallback( void (*func)(void) );
|
||||
|
||||
//adds a custom parameter
|
||||
void addParameter(WiFiManagerParameter *p);
|
||||
void addParameter(WiFiManagerParameter *p);
|
||||
|
||||
private:
|
||||
std::unique_ptr<DNSServer> dnsServer;
|
||||
private:
|
||||
std::unique_ptr<DNSServer> dnsServer;
|
||||
std::unique_ptr<ESP8266WebServer> server;
|
||||
|
||||
const int WM_DONE = 0;
|
||||
const int WM_WAIT = 10;
|
||||
|
||||
//const char PROGMEM HTTP_404[] = "HTTP/1.1 404 Not Found\r\n\r\n";
|
||||
//const String HTTP_200 = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
|
||||
const String HTTP_HEAD = "<!DOCTYPE html><html lang=\"en\"><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"/><title>{v}</title>";
|
||||
//const String HTTP_STYLE = "<style>div,input {margin-bottom: 5px;} body{width:200px;display:block;margin-left:auto;margin-right:auto;} button{padding:0.75rem 1rem;border:0;border-radius:0.317rem;background-color:#1fa3ec;color:#fff;line-height:1.5;cursor:pointer;}</style>";
|
||||
//const String HTTP_SCRIPT = "<script>function c(l){document.getElementById('s').value=l.innerText||l.textContent;document.getElementById('p').focus();}</script>";
|
||||
//const String HTTP_HEAD_END = "</head><body>";
|
||||
//const String HTTP_ITEM = "<div><a href='#' onclick='c(this)'>{v}</a></div>";
|
||||
//const String HTTP_FORM = "<form method='get' action='wifisave'><input id='s' name='s' length=32 placeholder='SSID'><input id='p' name='p' length=64 placeholder='password'><br/><button type='submit'>save</button></form>";
|
||||
//const String HTTP_SAVED = "<div>Credentials Saved<br />Trying to connect ESP to network.<br />If it fails reconnect to AP to try again</div>";
|
||||
//const String HTTP_END = "</body></html>";
|
||||
//const char HTTP_END[] PROGMEM = R"=====(
|
||||
//</body></html>
|
||||
//)=====";
|
||||
const int WM_DONE = 0;
|
||||
const int WM_WAIT = 10;
|
||||
|
||||
void begin();
|
||||
void begin(char const *apName);
|
||||
void begin(char const *apName, char const *apPasswd);
|
||||
|
||||
int _eepromStart;
|
||||
const char* _apName = "no-net";
|
||||
const char* _apPasswd = NULL;
|
||||
String _ssid = "";
|
||||
String _pass = "";
|
||||
const String HTTP_HEAD = "<!DOCTYPE html><html lang=\"en\"><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"/><title>{v}</title>";
|
||||
|
||||
void setupConfigPortal(char const *apName, char const *apPasswd);
|
||||
|
||||
int _eepromStart;
|
||||
const char* _apName = "no-net";
|
||||
const char* _apPasswd = NULL;
|
||||
String _ssid = "";
|
||||
String _pass = "";
|
||||
unsigned long timeout = 0;
|
||||
unsigned long start = 0;
|
||||
IPAddress _ip;
|
||||
IPAddress _gw;
|
||||
IPAddress _sn;
|
||||
int _paramsCount = 0;
|
||||
|
||||
String getEEPROMString(int start, int len);
|
||||
void setEEPROMString(int start, int len, String string);
|
||||
IPAddress _ip;
|
||||
IPAddress _gw;
|
||||
IPAddress _sn;
|
||||
int _paramsCount = 0;
|
||||
|
||||
bool keepLooping = true;
|
||||
int status = WL_IDLE_STATUS;
|
||||
void connectWifi(String ssid, String pass);
|
||||
String getEEPROMString(int start, int len);
|
||||
void setEEPROMString(int start, int len, String string);
|
||||
|
||||
// boolean keepLooping = true;
|
||||
int status = WL_IDLE_STATUS;
|
||||
int connectWifi(String ssid, String pass);
|
||||
|
||||
void handleRoot();
|
||||
void handleWifi(boolean scan);
|
||||
void handleWifiSave();
|
||||
void handleNotFound();
|
||||
void handle204();
|
||||
boolean captivePortal();
|
||||
|
||||
void handleRoot();
|
||||
void handleWifi(bool scan);
|
||||
void handleWifiSave();
|
||||
void handleNotFound();
|
||||
void handle204();
|
||||
boolean captivePortal();
|
||||
|
||||
// DNS server
|
||||
const byte DNS_PORT = 53;
|
||||
const byte DNS_PORT = 53;
|
||||
|
||||
//helpers
|
||||
int getRSSIasQuality(int RSSI);
|
||||
boolean isIp(String str);
|
||||
String toStringIp(IPAddress ip);
|
||||
int getRSSIasQuality(int RSSI);
|
||||
boolean isIp(String str);
|
||||
String toStringIp(IPAddress ip);
|
||||
|
||||
boolean connect;
|
||||
boolean _debug = true;
|
||||
boolean connect;
|
||||
boolean _debug = true;
|
||||
|
||||
void (*_apcallback)(void) = NULL;
|
||||
void (*_savecallback)(void) = NULL;
|
||||
|
||||
WiFiManagerParameter* _params[WIFI_MANAGER_MAX_PARAMS];
|
||||
//int _eepromLength;
|
||||
|
||||
template <typename Generic>
|
||||
void DEBUG_WM(Generic text);
|
||||
void DEBUG_WM(Generic text);
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user