Bluetooth Mesh: Light fixture
The Bluetooth® Mesh light fixture sample demonstrates how to set up a light control mesh server model application, and control a dimmable LED with Bluetooth Mesh using the Generic OnOff models.
This sample demonstrates how to implement the following Bluetooth Networked Lighting Control profiles:
Basic Lightness Controller NLC Profile
Energy Monitor NLC Profile
Requirements
The sample supports the following development kits:
Hardware platforms |
PCA |
Board name |
Board target |
---|---|---|---|
PCA20053 |
|
||
PCA10156 |
|
||
PCA10095 |
|
||
PCA10040 |
|
||
PCA10059 |
|
||
PCA10056 |
|
||
PCA10100 |
|
||
PCA10112 |
|
The sample also 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.
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
This sample can be used to control the state of light sources. In addition to generic on and off functions, it allows changing the light level (brightness) of an LED light and shows how the light can react to supported sensor messages. It also demonstrates how the Sensor models and the device properties can be used to report additional light source usage data.
The sample instantiates the following models:
As both Light Lightness Server and the Light LC Server extend the Generic OnOff Server, the two models need to be instantiated on separate elements. For more information, see documentation on Light Lightness Control Server.
Devices are nodes with a provisionee role in a mesh network. Provisioning is performed using the nRF Mesh mobile app. This mobile application is also used to configure key bindings, and publication and subscription settings of the Bluetooth Mesh model instances in the sample. After provisioning and configuring the mesh models supported by the sample in the nRF Mesh mobile app, you can control the dimmable LED on the development kit from the app.
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.
Models
The following table shows the mesh light fixture composition data for this sample:
Element 1 |
Element 2 |
---|---|
Config Server |
Gen. OnOff Server |
Health Server |
Light LC Server |
Gen. Level Server |
Light LC Setup Server |
Gen. OnOff Server |
|
Gen. DTT Server |
|
Gen. Power OnOff Server |
|
Gen. Power OnOff Setup Server |
|
Light Lightness Server |
|
Light Lightness Setup Server |
|
Scene Server |
|
Scene Setup Server |
|
Sensor Server |
|
Sensor Setup Server |
The models are used for the following purposes:
The first element contains a Config Server and a Health Server. The Config Server allows configurator devices to configure the node remotely. The Health Server provides
attention
callbacks that are used during provisioning to call your attention to the device. These callbacks trigger blinking of the LEDs.The next seven models in the first element are the product of a single instance of the Light Lightness Server. The application implements callbacks for the Light Lightness Server to control an LED on the device using the PWM (pulse width modulation) driver.
The next two models in the first element are the product of a single instance of the Scene Server. The Scene Server allows the device to store and recall scenes.
The last two models in the first element are the product of a single instance of the Sensor Server. The Sensor Server allows the device to report the value of the
bt_mesh_sensor_precise_tot_dev_energy_use
property.The three models in the second element are the product of a single instance of the Light Lightness Control (LC) Server. The Light LC Server controls the Light Lightness Server in the first element, deciding on parameters such as fade time, lighting levels for different states, and inactivity timing. In this sample, the Light LC Server is enabled by default at first boot.
Other nodes can control the Light Lightness Server through the Light LC Server, by sending On/Off messages to the Light LC Server or to the Generic OnOff Server in the second element.
Note
It is possible to bypass the Light LC Server by directly communicating with the Light Lightness Server on the first element.
For more details, see Light Lightness Server and Light Lightness Control Server.
Other nodes can store or recall scenes through Scene Server, by sending Scene messages.
They can also fetch the current value of the bt_mesh_sensor_precise_tot_dev_energy_use
property by sending Sensor Get messages.
The model handling is implemented in src/model_handler.c
, which uses the DK Buttons and LEDs library and the Pulse Width Modulation (PWM) API to control the LEDs on the development kit.
User interface
- Buttons:
Can be used to input the out-of-band (OOB) authentication value during provisioning. All buttons have the same functionality during this procedure. If the Emergency data storage feature is enabled and the provisioning and configuration are complete, Button 4 can be used to trigger storing for data with emergency data storage and halt the system.
- LEDs:
Show the OOB authentication value during provisioning if the “Push button” OOB method is used. LED 1 outputs the current light level of the Light Lightness Server in the first element. If the Emergency data storage feature is enabled and Button 4 is pressed LEDs 2 to LED 4 will light up to show that the board is halted.
Note
Thingy:53 supports only one RGB LED. Each RGB LED channel is used as separate LED.
Note
Thingy:53 and the nRF52840 Dongle do not support emergency data storage.
- Buttons:
Can be used to input the out-of-band (OOB) authentication value during provisioning. All buttons have the same functionality during this procedure. If the Emergency data storage feature is enabled and the provisioning and configuration are complete, Button 3 can be used to trigger storing for data with emergency data storage and halt the system.
- LEDs:
Show the OOB authentication value during provisioning if the “Push button” OOB method is used. LED 1 outputs the current light level of the Light Lightness Server in the first element. If the Emergency data storage feature is enabled and Button 3 is pressed LEDs 1 to LED 3 will light up to show that the board is halted.
Configuration
See Configuring and building 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.
The Kconfig option CONFIG_BT_MESH_LIGHT_CTRL_REG_SPEC
is set by default as it is necessary for the Light Lightness Control Server model according to the Bluetooth Mesh model specification.
The option enables a separate module called illuminance regulator.
For more information about the module, see the documentation on Illuminance regulator interface and Specification-defined illuminance regulator.
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
.A file for handling PWM driven control of the dimmable LED,
lc_pwm_led.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 the nRF21540 FEM is connected to the SoC. See Configuring devicetree for different ways of adding the overlay file.
Provide nRF21540 FEM capabilities by using a shield, for example 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
-DSHIELD=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 extension 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 How to work with build configurations in the nRF Connect for VS Code extension documentation for more information.
To build the sample from the command line for an nRF52840 DK with the nRF21540 EK attached, use the following command within the sample directory:
west build -b nrf52840dk/nrf52840 -- -DSHIELD=nrf21540ek
See Programming nRF21540 EK for information about how to program when you are using a board with a network core, for example the 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.
Emergency data storage
To build this sample with support for emergency data storage (EMDS), set EXTRA_CONF_FILE to overlay-emds.conf
using the respective CMake option.
This will save replay protection list (RPL) data and some of the Light Lightness Server data to the emergency data storage instead of to the Settings.
When using EMDS, certain considerations need to be taken regarding hardware choices in your application design.
See Application integration in the EMDS documentation for more information.
For more information about configuration files in the nRF Connect SDK, see Build and configuration system.
Building and running
This sample can be found under samples/bluetooth/mesh/light_ctrl
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 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
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.
When the development kit is started, it will keep its previous Light state as the BT_MESH_ON_POWER_UP_RESTORE
is set for the Light Lightness Server.
When Emergency data storage is enabled it is important that the Button 4 is used to store the data before the development kit is halted and then restarted.
When the development kit is started, it will keep its previous Light state as the BT_MESH_ON_POWER_UP_RESTORE
is set for the Light Lightness Server.
When Emergency data storage is enabled it is important that the Button 3 is used to store the data before the development kit is halted and then restarted.
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:
Tap Add node to start scanning for unprovisioned mesh devices.
Select the Mesh Light Fixture device to connect to it.
Tap Identify, and then Provision, to provision the device.
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 Bluetooth Mesh models using the nRF Mesh mobile app for details on how to configure the mesh models with the nRF Mesh mobile app.
Configure the Generic OnOff Server model on each element on the Mesh Light Fixture node:
Bind the model to Application Key 1.
Once the model is bound to the application key, you can control the LED on the device.
Open the Generic OnOff Server in the second element, then tap ON at the bottom of the Generic On Off Controls.
You should now see the following actions:
The LED fades from 0% to 100% over a defined transition time following the message Standby > On.
The LED stays at 100% for three seconds On.
The LED fades from 100% to
CONFIG_BT_MESH_LIGHT_CTRL_SRV_LVL_PROLONG
over five seconds On > Prolong.The LED stays at
CONFIG_BT_MESH_LIGHT_CTRL_SRV_LVL_PROLONG
for three seconds Prolong.The LED fades from
CONFIG_BT_MESH_LIGHT_CTRL_SRV_LVL_PROLONG
toCONFIG_BT_MESH_LIGHT_CTRL_SRV_LVL_STANDBY
over five seconds Prolong > Standby.
The default value of CONFIG_BT_MESH_LIGHT_CTRL_SRV_LVL_PROLONG
is 10000 (~15%).
The default value of CONFIG_BT_MESH_LIGHT_CTRL_SRV_LVL_STANDBY
is 0 (0%).
Note
The configuration of light levels, fade time, and timeouts can be changed by altering the configuration parameters in the prj.conf
file, and rebuilding the sample.
This sample can be configured to report energy usage sensor data to any device implementing the Sensor Client model by configuring the Sensor Server model on the Mesh Light Fixture node:
Bind the model to Application Key 1. Make sure to bind the same application key to the peer Sensor Client.
The Sensor Server model is now configured and able to receive messages from and send data to the peer Sensor Client.
Occupancy mode
You can combine this sample with the Bluetooth Mesh: Sensor sample to trigger the Occupancy On event on the Light LC Server by the Occupancy sensor.
To do this, first configure the Light LC Server on the Mesh Light Fixture node:
Bind the model to Application Key 1.
Set the subscription parameters: Create a dedicated group address.
Prepare the Bluetooth Mesh: Sensor sample:
Build, run and provision the Mesh Sensor node as described in the sample’s documentation.
Configure the Sensor Server that is instantiated on the Element 2 of the Bluetooth Mesh: Sensor sample:
Bind the model to Application Key 1.
Set the publication parameters:
Destination/publish address: Select the same group as the Light LC Server is subscribed to.
To evaluate occupancy inputs when light is not in Standby state:
Open the Mesh Light Fixture node in the mobile app.
Open the Generic OnOff Server in the second element, then tap ON at the bottom of the Generic On Off Controls. This will bring the Light LC Server out of Standby state.
Now, when the Light LC Server is not in the Standby state, press and release
Button 2
on the Mesh Sensor node to extend the light’s On state (otherwise the light will turn off after some time as per the state machine’s action).If the Light LC Server enters the Standby state, Occupancy sensor inputs will not have any effect because the default value of the Light LC Occupancy Mode state is
0
.
When using the nRF Mesh mobile app for iOS, you can change the value of the Light LC Occupancy Mode state to 1
, and see how Occupancy sensor inputs will turn the light ON from the Standby state.
Do this in the following way:
Open the Mesh Light Fixture node in the mobile app for iOS.
Go to the Light LC Server configuration that is located on the Element 2.
Scroll down to the OCCUPANCY MODE and tap ON to enable the Occupancy mode in the Standby state.
When the Light LC Server is in the Standby state, press
Button 2
on the Mesh Sensor node.
Dependencies
This sample uses the following nRF Connect SDK libraries:
In addition, it uses the following Zephyr libraries:
include/drivers/hwinfo.h
-
include/kernel.h
-
drivers/pwm.h
API:
include/bluetooth/bluetooth.h
-
include/bluetooth/mesh.h
The sample also uses the following secure firmware component: