Thread: CoAP Server

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

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 LED 4

  • /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.

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 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. This variable instructs the build system to append the appropriate devicetree overlay file. For example, to build the sample from the command line for an nRF52833 DK with the nRF21540 EK attached, use the following command within the sample directory:

    west build -b nrf52833dk_nrf52833 -- -DSHIELD=nrf21540_ek
    

    This command builds the application firmware. 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 Radio front-end module (FEM) support 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.

Requirements

The sample supports the following development kits:

Hardware platforms

PCA

Board name

Build target

nRF5340 DK

PCA10095

nrf5340dk_nrf5340

nrf5340dk_nrf5340_cpuapp

nRF5340 DK

PCA10095

nrf5340dk_nrf5340

nrf5340dk_nrf5340_cpuapp_ns

nRF52840 DK

PCA10056

nrf52840dk_nrf52840

nrf52840dk_nrf52840

nRF52833 DK

PCA10100

nrf52833dk_nrf52833

nrf52833dk_nrf52833

nRF21540 DK

PCA10112

nrf21540dk_nrf52840

nrf21540dk_nrf52840

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

User interface

Button 4:

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

LED 1:

On 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.

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.

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

Testing

After building the sample and programming it to your development kit, test it by performing the following steps:

  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 will gradually become Routers.

    Note

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

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

  5. Pair a client with a server by completing 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.

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

Running OpenThread CLI commands

You can connect to any of the Simple CoAP Server or Simple CoAP Client nodes through a serial port. For more details, see How to connect with PuTTY.

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: