Thread CoAP Examples are server and client examples that demonstrate interactions between nodes performing different Thread roles with the use of OpenThread and the built-in CoAP protocol.
The interactions are based on the usage of LEDs. A press of a button on the client node triggers unicast or multicast commands, which can change the state of LEDs on the server nodes.
The following examples are discussed on this page:
Most of these examples offer CLI support.
You can find the source code and the makefiles in the following folders:
<InstallFolder>\examples\thread\simple_coap_server
<InstallFolder>\examples\thread\freertos_coap_server
<InstallFolder>\examples\thread\simple_coap_client
<InstallFolder>\examples\thread\mtd_coap_client
All examples use LED assignments as described in Thread BSP LED and button reference for the following LEDs:
For example-specific LED assignments and button assignments, see the following sections.
On startup, the nodes automatically enter the network with the default parameters.
Concerning the device roles:
Together with the Simple CoAP Client example, the Thread Simple CoAP Server example demonstrates the Full Thread Device functionality (Thread Router role and REED role).
The application layer of the example is built on top of the CoAP protocol. The server nodes provide two resources:
/light
– Used to control LED4./provisioning
– Disabled by default, used in pairing mechanism.The only action that is triggered by Button 4 on the server node is Pairing.
The Thread FreeRTOS CoAP Server example demonstrates Full Thread Device functionality (Thread Router role and REED role) with FreeRTOS.
This example is not supported on Keil.
The application layer of the example is built on top of the CoAP protocol. The server nodes provide two resources:
/light
– Used to control blinking of LED3 and LED4./provisioning
– Disabled by default, used in Pairing.The only action that is triggered by a button on the server node is Pairing.
Together with the Simple CoAP Server example, the Thread Simple CoAP Client example demonstrates the Full Thread Device functionality (Thread Router role and REED role).
The client node is controlled by specific actions triggered by pressing the buttons.
/light
resource on a paired server device. If no device is paired with the specific client node, pressing the button has no effect./light
resource on a multicast address, to be received and processed by all servers. Sending a THREAD_COAP_UTILS_LIGHT_CMD_ON/THREAD_COAP_UTILS_LIGHT_CMD_OFF message instead of THREAD_COAP_UTILS_LIGHT_CMD_TOGGLE allows to synchronize the state of the LEDs on several server nodes./provisioning
resource.Thread MTD CoAP Client example demonstrates Minimal Thread Device functionality, with its two states:
The MTD node starts in the lowest-power SED state. It can then enter the MED state.
If an MTD node is battery-powered, set the SW6 switch to nRF_ONLY. This will result in powering off the LEDs and reducing the power consumption significantly.
The MTD client node is controlled by specific actions triggered by pressing the buttons.
/light
resource on a paired server device. If no device is paired with a specific client node, pressing the button has no effect./light
resource on a multicast address, to be received and processed by all servers. Sending a THREAD_COAP_UTILS_LIGHT_CMD_ON/THREAD_COAP_UTILS_LIGHT_CMD_OFF message instead of THREAD_COAP_UTILS_LIGHT_CMD_TOGGLE allows to synchronize the state of the LEDs on several server nodes./provisioning
resource.To enable pairing of a server node with a client node dynamically during runtime, a simple pairing mechanism has been implemented. This mechanism is not a part of Thread or CoAP specification, and was provided specifically for this set of examples.
In this mechanism, a single client node can be paired with only one server node, but multiple clients can control a single server node. The pairing procedure can be repeated to change the server that the client is paired with.
To pair a server node with a client node, you must first enable pairing on the server side. This can be done by pressing Button 4 on the server node.
Once the button is pressed, the server node will wait for five seconds for the pairing request message from the client (see below). This is indicated by rapid blinking of LED3.
Once pairing on any server node is enabled, the client can send a pairing request message. It is a multicast CoAP message on the server /provisioning
resource and is triggered by pressing Button 4 on the server node.
The client will store the data of the first server node that responds to the pairing message. Therefore, it is advised not to enable pairing on more than one server node at a time as the result may be nondeterministic.
When the devices are paired, the client node can control the state of LED4 on Simple CoAP Server nodes or blinking of LED3 and LED4 on FreeRTOS CoAP Server nodes by pressing the following buttons:
/light
resource.Optionally, you can connect to CoAP nodes through a serial port and run CLI commands to configure them manually using OpenThread CLI Reference:
Not all nodes support CLI commands:
The application uses the following UART settings: