ESP32 answers DHCP request with NAK

Posts: 2
Joined: Fri Sep 15, 2017 4:58 pm

ESP32 answers DHCP request with NAK

Postby tostasmistas » Mon Feb 26, 2018 6:14 pm

Hello everyone,

Currently I am setting up an ESP32 to act as a TCP server to which clients can connect in order to retrieve some information. I have configured my ESP32 in softAP mode, with an assigned static IP address and with a listening server socket. Below is the code for configuring the network interface:

Code: Select all


// assign a static IP to the network interface
tcpip_adapter_ip_info_t ip_info;
memset(&ip_info, 0, sizeof(ip_info));
IP4_ADDR(&ip_info.ip, 192, 168, 1, 1);
IP4_ADDR(&, 192, 168, 1, 1); // ESP32 acts as router, so GW address will be its own address
IP4_ADDR(&ip_info.netmask, 255, 255, 255, 0);
ESP_ERROR_CHECK(tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_AP, &ip_info));

ESP_ERROR_CHECK(esp_event_loop_init(esp32_wifi_eventHandler, NULL));

wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();

wifi_config_t apConfig = {
    .ap = {
strcpy((char *)apConfig.ap.ssid, ssid);
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &apConfig));
On the client side, I have setup a Python script (running on a Windows computer) which creates a client socket that connects to the ESP32's server socket through its known network interface static IP address.

I am able to successfully establish a connection and transmit data from the ESP32 to the Python client; however, I can only maintain this connection for an hour or so. After a while, the client issues a DHCP Request to which the ESP32 answers with a NAK, from what I've seen using Wireshark. After that, the ESP32 continues to send data, but the client never acknowledges such transmission. Below is a screenshot of the Wireshark capture.
wireshark-1.png (170.55 KiB) Viewed 2788 times
wireshark-1.png (170.55 KiB) Viewed 2788 times
I do not quite understand why the client is sending the DHCP request while the transmission is already in place after a while. Also, I do not understand why the ESP32 replies with a NAK.

My first approach in dealing with this issue was to stop the DHCP server on the ESP32 by calling this function:

Code: Select all

Doing this has solved the problem, considering that the client no longer issued DHCP requests. However, this meant I had to set the IP address of the WiFi interface of my Windows computer manually (to be something like 192.168.x.x) which is not a viable solution for customer deployment. The IP address should be set automatically as it is the case for when a DHCP server is running.

As such, I was wondering if there are any other configurations I could run on the ESP32 to help dealing with this problem. Any ideas? Thanks! :)
wireshark-2.png (188.9 KiB) Viewed 2788 times
Last edited by tostasmistas on Tue Feb 27, 2018 10:31 am, edited 1 time in total.

Posts: 4426
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32 answers DHCP request with NAK

Postby ESP_Sprite » Tue Feb 27, 2018 1:32 am

Can you see the contents of the DHCP request? Not sure if that's the case, but one reason for the ESP32 to NAK the request is if the request is for an IP outside the range the ESP32 serves.

Posts: 2
Joined: Fri Sep 15, 2017 4:58 pm

Re: ESP32 answers DHCP request with NAK

Postby tostasmistas » Tue Feb 27, 2018 10:34 am


I inspected the DHCP request with Wireshark but it seems to me that the IP address is fine, as it matches that of the ESP32:
dhcp-request.png (145.9 KiB) Viewed 2788 times

Posts: 24
Joined: Mon Aug 28, 2017 3:27 am

Re: ESP32 answers DHCP request with NAK

Postby zhangyanjiao » Tue Jun 19, 2018 12:12 pm

hi ,
I have tested on two ESP32 boards. One is AP, other is STA, both DHCP on. STA connects to AP, and AP offer IP to STA.
I set the IP of AP as and In the two cases, AP answers DHCP request with ACK.
4.1.png (33.51 KiB) Viewed 2559 times
1.1.png (36.18 KiB) Viewed 2559 times

Who is online

Users browsing this forum: No registered users and 46 guests