From 81045e643346232999af44aee4a63e734900e199 Mon Sep 17 00:00:00 2001 From: Deb Date: Thu, 15 Mar 2018 14:25:15 -0400 Subject: [PATCH] Added Custom DNS, fix newlines --- WiFiManager.cpp | 31 +++++++++++++++++++++++++++++-- WiFiManager.h | 3 +++ strings_en.h | 2 ++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/WiFiManager.cpp b/WiFiManager.cpp index fe57861..d91bace 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -586,8 +586,14 @@ uint8_t WiFiManager::connectWifi(String ssid, String pass) { // Setup static IP config if provided if (_sta_static_ip) { - WiFi.config(_sta_static_ip, _sta_static_gw, _sta_static_sn); - DEBUG_WM(DEBUG_VERBOSE,F("Custom STA IP/GW/Subnet:"),WiFi.localIP()); + if(_sta_static_dns) { + DEBUG_WM(DEBUG_VERBOSE,F("Custom STA IP/GW/Subnet/DNS")); + WiFi.config(_sta_static_ip, _sta_static_gw, _sta_static_sn, _sta_static_dns); + } else { + DEBUG_WM(DEBUG_VERBOSE,F("Custom STA IP/GW/Subnet")); + WiFi.config(_sta_static_ip, _sta_static_gw, _sta_static_sn); + } + DEBUG_WM(WiFi.localIP()); } // make sure sta is on before `begin` so it does not call enablesta->mode while persistent is ON ( which would save WM AP state to eeprom !) @@ -952,6 +958,7 @@ String WiFiManager::getStaticOut(){ // WiFi.gatewayIP().toString(); page += getIpForm(FPSTR(S_sn),FPSTR(S_subnet),(_sta_static_sn ? _sta_static_sn.toString() : "")); // @token subnet // WiFi.subnetMask().toString(); + if(_sta_static_dns) page += getIpForm(FPSTR(S_dns),FPSTR(S_staticdns),(_sta_static_dns ? _sta_static_dns.toString() : "")); // @token DNS page += FPSTR(HTTP_BR); // @todo remove these, use css } return page; @@ -1061,6 +1068,11 @@ void WiFiManager::handleWifiSave() { optionalIPFromString(&_sta_static_sn, sn.c_str()); DEBUG_WM(DEBUG_DEV,F("static netmask:"),sn); } + if (server->arg(FPSTR(S_dns)) != "") { + String dns = server->arg(FPSTR(S_dns)); + optionalIPFromString(&_sta_static_dns, dns.c_str()); + DEBUG_WM(DEBUG_DEV,F("static DNS:"),dns); + } String page = getHTTPHead(FPSTR(S_titlewifisaved)); // @token titlewifisaved page += FPSTR(HTTP_SAVED); @@ -1664,6 +1676,21 @@ void WiFiManager::setSTAStaticIPConfig(IPAddress ip, IPAddress gw, IPAddress sn) _sta_static_sn = sn; } +/** + * [setSTAStaticIPConfig description] + * @access public + * @param {[type]} IPAddress ip [description] + * @param {[type]} IPAddress gw [description] + * @param {[type]} IPAddress sn [description] + * @param {[type]} IPAddress dns [description] + */ +void WiFiManager::setSTAStaticIPConfig(IPAddress ip, IPAddress gw, IPAddress sn, IPAddress dns) { + _sta_static_ip = ip; + _sta_static_gw = gw; + _sta_static_sn = sn; + _sta_static_dns = dns; +} + /** * [setMinimumSignalQuality description] * @access public diff --git a/WiFiManager.h b/WiFiManager.h index 32f966e..e5509c2 100644 --- a/WiFiManager.h +++ b/WiFiManager.h @@ -174,6 +174,8 @@ class WiFiManager void setAPStaticIPConfig(IPAddress ip, IPAddress gw, IPAddress sn); //sets config for a static IP void setSTAStaticIPConfig(IPAddress ip, IPAddress gw, IPAddress sn); + //sets config for a static IP with DNS + void setSTAStaticIPConfig(IPAddress ip, IPAddress gw, IPAddress sn, IPAddress dns); //called when AP mode and config portal is started void setAPCallback( void (*func)(WiFiManager*) ); //called when settings have been changed and connection was successful @@ -246,6 +248,7 @@ class WiFiManager IPAddress _sta_static_ip; IPAddress _sta_static_gw; IPAddress _sta_static_sn; + IPAddress _sta_static_dns; // defaults const byte DNS_PORT = 53; diff --git a/strings_en.h b/strings_en.h index b3468b8..c0f752a 100644 --- a/strings_en.h +++ b/strings_en.h @@ -186,6 +186,7 @@ const char S_options[] PROGMEM = "options"; const char S_nonetworks[] PROGMEM = "No networks found. Refresh to scan again."; const char S_staticip[] PROGMEM = "Static IP"; const char S_staticgw[] PROGMEM = "Static Gateway"; +const char S_staticdns[] PROGMEM = "Static DNS"; const char S_subnet[] PROGMEM = "Subnet"; const char S_exiting[] PROGMEM = "Exiting"; const char S_resetting[] PROGMEM = "Module will reset in a few seconds."; @@ -223,6 +224,7 @@ const char R_status[] PROGMEM = "/status"; const char S_ip[] PROGMEM = "ip"; const char S_gw[] PROGMEM = "gw"; const char S_sn[] PROGMEM = "sn"; +const char S_dns[] PROGMEM = "dns"; // softap ssid default prefix #ifdef ESP8266