Wake up ESP32C6 from light sleep using a BT button
Posted: Thu Jun 20, 2024 7:43 am
Hi, I'm developing a project with VSCode and ESP-IDF for a ESP32-C6. I want to preserve as much battery as possible, so I'm trying to keep it in light sleep and use a Shelly BLU button ( https://shellyspain.com/shelly-blu-button1-blanco.html ) as a BlueTooth wake up source, but no luck so far.
In my code, I set the scan parameters to only accept devices from the white list:
The callback function looks like this:
Before starting the main loop, I call this function to initialize Bluetooth and start it:
With this setup, I can detect the press of the button if the chip is not in sleep mode but nothing else. For some reason, sometimes the C6 wakes up from the sleep mode with ESP_SLEEP_WAKEUP_BT as the reason, but it's not because of the button.
I have tried to set the automatic light mode, adding this code:
and in sdkconfig, activating support for power management, FreeRTOS tickless idle support and External 32KHz crystal as RTC clock source (although I don't have one currently).
But then the C6 keeps resetting itself everytime it tries to go to light sleep.
Can anyone offer some guide, please? I also attached the current sdkconfig file of the project, just in case someone sees something wrong there (right now there's no automatic light sleep configuration active)
Thank you in advance!
In my code, I set the scan parameters to only accept devices from the white list:
- #define TARGET_ADDR "90:ab:96:a0:85:86" //Shelly BLU button public address
- static esp_ble_scan_params_t ble_scan_params = {
- .scan_type = BLE_SCAN_TYPE_ACTIVE,
- .own_addr_type = BLE_ADDR_TYPE_PUBLIC,
- .scan_filter_policy = BLE_SCAN_FILTER_ALLOW_ONLY_WLST,
- .scan_interval = 0x50,
- .scan_window = 0x30,
- .scan_duplicate = BLE_SCAN_DUPLICATE_DISABLE
- };
- void gap_callback(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) {
- switch (event) {
- case ESP_GAP_BLE_SCAN_PARAM_SET_COMPLETE_EVT: {
- esp_ble_gap_start_scanning(0); //Scan forever
- break;
- }
- case ESP_GAP_BLE_SCAN_RESULT_EVT: {
- esp_ble_gap_cb_param_t *scan_result = (esp_ble_gap_cb_param_t *)param;
- switch (scan_result->scan_rst.search_evt) {
- case ESP_GAP_SEARCH_INQ_RES_EVT: {
- char addr_str[18];
- sprintf(addr_str, "%02x:%02x:%02x:%02x:%02x:%02x",
- scan_result->scan_rst.bda[0], scan_result->scan_rst.bda[1], scan_result->scan_rst.bda[2],
- scan_result->scan_rst.bda[3], scan_result->scan_rst.bda[4], scan_result->scan_rst.bda[5]);
- if (strcmp(addr_str, TARGET_ADDR) == 0)
- {
- if(scan_result->scan_rst.ble_adv[scan_result->scan_rst.adv_data_len - 1] != 0)
- {
- ESP_LOGI(TAG_BLE, "Advertised Device: %s", addr_str);
- }
- }
- break;
- }
- case ESP_GAP_SEARCH_INQ_CMPL_EVT: {
- ESP_LOGI(TAG_BLE, "Scan complete");
- scan_complete = true;
- break;
- }
- default:
- break;
- }
- break;
- }
- default:
- break;
- }
- }
- void ScanBT() {
- esp_err_t ret;
- // Initialize NVS
- 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);
- 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();
- ESP_ERROR_CHECK(esp_bt_controller_init(&bt_cfg));
- ESP_ERROR_CHECK(esp_bt_controller_enable(ESP_BT_MODE_BLE));
- ESP_ERROR_CHECK(esp_bluedroid_init());
- ESP_ERROR_CHECK(esp_bluedroid_enable());
- esp_ble_gap_clear_whitelist();
- ESP_ERROR_CHECK(esp_ble_gap_register_callback(gap_callback));
- uint8_t whitelist_addr[6] = {0x90, 0xab, 0x96, 0xa0, 0x85, 0x86}; //Shelly BLU button public address
- esp_ble_gap_update_whitelist(true, whitelist_addr, BLE_WL_ADDR_TYPE_PUBLIC);
- ESP_ERROR_CHECK(esp_ble_gap_set_scan_params(&ble_scan_params));
- esp_sleep_enable_bt_wakeup();
- ESP_LOGI(TAG_BLE, "Scanning...");
- scan_complete = false;
- esp_ble_gap_update_whitelist(true, whitelist_addr, BLE_WL_ADDR_TYPE_RANDOM);
- }
I have tried to set the automatic light mode, adding this code:
- esp_pm_config_t pm_config = {
- .max_freq_mhz = 80,
- .min_freq_mhz = 10,
- .light_sleep_enable = true
- };
- ESP_ERROR_CHECK( esp_pm_configure(&pm_config) );
But then the C6 keeps resetting itself everytime it tries to go to light sleep.
Can anyone offer some guide, please? I also attached the current sdkconfig file of the project, just in case someone sees something wrong there (right now there's no automatic light sleep configuration active)
Thank you in advance!