ESP32 answers DHCP request with NAK

tostasmistas
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

tcpip_adapter_init();

// 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(&ip_info.gw, 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();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));

ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
wifi_config_t apConfig = {
    .ap = {
	.ssid_len=0,\
	.password="",
	.channel=0,
	.authmode=WIFI_AUTH_OPEN,
	.ssid_hidden=0,
	.max_connection=4,
	.beacon_interval=100
     }
};
strcpy((char *)apConfig.ap.ssid, ssid);
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &apConfig));
ESP_ERROR_CHECK(esp_wifi_start());
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
wireshark-1.png (170.55 KiB) Viewed 6371 times
wireshark-1.png
wireshark-1.png (170.55 KiB) Viewed 6371 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

tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP)
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! :)
Attachments
wireshark-2.png
wireshark-2.png (188.9 KiB) Viewed 6371 times
Last edited by tostasmistas on Tue Feb 27, 2018 10:31 am, edited 1 time in total.

ESP_Sprite
Posts: 8926
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.

tostasmistas
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

Hi,

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: 192.168.1.1
dhcp-request.png
dhcp-request.png (145.9 KiB) Viewed 6371 times

zhangyanjiao
Posts: 34
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 192.168.4.1 and 192.168.1.1. In the two cases, AP answers DHCP request with ACK.
Attachments
4.1.png
4.1.png (33.51 KiB) Viewed 6142 times
1.1.png
1.1.png (36.18 KiB) Viewed 6142 times

Who is online

Users browsing this forum: No registered users and 108 guests