Skip to content

Commit eb28213

Browse files
committed
Allow DHCP to be started again by giving a zero IP address to STA config
Fixes: espressif#654
1 parent 1407654 commit eb28213

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

libraries/WiFi/src/WiFiSTA.cpp

+31-6
Original file line numberDiff line numberDiff line change
@@ -199,23 +199,48 @@ void WiFiSTAClass::_setStatus(wl_status_t status)
199199
*/
200200
bool WiFiSTAClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1, IPAddress dns2)
201201
{
202+
esp_err_t err = ESP_OK;
202203

203204
if(!WiFi.enableSTA(true)) {
204205
return false;
205206
}
206207
esp_wifi_start();
207208

208209
tcpip_adapter_ip_info_t info;
209-
info.ip.addr = static_cast<uint32_t>(local_ip);
210-
info.gw.addr = static_cast<uint32_t>(gateway);
211-
info.netmask.addr = static_cast<uint32_t>(subnet);
212210

213-
tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_STA);
214-
if(tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_STA, &info) == ESP_OK) {
215-
_useStaticIp = true;
211+
if(local_ip != (uint32_t)0x00000000){
212+
info.ip.addr = static_cast<uint32_t>(local_ip);
213+
info.gw.addr = static_cast<uint32_t>(gateway);
214+
info.netmask.addr = static_cast<uint32_t>(subnet);
216215
} else {
216+
info.ip.addr = 0;
217+
info.gw.addr = 0;
218+
info.netmask.addr = 0;
219+
}
220+
221+
err = tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_STA);
222+
if(err != ESP_OK && err != ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STOPPED){
223+
log_e("DHCP could not be stopped! Error: %d", err);
217224
return false;
218225
}
226+
227+
err = tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_STA, &info);
228+
if(err != ERR_OK){
229+
log_e("STA IP could not be configured! Error: %d", err);
230+
return false;
231+
}
232+
233+
if(info.ip.addr){
234+
_useStaticIp = true;
235+
} else {
236+
err = tcpip_adapter_dhcpc_start(TCPIP_ADAPTER_IF_STA);
237+
if(err != ESP_OK && err != ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STARTED){
238+
log_w("DHCP could not be started! Error: %d", err);
239+
return false;
240+
}
241+
_useStaticIp = false;
242+
}
243+
219244
ip_addr_t d;
220245
d.type = IPADDR_TYPE_V4;
221246

0 commit comments

Comments
 (0)