IP_EVENT_AP_STAIPASSIGNED is never called in AP-Mode

max1337
Posts: 4
Joined: Thu May 11, 2023 8:58 am

IP_EVENT_AP_STAIPASSIGNED is never called in AP-Mode

Postby max1337 » Wed May 17, 2023 1:42 pm

This is my AP init code:

Code: Select all

    ESP_ERROR_CHECK(esp_netif_init());
    ESP_ERROR_CHECK(esp_event_loop_create_default());
    esp_netif_create_default_wifi_ap();

    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    ESP_ERROR_CHECK(esp_wifi_init(&cfg));
	
    ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
                                                        ESP_EVENT_ANY_ID,
                                                        &wifi_ap_event_handler,
                                                        NULL,
                                                        NULL));
    wifi_config_t wifi_config = {
        .ap = {
            .ssid = EXAMPLE_ESP_WIFI_SSID,
            .ssid_len = strlen(EXAMPLE_ESP_WIFI_SSID),
            .channel = 1,
            .password = EXAMPLE_ESP_WIFI_PASS,
            .max_connection = 1,
            .authmode = WIFI_AUTH_WPA2_PSK,
            .pmf_cfg = {
                    .required = true,
            },
        },
    };

	ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
	ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifi_config));
	ESP_ERROR_CHECK(esp_wifi_start());
and this the event handler:

Code: Select all

static void wifi_ap_event_handler(void* arg, esp_event_base_t event_base,
                                    int32_t event_id, void* event_data) {
    if (event_id == WIFI_EVENT_AP_STACONNECTED) {
        wifi_event_ap_staconnected_t* event = (wifi_event_ap_staconnected_t*)event_data;
		printf("Station connected...\n");
    } else if (event_id == IP_EVENT_AP_STAIPASSIGNED) {
		ip_event_ap_staipassigned_t *event = (ip_event_ap_staipassigned_t*)event_data;
		printf("Station ip assigned...\n");
		last_sta_ip_address = event->ip.addr;
	} else if (event_id == WIFI_EVENT_AP_STADISCONNECTED) {
        wifi_event_ap_stadisconnected_t* event = (wifi_event_ap_stadisconnected_t*)event_data;
		printf("Station disconnected...\n");
		last_sta_ip_address = 0;
    }
}
Unfortunately, IP_EVENT_AP_STAIPASSIGNED is never called.

Does somebody have an idea why this is the case? Once a station connects to the AP, it reports:
esp_netif_lwip: DHCP server assigned IP to a client, IP is: 192.168.4.2
So the IP assignment seems to work.

boarchuz
Posts: 606
Joined: Tue Aug 21, 2018 5:28 am

Re: IP_EVENT_AP_STAIPASSIGNED is never called in AP-Mode

Postby boarchuz » Thu May 18, 2023 8:16 pm

You have registered a handler for WIFI_EVENTs, but not for IP_EVENTs.

Who is online

Users browsing this forum: Baidu [Spider], Google [Bot] and 54 guests