Skip to content
This repository was archived by the owner on Feb 8, 2024. It is now read-only.

Commit 7f12ab2

Browse files
authored
Add DNS field (#55)
1 parent 51805cf commit 7f12ab2

File tree

7 files changed

+116
-106
lines changed

7 files changed

+116
-106
lines changed

docs/wifi-manager.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Returns the SSID to which the ESP8266 is connected. Returns an empty string if a
4141

4242
```c++
4343
void setNewWifi(String newSSID, String newPass);
44-
void setNewWifi(String newSSID, String newPass, String newIp, String newSub, String newGw);
44+
void setNewWifi(String newSSID, String newPass, String newIp, String newSub, String newGw, String newDns);
4545
```
4646
Tries to connect to the WiFi network with SSID `newSSID` and password `newPass`. If this fails a reconnect to the known network will be attempted. If this also fails or if no previous network was known, a captive portal will be started. Alternatively the function can also be called with inputs for a static IP address if DHCP is not available.
4747

gui/js/comp/WifiPage.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ export function WifiPage(props) {
2424
if (dhcpForm) {
2525
fetch(`${props.API}/api/wifi/set?ssid=${escape(document.getElementById("ssid").value.trim())}&pass=${escape(document.getElementById("pass").value.trim())}`, { method: "POST" });
2626
} else {
27-
fetch(`${props.API}/api/wifi/setStatic?ssid=${escape(document.getElementById("ssid").value.trim())}&pass=${escape(document.getElementById("pass").value.trim())}&ip=${escape(document.getElementById("ip").value.trim())}&sub=${escape(document.getElementById("sub").value.trim())}&gw=${escape(document.getElementById("gw").value.trim())}`, { method: "POST" });
27+
fetch(`${props.API}/api/wifi/setStatic?ssid=${escape(document.getElementById("ssid").value.trim())}&pass=${escape(document.getElementById("pass").value.trim())}&ip=${escape(document.getElementById("ip").value.trim())}&sub=${escape(document.getElementById("sub").value.trim())}&gw=${escape(document.getElementById("gw").value.trim())}&dns=${escape(document.getElementById("dns").value.trim())}`, { method: "POST" });
2828
document.getElementById("ip").value = "";
2929
document.getElementById("gw").value = "";
3030
document.getElementById("sub").value = "";
31+
document.getElementById("dns").value = "";
3132
setDhcpForm(true);
3233
}
3334
document.getElementById("ssid").value = "";
@@ -47,6 +48,9 @@ export function WifiPage(props) {
4748
<p><label htmlFor="gw"><CornerDownRight /> Gateway:</label>
4849
<input type="text" id="gw" name="gw" autoCapitalize="none" />
4950
</p>
51+
<p><label htmlFor="dns"><CornerDownRight /> DNS:</label>
52+
<input type="text" id="dns" name="dns" autoCapitalize="none" />
53+
</p>
5054
</>;
5155
}
5256

src/WiFiManager.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ void WifiManager::begin(char const *apName)
2121
ip = IPAddress(configManager.internal.ip);
2222
gw = IPAddress(configManager.internal.gw);
2323
sub = IPAddress(configManager.internal.sub);
24+
dns = IPAddress(configManager.internal.dns);
2425

25-
if (ip.isSet() && gw.isSet() && sub.isSet())
26+
if (ip.isSet() || gw.isSet() || sub.isSet() || dns.isSet())
2627
{
2728
Serial.println(PSTR("Using static IP"));
28-
WiFi.config(ip, gw, sub);
29+
WiFi.config(ip, gw, sub, dns);
2930
}
3031

3132
if (WiFi.SSID() != "")
@@ -60,6 +61,7 @@ void WifiManager::forget()
6061
ip = IPAddress();
6162
sub = IPAddress();
6263
gw = IPAddress();
64+
dns = IPAddress();
6365

6466
//make EEPROM empty
6567
storeToEEPROM();
@@ -75,18 +77,20 @@ void WifiManager::setNewWifi(String newSSID, String newPass)
7577
ip = IPAddress();
7678
sub = IPAddress();
7779
gw = IPAddress();
80+
dns = IPAddress();
7881

7982
reconnect = true;
8083
}
8184

8285
//function to request a connection to new WiFi credentials
83-
void WifiManager::setNewWifi(String newSSID, String newPass, String newIp, String newSub, String newGw)
86+
void WifiManager::setNewWifi(String newSSID, String newPass, String newIp, String newSub, String newGw, String newDns)
8487
{
8588
ssid = newSSID;
8689
pass = newPass;
8790
ip.fromString(newIp);
8891
sub.fromString(newSub);
8992
gw.fromString(newGw);
93+
dns.fromString(newDns);
9094

9195
reconnect = true;
9296
}
@@ -97,7 +101,7 @@ void WifiManager::connectNewWifi(String newSSID, String newPass)
97101
delay(1000);
98102

99103
//set static IP or zeros if undefined
100-
WiFi.config(ip, gw, sub);
104+
WiFi.config(ip, gw, sub, dns);
101105

102106
//fix for auto connect racing issue
103107
if (!(WiFi.status() == WL_CONNECTED && (WiFi.SSID() == newSSID)) || ip.v4() != configManager.internal.ip)

src/WiFiManager.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class WifiManager
1515
IPAddress ip;
1616
IPAddress gw;
1717
IPAddress sub;
18+
IPAddress dns;
1819
bool reconnect = false;
1920
bool inCaptivePortal = false;
2021
char const *captivePortalName;
@@ -31,7 +32,7 @@ public :
3132
bool isCaptivePortal();
3233
String SSID();
3334
void setNewWifi(String newSSID, String newPass);
34-
void setNewWifi(String newSSID, String newPass, String newIp, String newSub, String newGw);
35+
void setNewWifi(String newSSID, String newPass, String newIp, String newSub, String newGw, String newDns);
3536
};
3637

3738
extern WifiManager WiFiManager;

src/configManager.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ struct internalData
1313
uint32_t ip;
1414
uint32_t gw;
1515
uint32_t sub;
16+
uint32_t dns;
1617
};
1718

1819
class config

src/generated/html.h

Lines changed: 98 additions & 98 deletions
Large diffs are not rendered by default.

src/webServer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void webServer::bindAll()
4646
//update WiFi details with static IP
4747
server.on(PSTR("/api/wifi/setStatic"), HTTP_POST, [](AsyncWebServerRequest *request) {
4848
request->send(200, PSTR("text/html"), ""); //respond first because of wifi change
49-
WiFiManager.setNewWifi(request->arg("ssid"), request->arg("pass"), request->arg("ip"), request->arg("sub"), request->arg("gw"));
49+
WiFiManager.setNewWifi(request->arg("ssid"), request->arg("pass"), request->arg("ip"), request->arg("sub"), request->arg("gw"), request->arg("dns"));
5050
});
5151

5252
//update WiFi details

0 commit comments

Comments
 (0)