From eb282131ba92dd81aa07cc2e2d36c7e189d716e6 Mon Sep 17 00:00:00 2001 From: me-no-dev Date: Fri, 22 Sep 2017 12:20:53 +0800 Subject: [PATCH] Allow DHCP to be started again by giving a zero IP address to STA config Fixes: https://github.com/espressif/arduino-esp32/issues/654 --- libraries/WiFi/src/WiFiSTA.cpp | 37 ++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/libraries/WiFi/src/WiFiSTA.cpp b/libraries/WiFi/src/WiFiSTA.cpp index 91e62743..8178c834 100644 --- a/libraries/WiFi/src/WiFiSTA.cpp +++ b/libraries/WiFi/src/WiFiSTA.cpp @@ -199,6 +199,7 @@ void WiFiSTAClass::_setStatus(wl_status_t status) */ bool WiFiSTAClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1, IPAddress dns2) { + esp_err_t err = ESP_OK; if(!WiFi.enableSTA(true)) { return false; @@ -206,16 +207,40 @@ bool WiFiSTAClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subne esp_wifi_start(); tcpip_adapter_ip_info_t info; - info.ip.addr = static_cast(local_ip); - info.gw.addr = static_cast(gateway); - info.netmask.addr = static_cast(subnet); - tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_STA); - if(tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_STA, &info) == ESP_OK) { - _useStaticIp = true; + if(local_ip != (uint32_t)0x00000000){ + info.ip.addr = static_cast(local_ip); + info.gw.addr = static_cast(gateway); + info.netmask.addr = static_cast(subnet); } else { + info.ip.addr = 0; + info.gw.addr = 0; + info.netmask.addr = 0; + } + + err = tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_STA); + if(err != ESP_OK && err != ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STOPPED){ + log_e("DHCP could not be stopped! Error: %d", err); return false; } + + err = tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_STA, &info); + if(err != ERR_OK){ + log_e("STA IP could not be configured! Error: %d", err); + return false; + } + + if(info.ip.addr){ + _useStaticIp = true; + } else { + err = tcpip_adapter_dhcpc_start(TCPIP_ADAPTER_IF_STA); + if(err != ESP_OK && err != ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STARTED){ + log_w("DHCP could not be started! Error: %d", err); + return false; + } + _useStaticIp = false; + } + ip_addr_t d; d.type = IPADDR_TYPE_V4;