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.
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.
Software FEM support is not present on nRF53 Series devices yet. For now, it also cannot be combined with multiprotocol operation.
You can add support for the nRF21540 front-end module to the sample. See Radio front-end module (FEM) support for more information.
To add support for the nRF21540 FEM, add the provided
dts-nrf21540-fem.overlay devicetree overlay file when building.
The file is located in the
Make sure that the GPIOs in the file correspond to those in which your front-end module is connected.
You must add the provided overlay file if you use the nRF21540 EK. If you use the nRF21540 DK, build your application for the nrf21540dk_nrf52840 board. The devicetree for the nRF21540 DK already contains the required FEM configuration, so you do not need to add the overlay file.
See Set devicetree overlays for different ways of adding the overlay file.
The easiest way to add the file when building is to set it in the
However, doing so will override the default settings.
For some boards, this sample requires additional overlay files, which are automatically included when building with the default settings.
When you set the
DTC_OVERLAY_FILE variable, you must specify all overlay files that are needed for building.
boards folder to see the additional overlay files.
Follow the instructions in Providing CMake options to specify the
For example, to build the sample from the command line for an nRF52833 DK with an attached nRF21540 EK, invoke the following command within the sample directory:
west build -b nrf52833dk_nrf52833 -- -DDTC_OVERLAY_FILE="boards/nrf52833dk_nrf52833.overlay;../common/dts-nrf21540-fem.overlay"
Alternatively, you can copy the contents of
dts-nrf21540-fem.overlay to the board’s overlay file.
To add support for other front-end modules, add the respective overlay files in the same way.
The sample supports the following development kits:
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.
- 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.
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 a sample application for information about how to build and program the application.
After building the sample and programming it to your development kit, test it by performing the following steps:
Program at least one development kit with the Thread: CoAP Client sample and reset it.
Turn on the Simple CoAP Client node. This node becomes the Thread network Leader.
Turn on all the other nodes, including the Simple CoAP Server nodes. They enter the network as Children, and will gradually become Routers.
It can take up to 15 seconds for Thread to establish network.
Press Button 2 on the client node to control LED 4 on all server nodes.
Pair a client with a server by completing the following steps:
Press Button 4 on a server node to enable pairing.
Press Button 3 on a client node to pair it with the server node in the pairing mode.
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.
In Zephyr shell, every OpenThread command needs to be preceded with the ot keyword.
ot channel 20.