BT initialization error
Posted: 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?
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?
- #include "freertos/FreeRTOS.h"
- #include "freertos/task.h"
- #include "esp_log.h"
- #include "nvs_flash.h"
- #include "esp_bt.h"
- #include "esp_bt_main.h"
- #include "esp_gatts_api.h"
- #include "esp_gap_ble_api.h"
- #include "esp_gatt_common_api.h"
- #include "esp_wifi.h"
- #include "esp_event.h"
- #include "esp_system.h"
- #include "driver/gpio.h"
- #include <stdbool.h>
- #include <string.h>
- #include <stdio.h>
- #include "esp_netif.h"
- #include "esp_netif_types.h"
- #include "esp_wifi_types.h"
- #define GATTS_SERVICE_UUID 0x00FF
- #define GATTS_CHAR_UUID_SSID 0xFF01
- #define GATTS_CHAR_UUID_PASS 0xFF02
- #define GATTS_NUM_HANDLE 6
- #define DEVICE_NAME "ESP32_C3_BLE_WIFI"
- #define BUTTON_GPIO 7
- static uint8_t adv_config_done = 0;
- 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;
- static uint16_t gatts_service_handle;
- static esp_gatt_if_t gatts_if = ESP_GATT_IF_NONE; // Correct initialization to avoid unused variable warning
- static uint8_t ssid[32] = {0};
- static uint8_t password[64] = {0};
- static bool credentials_received = false;
- static uint16_t char_handle_ssid;
- static uint16_t char_handle_pass;
- static esp_ble_adv_params_t adv_params = {
- .adv_int_min = 0x20,
- .adv_int_max = 0x40,
- .adv_type = ADV_TYPE_IND,
- .own_addr_type = BLE_ADDR_TYPE_PUBLIC,
- .channel_map = ADV_CHNL_ALL,
- .adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
- };
- 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);
- static void wifi_init_sta(void);
- static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) {
- switch (event) {
- case ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT:
- adv_config_done |= 1 << 0;
- if (adv_config_done == 0x03) {
- esp_ble_gap_start_advertising(&adv_params);
- }
- break;
- case ESP_GAP_BLE_SCAN_RSP_DATA_SET_COMPLETE_EVT:
- adv_config_done |= 1 << 1;
- if (adv_config_done == 0x03) {
- esp_ble_gap_start_advertising(&adv_params);
- }
- break;
- case ESP_GAP_BLE_ADV_START_COMPLETE_EVT:
- // Advertising started
- break;
- case ESP_GAP_BLE_ADV_STOP_COMPLETE_EVT:
- // Advertising stopped
- break;
- default:
- break;
- }
- }
- 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) {
- switch (event) {
- case ESP_GATTS_REG_EVT:
- gatts_if = gatts_if_param; // Address undeclared variable error
- esp_ble_gap_set_device_name(DEVICE_NAME);
- esp_ble_gap_config_adv_data(&adv_params); // Placeholder for advertising data configuration
- break;
- case ESP_GATTS_READ_EVT:
- case ESP_GATTS_WRITE_EVT:
- if (param->write.handle == char_handle_ssid) {
- // Handle SSID write event
- memcpy(ssid, param->write.value, param->write.len);
- ssid[param->write.len] = '\0'; // Null-terminate the SSID
- } else if (param->write.handle == char_handle_pass) {
- // Handle Password write event
- memcpy(password, param->write.value, param->write.len);
- password[param->write.len] = '\0'; // Null-terminate the password
- credentials_received = true;
- wifi_init_sta(); // Attempt to connect to WiFi
- }
- break;
- // Handle other events, especially those mentioned in the errors
- default:
- // Optionally log unhandled events
- break;
- }
- }
- void wifi_init_sta(void) {
- if (!credentials_received) {
- return; // Don't attempt to connect without credentials
- }
- // Initialize and start WiFi
- wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
- ESP_ERROR_CHECK(esp_wifi_init(&cfg));
- ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
- wifi_config_t wifi_config = {0};
- memcpy(wifi_config.sta.ssid, ssid, sizeof(ssid));
- memcpy(wifi_config.sta.password, password, sizeof(password));
- ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
- ESP_ERROR_CHECK(esp_wifi_start());
- ESP_ERROR_CHECK(esp_wifi_connect());
- }
- static void button_isr_handler(void* arg) {
- // Start BLE advertising for a limited time
- esp_ble_gap_start_advertising(&adv_params);
- }
- static void button_init(void) {
- esp_rom_gpio_pad_select_gpio(BUTTON_GPIO); // Correct the function call
- gpio_set_direction(BUTTON_GPIO, GPIO_MODE_INPUT);
- gpio_set_intr_type(BUTTON_GPIO, GPIO_INTR_POSEDGE);
- gpio_install_isr_service(0);
- gpio_isr_handler_add(BUTTON_GPIO, button_isr_handler, NULL);
- }
- void ble_func(void) {
- // Initialize NVS first, required for most operations
- 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()); // Erase and re-init if needed
- ret = nvs_flash_init();
- }
- ESP_ERROR_CHECK(ret);
- // // Release the memory for Classic Bluetooth before initializing the controller
- // ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT));
- esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
- ret = esp_bt_controller_init(&bt_cfg);
- if (ret) {
- ESP_LOGE("BLE", "Bluetooth controller initialize failed: %s", esp_err_to_name(ret));
- return;
- }
- ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);
- if (ret) {
- ESP_LOGE("BLE", "Bluetooth controller enable failed: %s", esp_err_to_name(ret));
- return;
- }
- ret = esp_bluedroid_init();
- if (ret) {
- ESP_LOGE("BLE", "Bluedroid initialize failed: %s", esp_err_to_name(ret));
- return;
- }
- ret = esp_bluedroid_enable();
- if (ret) {
- ESP_LOGE("BLE", "Bluedroid enable failed: %s", esp_err_to_name(ret));
- return;
- }
- // Initialize BLE GATT server
- esp_ble_gatts_register_callback(gatts_event_handler);
- esp_ble_gap_register_callback(gap_event_handler);
- esp_ble_gatts_app_register(GATTS_SERVICE_UUID);
- // Initialize button
- button_init();
- }
- void ble_task(void *pvParameters) {
- ble_func();
- xTaskCreate(ble_task, "BLE Task", 2048, NULL, 10, NULL);
- vTaskDelete(NULL); // Clean up the task when finished.
- }