BLE 4 Coded

NickRoma
Posts: 1
Joined: Mon Jan 09, 2023 1:26 pm

BLE 4 Coded

Postby NickRoma » Mon Jan 09, 2023 1:49 pm

I am using an ESP32-S3-WROOM-1 chip as a BLE 5 GATT client (this is what we call the “brain”) and an ESP32-C3-WROOM-02 as the BLE 5 GATT server (this is a “sensor”). Both devices are headless, there are no buttons or screens.

To conserve battery life on the sensor, the sensor will go into a deep sleep state and wake up periodically to transmit information to the brain. I had a lot of trouble understanding the best way to implement the BLE functionality, but I was able to piece together a solution based on the ESP32 code examples. Although it seems to work, it needs to be improved. Let me share with you, my concerns. I greatly appreciate any help you can offer.

To aid in my explanation, I have provided you a link to a folder on my OneDrive. The password is: 3SPHelp$

https://1drv.ms/u/s!AqYgHKn2LJ7yhbYRWzB4R_Arxl-HAg?e=0X7LdK

• ble_client.c – BLE code used on the brain. Some contents of the file has been changed/deleted.
• ble_server.c – BLE code used on the sensor. Some contents of the file has been changed/deleted.
• ble5-gatt-slow-esp-idf.mp4 – Video to demonstrate the poor performance connecting via BLE.

Q1. It takes a very long time to establish a connection between the GATT server and client.

When the sensor wakes up, the process of connecting, reading data, and disconnecting will take about 27 seconds. This is a very long time and I wonder how it can be improved. If you watch the video I have provided you, it will be clear what I am referring to.

In the video:

• The brain is shown on the left and the sensor is on the right. Ignore the information that is displayed on the screen when the video begins.
• At 0:03 the sensor begins wakes up and begins to advertise. The brain starts the connection process almost immediately - this is fantastic.
• At 0:07 the connection is established. It took almost 4 seconds. Now the devices show the “ESP_LE_AUTH_REQ_SC_BOND” message.
• At 0:20 the devices have bonded. This took 13 seconds. It is too long in my opinion.
• The last several seconds of the video, data is transferred.

How can I improve the length of time needed for the entire process I illustrated above. As I mentioned earlier, I struggled to understand best practices implementing BLE using IDF. Do I even need to “bond” the devices? Can I skip any of the steps?


Q2. Am I using BLE 5 long range (coded)?

At the time I developed this, there were very few examples with BLE 5 coded. In this design, I want to use BLE 5 coded at the longest range possible. I do not need to send a lot of data and I do not need to send data at a fast speed. Distance is very important to me.

I was unable to confirm my code establishes a BLE 5 coded connection. As an example, I couldn’t figure out how to have the sensor send the BLE advertisement using BLE 5 coded. If the advertisement is not received by the brain, then it will not know to establish a connection.

Are you able to either review my code files to see if I have correctly set the BLE protocols and advertising correctly? Or, if you can provide a more complete BLE 5 coded example with GATT I am happy to do this myself.

ESP_zhanghaipeng
Posts: 42
Joined: Thu May 04, 2023 3:49 am

Re: BLE 4 Coded

Postby ESP_zhanghaipeng » Wed Jun 14, 2023 9:40 am

Answer of Q1:

The process of connecting, reading data, and disconnecting takes approximately 27 seconds, which is likely due to an improper interval setting. To further optimize it, you can set a smaller interval using the following configuration:

```
const esp_ble_gap_conn_params_t phy_coded_conn_params = {
.scan_interval = 0x40,
.scan_window = 0x40,
.interval_min = 10,
.interval_max = 10,
.latency = 0,
.supervision_timeout = 600,
.min_ce_len = 0,
.max_ce_len = 0,
};
```



Answer of Q2:

Whether to use BLE 5 coded phy is determined by the server-side. You can use "esp_ble_gap_read_phy" to check if it is being used. If you want to change the phy, you can reconfigure it using "esp_ble_gap_set_preferred_phy".

BTW:
I haven't seen the file provided by OneDrive. It might have been deleted. If you still have any questions, you can upload the materials in the form of attachments.

alebel
Posts: 1
Joined: Thu Oct 05, 2023 10:54 am

Re: BLE 4 Coded

Postby alebel » Thu Oct 05, 2023 10:59 am

Can you please share the example how to use "esp_ble_gap_set_preferred_phy" ?
In my case when I use "esp_ble_gap_set_preferred_phy" I got below error:
BT_BTM: BTM_BleSetPreferPhy, invalid parameters

ESP_zhanghaipeng
Posts: 42
Joined: Thu May 04, 2023 3:49 am

Re: BLE 4 Coded

Postby ESP_zhanghaipeng » Sun Oct 08, 2023 11:29 am

How do you use the `esp_ble_gap_set_preferred_phy` API?



```
//when connected
esp_bd_addr_t remote_device_address = {0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC};//peer bluetooth addr
esp_ble_gap_all_phys_t all_phys = 0;
esp_ble_gap_phy_mask_t tx_phy = ESP_BLE_GAP_PHY_2M_PREF_MASK; // 2Mbps data rate
esp_ble_gap_phy_mask_t rx_phy = ESP_BLE_GAP_PHY_2M_PREF_MASK;
esp_ble_gap_prefer_phy_options_t phy_options = ESP_BLE_GAP_PHY_OPTIONS_NO_PREF;

esp_err_t result = esp_ble_gap_set_preferred_phy(remote_device_address,
all_phys,tx_phy,rx_phyphy_options)
```

Each parameter's specific description can be found in the BLUETOOTH CORE SPECIFICATION Version 5.4 | Vol 4, Part E page 2440.
set_pre_phy.png
set_pre_phy.png (212.26 KiB) Viewed 2334 times

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

Re: BLE 4 Coded

Postby MicroController » Sun Oct 08, 2023 4:11 pm

I want to use BLE 5 coded at the longest range possible
Then you probably want to use ESP_zhanghaipeng's code with

Code: Select all

esp_ble_gap_phy_mask_t tx_phy = ESP_BLE_GAP_PHY_CODED_PREF_MASK;
esp_ble_gap_phy_mask_t rx_phy = ESP_BLE_GAP_PHY_CODED_PREF_MASK;
esp_ble_gap_prefer_phy_options_t phy_options = ESP_BLE_GAP_PHY_OPTIONS_PREF_S8_CODING;
and/or

Code: Select all

esp_ble_gap_set_preferred_default_phy(ESP_BLE_GAP_PHY_CODED_PREF_MASK, ESP_BLE_GAP_PHY_CODED_PREF_MASK);
And you may want to use "extended advertisements", allowing you to set esp_ble_gap_ext_adv_params_t::primary_phy to ESP_BLE_GAP_PRI_PHY_CODED.

Or, you skip the complexity and overhead of Bluetooth alltogether and just use ESP-NOW.

Who is online

Users browsing this forum: Accound and 96 guests