mirror of
https://github.com/alexhopeoconnor/WiFiManager.git
synced 2026-10-04 02:48:13 +10:00
added ability of returning even if connection fails
added example that resets after configuration if connection is unsuccessful
This commit is contained in:
+25
-7
@@ -108,13 +108,13 @@ boolean WiFiManager::autoConnect(char const *apName, char const *apPassword) {
|
||||
DEBUG_WM(F("AutoConnect"));
|
||||
|
||||
// read eeprom for ssid and pass
|
||||
String ssid = getSSID();
|
||||
String pass = getPassword();
|
||||
//String ssid = getSSID();
|
||||
//String pass = getPassword();
|
||||
|
||||
// attempt to connect; should it fail, fall back to AP
|
||||
WiFi.mode(WIFI_STA);
|
||||
|
||||
if (connectWifi(ssid, pass) == WL_CONNECTED) {
|
||||
if (connectWifi("", "") == WL_CONNECTED) {
|
||||
DEBUG_WM(F("IP Address:"));
|
||||
DEBUG_WM(WiFi.localIP());
|
||||
//connected
|
||||
@@ -150,7 +150,8 @@ boolean WiFiManager::startConfigPortal(char const *apName, char const *apPasswo
|
||||
connect = false;
|
||||
delay(2000);
|
||||
DEBUG_WM(F("Connecting to new AP"));
|
||||
// using user-provided _ssid, _pass in place of system-stored ssid amd pass
|
||||
|
||||
// using user-provided _ssid, _pass in place of system-stored ssid and pass
|
||||
if (connectWifi(_ssid, _pass) != WL_CONNECTED) {
|
||||
DEBUG_WM(F("Failed to connect."));
|
||||
} else {
|
||||
@@ -163,7 +164,16 @@ boolean WiFiManager::startConfigPortal(char const *apName, char const *apPasswo
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
if(_shouldBreakAfterConfig) {
|
||||
//flag set to exit after config after trying to connect
|
||||
//notify that configuration has changed and any optional parameters should be saved
|
||||
if ( _savecallback != NULL) {
|
||||
//todo: check if any custom parameters actually exist, and check if they really changed maybe
|
||||
_savecallback();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
yield();
|
||||
}
|
||||
@@ -185,8 +195,13 @@ int WiFiManager::connectWifi(String ssid, String pass) {
|
||||
DEBUG_WM(WiFi.localIP());
|
||||
}
|
||||
|
||||
|
||||
WiFi.begin(ssid.c_str(), pass.c_str());
|
||||
if(ssid !="" && pass != "") {
|
||||
WiFi.begin(ssid.c_str(), pass.c_str());
|
||||
} else {
|
||||
DEBUG_WM("Using last saved values, should be faster");
|
||||
WiFi.begin();
|
||||
}
|
||||
|
||||
int connRes = WiFi.waitForConnectResult();
|
||||
DEBUG_WM ("Connection result: ");
|
||||
DEBUG_WM ( connRes );
|
||||
@@ -285,6 +300,9 @@ void WiFiManager::setMinimumSignalQuality(int quality) {
|
||||
_minimumQuality = quality;
|
||||
}
|
||||
|
||||
void WiFiManager::setBreakAfterConfig(boolean shouldBreak) {
|
||||
_shouldBreakAfterConfig = shouldBreak;
|
||||
}
|
||||
|
||||
/** Handle root or redirect to captive portal */
|
||||
void WiFiManager::handleRoot() {
|
||||
|
||||
@@ -92,6 +92,8 @@ class WiFiManager
|
||||
void setSaveConfigCallback( void (*func)(void) );
|
||||
//adds a custom parameter
|
||||
void addParameter(WiFiManagerParameter *p);
|
||||
//if this is set, it will exit after config, even if connection is unsucessful.
|
||||
void setBreakAfterConfig(boolean shouldBreak);
|
||||
|
||||
private:
|
||||
std::unique_ptr<DNSServer> dnsServer;
|
||||
@@ -119,6 +121,7 @@ class WiFiManager
|
||||
IPAddress _sta_static_sn;
|
||||
int _paramsCount = 0;
|
||||
int _minimumQuality = -1;
|
||||
boolean _shouldBreakAfterConfig = false;
|
||||
|
||||
//String getEEPROMString(int start, int len);
|
||||
//void setEEPROMString(int start, int len, String string);
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
#include <FS.h> //this needs to be first, or it all crashes and burns...
|
||||
|
||||
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
|
||||
|
||||
//needed for library
|
||||
#include <DNSServer.h>
|
||||
#include <ESP8266WebServer.h>
|
||||
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
|
||||
|
||||
void setup() {
|
||||
// put your setup code here, to run once:
|
||||
Serial.begin(115200);
|
||||
Serial.println();
|
||||
|
||||
//WiFiManager
|
||||
//Local intialization. Once its business is done, there is no need to keep it around
|
||||
WiFiManager wifiManager;
|
||||
|
||||
//exit after config instead of connecting
|
||||
wifiManager.setBreakAfterConfig(true);
|
||||
|
||||
//reset settings - for testing
|
||||
//wifiManager.resetSettings();
|
||||
|
||||
|
||||
//tries to connect to last known settings
|
||||
//if it does not connect it starts an access point with the specified name
|
||||
//here "AutoConnectAP" with password "password"
|
||||
//and goes into a blocking loop awaiting configuration
|
||||
if (!wifiManager.autoConnect("AutoConnectAP", "password")) {
|
||||
Serial.println("failed to connect, we should reset as see if it connects");
|
||||
delay(3000);
|
||||
ESP.reset();
|
||||
delay(5000);
|
||||
}
|
||||
|
||||
//if you get here you have connected to the WiFi
|
||||
Serial.println("connected...yeey :)");
|
||||
|
||||
|
||||
Serial.println("local ip");
|
||||
Serial.println(WiFi.localIP());
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// put your main code here, to run repeatedly:
|
||||
|
||||
|
||||
}
|
||||
+12
-1
@@ -7,6 +7,7 @@
|
||||
#######################################
|
||||
|
||||
WiFiManager KEYWORD1
|
||||
WiFiManagerParameter KEYWORD1
|
||||
|
||||
|
||||
#######################################
|
||||
@@ -15,12 +16,22 @@ WiFiManager KEYWORD1
|
||||
autoConnect KEYWORD2
|
||||
getSSID KEYWORD2
|
||||
getPassword KEYWORD2
|
||||
getConfigPortalSSID KEYWORD2
|
||||
resetSettings KEYWORD2
|
||||
urldecode KEYWORD2
|
||||
setTimeout KEYWORD2
|
||||
setDebugOutput KEYWORD2
|
||||
setAPConfig KEYWORD2
|
||||
setMinimumSignalQuality KEYWORD2
|
||||
setAPStaticIPConfig KEYWORD2
|
||||
setSTAStaticIPConfig KEYWORD2
|
||||
setAPCallback KEYWORD2
|
||||
setSaveConfigCallback KEYWORD2
|
||||
addParameter KEYWORD2
|
||||
|
||||
getID KEYWORD2
|
||||
getValue KEYWORD2
|
||||
getPlaceholder KEYWORD2
|
||||
getValueLength KEYWORD2
|
||||
|
||||
#######################################
|
||||
# Constants (LITERAL1)
|
||||
|
||||
Reference in New Issue
Block a user