Some hostname testing

This commit is contained in:
tablatronix
2020-05-08 23:31:02 -05:00
parent 293f705b0f
commit 5aebb1dd51
4 changed files with 135 additions and 81 deletions
+92 -80
View File
@@ -247,99 +247,111 @@ boolean WiFiManager::autoConnect(char const *apName, char const *apPassword) {
DEBUG_WM(F("AutoConnect"));
if(getWiFiIsSaved()){
_begin();
_begin();
// 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();
// 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();
#ifdef ESP32
if(esp32persistent) WiFi.persistent(false); // disable persistent for esp32 after esp_wifi_start or else saves wont work
#endif
_usermode = WIFI_STA; // When using autoconnect , assume the user wants sta mode on permanently.
// 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
@@ -479,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