(SOLVED)ESP-IDF: E (588) wifi:failed to post WiFi event=40 ret=259

CraftyZA
Posts: 5
Joined: Fri Mar 22, 2024 6:29 pm

(SOLVED)ESP-IDF: E (588) wifi:failed to post WiFi event=40 ret=259

Postby CraftyZA » Fri Mar 22, 2024 6:51 pm

I'm trying something new. I want to detect certain management packets, then trigger an alarm when detected.

The error above comes from the libnet80211.a library.
I was hoping to see the source somewhere to find out what event=40 ret=259 could mean.
The highlighted code is the line that breaks.

I've tried running the code on a ESP32S as well as a ESP WROOM 32 (the one that has the UNO layout)

Code: Select all

void app_main(void) {
    printf("Starting packet Detection...\n");

    esp_log_level_set("*", ESP_LOG_VERBOSE);

    // Initialize NVS
    esp_err_t ret = nvs_flash_init();
    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
        ESP_ERROR_CHECK(nvs_flash_erase());
        ret = nvs_flash_init();
    }
    ESP_ERROR_CHECK(ret);

    // Initialize Wi-Fi
    printf("Wifi Init in progress...\n");
    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_NULL)); // Set Wi-Fi mode to NULL
    [b][color=#FF0000]ESP_ERROR_CHECK(esp_wifi_start());[/color][/b] // Start Wi-Fi
    esp_wifi_start();
    // Set Wi-Fi mode to promiscuous
    printf("Setting WIFI to promiscuous mode...\n");
    ESP_ERROR_CHECK(esp_wifi_set_promiscuous(true));
    ESP_ERROR_CHECK(esp_wifi_set_promiscuous_rx_cb(&wifi_sniffer_packet_handler));

    ESP_LOGI(TAG, "Wi-Fi initialized");
    printf("Setup Complete. \n");

    // Example: Setup button GPIO pin and attach interrupt handler to stop the alarm
    gpio_set_direction(STOP_BUTTON_GPIO_PIN, GPIO_MODE_INPUT);
    gpio_set_intr_type(STOP_BUTTON_GPIO_PIN, GPIO_INTR_NEGEDGE);
    gpio_install_isr_service(0);
    gpio_isr_handler_add(STOP_BUTTON_GPIO_PIN, stop_alarm, NULL);

    // Main loop
    while (1) {
        // Just keep swimming, just keep swimming
        vTaskDelay(1000 / portTICK_PERIOD_MS); // Delay for 1 second
    }

    // Deinitialize Wi-Fi before application exits
    // ESP_ERROR_CHECK(esp_wifi_stop()); // Stop Wi-Fi
    // ESP_ERROR_CHECK(esp_wifi_deinit()); // Deinitialize Wi-Fi
    // ESP_LOGI(TAG, "Wi-Fi deinitialized");
}


Full code available at:
https://github.com/CraftyZA/DetectWifiDeauth
Last edited by CraftyZA on Sat Mar 23, 2024 4:08 pm, edited 1 time in total.

liaifat85
Posts: 200
Joined: Wed Dec 06, 2023 2:46 pm

Re: ESP-IDF: E (588) wifi:failed to post WiFi event=40 ret=259

Postby liaifat85 » Sat Mar 23, 2024 7:30 am

You can print debug information before and after the Wi-Fi initialization steps. This may let you find out where the problem is occurring.

CraftyZA
Posts: 5
Joined: Fri Mar 22, 2024 6:29 pm

Re: ESP-IDF: E (588) wifi:failed to post WiFi event=40 ret=259

Postby CraftyZA » Sat Mar 23, 2024 12:44 pm

I've made the code pretty verbose in my last update.
Updated main function:

Code: Select all

void app_main(void) {
    printf("Starting packet Detection...\n");

    esp_log_level_set("*", ESP_LOG_VERBOSE);

    ESP_LOGI(TAG, "Before WiFi Initialization");

    // Initialize NVS
    esp_err_t ret = nvs_flash_init();
    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
        ESP_ERROR_CHECK(nvs_flash_erase());
        ret = nvs_flash_init();
    }
    ESP_ERROR_CHECK(ret);

    // Debug info after NVS initialization
    ESP_LOGI(TAG, "NVS Initialized");

    // Initialize Wi-Fi
    printf("WiFi Init in progress...\n");
    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    esp_err_t wifi_init_result = esp_wifi_init(&cfg);
    if (wifi_init_result != ESP_OK) {
        ESP_LOGE(TAG, "Failed to initialize Wi-Fi: %d", wifi_init_result);
    } else {
        ESP_LOGI(TAG, "Wi-Fi Successfully initialized");
    }
    esp_err_t wifi_storage_result = esp_wifi_set_storage(WIFI_STORAGE_RAM);
    if (wifi_storage_result != ESP_OK) {
        ESP_LOGE(TAG, "Failed to set Wi-Fi storage: %d", wifi_storage_result);
    } else {
        ESP_LOGI(TAG, "Wi-Fi RAM storage set");
    }
    esp_err_t wifi_mode_result = esp_wifi_set_mode(WIFI_MODE_NULL);
    if (wifi_mode_result != ESP_OK) {
        ESP_LOGE(TAG, "Failed to set Wi-Fi mode to NULL: %d", wifi_mode_result);
    } else {
        ESP_LOGI(TAG, "Wi-Fi Mode set to NULL");
    }

    esp_err_t wifi_start_result = esp_wifi_start();
    if (wifi_start_result != ESP_OK) {
        ESP_LOGE(TAG, "Failed to start WiFi: %s", esp_err_to_name(wifi_start_result));
    } else {
        ESP_LOGI(TAG, "Started WiFi!");
    }
    
    ESP_LOGI(TAG, "After WiFi Initialization");

    // Set Wi-Fi mode to promiscuous
    printf("Setting Wi-Fi to promiscuous mode...\n");
    ESP_ERROR_CHECK(esp_wifi_set_promiscuous(true));
    ESP_ERROR_CHECK(esp_wifi_set_promiscuous_rx_cb(&wifi_sniffer_packet_handler));

    ESP_LOGI(TAG, "Wi-Fi initialized");
    printf("Setup Complete. \n");

    // Example: Setup button GPIO pin and attach interrupt handler to stop the alarm
    gpio_set_direction(STOP_BUTTON_GPIO_PIN, GPIO_MODE_INPUT);
    gpio_set_intr_type(STOP_BUTTON_GPIO_PIN, GPIO_INTR_NEGEDGE);
    gpio_install_isr_service(0);
    gpio_isr_handler_add(STOP_BUTTON_GPIO_PIN, stop_alarm, NULL);

    // Main loop
    while (1) {
        // Just keep swimming, just keep swimming
        vTaskDelay(1000 / portTICK_PERIOD_MS); // Delay for 1 second
    }

    // Deinitialize Wi-Fi before application exits
    // ESP_ERROR_CHECK(esp_wifi_stop()); // Stop Wi-Fi
    // ESP_ERROR_CHECK(esp_wifi_deinit()); // Deinitialize Wi-Fi
    // ESP_LOGI(TAG, "Wi-Fi deinitialized");
}

Here is the output.

Code: Select all

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0030,len:7172
load:0x40078000,len:15540
load:0x40080400,len:4
ho 8 tail 4 room 4
load:0x40080404,len:3904
entry 0x40080640
I (30) boot: ESP-IDF v5.2.1-dirty 2nd stage bootloader
I (30) boot: compile time Mar 22 2024 12:31:46
I (30) boot: Multicore bootloader
I (35) boot: chip revision: v1.0
I (39) boot.esp32: SPI Speed      : 40MHz
I (43) boot.esp32: SPI Mode       : DIO
I (48) boot.esp32: SPI Flash Size : 4MB
I (53) boot: Enabling RNG early entropy source...
I (58) boot: Partition Table:
I (62) boot: ## Label            Usage          Type ST Offset   Length
I (69) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (76) boot:  1 phy_init         RF data          01 01 0000f000 00001000
I (84) boot:  2 factory          factory app      00 00 00010000 00100000
I (91) boot: End of partition table
I (95) esp_image: segment 0: paddr=00010020 vaddr=3f400020 size=1d424h (119844) map
I (129) esp_image: segment 1: paddr=0002d44c vaddr=3ffb0000 size=02bcch ( 11212) load
I (131) esp_image: segment 2: paddr=00030020 vaddr=400d0020 size=6a94ch (436556) map
I (224) esp_image: segment 3: paddr=0009a974 vaddr=3ffb2bcc size=01010h (  4112) load
I (226) esp_image: segment 4: paddr=0009b98c vaddr=40080000 size=166cch ( 91852) load
I (265) boot: Loaded app from partition at offset 0x10000
I (265) boot: Disabling RNG early entropy source...
I (276) cpu_start: Multicore app
I (285) cpu_start: Pro cpu start user code
I (285) cpu_start: cpu freq: 160000000 Hz
I (285) cpu_start: Application information:
I (288) cpu_start: Project name:     deauth_detect
I (294) cpu_start: App version:      fc0bda0-dirty
I (299) cpu_start: Compile time:     Mar 23 2024 08:25:36
I (305) cpu_start: ELF file SHA256:  2e0eda11a...
I (311) cpu_start: ESP-IDF:          v5.2.1-dirty
I (316) cpu_start: Min chip rev:     v0.0
I (321) cpu_start: Max chip rev:     v3.99
I (325) cpu_start: Chip rev:         v1.0
I (330) heap_init: Initializing. RAM available for dynamic allocation:
I (338) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (343) heap_init: At 3FFB72F8 len 00028D08 (163 KiB): DRAM
I (350) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (356) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (362) heap_init: At 400966CC len 00009934 (38 KiB): IRAM
I (370) spi_flash: detected chip: generic
I (373) spi_flash: flash io: dio
I (378) main_task: Started on CPU0
I (388) main_task: Calling app_main()
Starting packet Detection...
I (388) deauth_sniffer: Before WiFi Initialization
I (408) deauth_sniffer: NVS Initialized
WiFi Init in progress...
I (418) wifi:wifi driver task: 3ffbcbf0, prio:23, stack:6656, core=0
I (418) wifi:wifi firmware version: a9f5b59
I (418) wifi:wifi certification version: v7.0
I (418) wifi:config NVS flash: enabled
I (418) wifi:config nano formating: disabled
I (428) wifi:Init data frame dynamic rx buffer num: 32
I (428) wifi:Init static rx mgmt buffer num: 5
I (438) wifi:Init management short buffer num: 32
I (438) wifi:Init dynamic tx buffer num: 32
I (448) wifi:Init static rx buffer size: 1600
I (448) wifi:Init static rx buffer num: 10
I (448) wifi:Init dynamic rx buffer num: 32
I (458) wifi_init: rx ba win: 6
I (458) wifi_init: tcpip mbox: 32
I (468) wifi_init: udp mbox: 6
I (468) wifi_init: tcp mbox: 6
I (468) wifi_init: tcp tx win: 5760
I (478) wifi_init: tcp rx win: 5760
I (478) wifi_init: tcp mss: 1440
I (478) wifi_init: WiFi IRAM OP enabled
I (488) wifi_init: WiFi RX IRAM OP enabled
I (488) deauth_sniffer: Wi-Fi Successfully initialized
I (498) deauth_sniffer: Wi-Fi RAM storage set
I (508) deauth_sniffer: Wi-Fi Mode set to NULL
I (508) phy_init: phy_version 4791,2c4672b,Dec 20 2023,16:06:06
E (598) wifi:failed to post WiFi event=40 ret=259
I (608) wifi:mode : null
I (608) deauth_sniffer: Started WiFi!
I (608) deauth_sniffer: After WiFi Initialization
Setting Wi-Fi to promiscuous mode...
I (608) wifi:ic_enable_sniffer
I (618) deauth_sniffer: Wi-Fi initialized
Setup Complete.

CraftyZA
Posts: 5
Joined: Fri Mar 22, 2024 6:29 pm

Re: ESP-IDF: E (588) wifi:failed to post WiFi event=40 ret=259

Postby CraftyZA » Sat Mar 23, 2024 12:49 pm

I've made the code pretty verbose in my last update.
Updated main function:

Code: Select all

void app_main(void) {
    printf("Starting packet Detection...\n");

    esp_log_level_set("*", ESP_LOG_VERBOSE);

    ESP_LOGI(TAG, "Before WiFi Initialization");

    // Initialize NVS
    esp_err_t ret = nvs_flash_init();
    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
        ESP_ERROR_CHECK(nvs_flash_erase());
        ret = nvs_flash_init();
    }
    ESP_ERROR_CHECK(ret);

    // Debug info after NVS initialization
    ESP_LOGI(TAG, "NVS Initialized");

    // Initialize Wi-Fi
    printf("WiFi Init in progress...\n");
    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    esp_err_t wifi_init_result = esp_wifi_init(&cfg);
    if (wifi_init_result != ESP_OK) {
        ESP_LOGE(TAG, "Failed to initialize Wi-Fi: %d", wifi_init_result);
    } else {
        ESP_LOGI(TAG, "Wi-Fi Successfully initialized");
    }
    esp_err_t wifi_storage_result = esp_wifi_set_storage(WIFI_STORAGE_RAM);
    if (wifi_storage_result != ESP_OK) {
        ESP_LOGE(TAG, "Failed to set Wi-Fi storage: %d", wifi_storage_result);
    } else {
        ESP_LOGI(TAG, "Wi-Fi RAM storage set");
    }
    esp_err_t wifi_mode_result = esp_wifi_set_mode(WIFI_MODE_NULL);
    if (wifi_mode_result != ESP_OK) {
        ESP_LOGE(TAG, "Failed to set Wi-Fi mode to NULL: %d", wifi_mode_result);
    } else {
        ESP_LOGI(TAG, "Wi-Fi Mode set to NULL");
    }

    esp_err_t wifi_start_result = esp_wifi_start();
    if (wifi_start_result != ESP_OK) {
        ESP_LOGE(TAG, "Failed to start WiFi: %s", esp_err_to_name(wifi_start_result));
    } else {
        ESP_LOGI(TAG, "Started WiFi!");
    }
    
    ESP_LOGI(TAG, "After WiFi Initialization");

    // Set Wi-Fi mode to promiscuous
    printf("Setting Wi-Fi to promiscuous mode...\n");
    ESP_ERROR_CHECK(esp_wifi_set_promiscuous(true));
    ESP_ERROR_CHECK(esp_wifi_set_promiscuous_rx_cb(&wifi_sniffer_packet_handler));

    ESP_LOGI(TAG, "Wi-Fi initialized");
    printf("Setup Complete. \n");

    // Example: Setup button GPIO pin and attach interrupt handler to stop the alarm
    gpio_set_direction(STOP_BUTTON_GPIO_PIN, GPIO_MODE_INPUT);
    gpio_set_intr_type(STOP_BUTTON_GPIO_PIN, GPIO_INTR_NEGEDGE);
    gpio_install_isr_service(0);
    gpio_isr_handler_add(STOP_BUTTON_GPIO_PIN, stop_alarm, NULL);

    // Main loop
    while (1) {
        // Just keep swimming, just keep swimming
        vTaskDelay(1000 / portTICK_PERIOD_MS); // Delay for 1 second
    }

    // Deinitialize Wi-Fi before application exits
    // ESP_ERROR_CHECK(esp_wifi_stop()); // Stop Wi-Fi
    // ESP_ERROR_CHECK(esp_wifi_deinit()); // Deinitialize Wi-Fi
    // ESP_LOGI(TAG, "Wi-Fi deinitialized");
}

Here is the output.

Code: Select all

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0030,len:7172
load:0x40078000,len:15540
load:0x40080400,len:4
ho 8 tail 4 room 4
load:0x40080404,len:3904
entry 0x40080640
I (30) boot: ESP-IDF v5.2.1-dirty 2nd stage bootloader
I (30) boot: compile time Mar 22 2024 12:31:46
I (30) boot: Multicore bootloader
I (35) boot: chip revision: v1.0
I (39) boot.esp32: SPI Speed      : 40MHz
I (43) boot.esp32: SPI Mode       : DIO
I (48) boot.esp32: SPI Flash Size : 4MB
I (53) boot: Enabling RNG early entropy source...
I (58) boot: Partition Table:
I (62) boot: ## Label            Usage          Type ST Offset   Length
I (69) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (76) boot:  1 phy_init         RF data          01 01 0000f000 00001000
I (84) boot:  2 factory          factory app      00 00 00010000 00100000
I (91) boot: End of partition table
I (95) esp_image: segment 0: paddr=00010020 vaddr=3f400020 size=1d424h (119844) map
I (129) esp_image: segment 1: paddr=0002d44c vaddr=3ffb0000 size=02bcch ( 11212) load
I (131) esp_image: segment 2: paddr=00030020 vaddr=400d0020 size=6a94ch (436556) map
I (224) esp_image: segment 3: paddr=0009a974 vaddr=3ffb2bcc size=01010h (  4112) load
I (226) esp_image: segment 4: paddr=0009b98c vaddr=40080000 size=166cch ( 91852) load
I (265) boot: Loaded app from partition at offset 0x10000
I (265) boot: Disabling RNG early entropy source...
I (276) cpu_start: Multicore app
I (285) cpu_start: Pro cpu start user code
I (285) cpu_start: cpu freq: 160000000 Hz
I (285) cpu_start: Application information:
I (288) cpu_start: Project name:     deauth_detect
I (294) cpu_start: App version:      fc0bda0-dirty
I (299) cpu_start: Compile time:     Mar 23 2024 08:25:36
I (305) cpu_start: ELF file SHA256:  2e0eda11a...
I (311) cpu_start: ESP-IDF:          v5.2.1-dirty
I (316) cpu_start: Min chip rev:     v0.0
I (321) cpu_start: Max chip rev:     v3.99
I (325) cpu_start: Chip rev:         v1.0
I (330) heap_init: Initializing. RAM available for dynamic allocation:
I (338) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (343) heap_init: At 3FFB72F8 len 00028D08 (163 KiB): DRAM
I (350) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (356) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (362) heap_init: At 400966CC len 00009934 (38 KiB): IRAM
I (370) spi_flash: detected chip: generic
I (373) spi_flash: flash io: dio
I (378) main_task: Started on CPU0
I (388) main_task: Calling app_main()
Starting packet Detection...
I (388) deauth_sniffer: Before WiFi Initialization
I (408) deauth_sniffer: NVS Initialized
WiFi Init in progress...
I (418) wifi:wifi driver task: 3ffbcbf0, prio:23, stack:6656, core=0
I (418) wifi:wifi firmware version: a9f5b59
I (418) wifi:wifi certification version: v7.0
I (418) wifi:config NVS flash: enabled
I (418) wifi:config nano formating: disabled
I (428) wifi:Init data frame dynamic rx buffer num: 32
I (428) wifi:Init static rx mgmt buffer num: 5
I (438) wifi:Init management short buffer num: 32
I (438) wifi:Init dynamic tx buffer num: 32
I (448) wifi:Init static rx buffer size: 1600
I (448) wifi:Init static rx buffer num: 10
I (448) wifi:Init dynamic rx buffer num: 32
I (458) wifi_init: rx ba win: 6
I (458) wifi_init: tcpip mbox: 32
I (468) wifi_init: udp mbox: 6
I (468) wifi_init: tcp mbox: 6
I (468) wifi_init: tcp tx win: 5760
I (478) wifi_init: tcp rx win: 5760
I (478) wifi_init: tcp mss: 1440
I (478) wifi_init: WiFi IRAM OP enabled
I (488) wifi_init: WiFi RX IRAM OP enabled
I (488) deauth_sniffer: Wi-Fi Successfully initialized
I (498) deauth_sniffer: Wi-Fi RAM storage set
I (508) deauth_sniffer: Wi-Fi Mode set to NULL
I (508) phy_init: phy_version 4791,2c4672b,Dec 20 2023,16:06:06
E (598) wifi:failed to post WiFi event=40 ret=259
I (608) wifi:mode : null
I (608) deauth_sniffer: Started WiFi!
I (608) deauth_sniffer: After WiFi Initialization
Setting Wi-Fi to promiscuous mode...
I (608) wifi:ic_enable_sniffer
I (618) deauth_sniffer: Wi-Fi initialized
Setup Complete.

CraftyZA
Posts: 5
Joined: Fri Mar 22, 2024 6:29 pm

Re: (SOLVED)ESP-IDF: E (588) wifi:failed to post WiFi event=40 ret=259

Postby CraftyZA » Sat Mar 23, 2024 4:10 pm

Still not 100% sure what the issue was, but refactored to just the basics.
Will add new code bit by bit, and keep testing.

FARLY7
Posts: 1
Joined: Fri Jan 20, 2023 11:02 am

Re: (SOLVED)ESP-IDF: E (588) wifi:failed to post WiFi event=40 ret=259

Postby FARLY7 » Fri May 17, 2024 7:52 am

CraftyZA wrote:
Sat Mar 23, 2024 4:10 pm
Still not 100% sure what the issue was, but refactored to just the basics.
Will add new code bit by bit, and keep testing.
Hi CraftyZA,

Did you find a solution or reason for the error?
I am also facing the same after updating esp-idf to v5.2.1. I never used to have this error.

Thanks,
Seans

CraftyZA
Posts: 5
Joined: Fri Mar 22, 2024 6:29 pm

Re: (SOLVED)ESP-IDF: E (588) wifi:failed to post WiFi event=40 ret=259

Postby CraftyZA » Fri May 17, 2024 3:19 pm

FARLY7 wrote:
Fri May 17, 2024 7:52 am
CraftyZA wrote:
Sat Mar 23, 2024 4:10 pm
Still not 100% sure what the issue was, but refactored to just the basics.
Will add new code bit by bit, and keep testing.
Hi CraftyZA,

Did you find a solution or reason for the error?
I am also facing the same after updating esp-idf to v5.2.1. I never used to have this error.

Thanks,
Seans
I think it may have been the loop.
https://github.com/CraftyZA/DetectWifiH ... ain/main.c

The code above works. Maybe compare and see what is different in your code?

Timmwardion
Posts: 5
Joined: Wed Nov 29, 2023 1:59 am

Re: (SOLVED)ESP-IDF: E (588) wifi:failed to post WiFi event=40 ret=259

Postby Timmwardion » Sun May 26, 2024 2:37 am

I can confirm that I added the event loop line above:

Code: Select all

 ESP_ERROR_CHECK(esp_event_loop_create_default());
And also changed

Code: Select all

err = esp_wifi_set_mode(WIFI_MODE_STA);
to

Code: Select all

err = esp_wifi_set_mode(WIFI_MODE_NULL);
Errors have stopped completely.

Thanks CraftyZA

Who is online

Users browsing this forum: Google [Bot] and 85 guests