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

Commit

Permalink
Add DNS field (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
maakbaas authored Nov 18, 2020
1 parent 51805cf commit 7f12ab2
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 106 deletions.
2 changes: 1 addition & 1 deletion docs/wifi-manager.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Returns the SSID to which the ESP8266 is connected. Returns an empty string if a

```c++
void setNewWifi(String newSSID, String newPass);
void setNewWifi(String newSSID, String newPass, String newIp, String newSub, String newGw);
void setNewWifi(String newSSID, String newPass, String newIp, String newSub, String newGw, String newDns);
```
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.
Expand Down
6 changes: 5 additions & 1 deletion gui/js/comp/WifiPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ export function WifiPage(props) {
if (dhcpForm) {
fetch(`${props.API}/api/wifi/set?ssid=${escape(document.getElementById("ssid").value.trim())}&pass=${escape(document.getElementById("pass").value.trim())}`, { method: "POST" });
} else {
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" });
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" });
document.getElementById("ip").value = "";
document.getElementById("gw").value = "";
document.getElementById("sub").value = "";
document.getElementById("dns").value = "";
setDhcpForm(true);
}
document.getElementById("ssid").value = "";
Expand All @@ -47,6 +48,9 @@ export function WifiPage(props) {
<p><label htmlFor="gw"><CornerDownRight /> Gateway:</label>
<input type="text" id="gw" name="gw" autoCapitalize="none" />
</p>
<p><label htmlFor="dns"><CornerDownRight /> DNS:</label>
<input type="text" id="dns" name="dns" autoCapitalize="none" />
</p>
</>;
}

Expand Down
12 changes: 8 additions & 4 deletions src/WiFiManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ void WifiManager::begin(char const *apName)
ip = IPAddress(configManager.internal.ip);
gw = IPAddress(configManager.internal.gw);
sub = IPAddress(configManager.internal.sub);
dns = IPAddress(configManager.internal.dns);

if (ip.isSet() && gw.isSet() && sub.isSet())
if (ip.isSet() || gw.isSet() || sub.isSet() || dns.isSet())
{
Serial.println(PSTR("Using static IP"));
WiFi.config(ip, gw, sub);
WiFi.config(ip, gw, sub, dns);
}

if (WiFi.SSID() != "")
Expand Down Expand Up @@ -60,6 +61,7 @@ void WifiManager::forget()
ip = IPAddress();
sub = IPAddress();
gw = IPAddress();
dns = IPAddress();

//make EEPROM empty
storeToEEPROM();
Expand All @@ -75,18 +77,20 @@ void WifiManager::setNewWifi(String newSSID, String newPass)
ip = IPAddress();
sub = IPAddress();
gw = IPAddress();
dns = IPAddress();

reconnect = true;
}

//function to request a connection to new WiFi credentials
void WifiManager::setNewWifi(String newSSID, String newPass, String newIp, String newSub, String newGw)
void WifiManager::setNewWifi(String newSSID, String newPass, String newIp, String newSub, String newGw, String newDns)
{
ssid = newSSID;
pass = newPass;
ip.fromString(newIp);
sub.fromString(newSub);
gw.fromString(newGw);
dns.fromString(newDns);

reconnect = true;
}
Expand All @@ -97,7 +101,7 @@ void WifiManager::connectNewWifi(String newSSID, String newPass)
delay(1000);

//set static IP or zeros if undefined
WiFi.config(ip, gw, sub);
WiFi.config(ip, gw, sub, dns);

//fix for auto connect racing issue
if (!(WiFi.status() == WL_CONNECTED && (WiFi.SSID() == newSSID)) || ip.v4() != configManager.internal.ip)
Expand Down
3 changes: 2 additions & 1 deletion src/WiFiManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class WifiManager
IPAddress ip;
IPAddress gw;
IPAddress sub;
IPAddress dns;
bool reconnect = false;
bool inCaptivePortal = false;
char const *captivePortalName;
Expand All @@ -31,7 +32,7 @@ public :
bool isCaptivePortal();
String SSID();
void setNewWifi(String newSSID, String newPass);
void setNewWifi(String newSSID, String newPass, String newIp, String newSub, String newGw);
void setNewWifi(String newSSID, String newPass, String newIp, String newSub, String newGw, String newDns);
};

extern WifiManager WiFiManager;
Expand Down
1 change: 1 addition & 0 deletions src/configManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ struct internalData
uint32_t ip;
uint32_t gw;
uint32_t sub;
uint32_t dns;
};

class config
Expand Down
196 changes: 98 additions & 98 deletions src/generated/html.h

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/webServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void webServer::bindAll()
//update WiFi details with static IP
server.on(PSTR("/api/wifi/setStatic"), HTTP_POST, [](AsyncWebServerRequest *request) {
request->send(200, PSTR("text/html"), ""); //respond first because of wifi change
WiFiManager.setNewWifi(request->arg("ssid"), request->arg("pass"), request->arg("ip"), request->arg("sub"), request->arg("gw"));
WiFiManager.setNewWifi(request->arg("ssid"), request->arg("pass"), request->arg("ip"), request->arg("sub"), request->arg("gw"), request->arg("dns"));
});

//update WiFi details
Expand Down

0 comments on commit 7f12ab2

Please sign in to comment.