Skip to content

Commit b4d483d

Browse files
chadoumingigrr
authored andcommitted
Add function overload to config to include DNS
Disable DHCP if using static IP With dhcp disabled, I noticed about 50% less conncection time
1 parent 580a602 commit b4d483d

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

hardware/esp8266com/esp8266/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ extern "C" void esp_yield();
4040
ESP8266WiFiClass::ESP8266WiFiClass()
4141
: _useApMode(false)
4242
, _useClientMode(false)
43+
, _useStaticIp(false)
4344
{
4445
}
4546

@@ -103,7 +104,8 @@ int ESP8266WiFiClass::begin(const char* ssid, const char *passphrase, int32_t ch
103104
wifi_set_channel(channel);
104105
}
105106

106-
wifi_station_dhcpc_start();
107+
if(!_useStaticIp)
108+
wifi_station_dhcpc_start();
107109
return status();
108110
}
109111

@@ -115,6 +117,8 @@ uint8_t ESP8266WiFiClass::waitForConnectResult(){
115117
return status();
116118
}
117119

120+
121+
// You will have to set the DNS-Server manually later since this will not enable DHCP
118122
void ESP8266WiFiClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet)
119123
{
120124
struct ip_info info;
@@ -124,6 +128,26 @@ void ESP8266WiFiClass::config(IPAddress local_ip, IPAddress gateway, IPAddress s
124128

125129
wifi_station_dhcpc_stop();
126130
wifi_set_ip_info(STATION_IF, &info);
131+
132+
_useStaticIp = true;
133+
}
134+
135+
void ESP8266WiFiClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns)
136+
{
137+
struct ip_info info;
138+
info.ip.addr = static_cast<uint32_t>(local_ip);
139+
info.gw.addr = static_cast<uint32_t>(gateway);
140+
info.netmask.addr = static_cast<uint32_t>(subnet);
141+
142+
wifi_station_dhcpc_stop();
143+
wifi_set_ip_info(STATION_IF, &info);
144+
145+
// Set DNS-Server
146+
ip_addr_t d;
147+
d.addr = static_cast<uint32_t>(dns);
148+
dns_setserver(0,&d);
149+
150+
_useStaticIp = true;
127151
}
128152

129153
int ESP8266WiFiClass::disconnect()

hardware/esp8266com/esp8266/libraries/ESP8266WiFi/src/ESP8266WiFi.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ class ESP8266WiFiClass
7575
*/
7676
void softAP(const char* ssid, const char* passphrase, int channel = 1);
7777

78-
7978
/* Change Ip configuration settings disabling the dhcp client
8079
*
8180
* param local_ip: Static ip configuration
@@ -84,6 +83,15 @@ class ESP8266WiFiClass
8483
*/
8584
void config(IPAddress local_ip, IPAddress gateway, IPAddress subnet);
8685

86+
/* Change Ip configuration settings disabling the dhcp client
87+
*
88+
* param local_ip: Static ip configuration
89+
* param gateway: Static gateway configuration
90+
* param subnet: Static Subnet mask
91+
* param dns: Defined DNS
92+
*/
93+
void config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns);
94+
8795
/* Configure access point
8896
*
8997
* param local_ip: access point IP
@@ -310,7 +318,8 @@ class ESP8266WiFiClass
310318

311319
bool _useApMode;
312320
bool _useClientMode;
313-
321+
bool _useStaticIp;
322+
314323
static size_t _scanCount;
315324
static void* _scanResult;
316325

0 commit comments

Comments
 (0)