浏览代码

Merge pull request #39 from baggior/master

STA mode static IP DNS not set
Alan Steremberg 6 年之前
父节点
当前提交
85b094c944
共有 2 个文件被更改,包括 38 次插入4 次删除
  1. 35 3
      ESPAsyncWiFiManager.cpp
  2. 3 1
      ESPAsyncWiFiManager.h

+ 35 - 3
ESPAsyncWiFiManager.cpp

@@ -504,8 +504,8 @@ int AsyncWiFiManager::connectWifi(String ssid, String pass) {
 
   // check if we've got static_ip settings, if we do, use those.
   if (_sta_static_ip) {
-    DEBUG_WM(F("Custom STA IP/GW/Subnet"));
-    WiFi.config(_sta_static_ip, _sta_static_gw, _sta_static_sn);
+    DEBUG_WM(F("Custom STA IP/GW/Subnet/DNS"));
+    WiFi.config(_sta_static_ip, _sta_static_gw, _sta_static_sn, _sta_static_dns1, _sta_static_dns2);
     DEBUG_WM(WiFi.localIP());
   }
   //fix for auto connect racing issue
@@ -657,10 +657,12 @@ void AsyncWiFiManager::setAPStaticIPConfig(IPAddress ip, IPAddress gw, IPAddress
   _ap_static_sn = sn;
 }
 
-void AsyncWiFiManager::setSTAStaticIPConfig(IPAddress ip, IPAddress gw, IPAddress sn) {
+void AsyncWiFiManager::setSTAStaticIPConfig(IPAddress ip, IPAddress gw, IPAddress sn, IPAddress dns1, IPAddress dns2) {
   _sta_static_ip = ip;
   _sta_static_gw = gw;
   _sta_static_sn = sn;
+  _sta_static_dns1 = dns1;
+  _sta_static_dns2 = dns2;
 }
 
 void AsyncWiFiManager::setMinimumSignalQuality(int quality) {
@@ -789,6 +791,24 @@ void AsyncWiFiManager::handleWifi(AsyncWebServerRequest *request,boolean scan) {
 
     page += item;
 
+    item = FPSTR(HTTP_FORM_PARAM);
+    item.replace("{i}", "dns1");
+    item.replace("{n}", "dns1");
+    item.replace("{p}", "DNS1");
+    item.replace("{l}", "15");
+    item.replace("{v}", _sta_static_dns1.toString());
+
+    page += item;
+
+    item = FPSTR(HTTP_FORM_PARAM);
+    item.replace("{i}", "dns2");
+    item.replace("{n}", "dns2");
+    item.replace("{p}", "DNS2");
+    item.replace("{l}", "15");
+    item.replace("{v}", _sta_static_dns2.toString());
+
+    page += item;
+
     page += "<br/>";
   }
 
@@ -845,6 +865,18 @@ void AsyncWiFiManager::handleWifiSave(AsyncWebServerRequest *request) {
     String sn = request->arg("sn");
     optionalIPFromString(&_sta_static_sn, sn.c_str());
   }
+  if (request->hasArg("dns1")) {
+    DEBUG_WM(F("static DNS 1"));
+    DEBUG_WM(request->arg("dns1"));
+    String dns1 = request->arg("dns1");
+    optionalIPFromString(&_sta_static_dns1, dns1.c_str());
+  }
+  if (request->hasArg("dns2")) {
+    DEBUG_WM(F("static DNS 2"));
+    DEBUG_WM(request->arg("dns2"));
+    String dns2 = request->arg("dns2");
+    optionalIPFromString(&_sta_static_dns2, dns2.c_str());
+  }
 
   String page = FPSTR(WFM_HTTP_HEAD);
   page.replace("{v}", "Credentials Saved");

+ 3 - 1
ESPAsyncWiFiManager.h

@@ -141,7 +141,7 @@ public:
   //sets a custom ip /gateway /subnet configuration
   void          setAPStaticIPConfig(IPAddress ip, IPAddress gw, IPAddress sn);
   //sets config for a static IP
-  void          setSTAStaticIPConfig(IPAddress ip, IPAddress gw, IPAddress sn);
+  void          setSTAStaticIPConfig(IPAddress ip, IPAddress gw, IPAddress sn, IPAddress dns1=(uint32_t)0x00000000, IPAddress dns2=(uint32_t)0x00000000);
   //called when AP mode and config portal is started
   void          setAPCallback( void (*func)(AsyncWiFiManager*) );
   //called when settings have been changed and connection was successful
@@ -196,6 +196,8 @@ private:
   IPAddress     _sta_static_ip;
   IPAddress     _sta_static_gw;
   IPAddress     _sta_static_sn;
+  IPAddress     _sta_static_dns1= (uint32_t)0x00000000;
+  IPAddress     _sta_static_dns2= (uint32_t)0x00000000;
 
   int           _paramsCount            = 0;
   int           _minimumQuality         = -1;