-
Notifications
You must be signed in to change notification settings - Fork 7.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
esp-idf dhcp client gives different ip each time we boot, arduino-esp32 does not #2182
Comments
@korstiaanS please try the following two functions (replace them in your code) and since you have a reliable way to reproduce it (I could not) I would ask you to please try and figure out which line/order of items is triggering it. The two functions are a simplified version of static esp_err_t event_handler(void *ctx, system_event_t *event)
{
switch(event->event_id) {
case SYSTEM_EVENT_STA_GOT_IP:
ESP_LOGI(TAG, "got ip:%s",
ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip));
xEventGroupSetBits(wifi_event_group, WIFI_CONNECTED_BIT);
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
ESP_LOGW(TAG, "disconnected: %u",
event->event_info.disconnected.reason);
esp_wifi_connect();
xEventGroupClearBits(wifi_event_group, WIFI_CONNECTED_BIT);
break;
default:
break;
}
return ESP_OK;
}
void wifi_init_sta(){
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
wifi_config_t conf;
memset(&conf, 0, sizeof(wifi_config_t));
strcpy((char*)(conf.sta.ssid), EXAMPLE_ESP_WIFI_SSID);
strcpy((char*)(conf.sta.password), EXAMPLE_ESP_WIFI_PASS);
wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
tcpip_adapter_init();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_wifi_start());
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &conf));
esp_err_t err = tcpip_adapter_dhcpc_start(TCPIP_ADAPTER_IF_STA);
if(err == ESP_ERR_TCPIP_ADAPTER_DHCPC_START_FAILED){
ESP_ERROR_CHECK(err);
}
ESP_ERROR_CHECK(esp_wifi_connect());
ESP_LOGI(TAG, "wifi_init_sta finished.");
ESP_LOGI(TAG, "connect to ap");
} |
Hi, Same result. First boot:
Second boot:
|
OK, new idea :) since it looks like the only difference left. Arduino spins a task from |
No. |
Has nothing to do with esp but they had simular problems. |
OK Then... maybe there is something in menuconfig causing this issue? Can you try Arduino's sdkconfig and see if there are any differences (there are some DHCP/ARP options in menuconfig). |
(Yes, parameters. I asked this already on the Homekit issues that it might be a parameter...) Hi, yes, I found it! Solved? |
Yes :) it just took a couple of back and fourth to figure out what could be it. I will ask the default value to be changed to no. And now we will know what could be the reason for the future. Thanks @korstiaanS for sticking with it and helping debug the issue! |
Thank you for helping solving this problem! PS: Espressif/ESP is for me one of best inventions of the last decade. Congrats to all of you for bringing such beautifull products. |
Closing this now as resolved :) ping us back if we can help with anything else |
So is there something on the network that is actually replying to the arp request saying the IP is in use? Maybe this: http://lwip.100.n7.nabble.com/broken-DHCP-ARP-interaction-tp22638p22739.html |
some routers/ap are reported to reply on behalf of the client, so that is probably what is happening. |
This sounds fixable |
I use arduino-esp32 and it worked flawlessly until recently, where I did notice that on networks with ARP-caching that DHCP it is not working. Took me quite some time to figure out why LWIP DECLINES an ACKd IP. @me-no-dev Wouldn't it be possible to do an MAC-address check (e.g. check if it's own address), so that IF routers/APs reply on behalf of the client, the DHCP still works? Or is there a technical reasoning why this isn't happening/done? To me: It absolutely makes no sense, especially in cases like mine, where I quite regularly reboot the ESP32 because of battery-saving. |
@tynx can you please take tcpdump of the exchange? We recently updated LwIP to 2.0.3 and I guess that the issue is a result of that. A dump can help trace the packet through the code :) |
So I went into the code and saw that only this line can be the reason for it: At least if you trust the search-engine of github. So it has to be in dhcp_arp_reply and if I understand the code correctly we do a check if the IP is already used in the network. If another device uses that IP it will respond to the ARP-request leading to LWIP to decline the ACKd IP. The fun part is, that this is not an issue, as LWIP will not respond to an ARP request from itself, so no worries there. But if an AP/station/network-component answers on behalf of the client (which seems to be a feature called ARP-caching?) then the LWIP declines the IP because an ARP-Reply with the MAC of itself. As far as I can tell it would be possible to do an MAC-address comparison (does not equal) on line https://github.com/espressif/esp-lwip/blob/3b88e84f9b05e76a1ad96e6650a795df74af23aa/src/core/ipv4/dhcp.c#L976 and the problem should be gone. But I might be wrong? If you want I can try to implement it and do a PR? |
You should probably do the filtering in etharp_input https://gist.github.com/negativekelvin/0373847ebc5d47bd14baee7f21423f86/revisions |
Hi,
We have the following problem:
ESP32 compiled with latest esp-idf on certain networks gets from the dhcp server each time we boot another ip address.
When we compile with latest arduino-esp32 with all the same hardware it runs fine and we get always the same ip -> like it should be.
Other facts:
First boot
After second boot:
After third boot:
This is not happening with arduino-esp32.
I also added the 2 test projects, 1 for esp-idf and 1 for arduino-esp32:
simple_wifi.zip
WiFiClient.zip
This is the generated output from arduino (all tested with the same hardware):
Each time same IP.
This is the output from esp-idf:
Please help to get this fixed!
Korstiaan
The text was updated successfully, but these errors were encountered: