Bluetooth: Mesh Light Switch

The Bluetooth Mesh Light Switch sample demonstrates how to set up a basic Mesh client model application and control LEDs with the Bluetooth Mesh, using the Generic OnOff models.

Overview

This sample is split into two source files:

  • A main file to handle initialization.

  • One additional file for handling Mesh models

Provisioning

Provisioning is handled by the Bluetooth Mesh provisioning handler for Nordic DKs.

Models

This sample application has the following composition data:

Mesh light composition data

Element 1

Element 2

Element 3

Element 4

Config Server

Gen. OnOff Client

Gen. OnOff Client

Gen. OnOff Client

Health Server

Gen. OnOff Client

The Generic OnOff Client instances in elements 1-4 are controlled by the buttons on the Device Kit. The Config server allows configurator devices to configure the node remotely. The Health server is used to call attention to the device during provisioning.

The model handling is implemented in src/model_handler.c, which uses the DK Button and LED library to detect button presses on the board.

The model handler calls bt_mesh_onoff_cli_set() to turn the LEDs of a Mesh Light device on or off. As we pass a response buffer to the function, it will will block until the model receives a response from the target device. If the function returns successfully, the response buffer will contain the response from the Mesh Light. In this example, the contents of the response will be ignored, as we handle all incoming OnOff status messages in the status callback. Note that the response structure is still required, as the Generic OnOff Client model won’t request a response from the Server if we pass NULL in the response parameter.

The button handling is deferred to its own button handling thread to avoid blocking the system workqueue. As the call to bt_mesh_onoff_cli_set() blocks until it receives a response, the caller might be blocked for seconds, which would severely impact other activities on the device. This button handling thread is a loop that waits for a semaphore signaled by the button handler callback, and triggers calls to the appropriate Generic OnOff Client model.

Requirements

  • One of the following development boards:

    • nRF52840 Development Kit board (PCA10056)

    • nRF52 Development Kit board (PCA10040)

    • nRF51 Development Kit board (PCA10028)

  • The Nordic Semiconductor nRF Mesh app for Android or iOS.

  • The Bluetooth: Mesh Light sample application programmed on a separate device, and configured according to the Mesh Light sample’s Testing guide.

User interface

Buttons:

Buttons are used to control their own Generic OnOff Clients. When pressed, the button will toggle the LED state on a Bluetooth: Mesh Light device.

LEDs:

Shows the last known OnOff state of the targeted Bluetooth: Mesh Light board.

Building and running

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

See Building and programming a sample application for information about how to build and program the application.

Testing

Important

The Light Switch sample cannot demonstrate any functionality on its own, and needs a device with the Bluetooth: Mesh Light sample running in the same mesh network. Before testing the Mesh Light Switch, go through the Mesh Light’s Testing guide with a different board.

After programming the sample to your board, you can test it by using the Nordic Semiconductor nRF Mesh app for Android or iOS.

Provisioning and configuration

Before communicating with the mesh models, the device must be provisioned. The provisioning assigns an address range to the device, and adds it to the mesh network. Provisioning is done through the nRF Mesh app:

  1. Press “Add node” to start scanning for unprovisioned mesh devices.

  2. Press the “Mesh Light Switch” device to connect to it.

  3. Press “Identify”, then “Provision” to provision the device.

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

  5. Once the provisioning and initial configuration is complete, the app will go back to the Network screen. Press the Mesh Light Switch node in the list. You’ll see basic information about your mesh node and its configuration.

  6. In the Mesh node view, expand the first element. It contains the list of models in the first element of the node.

  7. Press the “Generic OnOff Client” model to see its configuration.

  8. Models must be bound to application keys to be open to communication. Do this by pressing “BIND KEY” at the top of the screen. Select “Application Key 1” from the list.

  9. In addition to the application key binding, Client models need publish parameters to be configured. The publish parameters define how the model should send its messages. Press “SET PUBLICATION” to configure the publish parameters.

  10. Set the Publish Address to the first unicast address of the Mesh Light node. Set the Retransmit Count to zero (“Disabled”) to prevent the model from sending each button press multiple times. The rest of the publish parameters can be left at their default values. Press “APPLY” to accept the configuration.

  11. You should now be able to control the first LED on the Mesh Light device by pressing Button 1 on the Mesh Light Switch device kit.

Repeat steps 7-10 for each of the elements on the node to control each of the four LEDs on the Mesh Light device.

Dependencies

This sample uses the following nRF Connect SDK libraries:

In addition, it uses the following Zephyr libraries: