Thread: CoAP Server

The Thread CoAP Server sample demonstrates controlling light resources within an OpenThread network. This sample exposes resources in the network. To access them, you need another sample that is compatible with the OpenThread network. The recommended sample referenced on this page is Thread: CoAP Client.

Requirements

The sample supports the following development kits:

Hardware platforms

PCA

Board name

Board target

nRF54LM20 DK

PCA10184

nrf54lm20dk

nrf54lm20dk/nrf54lm20b/cpuapp nrf54lm20dk/nrf54lm20a/cpuapp

nRF54L15 DK

PCA10156

nrf54l15dk

nrf54l15dk/nrf54l15/cpuapp

nRF5340 DK

PCA10095

nrf5340dk

nrf5340dk/nrf5340/cpuapp

nRF52840 DK

PCA10056

nrf52840dk

nrf52840dk/nrf52840

nRF21540 DK

PCA10112

nrf21540dk

nrf21540dk/nrf52840

You can use one or more of these development kits as the Thread CoAP Server. You also need one or more compatible development kits programmed with the Thread: CoAP Client sample.

Overview

This sample demonstrates how to expose resources that can be accessed by other devices in the same Thread network. You can use this sample application as a starting point to implement a CoAP application.

The following CoAP resources are exposed on the network by this sample:

  • /light - used to control LEDs

  • /provisioning - used to perform provisioning

This sample uses the native OpenThread CoAP API for communication. For new application development, use Zephyr’s CoAP API. For example usage of the Zephyr CoAP API, see the Thread: CoAP Client sample.

Application architecture options

The sample supports two application architecture options depending on if you are using the Zephyr networking layer.

  • Direct IEEE 802.15.4 radio integration with OpenThread Stack (default)

  • Integration with Zephyr networking layer

To learn more about the differences between the two architectures, see the OpenThread stack architecture page. To learn how to switch between the two architectures, see the Enable OpenThread in the nRF Connect SDK user guide.

Additionally, you can use the ot-zephyr-l2 snippet to switch between the two architectures. See the Snippets section for details.

User interface

Button 4:

Pressing results in entering the pairing mode for a limited period of time.

LED 1:

Lit when the OpenThread connection is established.

LED 3:

Blinks when the pairing mode is enabled.

LED 4:

Turned on and off by messages sent from the client nodes.

Configuration

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

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 nRF21540 DK.

  • 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 nRF Connect for VS Code 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 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 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.

Snippets

The sample supports Snippets for typical use cases, and to activate sample extensions. You can find the OpenThread-related snippets in the snippets/openthread directory of the nRF Connect SDK main directory. All snippets with the ot prefix are suitable for this sample.

Specify the corresponding snippet names in the coap_server_SNIPPET CMake option. For more information about using snippets, see Using Snippets in the Zephyr documentation.

To use the snippets, you need to add the sample name as a prefix before the SNIPPET CMake argument.

For example:

west build -b board_target -p -- -Dcli_SNIPPET="ot-logging;ot-usb"

The following snippets are available:

Building and running

Make sure to enable the OpenThread stack before building and testing this sample. See Thread for more information.

This sample can be found under samples/openthread/coap_server in the nRF Connect SDK folder structure.

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 building the sample and programming it to your development kit, complete the following steps to test it:

  1. Program at least one development kit with the Thread: CoAP Client sample and reset it.

  2. Turn on the Simple CoAP Client node. This node becomes the Thread network Leader.

  3. Turn on all the other nodes, including the Simple CoAP Server nodes. They enter the network as Children, and gradually become Routers.

Note

It can take up to 15 seconds for Thread to establish the network.

  1. Press Button 2 on the client node to control LED 4 on all server nodes.

  2. To pair a client with a server, complete the following steps:

    1. Press Button 4 on a server node to enable pairing.

    2. Press Button 3 on a client node to pair it with the server node in the pairing mode.

  3. Press Button 1 on the client node to control the LED 4 on the paired server node.

Running OpenThread CLI commands

You can connect to any of the Simple CoAP Server or Simple CoAP Client nodes with a terminal emulator that supports VT100/ANSI escape characters (for example, the Serial Terminal app). See Testing and optimization for the required settings and steps.

Once the serial connection is ready, you can run OpenThread CLI commands. For complete CLI documentation, refer to OpenThread CLI Reference.

Note

In Zephyr shell, every OpenThread command needs to be preceded with the ot keyword. For example, ot channel 20.

Dependencies

This sample uses the following nRF Connect SDK libraries:

In addition, it uses the following Zephyr libraries:

OpenThread CoAP API is used in this sample: