mirror of
https://github.com/alexhopeoconnor/WiFiManager.git
synced 2026-10-04 02:48:13 +10:00
Merge branch 'development' into ota
This commit is contained in:
+104
-86
@@ -216,7 +216,7 @@ WiFiManager::~WiFiManager() {
|
||||
void WiFiManager::_begin(){
|
||||
if(_hasBegun) return;
|
||||
_hasBegun = true;
|
||||
_usermode = WiFi.getMode();
|
||||
// _usermode = WiFi.getMode();
|
||||
|
||||
#ifndef ESP32
|
||||
WiFi.persistent(false); // disable persistent so scannetworks and mode switching do not cause overwrites
|
||||
@@ -245,97 +245,113 @@ boolean WiFiManager::autoConnect() {
|
||||
*/
|
||||
boolean WiFiManager::autoConnect(char const *apName, char const *apPassword) {
|
||||
DEBUG_WM(F("AutoConnect"));
|
||||
_begin();
|
||||
if(getWiFiIsSaved()){
|
||||
|
||||
// attempt to connect using saved settings, on fail fallback to AP config portal
|
||||
if(!WiFi.enableSTA(true)){
|
||||
// handle failure mode Brownout detector etc.
|
||||
DEBUG_WM(DEBUG_ERROR,"[FATAL] Unable to enable wifi!");
|
||||
return false;
|
||||
}
|
||||
|
||||
WiFiSetCountry();
|
||||
_begin();
|
||||
|
||||
#ifdef ESP32
|
||||
if(esp32persistent) WiFi.persistent(false); // disable persistent for esp32 after esp_wifi_start or else saves wont work
|
||||
#endif
|
||||
// attempt to connect using saved settings, on fail fallback to AP config portal
|
||||
if(!WiFi.enableSTA(true)){
|
||||
// handle failure mode Brownout detector etc.
|
||||
DEBUG_WM(DEBUG_ERROR,"[FATAL] Unable to enable wifi!");
|
||||
return false;
|
||||
}
|
||||
|
||||
WiFiSetCountry();
|
||||
|
||||
_usermode = WIFI_STA;
|
||||
|
||||
// no getter for autoreconnectpolicy before this
|
||||
// https://github.com/esp8266/Arduino/pull/4359
|
||||
// so we must force it on else, if not connectimeout then waitforconnectionresult gets stuck endless loop
|
||||
WiFi_autoReconnect();
|
||||
|
||||
// set hostname before stating
|
||||
if((String)_hostname != ""){
|
||||
DEBUG_WM(DEBUG_VERBOSE,"Setting hostname:",_hostname);
|
||||
bool res = true;
|
||||
#ifdef ESP8266
|
||||
res = WiFi.hostname(_hostname);
|
||||
#ifdef ESP8266MDNS_H
|
||||
DEBUG_WM(DEBUG_VERBOSE,"Setting MDNS hostname");
|
||||
if(MDNS.begin(_hostname)){
|
||||
MDNS.addService("http", "tcp", 80);
|
||||
}
|
||||
#endif
|
||||
#elif defined(ESP32)
|
||||
// @note hostname must be set after STA_START
|
||||
delay(200); // do not remove, give time for STA_START
|
||||
res = WiFi.setHostname(_hostname);
|
||||
#ifdef ESP32MDNS_H
|
||||
DEBUG_WM(DEBUG_VERBOSE,"Setting MDNS hostname");
|
||||
if(MDNS.begin(_hostname)){
|
||||
MDNS.addService("http", "tcp", 80);
|
||||
}
|
||||
#endif
|
||||
#ifdef ESP32
|
||||
if(esp32persistent) WiFi.persistent(false); // disable persistent for esp32 after esp_wifi_start or else saves wont work
|
||||
#endif
|
||||
|
||||
if(!res)DEBUG_WM(DEBUG_ERROR,F("[ERROR] hostname: set failed!"));
|
||||
|
||||
if(WiFi.status() == WL_CONNECTED){
|
||||
DEBUG_WM(DEBUG_VERBOSE,F("reconnecting to set new hostname"));
|
||||
// WiFi.reconnect(); // This does not reset dhcp
|
||||
WiFi_Disconnect();
|
||||
delay(200); // do not remove, need a delay for disconnect to change status()
|
||||
}
|
||||
}
|
||||
|
||||
// if already connected, or try stored connect
|
||||
// @note @todo ESP32 has no autoconnect, so connectwifi will always be called unless user called begin etc before
|
||||
// @todo check if correct ssid == saved ssid when already connected
|
||||
bool connected = false;
|
||||
if (WiFi.status() == WL_CONNECTED){
|
||||
connected = true;
|
||||
DEBUG_WM(F("AutoConnect: ESP Already Connected"));
|
||||
setSTAConfig();
|
||||
}
|
||||
_usermode = WIFI_STA; // When using autoconnect , assume the user wants sta mode on permanently.
|
||||
|
||||
if(connected || connectWifi("", "") == WL_CONNECTED){
|
||||
//connected
|
||||
DEBUG_WM(F("AutoConnect: SUCCESS"));
|
||||
DEBUG_WM(F("STA IP Address:"),WiFi.localIP());
|
||||
_lastconxresult = WL_CONNECTED;
|
||||
// no getter for autoreconnectpolicy before this
|
||||
// https://github.com/esp8266/Arduino/pull/4359
|
||||
// so we must force it on else, if not connectimeout then waitforconnectionresult gets stuck endless loop
|
||||
WiFi_autoReconnect();
|
||||
|
||||
// set hostname before stating
|
||||
if((String)_hostname != ""){
|
||||
#ifdef ESP8266
|
||||
DEBUG_WM(DEBUG_DEV,"hostname: STA",WiFi.hostname());
|
||||
#elif defined(ESP32)
|
||||
DEBUG_WM(DEBUG_DEV,"hostname: STA",WiFi.getHostname());
|
||||
#endif
|
||||
setupHostname(true);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// possibly skip the config portal
|
||||
if (!_enableConfigPortal) {
|
||||
return false;
|
||||
}
|
||||
// if already connected, or try stored connect
|
||||
// @note @todo ESP32 has no autoconnect, so connectwifi will always be called unless user called begin etc before
|
||||
// @todo check if correct ssid == saved ssid when already connected
|
||||
bool connected = false;
|
||||
if (WiFi.status() == WL_CONNECTED){
|
||||
connected = true;
|
||||
DEBUG_WM(F("AutoConnect: ESP Already Connected"));
|
||||
setSTAConfig();
|
||||
}
|
||||
|
||||
DEBUG_WM(F("AutoConnect: FAILED"));
|
||||
if(connected || connectWifi("", "") == WL_CONNECTED){
|
||||
//connected
|
||||
DEBUG_WM(F("AutoConnect: SUCCESS"));
|
||||
DEBUG_WM(F("STA IP Address:"),WiFi.localIP());
|
||||
_lastconxresult = WL_CONNECTED;
|
||||
|
||||
if((String)_hostname != ""){
|
||||
#ifdef ESP8266
|
||||
DEBUG_WM(DEBUG_DEV,"hostname: STA",WiFi.hostname());
|
||||
#elif defined(ESP32)
|
||||
DEBUG_WM(DEBUG_DEV,"hostname: STA",WiFi.getHostname());
|
||||
#endif
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// possibly skip the config portal
|
||||
if (!_enableConfigPortal) {
|
||||
return false;
|
||||
}
|
||||
|
||||
DEBUG_WM(F("AutoConnect: FAILED"));
|
||||
}
|
||||
else DEBUG_WM(F("No Credentials are Saved, skipping connect"));
|
||||
|
||||
// not connected start configportal
|
||||
return startConfigPortal(apName, apPassword);
|
||||
bool res = startConfigPortal(apName, apPassword);
|
||||
return res;
|
||||
}
|
||||
|
||||
bool WiFiManager::setupHostname(bool restart){
|
||||
DEBUG_WM(DEBUG_VERBOSE,"Setting hostname:",_hostname);
|
||||
if((String)_hostname == "") {
|
||||
DEBUG_WM(DEBUG_VERBOSE,"No Hostname to set");
|
||||
return false;
|
||||
}
|
||||
bool res = true;
|
||||
#ifdef ESP8266
|
||||
res = WiFi.hostname(_hostname);
|
||||
#ifdef ESP8266MDNS_H
|
||||
DEBUG_WM(DEBUG_VERBOSE,"Setting MDNS hostname");
|
||||
if(MDNS.begin(_hostname)){
|
||||
MDNS.addService("http", "tcp", 80);
|
||||
}
|
||||
#endif
|
||||
#elif defined(ESP32)
|
||||
// @note hostname must be set after STA_START
|
||||
delay(200); // do not remove, give time for STA_START
|
||||
res = WiFi.setHostname(_hostname);
|
||||
#ifdef ESP32MDNS_H
|
||||
DEBUG_WM(DEBUG_VERBOSE,"Setting MDNS hostname");
|
||||
if(MDNS.begin(_hostname)){
|
||||
MDNS.addService("http", "tcp", 80);
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
if(!res)DEBUG_WM(DEBUG_ERROR,F("[ERROR] hostname: set failed!"));
|
||||
|
||||
// in sta mode restart , not sure about softap
|
||||
if(restart && (WiFi.status() == WL_CONNECTED)){
|
||||
DEBUG_WM(DEBUG_VERBOSE,F("reconnecting to set new hostname"));
|
||||
// WiFi.reconnect(); // This does not reset dhcp
|
||||
WiFi_Disconnect();
|
||||
delay(200); // do not remove, need a delay for disconnect to change status()
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
// CONFIG PORTAL
|
||||
@@ -475,7 +491,7 @@ void WiFiManager::setupConfigPortal() {
|
||||
/* Setup the DNS server redirecting all the domains to the apIP */
|
||||
dnsServer->setErrorReplyCode(DNSReplyCode::NoError);
|
||||
// DEBUG_WM("dns server started port: ",DNS_PORT);
|
||||
DEBUG_WM(DEBUG_DEV,"dns server started with ip: ",WiFi.softAPIP());
|
||||
DEBUG_WM(DEBUG_DEV,"dns server started with ip: ",WiFi.softAPIP()); // @todo not showing ip
|
||||
dnsServer->start(DNS_PORT, F("*"), WiFi.softAPIP());
|
||||
|
||||
// @todo new callback, webserver started, callback cannot override handlers, but can grab them first
|
||||
@@ -623,7 +639,7 @@ uint8_t WiFiManager::processConfigPortal(){
|
||||
// Waiting for save...
|
||||
if(connect) {
|
||||
connect = false;
|
||||
DEBUG_WM(DEBUG_VERBOSE,F("process connect"));
|
||||
DEBUG_WM(DEBUG_VERBOSE,F("processing save"));
|
||||
if(_enableCaptivePortal) delay(_cpclosedelay); // keeps the captiveportal from closing to fast.
|
||||
|
||||
// skip wifi if no ssid
|
||||
@@ -648,10 +664,12 @@ uint8_t WiFiManager::processConfigPortal(){
|
||||
}
|
||||
|
||||
if (_shouldBreakAfterConfig) {
|
||||
|
||||
// do save callback
|
||||
// @todo this is more of an exiting callback than a save, clarify when this should actually occur
|
||||
// confirm or verify data was saved to make this more accurate callback
|
||||
if ( _savewificallback != NULL) {
|
||||
DEBUG_WM(DEBUG_VERBOSE,F("WiFi/Param save callback"));
|
||||
_savewificallback();
|
||||
}
|
||||
shutdownConfigPortal();
|
||||
@@ -750,7 +768,7 @@ uint8_t WiFiManager::connectWifi(String ssid, String pass) {
|
||||
connRes = waitForConnectResult();
|
||||
}
|
||||
else {
|
||||
DEBUG_WM(F("No saved credentials, skipping wifi"));
|
||||
DEBUG_WM(F("No wifi save required, skipping"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -941,7 +959,7 @@ void WiFiManager::handleRoot() {
|
||||
DEBUG_WM(DEBUG_VERBOSE,F("<- HTTP Root"));
|
||||
if (captivePortal()) return; // If captive portal redirect instead of displaying the page
|
||||
handleRequest();
|
||||
String page = getHTTPHead(FPSTR(S_options)); // @token options
|
||||
String page = getHTTPHead(FPSTR(S_options)); // @token options @todo replace options with title
|
||||
String str = FPSTR(HTTP_ROOT_MAIN);
|
||||
str.replace(FPSTR(T_v),configPortalActive ? _apName : WiFi.localIP().toString()); // use ip if ap is not active for heading
|
||||
page += str;
|
||||
@@ -1939,7 +1957,6 @@ bool WiFiManager::erase(bool opt){
|
||||
// if opt true, do nvs erase
|
||||
if(opt){
|
||||
DEBUG_WM("Erasing NVS");
|
||||
int err;
|
||||
esp_err_t err;
|
||||
err = nvs_flash_init();
|
||||
DEBUG_WM(DEBUG_VERBOSE,"nvs_flash_init: ",err!=ESP_OK ? (String)err : "Success");
|
||||
@@ -1971,7 +1988,7 @@ bool WiFiManager::erase(bool opt){
|
||||
* @access public
|
||||
*/
|
||||
void WiFiManager::resetSettings() {
|
||||
DEBUG_WM(F("SETTINGS ERASED"));
|
||||
DEBUG_WM(F("resetSettings"));
|
||||
WiFi_enableSTA(true,true); // must be sta to disconnect erase
|
||||
|
||||
if (_resetcallback != NULL)
|
||||
@@ -1983,7 +2000,8 @@ void WiFiManager::resetSettings() {
|
||||
WiFi.persistent(true);
|
||||
WiFi.disconnect(true);
|
||||
WiFi.persistent(false);
|
||||
#endif
|
||||
#endif
|
||||
DEBUG_WM(F("SETTINGS ERASED"));
|
||||
}
|
||||
|
||||
// SETTERS
|
||||
|
||||
+3
-2
@@ -336,7 +336,7 @@ class WiFiManager
|
||||
unsigned long _saveTimeout = 0; // ms stop trying to connect to ap on saves, in case bugs in esp waitforconnectresult
|
||||
unsigned long _configPortalStart = 0; // ms config portal start time (updated for timeouts)
|
||||
unsigned long _webPortalAccessed = 0; // ms last web access time
|
||||
WiFiMode_t _usermode = WIFI_OFF;
|
||||
WiFiMode_t _usermode = WIFI_STA; // Default user mode
|
||||
String _wifissidprefix = FPSTR(S_ssidpre); // auto apname prefix prefix+chipid
|
||||
uint8_t _lastconxresult = WL_IDLE_STATUS;
|
||||
int _numNetworks = 0;
|
||||
@@ -397,7 +397,8 @@ class WiFiManager
|
||||
|
||||
void setupConfigPortal();
|
||||
bool shutdownConfigPortal();
|
||||
|
||||
bool setupHostname(bool restart);
|
||||
|
||||
#ifdef NO_EXTRA_4K_HEAP
|
||||
boolean _tryWPS = false; // try WPS on save failure, unsupported
|
||||
void startWPS();
|
||||
|
||||
@@ -170,7 +170,7 @@ void setup() {
|
||||
std::vector<const char *> menu = {"wifi","wifinoscan","info","param","close","sep","erase","restart","exit"};
|
||||
// wm.setMenu(menu); // custom menu, pass vector
|
||||
|
||||
wm.setParamsPage(true); // move params to seperate page, not wifi, do not combine with setmenu!
|
||||
// wm.setParamsPage(true); // move params to seperate page, not wifi, do not combine with setmenu!
|
||||
|
||||
// set static sta ip
|
||||
// wm.setSTAStaticIPConfig(IPAddress(10,0,1,99), IPAddress(10,0,1,1), IPAddress(255,255,255,0));
|
||||
@@ -204,7 +204,7 @@ void setup() {
|
||||
|
||||
// wm.setCleanConnect(true); // disconenct before connect, clean connect
|
||||
|
||||
// wm.setBreakAfterConfig(true);
|
||||
wm.setBreakAfterConfig(true);
|
||||
|
||||
//fetches ssid and pass and tries to connect
|
||||
//if it does not connect it starts an access point with the specified name
|
||||
@@ -212,7 +212,7 @@ void setup() {
|
||||
//and goes into a blocking loop awaiting configuration
|
||||
wifiInfo();
|
||||
print_oled(F("Connecting..."),2);
|
||||
if(!AUTOSTARTCP || !wm.autoConnect("WM_AutoConnectAP")) {
|
||||
if(!wm.autoConnect("WM_AutoConnectAP","12345678")) {
|
||||
Serial.println("failed to connect and hit timeout");
|
||||
print_oled("Not Connected",2);
|
||||
}
|
||||
|
||||
@@ -6,6 +6,13 @@
|
||||
*/
|
||||
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
|
||||
|
||||
// include MDNS
|
||||
#ifdef ESP8266
|
||||
#include <ESP8266mDNS.h>
|
||||
#elif defined(ESP32)
|
||||
#include <ESPmDNS.h>
|
||||
#endif
|
||||
|
||||
// select which pin will trigger the configuration portal when set to LOW
|
||||
#define TRIGGER_PIN 0
|
||||
|
||||
@@ -20,11 +27,21 @@ void setup() {
|
||||
WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP
|
||||
// put your setup code here, to run once
|
||||
Serial.begin(115200);
|
||||
Serial.setDebugOutput(true);
|
||||
delay(1000);
|
||||
Serial.println("\n Starting");
|
||||
|
||||
pinMode(TRIGGER_PIN, INPUT_PULLUP);
|
||||
|
||||
// wm.resetSettings();
|
||||
wm.setHostname("MDNSEXAMPLE");
|
||||
// wm.setEnableConfigPortal(false);
|
||||
// wm.setConfigPortalBlocking(false);
|
||||
wm.autoConnect();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
MDNS.update();
|
||||
doWiFiManager();
|
||||
// put your main code here, to run repeatedly:
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
|
||||
|
||||
bool _enteredConfigMode = false;
|
||||
|
||||
void setup(){
|
||||
Serial.begin(115200);
|
||||
WiFiManager wifiManager;
|
||||
|
||||
// wifiManager.setAPCallback([this](WiFiManager* wifiManager) {
|
||||
wifiManager.setAPCallback([&](WiFiManager* wifiManager) {
|
||||
Serial.printf("Entered config mode:ip=%s, ssid='%s'\n",
|
||||
WiFi.softAPIP().toString().c_str(),
|
||||
wifiManager->getConfigPortalSSID().c_str());
|
||||
_enteredConfigMode = true;
|
||||
});
|
||||
wifiManager.resetSettings();
|
||||
if (!wifiManager.autoConnect()) {
|
||||
Serial.printf("*** Failed to connect and hit timeout\n");
|
||||
ESP.reset();
|
||||
delay(1000);
|
||||
}
|
||||
}
|
||||
|
||||
void loop(){}
|
||||
+2
-1
@@ -25,7 +25,8 @@ const char HTTP_SCRIPT[] PROGMEM = "<script>function c(l){"
|
||||
"if(p)document.getElementById('p').focus();}</script>";
|
||||
|
||||
const char HTTP_HEAD_END[] PROGMEM = "</head><body class='{c}'><div class='wrap'>";
|
||||
|
||||
// example of embedded logo, base64 encoded inline, No styling here
|
||||
// const char HTTP_ROOT_MAIN[] PROGMEM = "<img title=' alt=' src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAADQElEQVRoQ+2YjW0VQQyE7Q6gAkgFkAogFUAqgFQAVACpAKiAUAFQAaECQgWECggVGH1PPrRvn3dv9/YkFOksoUhhfzwz9ngvKrc89JbnLxuA/63gpsCmwCADWwkNEji8fVNgotDM7osI/x777x5l9F6JyB8R4eeVql4P0y8yNsjM7KGIPBORp558T04A+CwiH1UVUItiUQmZ2XMReSEiAFgjAPBeVS96D+sCYGaUx4cFbLfmhSpnqnrZuqEJgJnd8cQplVLciAgX//Cf0ToIeOB9wpmloLQAwpnVmAXgdf6pwjpJIz+XNoeZQQZlODV9vhc1Tuf6owrAk/8qIhFbJH7eI3eEzsvydQEICqBEkZwiALfF70HyHPpqScPV5HFjeFu476SkRA0AzOfy4hYwstj2ZkDgaphE7m6XqnoS7Q0BOPs/sw0kDROzjdXcCMFCNwzIy0EcRcOvBACfh4k0wgOmBX4xjfmk4DKTS31hgNWIKBCI8gdzogTgjYjQWFMw+o9LzJoZ63GUmjWm2wGDc7EvDDOj/1IVMIyD9SUAL0WEhpriRlXv5je5S+U1i2N88zdPuoVkeB+ls4SyxCoP3kVm9jsjpEsBLoOBNC5U9SwpGdakFkviuFP1keblATkTENTYcxkzgxTKOI3jyDxqLkQT87pMA++H3XvJBYtsNbBN6vuXq5S737WqHkW1VgMQNXJ0RshMqbbT33sJ5kpHWymzcJjNTeJIymJZtSQd9NHQHS1vodoFoTMkfbJzpRnLzB2vi6BZAJxWaCr+62BC+jzAxVJb3dmmiLzLwZhZNPE5e880Suo2AZgB8e8idxherqUPnT3brBDTlPxO3Z66rVwIwySXugdNd+5ejhqp/+NmgIwGX3Py3QBmlEi54KlwmjkOytQ+iJrLJj23S4GkOeecg8G091no737qvRRdzE+HLALQoMTBbJgBsCj5RSWUlUVJiZ4SOljb05eLFWgoJ5oY6yTyJp62D39jDANoKKcSocPJD5dQYzlFAFZJflUArgTPZKZwLXAnHmerfJquUkKZEgyzqOb5TuDt1P3nwxobqwPocZA11m4A1mBx5IxNgRH21ti7KbAGiyNn3HoF/gJ0w05A8xclpwAAAABJRU5ErkJggg==' /><h1>{v}</h1><h3>WiFiManager</h3>";
|
||||
const char HTTP_ROOT_MAIN[] PROGMEM = "<h1>{v}</h1><h3>WiFiManager</h3>";
|
||||
const char * const HTTP_PORTAL_MENU[] PROGMEM = {
|
||||
"<form action='/wifi' method='get'><button>Configure WiFi</button></form><br/>\n", // MENU_WIFI
|
||||
|
||||
Reference in New Issue
Block a user