🛠 Publicité BLE 5 en longue portée (Coded PHY)

ESP32-C3 · intermediaire · Schéma : IMG-esp32-c3-intermediaire-3-ble5-advertise-long-range.svg — ESP32-C3 émettant une publicité BLE 5 sur PHY codé 125 kbit/s, ondes de portée doublée vers un smartphone

Objectives

The ESP32-C3 is a Bluetooth 5.0 radio: it supports the coded PHY (Coded PHY, 125 or 500 kbit/s), which doubles or quadruples the advertising range compared to classic BLE 4.2 at 1 Mbit/s. You will configure the Bluedroid stack to transmit on this extended PHY and verify it with a compatible central device.

Hardware

Full code

#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include "esp_gap_ble_api.h"

// Publicite : drapeau LE General + nom "C3 LONGRANGE"
static uint8_t adv_data[] = {
  0x02, 0x01, 0x06,
  0x0D, 0x09, 'C','3',' ','L','O','N','G','R','A','N','G','E'
};

void setup {
  Serial.begin(115200);
  BLEDevice::init("C3-LONGRANGE");

  // BLE 5 : emission sur PHY code 125 kbit/s (longue portee)
  esp_ble_gap_config_own_le_phy(BLE_LE_CODED_PHY);
  esp_ble_gap_config_adv_data_raw(adv_data, sizeof(adv_data));

  esp_ble_adv_params_t params = {
    .adv_int_min        = 0x100,
    .adv_int_max        = 0x160,
    .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,
  };
  esp_ble_gap_start_advertising(&params);
  Serial.println(F("Publicite emise sur PHY code (BLE 5)"));
}

void loop { delay(1000); }

Expected verification

nRF Connect on a BLE 5 central displays “C3 LONGRANGE”; by moving the board several tens of meters away outdoors, the advertising remains received where classic 1 Mbit/s advertising would have disappeared. The controller log indicates the coded PHY used.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials