ESP32 connects to Access Point but can't get IP address

monkey
Posts: 21
Joined: Mon Jun 17, 2019 10:47 pm

Re: ESP32 connects to Access Point but can't get IP address

Postby monkey » Fri Sep 23, 2022 9:00 am

Thought I'd offer this work-around. Like others, I've observed the behaviour described, and the suggested solutions don't work. So, I figure a recovery mechanism is about as good as we can do:

Code: Select all

/* Delay & Recover if WiFi doesn't get allocated an IP address */

    //  uint32_t timeOutCntr = 24 * 6 * 60 * 24;    // 24 hours = 10 seconds * 6 * 60 * 24
    uint32_t timeOutCntr = 12;                     // 2 minutes = 10 seconds * 12
    while (pdTRUE) {
        EventBits_t handlerEventBits;
        const TickType_t xTicksToWait = 10000 / portTICK_PERIOD_MS;
        
        handlerEventBits = xEventGroupWaitBits(
            wifi_event_group,                                          // The event group being tested.
            HANDLER_EVENT_STA_GOT_IP | HANDLER_EVENT_STA_CONNECTED,    // The bits within the event group to wait for.
            pdFALSE,                                                   // BITS should NOT be cleared before returning.
            pdTRUE,                                                    // Wait for ALL bits, holds it when no IP.
            xTicksToWait                                               // Wait a maximum of 10 s for either bit to be set.
        );

        // Connected and has IP Address OK!
        if( ( handlerEventBits & HANDLER_EVENT_STA_GOT_IP ) != 0 )
        {
            // xEventGroupWaitBits() returned because HANDLER_EVENT_STA_GOT_IP was set.
            ESP_LOGI(TAG, "IP Address allocated successfully. Starting Cloud Task shortly.");
            break;
        }
        // Trying to Connect but Failing - Let's reset after a timeout!
        else if( ( handlerEventBits & HANDLER_EVENT_STA_CONNECTED ) != 0 )
        {
            // xEventGroupWaitBits() returned because HANDLER_EVENT_STA_CONNECTED was set.
            ESP_LOGI(TAG, "WiFi Connection Started, but no IP Address was available! Restarting after %d more checks.", timeOutCntr);
            if (timeOutCntr-- <= 1) {
                esp_restart();
            }
        }
    }

Ritesh
Posts: 1383
Joined: Tue Sep 06, 2016 9:37 am
Location: India
Contact:

Re: ESP32 connects to Access Point but can't get IP address

Postby Ritesh » Wed Sep 28, 2022 6:55 am

monkey wrote:
Fri Sep 23, 2022 9:00 am
Thought I'd offer this work-around. Like others, I've observed the behaviour described, and the suggested solutions don't work. So, I figure a recovery mechanism is about as good as we can do:

Code: Select all

/* Delay & Recover if WiFi doesn't get allocated an IP address */

    //  uint32_t timeOutCntr = 24 * 6 * 60 * 24;    // 24 hours = 10 seconds * 6 * 60 * 24
    uint32_t timeOutCntr = 12;                     // 2 minutes = 10 seconds * 12
    while (pdTRUE) {
        EventBits_t handlerEventBits;
        const TickType_t xTicksToWait = 10000 / portTICK_PERIOD_MS;
        
        handlerEventBits = xEventGroupWaitBits(
            wifi_event_group,                                          // The event group being tested.
            HANDLER_EVENT_STA_GOT_IP | HANDLER_EVENT_STA_CONNECTED,    // The bits within the event group to wait for.
            pdFALSE,                                                   // BITS should NOT be cleared before returning.
            pdTRUE,                                                    // Wait for ALL bits, holds it when no IP.
            xTicksToWait                                               // Wait a maximum of 10 s for either bit to be set.
        );

        // Connected and has IP Address OK!
        if( ( handlerEventBits & HANDLER_EVENT_STA_GOT_IP ) != 0 )
        {
            // xEventGroupWaitBits() returned because HANDLER_EVENT_STA_GOT_IP was set.
            ESP_LOGI(TAG, "IP Address allocated successfully. Starting Cloud Task shortly.");
            break;
        }
        // Trying to Connect but Failing - Let's reset after a timeout!
        else if( ( handlerEventBits & HANDLER_EVENT_STA_CONNECTED ) != 0 )
        {
            // xEventGroupWaitBits() returned because HANDLER_EVENT_STA_CONNECTED was set.
            ESP_LOGI(TAG, "WiFi Connection Started, but no IP Address was available! Restarting after %d more checks.", timeOutCntr);
            if (timeOutCntr-- <= 1) {
                esp_restart();
            }
        }
    }
Thanks for providing updates with details

So, Was it working perfectly without any issue? If that is the case then we can ask to Espressif Team to review and include that workaround solution.
Regards,
Ritesh Prajapati

Ritesh
Posts: 1383
Joined: Tue Sep 06, 2016 9:37 am
Location: India
Contact:

Re: ESP32 connects to Access Point but can't get IP address

Postby Ritesh » Wed Sep 28, 2022 6:57 am

azizjiwani wrote:
Thu Sep 22, 2022 4:23 am
Hi, we are also facing the same issue but for us some of the devices are getting IP and some aren't.

- In the router - MAC id whitelisting is done correctly, we have checked free ips are available.
- Enabled DHCP logs, discover request is being sent from the devices but not all of them is receiving response from DHCP server (router)

dhcp logs.jpg

CONFIG_LWIP_DHCP_DOES_ARP_CHECK isn't enabled. Any ideas on how to resolve this.

Thanks in advance for your help.
Aziz
Hello

Have you checked that is there any connection limit as DHCP Server End? Did you check with other router or Hot-Spot and facing same issue?
Regards,
Ritesh Prajapati

monkey
Posts: 21
Joined: Mon Jun 17, 2019 10:47 pm

Re: ESP32 connects to Access Point but can't get IP address

Postby monkey » Tue Jun 06, 2023 3:15 am

@Ritesh

I would say that the work around I use circumvents the problem that I'm experiencing. In my case, there is strong evidence that the network is actually misbehaving. Espressif could consider whether to take this scenario into consideration and respond accordingly without the user needing to explicitly code this in. It would make the ESP32 more robust even with poor network conditions.

Ritesh
Posts: 1383
Joined: Tue Sep 06, 2016 9:37 am
Location: India
Contact:

Re: ESP32 connects to Access Point but can't get IP address

Postby Ritesh » Wed Jun 07, 2023 9:19 am

monkey wrote:
Tue Jun 06, 2023 3:15 am
@Ritesh

I would say that the work around I use circumvents the problem that I'm experiencing. In my case, there is strong evidence that the network is actually misbehaving. Espressif could consider whether to take this scenario into consideration and respond accordingly without the user needing to explicitly code this in. It would make the ESP32 more robust even with poor network conditions.
Absolutely correct and I agreed on your point as well
Regards,
Ritesh Prajapati

aintnodat
Posts: 1
Joined: Mon Dec 11, 2023 5:05 pm

Re: ESP32 connects to Access Point but can't get IP address

Postby aintnodat » Mon Dec 11, 2023 5:28 pm

Leaving this here as a reference to anyone who faces this problem, they could run into the same issue as mine. I got an esp32-Wroom 32E and tried the wifi examples but for some reason it would say connected to the access point but give no IP address. After some debugging I found that the got_ip event never triggers and the WIFI_CONNECTED_BIT is never set. Looking and the logs showed this line

Code: Select all

I (1604) wifi:mode : sta (00:00:00:00:00:00)
which seemed odd to me. I dont know if esp boards come with no mac address pre-programmed, but mine came like that. I know from previous networking experience that some routers have issues with devices showing mac addresses as zeros, which led me to the way of trying to set the mac address of the interface on my chip. so just before calling to wifi_init_sta(); I added a statement to set my mac address to a randomly generated address online.

Code: Select all

uint8_t newMACAddress[] = {0x27, 0xFD, 0x33, 0x1B, 0x88, 0xEE};
esp_iface_mac_addr_set(newMACAddress, ESP_MAC_WIFI_STA);
build and flash, the new mac address is showing in the logs

Code: Select all

I (1604) wifi:mode : sta (27:FD:33:1B:88:EE)
....
I (3204) wifi station: In event handler for ip event...
I (3214) wifi station: got ip:192.168.1.67
I (3214) wifi station: Done setting Wifi status bits
I (3224) wifi station: connected to ap SSID:xxxxx password:xxxxx
I (3224) wifi station: Back from init sta
I (3234) main_task: Returned from app_main()
and the WIFI connected and returned an IP address. :D

Hope this will help.

Ritesh
Posts: 1383
Joined: Tue Sep 06, 2016 9:37 am
Location: India
Contact:

Re: ESP32 connects to Access Point but can't get IP address

Postby Ritesh » Mon Dec 18, 2023 12:16 pm

aintnodat wrote:
Mon Dec 11, 2023 5:28 pm
Leaving this here as a reference to anyone who faces this problem, they could run into the same issue as mine. I got an esp32-Wroom 32E and tried the wifi examples but for some reason it would say connected to the access point but give no IP address. After some debugging I found that the got_ip event never triggers and the WIFI_CONNECTED_BIT is never set. Looking and the logs showed this line

Code: Select all

I (1604) wifi:mode : sta (00:00:00:00:00:00)
which seemed odd to me. I dont know if esp boards come with no mac address pre-programmed, but mine came like that. I know from previous networking experience that some routers have issues with devices showing mac addresses as zeros, which led me to the way of trying to set the mac address of the interface on my chip. so just before calling to wifi_init_sta(); I added a statement to set my mac address to a randomly generated address online.

Code: Select all

uint8_t newMACAddress[] = {0x27, 0xFD, 0x33, 0x1B, 0x88, 0xEE};
esp_iface_mac_addr_set(newMACAddress, ESP_MAC_WIFI_STA);
build and flash, the new mac address is showing in the logs

Code: Select all

I (1604) wifi:mode : sta (27:FD:33:1B:88:EE)
....
I (3204) wifi station: In event handler for ip event...
I (3214) wifi station: got ip:192.168.1.67
I (3214) wifi station: Done setting Wifi status bits
I (3224) wifi station: connected to ap SSID:xxxxx password:xxxxx
I (3224) wifi station: Back from init sta
I (3234) main_task: Returned from app_main()
and the WIFI connected and returned an IP address. :D

Hope this will help.
Great. Thanks for providing updates.

By default, MAC Address for all STA, AP, Ethernet and Bluetooth have been stored into EFUSE Memory which is already programmed while manufacturing modules.

So, I am also bit surprised that how your issue has been resolved after just setting MAC Address.

Anyways, This will be also useful post in case if anyone has faced this type of issue at their end.
Regards,
Ritesh Prajapati

hdo261
Posts: 1
Joined: Wed Aug 07, 2024 7:49 am

Re: ESP32 connects to Access Point but can't get IP address

Postby hdo261 » Wed Aug 07, 2024 7:51 am

I'm new to the esp32 game and also have the IP address problem.

Can someone give me a hint where to put the workaround code mentioned below.

Thanks.

hdo

monkey wrote:
Fri Sep 23, 2022 9:00 am
Thought I'd offer this work-around. Like others, I've observed the behaviour described, and the suggested solutions don't work. So, I figure a recovery mechanism is about as good as we can do:

Code: Select all

/* Delay & Recover if WiFi doesn't get allocated an IP address */

    //  uint32_t timeOutCntr = 24 * 6 * 60 * 24;    // 24 hours = 10 seconds * 6 * 60 * 24
    uint32_t timeOutCntr = 12;                     // 2 minutes = 10 seconds * 12
    while (pdTRUE) {
        EventBits_t handlerEventBits;
        const TickType_t xTicksToWait = 10000 / portTICK_PERIOD_MS;
        
        handlerEventBits = xEventGroupWaitBits(
            wifi_event_group,                                          // The event group being tested.
            HANDLER_EVENT_STA_GOT_IP | HANDLER_EVENT_STA_CONNECTED,    // The bits within the event group to wait for.
            pdFALSE,                                                   // BITS should NOT be cleared before returning.
            pdTRUE,                                                    // Wait for ALL bits, holds it when no IP.
            xTicksToWait                                               // Wait a maximum of 10 s for either bit to be set.
        );

        // Connected and has IP Address OK!
        if( ( handlerEventBits & HANDLER_EVENT_STA_GOT_IP ) != 0 )
        {
            // xEventGroupWaitBits() returned because HANDLER_EVENT_STA_GOT_IP was set.
            ESP_LOGI(TAG, "IP Address allocated successfully. Starting Cloud Task shortly.");
            break;
        }
        // Trying to Connect but Failing - Let's reset after a timeout!
        else if( ( handlerEventBits & HANDLER_EVENT_STA_CONNECTED ) != 0 )
        {
            // xEventGroupWaitBits() returned because HANDLER_EVENT_STA_CONNECTED was set.
            ESP_LOGI(TAG, "WiFi Connection Started, but no IP Address was available! Restarting after %d more checks.", timeOutCntr);
            if (timeOutCntr-- <= 1) {
                esp_restart();
            }
        }
    }

Who is online

Users browsing this forum: No registered users and 233 guests