Bluetooth Mesh: Sensor

The Bluetooth® Mesh sensor sample demonstrates how to set up a basic mesh Sensor Server model application that provides sensor data to one Sensor Client model. Eight different sensor types are used to showcase different ways for the server to publish data. In addition, the samples demonstrate usage of both single-channel sensor types and sensor series types, as well as how to add and write to a sensor setting.

Note

This sample must be paired with the Bluetooth Mesh: Sensor observer sample to show any functionality. The mesh sensor provides the sensor data used by the observer.

This sample demonstrates how to implement the following Networked Lighting Control profiles:

  • Ambient Light Sensor NLC Profile

  • Occupancy Sensor NLC Profile

Requirements

The sample supports the following development kits:

Hardware platforms

PCA

Board name

Board target

Thingy:53

PCA20053

thingy53

thingy53/nrf5340/cpuapp

nRF54L15 PDK

PCA10156

nrf54l15pdk

nrf54l15pdk/nrf54l15/cpuapp

nRF52 DK

PCA10040

nrf52dk

nrf52dk/nrf52832

nRF52840 DK

PCA10056

nrf52840dk

nrf52840dk/nrf52840

nRF21540 DK

PCA10112

nrf21540dk

nrf21540dk/nrf52840

For provisioning and configuring of the mesh model instances, the sample requires a smartphone with Nordic Semiconductor’s nRF Mesh mobile app installed in one of the following versions:

Note

If you build this application for Thingy:53, it enables additional features. See Application guide for Thingy:53 for details.

Additionally, the sample requires the Bluetooth Mesh: Sensor observer sample application. The application needs to be programmed on a separate device, and configured according to the sensor observer sample’s testing guide.

When built for a board target with the */ns variant, the sample is configured to compile and run as a non-secure application with Cortex-M Security Extensions enabled. Therefore, it automatically includes Trusted Firmware-M that prepares the required peripherals and secure services to be available for the application.

Overview

The following Bluetooth Mesh sensor types, and their settings, are used in this sample:

Note

These values can be requested through shell commands by the Bluetooth Mesh: Sensor observer.

Moreover, the on-chip TEMP_NRF5 temperature sensor is used for the nRF52 series, and the BME680 temperature sensor for Thingy:53.

Note

When running this sample on Thingy:53, some functionality will not be available as the device only has two buttons. The two buttons on Thingy:53 will be used for the ambient light sensor and presence detected sensor functionality as described for Button 1 and Button 2 in this documentation. Button 2 can be accessed by removing the top part of the casing.

Provisioning

The provisioning is handled by the Bluetooth Mesh provisioning handler for Nordic DKs. It supports four types of out-of-band (OOB) authentication methods, and uses the Hardware Information driver to generate a deterministic UUID to uniquely represent the device.

Use nRF Mesh mobile app for provisioning and configuring of models supported by the sample.

Models

The following table shows the Bluetooth Mesh sensor composition data for this sample:

Element 1

Element 2

Element 3

Element 4

Element 5

Config Server

Sensor Server

Sensor Server

Sensor Server

Sensor Server

Health Server

Sensor Setup Server

Sensor Setup Server

Sensor Setup Server

Sensor Setup Server

Sensor Server

Sensor Setup Server

The models are used for the following purposes:

  • Config Server allows configurator devices to configure the node remotely.

  • Health Server provides attention callbacks that are used during provisioning to call your attention to the device. These callbacks trigger blinking of the LEDs.

  • Sensor Server instances provide sensor data to one or more mesh sensor observers.

  • Sensor Setup Server instances are used for configuration of the corresponding Sensor Server instances.

The model handling is implemented in src/model_handler.c. It uses the TEMP_NRF5 or BME680 temperature sensor depending on the platform.

The sample has a descriptor related to the bt_mesh_sensor_present_dev_op_temp sensor, which specifies tolerance values for the TEMP_NRF5 temperature sensor calculated based on the nRF52832 Temperature Sensor Electrical Specification. The descriptor also specifies the temperature sensor’s sampling type, which is BT_MESH_SENSOR_SAMPLING_INSTANTANEOUS.

The DK Buttons and LEDs library is used to detect button presses.

The Zephyr settings API is used to persistently store the following settings given that CONFIG_BT_SETTING is enabled:

User interface

Buttons:

Can be used to input the OOB authentication value during provisioning. All buttons have the same functionality during the provisioning procedure.

Once the provisioning procedure has completed, the buttons will have the following functionality:

Button 1:

Simulates different ambient light sensor values. These dummy values represent raw values coming from an ambient light sensor.

Button 2:

Simulates presence detected. For how long the button has to be pressed before the presence is detected depends on the motion threshold. The motion threshold has five steps from 0 % (representing 0 seconds) to 100 % (representing 10 seconds) separated by 25 %-steps.

Button 3:

Simulates motion sensed.

Button 4:

Simulates different people count sensor values. These dummy values represent raw values coming from a people count sensor.

Configuration

See Configuring and building an application for information about how to permanently or temporarily change the configuration.

For nRF5340 and Thingy:53, the extended advertiser has to be set manually for the network core, because the Bluetooth® Low Energy does not know that the Bluetooth Mesh is enabled when built for this core. This is already done for this sample by setting CONFIG_BT_EXT_ADV=y for the network core.

Source file setup

This sample is split into the following source files:

  • A main.c file to handle initialization.

  • A file for handling mesh models, model_handler.c.

FEM support

You can add support for the nRF21540 front-end module to this sample by using one of the following options, depending on your hardware:

  • Build the sample for one board that contains the nRF21540 FEM, such as nrf21540dk/nrf52840.

  • Manually create a devicetree overlay file that describes how FEM is connected to the nRF5 SoC in your device. See Set devicetree overlays for different ways of adding the overlay file.

  • Provide nRF21540 FEM capabilities by using a shield, for example the Developing with the nRF21540 EK shield that is available in the nRF Connect SDK. In this case, build the project for a board connected to the shield you are using with an appropriate variable included in the build command, for example SHIELD=nrf21540ek. This variable instructs the build system to append the appropriate devicetree overlay file.

    To build the sample in the nRF Connect for VS Code IDE for an nRF52840 DK with the nRF21540 EK attached, add the shield variable in the build configuration’s Extra CMake arguments and rebuild the build configuration. For example: -DSHIELD=nrf21540ek.

    See nRF Connect for VS Code extension pack documentation for more information.

    See Programming nRF21540 EK for information about how to program when you are using a board with a network core, for example nRF5340 DK.

Each of these options adds the description of the nRF21540 FEM to the devicetree. See Developing with Front-End Modules for more information about FEM in the nRF Connect SDK.

To add support for other front-end modules, add the respective devicetree file entries to the board devicetree file or the devicetree overlay file.

Building and running

This sample can be found under samples/bluetooth/mesh/sensor_server in the nRF Connect SDK folder structure.

When built as firmware image for a board target with the */ns variant, the sample has Cortex-M Security Extensions (CMSE) enabled and separates the firmware between Non-Secure Processing Environment (NSPE) and Secure Processing Environment (SPE). Because of this, it automatically includes the Trusted Firmware-M (TF-M). To read more about CMSE, see Processing environments.

To build the sample, follow the instructions in Configuring and building an application for your preferred building environment. See also Programming an application for programming steps and Testing and optimization for general information about testing and debugging in the nRF Connect SDK.

Note

When building repository applications in the SDK repositories, building with sysbuild is enabled by default. If you work with out-of-tree freestanding applications, you need to manually pass the --sysbuild parameter to every build command or configure west to always use it.

Testing

Note

The Bluetooth Mesh sensor sample cannot demonstrate any functionality on its own, and needs a device with the Bluetooth Mesh: Sensor observer sample running in the same mesh network. Before testing the sensor sample, go through the sensor observer sample’s testing guide with a different development kit.

After programming the sample to your development kit, you can test it by using a smartphone with nRF Mesh mobile app installed. Testing consists of provisioning the device, and configuring it for communication with the mesh models.

Provisioning the device

The provisioning assigns an address range to the device, and adds it to the mesh network. Complete the following steps in the nRF Mesh app:

  1. Tap Add node to start scanning for unprovisioned mesh devices.

  2. Select the Mesh Sensor device to connect to it.

  3. Tap Identify, and then Provision, to provision the device.

  4. When prompted, select an OOB method and follow the instructions in the app.

Once the provisioning is complete, the app returns to the Network screen.

Configuring models

See Configuring mesh models using the nRF Mesh mobile app for details on how to configure the mesh models with the nRF Mesh mobile app.

Configure Sensor Server model on each element on the Mesh Sensor node:

  • Bind the model to Application Key 1.

  • Set the publication parameters:

    • Destination/publish address: Select an existing group or create a new one, but make sure that the Sensor Client subscribes to the same group.

    • Retransmit count: Set the count to zero (Disabled), to avoid duplicate logging in the Sensor Client’s UART terminal.

  • Set the subscription parameters: Select an existing group or create a new one, but make sure that the Sensor Client publishes to the same group.

The Sensor Server models are now configured and able to send data to the Sensor Client.

Configure the corresponding Sensor Setup Server model on each element on the Mesh Sensor node:

  • Bind the model to Application Key 1.

  • Set the subscription parameters: Select an existing group or create a new one, but make sure that the Sensor Client publishes to the same group.

The Sensor Setup Server models are now configured and able to receive sensor setting messages from the Sensor Client.

Note

To enable Sensor Server configuration by a Sensor Client, an application key must be bound to the Sensor Setup Server. This functionality must also be programmed in the Sensor Client device.

Dependencies

This sample uses the following nRF Connect SDK libraries:

In addition, it uses the following Zephyr libraries:

The sample also uses the following secure firmware component: