nRF9160: nRF CoAP Client

The nRF CoAP Client sample demonstrates the communication between a public CoAP server and an nRF9160 SiP that acts as the CoAP client.

Overview

The nRF CoAP Client sample performs the following actions:

  1. Connect to the configured public CoAP test server (specified by the Kconfig parameter CONFIG_COAP_SERVER_HOSTNAME).

  2. Send periodic GET request for a test resource (specified by the Kconfig parameter CONFIG_COAP_RESOURCE) that is available on the server.

  3. Display the received data about the resource on a terminal emulator.

The public CoAP server used in this sample is Californium CoAP server (coap://californium.eclipseprojects.io:5683). This server runs Eclipse Californium, which is an open source implementation of the CoAP protocol that is targeted at the development and testing of IoT applications. An nRF9160 DK is used as the CoAP client.

This sample uses the resource obs (Californium observable resource) in the communication between the CoAP client and the public CoAP server. The communication follows the standard request/response pattern and is based on the change in the state of the value of the resource. The sample queries one resource at a time. Other resources can be configured using the Kconfig option CONFIG_COAP_RESOURCE.

Requirements

The sample supports the following development kit:

Hardware platforms

PCA

Board name

Build target

nRF9160 DK

PCA10090

nrf9160dk_nrf9160

nrf9160dk_nrf9160ns

The sample also requires a public CoAP server IP address or URL available on the internet.

The sample is configured to compile and run as a non-secure application on nRF91’s Cortex-M33. Therefore, it automatically includes the Secure Partition Manager that prepares the required peripherals to be available for the application.

You can also configure it to use TF-M instead of Secure Partition Manager.

Building and running

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

The sample is built as a non-secure firmware image for the nrf9160dk_nrf9160ns build target. Because of this, it automatically includes the Secure Partition Manager. You can also configure it to use TF-M instead of SPM.

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

Testing

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

  1. Connect your nRF9160 DK to the PC using a USB cable and power on or reset your nRF9160 DK.

  2. Open a terminal emulator and observe that the following information is displayed:

    The nRF CoAP client sample started
    
  3. Observe that the discovered IP address of the public CoAP server is displayed on the terminal emulator.

  4. Observe that the nRF9160 DK sends periodic CoAP GET requests to the configured server for a configured resource after it gets LTE connection.

  5. Observe that the sample either displays the response data received from the server or indicates a time-out on the terminal. For more information on the response codes, see COAP response codes.

Sample Output

The sample displays the data in the following format:

CoAP request sent: token 0x9772
CoAP response: code: 0x45, token 0x9772, payload: 15:39:40

Instead of displaying every single CoAP frame content, the sample displays only the essential data. For the above sample output, the information displayed on the terminal conveys the following:

  • code:0x45 - CoAP response code (2.05 - Content), which is constant across responses

  • token 0x9772 - CoAP token, which is unique per request/response pair

  • payload: 15:39:40 - the actual message payload (current time in UTC format) from the resource that is queried in this sample

Dependencies

This sample uses the following nRF Connect SDK libraries:

It uses the following sdk-nrfxlib library:

It uses the following Zephyr library:

In addition, it uses the following sample: