STM32 Simple BLE Beacon

Introduction

This is a simple BLE Beacon sample design using STM32 Leaf. Send the string “HELLO BEACON” for a period of time, then repeat the sleep cycle.

Sample source code

The source code for the sketches and tools used can be viewed in the repository below.

Sample-Sketches/STM32_Simple_Beacon at master · Leafony/Sample-Sketches

What to prepare

Leaf to use

How it works

Explanation of the sketch

Flowchart

As shown in the figure below, the loop() function of STM32_Simple_Beacon.ino works by sending Beacon packets (Advertising data) while the STM32 leaf and BLE leaf alternately repeat the sleep state and active state for a certain period of time. We have achieved a power-saving system.

When the STM32 leaf executes StartAdvData(), it sets the Advertising data it wants to broadcast on the BLE leaf and sends a command to start sending it.

Since the STM32 leaf does not do anything while the BLE leaf is Advertising, it enters the deep sleep mode with the LowPower.deepSleep() function, and returns to the active state after the time set with WAKE_INTERVAL has passed. During this time, the STM32 leaf operates in a very power-saving state by turning off the power of unnecessary circuit blocks, etc. so that it can operate with the minimum necessary power.

Since the STM32 leaf does not do anything while the BLE leaf is Advertising, it enters the deep sleep mode with the LowPower.deepSleep() function, and returns to the active state after the time set with WAKE_INTERVAL has passed. During this time, the STM32 leaf operates in a very power-saving state by turning off the power of unnecessary circuit blocks, etc. so that it can operate with the minimum necessary power.

After the STM32 leaf comes back from deep sleep, we immediately wake up the BLE leaf and run StartAdvData() again to return to the initial process.

Advertising data specifications

In the StartAdvData() function of STM32_Simple_Beacon.ino, we set the BLE leaf to Advertising data, set the device name to “Leaf_A” that will be displayed when the BLE Client (PC, smartphone, Raspberry Pi, or other device connected to BLE with Leafony) detects Leafony and sends the string “HELLO BEACON” as user data.

The adv_data[] array is the Advertising data. As described in the comments, this array is the data that connects the chunks of data AD Structure 1 to AD Structure 3. The contents of these data will be described later.

Register adv_data to the BLE leaf with ble112.ble_cmd_le_gap_set_adv_data() and run ble112.checkActivity() to wait for the signal from the BLE leaf side that the registration is completed.

By running ble112.ble_cmd_le_gap_start_advertising(), BLE will start Advertising and start broadcasting the Advertising data that you just set. By setting LE_GA_SCANNABLE_NON_CONNECTALBE, the BLE Client side can scan the Advertising data, but it is not allowed to establish a connection with this device. This setting is to save power by reducing the processing of the events that occur when a connection request comes in.

void StartAdvData()
{
  // Advertising data; 25byte MAX
  uint8_t adv_data[] = {
    // AD Structure 1: Flag
    (2),  //0: field length
    BGLIB_GAP_AD_TYPE_FLAGS,  //1: field type (0x01)
    (6),  //2: data
    // AD Structure 2: Complete local name
    (7),  //3: field length
    BGLIB_GAP_AD_TYPE_LOCALNAME_COMPLETE,  //4: field type (0x09)
    ('L'),  //5:
    ('e'),  //6:
    ('a'),  //7:
    ('f'),  //8:
    ('_'),  //9:
    ('A'),  //10:
    // AD Structure 3: Manufacture specific
    (13),   //11: field length
    (0xff), //12: field type (0xff)
    ('H'),  //13:
    ('E'),  //14:
    ('L'),  //15:
    ('L'),  //16:
    ('O'),  //17:
    (' '),  //18:
    ('B'),  //19:
    ('E'),  //20:
    ('A'),  //21:
    ('C'),  //22:
    ('O'),  //23:
    ('N'),  //24:
  };

  // Register advertising packet
  uint8_t stLen = sizeof(adv_data);
  ble112.ble_cmd_le_gap_set_adv_data(SCAN_RSP_ADVERTISING_PACKETS, stLen, adv_data);
  while (ble112.checkActivity(1000));

  // index = 0  LE_GAP_SCANNABLE_NON_CONNECTABLE / LE_GAP_UNDIRECTED_CONNECTABLE
  ble112.ble_cmd_le_gap_start_advertising(0, LE_GAP_USER_DATA, LE_GAP_SCANNABLE_NON_CONNECTABLE);
  while (ble112.checkActivity(1000));
}

The figure below shows the format of the Advertising data used in BLE Advertising.

Advertising data is composed of multiple chunks of data called AD Structure. The content of each AD Structure is divided into Length and Data, and the number of octets (bytes) of Data is registered in Length.

The contents of Data are further divided into AD Type and AD Data, with AD Type telling you what kind of data this AD Structure is.

For example, in the above code, AD Structure 2 has a Length of 7, the AD Type is BGLIB_GAP_AD_TYPE_LOCALNAME_COMPLETE (0x09) which is the local name of the device, and Leaf_A is registered in Data.

The AD Type of AD Structure 3 is 0xff, and any data can be registered in this AD Structure. The Data is the text “HELLO BEACON”, and you can broadcast any data on Advertising data by changing this data.

BLE leaf’s motion

  • Various settings
  • Event handler

Back to previous page


Last modified 15.03.2021