wifi:bcn_timout,ap_probe_send_start when BLE is working with WiFi

zazas321
Posts: 231
Joined: Mon Feb 01, 2021 9:41 am

wifi:bcn_timout,ap_probe_send_start when BLE is working with WiFi

Postby zazas321 » Tue Jan 04, 2022 9:45 am

Hello. For my project, I have BLE running so I can communicate with my device via the phone. Also, I want to run WiFi to be able to synchronise NTP time and also create a access point webserver to select SSID and Password to connect to.

The program logic is very simple. The BLE and WiFi is initialised. WiFi Station mode is started by default and try to connect to a ssid and password which is saved on the flash memory. The WiFi will try to connect 10 times and if not sucesfull, the WiFi mode will change to AP and I will be able to connect to the webserver via the phone or computer and type in SSID and Password that I want to connect to. After I click button on the webserver, ESP32 will try to connect to the SSID that I have provided in the webserver.

https://ibb.co/8Bm4thZ

The following code will initialise WiFi and start Station mode

Code: Select all

bool wifi_init(){

    static httpd_handle_t server = NULL;
    esp_event_handler_instance_t instance_any_id;
    esp_event_handler_instance_t instance_got_ip;

    ESP_ERROR_CHECK(esp_netif_init());
    ESP_ERROR_CHECK(esp_event_loop_create_default());
    esp_netif_create_default_wifi_ap();
    esp_netif_create_default_wifi_sta();
    
    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,
                                                        &event_handler,
                                                        NULL,
                                                        &instance_any_id));
                                                        
    ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
                                                        IP_EVENT_STA_GOT_IP,
                                                        &event_handler,
                                                        NULL,
                                                        &instance_got_ip));



//webserver event handler

    ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
                                                        ESP_EVENT_ANY_ID,
                                                        &wifi_event_handler,
                                                        NULL,
                                                        NULL));
                                


    ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_AP_STAIPASSIGNED, &connect_handler, &server));

    

    char ssid[30] = "test";
    char password[30] = "test";
    return (wifi_STA_start( (uint8_t*)ssid,(uint8_t*)password ));                                                 
}

The following code handles the wifi Station mode connection :

Code: Select all


bool wifi_STA_start(uint8_t* SSID, uint8_t* password){

    bool connection_status = 0;
    s_wifi_event_group = xEventGroupCreate();
    wifi_config_t wifi_config = {
        .sta = {
            //.ssid = EXAMPLE_ESP_WIFI_SSID_STA,
            //.password = EXAMPLE_ESP_WIFI_PASS_STA,
            // Setting a password implies station will connect to all security modes including WEP/WPA.
            // However these modes are deprecated and not advisable to be used. Incase your Access point
            //  doesn't support WPA2, these mode can be enabled by commenting below line 
	     .threshold.authmode = WIFI_AUTH_WPA2_PSK,

            .pmf_cfg = {
                .capable = true,
                .required = false
            },
        },
    };
    strcpy((char *)wifi_config.sta.ssid,(char *)SSID);
    strcpy((char *)wifi_config.sta.password,(char *)password);

    if(wifi_mode == 1){ // if previous mode was 1(AP)
        esp_wifi_stop();
        esp_wifi_set_mode(WIFI_MODE_NULL);
    }

    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
    ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
    ESP_ERROR_CHECK(esp_wifi_start() );

    // Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum number 
    //of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) 
    EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
            WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
            pdFALSE,
            pdFALSE,
            portMAX_DELAY);

    //xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually happened. 
    if (bits & WIFI_CONNECTED_BIT) {
        ESP_LOGI(TAG_WIFI, "connected to ap SSID:%s, password:%s",
                 SSID, password);
                 connection_status = 1;
                 wifi_mode = 0;


    } else if (bits & WIFI_FAIL_BIT) {
        ESP_LOGI(TAG_WIFI, "Failed to connect to SSID:%s, password:%s",
                 SSID, password);
                 connection_status = 0;
    } else {
        ESP_LOGE(TAG_WIFI, "UNEXPECTED EVENT");
    }
    //The event will not be processed after unregister 
    //ESP_ERROR_CHECK(esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, instance_got_ip));
    //ESP_ERROR_CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, instance_any_id));
    vEventGroupDelete(s_wifi_event_group);
    return connection_status;   
}


The following code will handle the WiFi AP:

Code: Select all


bool wifi_AP_start(){

    wifi_config_t ap_config = {
        .ap = {
            .ssid = EXAMPLE_ESP_WIFI_SSID_AP,
            .ssid_len = strlen(EXAMPLE_ESP_WIFI_SSID_AP),
            .channel = EXAMPLE_ESP_WIFI_CHANNEL,
            .password = EXAMPLE_ESP_WIFI_PASS_AP,
            .max_connection = EXAMPLE_MAX_STA_CONN,
            .authmode = WIFI_AUTH_WPA_WPA2_PSK
        },
    };
    if (strlen(EXAMPLE_ESP_WIFI_PASS_AP) == 0) {
        ap_config.ap.authmode = WIFI_AUTH_OPEN;
    }

    if(wifi_mode == 0){
        esp_wifi_disconnect(); //<----- ONLY IF YOU WERE STA, NOT NEEDED WHEN STOPPING AP
        esp_wifi_stop();
        esp_wifi_set_mode(WIFI_MODE_NULL);
        vTaskDelay(100/portTICK_PERIOD_MS);
    }

    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP) );
    ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &ap_config) );
    ESP_ERROR_CHECK(esp_wifi_start() );

    ESP_LOGI(TAG_WEBSERVER, "wifi_init_softap finished. SSID:%s password:%s channel:%d",
             EXAMPLE_ESP_WIFI_SSID_AP, EXAMPLE_ESP_WIFI_PASS_AP, EXAMPLE_ESP_WIFI_CHANNEL);
    wifi_mode = 1;
    return 0;
}

In my main.c I call:

Code: Select all


    if(wifi_init() == 0){
        printf("failed to connect to WIFI, start AP \n");
        wifi_AP_start();
    }

After I click a button on my webserver ( If initial connection to STA is not sucesfull ), the following code will be executed:

Code: Select all


static esp_err_t set_wifi_handle(httpd_req_t *req)
{
    esp_err_t error;
    ESP_LOGI(TAG_WEBSERVER,"WIFI CREDENTIALS HAVE BEEN SET");

    wifi_STA_start((uint8_t*)wifi_ssid,(uint8_t*)wifi_password); // wifi ssid and wifi password contains the input from the webserver
    const char* resp_str = (const char*) req->user_ctx;
    error = httpd_resp_send(req,resp_str,strlen(resp_str));
    if(error !=ESP_OK){
        ESP_LOGI(TAG_WEBSERVER,"Error %d while sending response \n",error);
    }
    else{
        ESP_LOGI(TAG_WEBSERVER,"response sent sucesfully \n");
    }
    return error;

The connection will be succesfull if valid SSID and password are selected, however, shortly after the connection, I get the message on the serial monitor:

Code: Select all

I (335288) wifi:bcn_timout,ap_probe_send_start
The full log:

Code: Select all

I (3388) PCA9539: I2C initialized successfully
I (3388) gpio: GPIO[2]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0 
I (3908) BTDM_INIT: BT controller compile version [bfbbe1e]
I (3908) system_api: Base MAC address is not set
I (3908) system_api: read default base MAC address from EFUSE
I (3918) phy_init: phy_version 4670,719f9f6,Feb 18 2021,17:07:07
I (4258) SEC_GATTS_DEMO: BLE_setup init bluetooth
94:3C:C6:01:1D:46I (4348) SEC_GATTS_DEMO: The number handle = 4
I (4378) SEC_GATTS_DEMO: advertising start success
I (4388) wifi:wifi driver task: 3ffdd39c, prio:23, stack:6656, core=0
I (4408) wifi:wifi firmware version: 88c8747
I (4408) wifi:wifi certification version: v7.0
I (4408) wifi:config NVS flash: enabled
I (4408) wifi:config nano formating: disabled
I (4408) wifi:Init data frame dynamic rx buffer num: 32
I (4418) wifi:Init management frame dynamic rx buffer num: 32
I (4418) wifi:Init management short buffer num: 32
I (4428) wifi:Init dynamic tx buffer num: 32
I (4428) wifi:Init static rx buffer size: 1600
I (4438) wifi:Init static rx buffer num: 10
I (4438) wifi:Init dynamic rx buffer num: 32
I (4438) wifi_init: rx ba win: 6
I (4448) wifi_init: tcpip mbox: 32
I (4448) wifi_init: udp mbox: 6
I (4458) wifi_init: tcp mbox: 6
I (4458) wifi_init: tcp tx win: 5744
I (4458) wifi_init: tcp rx win: 5744
I (4468) wifi_init: tcp mss: 1440
I (4468) wifi_init: WiFi IRAM OP enabled
I (4478) wifi_init: WiFi RX IRAM OP enabled
I (4668) wifi:mode : sta (94:3c:c6:01:1d:44)
I (4668) wifi:enable tsf
I (7808) wifi station: retry to connect to the AP
I (7808) wifi station: connect to the AP fail
I (10948) wifi station: retry to connect to the AP
I (10958) wifi station: connect to the AP fail
I (14098) wifi station: retry to connect to the AP
I (14098) wifi station: connect to the AP fail
I (17238) wifi station: retry to connect to the AP
I (17238) wifi station: connect to the AP fail
I (20378) wifi station: retry to connect to the AP
I (20378) wifi station: connect to the AP fail
I (23508) wifi station: retry to connect to the AP
I (23508) wifi station: connect to the AP fail
I (26648) wifi station: retry to connect to the AP
I (26648) wifi station: connect to the AP fail
I (29788) wifi station: retry to connect to the AP
I (29788) wifi station: connect to the AP fail
I (32928) wifi station: retry to connect to the AP
I (32928) wifi station: connect to the AP fail
I (36068) wifi station: retry to connect to the AP
I (36068) wifi station: connect to the AP fail
I (39208) wifi station: connect to the AP fail
I (39208) wifi station: Failed to connect to SSID:test.4G, password:test
failed to connect to WIFI, start AP
I (39218) wifi:flush txq
I (39218) wifi:stop sw txq
I (39218) wifi:lmac stop hw txq
I (39358) wifi:mode : softAP (94:3c:c6:01:1d:45)
I (39358) wifi:Total power save buffer number: 16
I (39358) wifi:Init max length of beacon: 752/752
I (39358) wifi:Init max length of beacon: 752/752
I (39358) webserver: wifi_init_softap finished. SSID:default_ssid password:default_pass channel:1
initial app_temp_global = 7
I (39378) uart: queue free spaces: 10
I (55038) wifi:new:<1,1>, old:<1,1>, ap:<1,1>, sta:<255,255>, prof:1
I (55038) wifi:station: 4c:1d:96:35:0f:89 join, AID=1, bgn, 40U
I (55068) webserver: station 4c:1d:96:35:0f:89 join, AID=1
I (55078) esp_netif_lwip: DHCP server assigned IP to a station, IP is: 192.168.4.2
I (55078) webserver: Starting webserver
I (55088) webserver: Starting server on port: '80'
I (55088) webserver: Registering URI handlers
I (60888) webserver: This is index page
I (60888) webserver: response sent sucesfully

W (63708) wifi:<ba-add>idx:4 (ifx:1, 4c:1d:96:35:0f:89), tid:0, ssn:121, winSize:64
I (301818) webserver: WIFI CREDENTIALS HAVE BEEN SET
I (301828) wifi:station: 4c:1d:96:35:0f:89 leave, AID = 1, bss_flags is 658531, bss:0x3ffbbc68
I (301828) wifi:new:<1,0>, old:<1,1>, ap:<1,1>, sta:<255,255>, prof:1
W (301828) wifi:<ba-del>idx
I (301838) webserver: station 4c:1d:96:35:0f:89 leave, AID=1
I (301898) wifi:flush txq
I (301898) wifi:stop sw txq
I (301898) wifi:lmac stop hw txq
I (302088) wifi:mode : sta (94:3c:c6:01:1d:44)
I (302088) wifi:enable tsf
I (302108) wifi:new:<4,0>, old:<1,0>, ap:<255,255>, sta:<4,0>, prof:1
I (302108) wifi:state: init -> auth (b0)
I (302118) wifi:state: auth -> assoc (0)
I (302128) wifi:state: assoc -> run (10)
I (302138) wifi:connected with my_wifi_ssid, aid = 4, channel 4, BW20, bssid = 02:1e:42:28:f1:a7
I (302148) wifi:security: WPA2-PSK, phy: bgn, rssi: -65
I (302158) wifi:pm start, type: 1

I (302168) wifi:AP's beacon interval = 102400 us, DTIM period = 2
W (302168) wifi:<ba-add>idx:0 (ifx:0, 02:1e:42:28:f1:a7), tid:6, ssn:2, winSize:64
I (302868) esp_netif_handlers: sta ip: 192.168.1.231, mask: 255.255.255.0, gw: 192.168.1.1
I (302868) wifi station: got ip:192.168.1.231
I (302868) wifi station: connected to ap SSID:my_wifi_ssid, password:my_wifi_password
W (302878) httpd_txrx: httpd_sock_err: error in send : 113
I (302888) webserver: Error 45062 while sending response

W (302888) httpd_uri: httpd_uri: uri handler execution failed
W (302898) httpd: httpd_accept_conn: error in accept (128)
W (302908) httpd: httpd_server: error accepting new connection
I (308168) wifi:bcn_timout,ap_probe_send_start
I (317268) wifi:bcn_timout,ap_probe_send_start
I (326278) wifi:bcn_timout,ap_probe_send_start
I (335288) wifi:bcn_timout,ap_probe_send_start
I (344448) wifi:bcn_timout,ap_probe_send_start
I (353458) wifi:bcn_timout,ap_probe_send_start
I (362478) wifi:bcn_timout,ap_probe_send_start

I have noticed that when I remove all my BLE code, I do not have this issue anymore which leads me to believe that this could be memory problem. My program is quite complicated and ofcourse there are more things happening other than WiFi and BLE. Could someone point me in the right direction how I can debug this and confirm whether this really is memory issue or it is something else? Thanks in advance.

Things I have tried so far:

1. modify the Freertos timer task stack size to 4096 (was 2048 initially)
No difference
2. Disable WiFi and BLE coexistance (was enabled initially)
Seems to fix the issue. I havent tried to run it for long time but it does not timeout immediately. Can someone help me understand whether I should have it ON or OFF and what exactly is the difference

Who is online

Users browsing this forum: kaxx1975 and 62 guests