BT initialization error

qubyts
Posts: 1
Joined: Mon Feb 05, 2024 6:33 pm

BT initialization error

Postby qubyts » Mon Feb 05, 2024 6:38 pm

I'm trying to implement ble to enter wifi credentials, however i get this error when after flashing at startup on my esp32c3-devkit1m
BLE: Bluetooth controller initialize failed: ESP_ERR_INVALID_STATE[1B]
BLE: BluetGuru Meditation Error: Core 0 panic'ed (Interrupt wdt timeout on CPU0).

Anyone knows what might be going wrong here?

  1. #include "freertos/FreeRTOS.h"
  2. #include "freertos/task.h"
  3. #include "esp_log.h"
  4. #include "nvs_flash.h"
  5. #include "esp_bt.h"
  6. #include "esp_bt_main.h"
  7. #include "esp_gatts_api.h"
  8. #include "esp_gap_ble_api.h"
  9. #include "esp_gatt_common_api.h"
  10. #include "esp_wifi.h"
  11. #include "esp_event.h"
  12. #include "esp_system.h"
  13. #include "driver/gpio.h"
  14. #include <stdbool.h>
  15. #include <string.h>
  16. #include <stdio.h>
  17. #include "esp_netif.h"
  18. #include "esp_netif_types.h"
  19. #include "esp_wifi_types.h"
  20.  
  21.  
  22. #define GATTS_SERVICE_UUID   0x00FF
  23. #define GATTS_CHAR_UUID_SSID 0xFF01
  24. #define GATTS_CHAR_UUID_PASS 0xFF02
  25. #define GATTS_NUM_HANDLE     6
  26. #define DEVICE_NAME          "ESP32_C3_BLE_WIFI"
  27. #define BUTTON_GPIO          7
  28.  
  29. static uint8_t adv_config_done = 0;
  30. static esp_gatt_char_prop_t a_property = ESP_GATT_CHAR_PROP_BIT_READ | ESP_GATT_CHAR_PROP_BIT_WRITE | ESP_GATT_CHAR_PROP_BIT_NOTIFY;
  31. static uint16_t gatts_service_handle;
  32. static esp_gatt_if_t gatts_if = ESP_GATT_IF_NONE; // Correct initialization to avoid unused variable warning
  33.  
  34. static uint8_t ssid[32] = {0};
  35. static uint8_t password[64] = {0};
  36. static bool credentials_received = false;
  37. static uint16_t char_handle_ssid;
  38. static uint16_t char_handle_pass;
  39.  
  40.  
  41. static esp_ble_adv_params_t adv_params = {
  42.     .adv_int_min       = 0x20,
  43.     .adv_int_max       = 0x40,
  44.     .adv_type          = ADV_TYPE_IND,
  45.     .own_addr_type     = BLE_ADDR_TYPE_PUBLIC,
  46.     .channel_map       = ADV_CHNL_ALL,
  47.     .adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
  48. };
  49.  
  50. static void gatts_profile_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
  51. static void wifi_init_sta(void);
  52.  
  53. static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) {
  54.     switch (event) {
  55.         case ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT:
  56.             adv_config_done |= 1 << 0;
  57.             if (adv_config_done == 0x03) {
  58.                 esp_ble_gap_start_advertising(&adv_params);
  59.             }
  60.             break;
  61.         case ESP_GAP_BLE_SCAN_RSP_DATA_SET_COMPLETE_EVT:
  62.             adv_config_done |= 1 << 1;
  63.             if (adv_config_done == 0x03) {
  64.                 esp_ble_gap_start_advertising(&adv_params);
  65.             }
  66.             break;
  67.         case ESP_GAP_BLE_ADV_START_COMPLETE_EVT:
  68.             // Advertising started
  69.             break;
  70.         case ESP_GAP_BLE_ADV_STOP_COMPLETE_EVT:
  71.             // Advertising stopped
  72.             break;
  73.         default:
  74.             break;
  75.     }
  76. }
  77.  
  78. static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if_param, esp_ble_gatts_cb_param_t *param) {
  79.     switch (event) {
  80.         case ESP_GATTS_REG_EVT:
  81.             gatts_if = gatts_if_param; // Address undeclared variable error
  82.             esp_ble_gap_set_device_name(DEVICE_NAME);
  83.             esp_ble_gap_config_adv_data(&adv_params); // Placeholder for advertising data configuration
  84.             break;
  85.         case ESP_GATTS_READ_EVT:
  86.         case ESP_GATTS_WRITE_EVT:
  87.             if (param->write.handle == char_handle_ssid) {
  88.                 // Handle SSID write event
  89.                 memcpy(ssid, param->write.value, param->write.len);
  90.                 ssid[param->write.len] = '\0'; // Null-terminate the SSID
  91.             } else if (param->write.handle == char_handle_pass) {
  92.                 // Handle Password write event
  93.                 memcpy(password, param->write.value, param->write.len);
  94.                 password[param->write.len] = '\0'; // Null-terminate the password
  95.                 credentials_received = true;
  96.                 wifi_init_sta(); // Attempt to connect to WiFi
  97.             }
  98.             break;
  99.         // Handle other events, especially those mentioned in the errors
  100.         default:
  101.             // Optionally log unhandled events
  102.             break;
  103.     }
  104. }
  105.  
  106.  
  107. void wifi_init_sta(void) {
  108.     if (!credentials_received) {
  109.         return; // Don't attempt to connect without credentials
  110.     }
  111.  
  112.     // Initialize and start WiFi
  113.     wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  114.     ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  115.     ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
  116.  
  117.     wifi_config_t wifi_config = {0};
  118.     memcpy(wifi_config.sta.ssid, ssid, sizeof(ssid));
  119.     memcpy(wifi_config.sta.password, password, sizeof(password));
  120.  
  121.     ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
  122.     ESP_ERROR_CHECK(esp_wifi_start());
  123.     ESP_ERROR_CHECK(esp_wifi_connect());
  124. }
  125.  
  126. static void button_isr_handler(void* arg) {
  127.     // Start BLE advertising for a limited time
  128.     esp_ble_gap_start_advertising(&adv_params);
  129. }
  130.  
  131. static void button_init(void) {
  132.     esp_rom_gpio_pad_select_gpio(BUTTON_GPIO); // Correct the function call
  133.         gpio_set_direction(BUTTON_GPIO, GPIO_MODE_INPUT);
  134.         gpio_set_intr_type(BUTTON_GPIO, GPIO_INTR_POSEDGE);
  135.         gpio_install_isr_service(0);
  136.         gpio_isr_handler_add(BUTTON_GPIO, button_isr_handler, NULL);
  137. }
  138.  
  139. void ble_func(void) {
  140.        // Initialize NVS first, required for most operations
  141.         esp_err_t ret = nvs_flash_init();
  142.         if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  143.             ESP_ERROR_CHECK(nvs_flash_erase()); // Erase and re-init if needed
  144.             ret = nvs_flash_init();
  145.         }
  146.         ESP_ERROR_CHECK(ret);
  147.  
  148. //      // Release the memory for Classic Bluetooth before initializing the controller
  149. //      ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT));
  150.  
  151.         esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
  152.         ret = esp_bt_controller_init(&bt_cfg);
  153.         if (ret) {
  154.             ESP_LOGE("BLE", "Bluetooth controller initialize failed: %s", esp_err_to_name(ret));
  155.             return;
  156.         }
  157.  
  158.         ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);
  159.         if (ret) {
  160.             ESP_LOGE("BLE", "Bluetooth controller enable failed: %s", esp_err_to_name(ret));
  161.             return;
  162.         }
  163.  
  164.     ret = esp_bluedroid_init();
  165.     if (ret) {
  166.         ESP_LOGE("BLE", "Bluedroid initialize failed: %s", esp_err_to_name(ret));
  167.         return;
  168.     }
  169.  
  170.     ret = esp_bluedroid_enable();
  171.     if (ret) {
  172.         ESP_LOGE("BLE", "Bluedroid enable failed: %s", esp_err_to_name(ret));
  173.         return;
  174.     }
  175.  
  176.     // Initialize BLE GATT server
  177.     esp_ble_gatts_register_callback(gatts_event_handler);
  178.     esp_ble_gap_register_callback(gap_event_handler);
  179.     esp_ble_gatts_app_register(GATTS_SERVICE_UUID);
  180.  
  181.     // Initialize button
  182.     button_init();
  183. }
  184.  
  185. void ble_task(void *pvParameters) {
  186.     ble_func();
  187.     xTaskCreate(ble_task, "BLE Task", 2048, NULL, 10, NULL);
  188.  
  189.     vTaskDelete(NULL); // Clean up the task when finished.
  190. }

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

Re: BT initialization error

Postby liaifat85 » Wed Feb 07, 2024 12:40 pm

"Bluetooth controller initialize failed: ESP_ERR_INVALID_STATE," typically indicates a problem with the Bluetooth controller during initialization. Please check your Bluetooth configuration settings.

MicroController
Posts: 1597
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: BT initialization error

Postby MicroController » Fri Feb 09, 2024 11:39 am

Code: Select all

void ble_task(void *pvParameters) {
    ble_func();
    xTaskCreate(ble_task, "BLE Task", 2048, NULL, 10, NULL);
This recursively executes ble_task, and ble_func(), over and over again; I expect esp_bt_controller_init() to return ESP_ERR_INVALID_STATE when it is invoked for the 2nd time, when the controller is already initialized.

Who is online

Users browsing this forum: Bing [Bot] and 57 guests