CoAP Client
This sample demonstrates the communication between a public CoAP server and a CoAP client application that is running on a Nordic Semiconductor SoC that enables IP networking through cellular or Wi-Fi® connectivity.
Cellular connectivity is supported on the nRF91 Series SiPs, while Wi-Fi connectivity is supported on the nRF52 or nRF53 Series SoCs hosting the nRF70 Series Wi-Fi companion ICs.
The sample uses the connection manager that provides a common connectivity API for LTE and Wi-Fi stacks.
Requirements
The sample supports the following development kits:
Hardware platforms |
PCA |
Board name |
Board target |
---|---|---|---|
PCA20035 |
thingy91 |
|
|
PCA10153 |
|
||
PCA10090 |
|
||
PCA10171 |
|
||
PCA10143 |
|
||
Native Simulator |
native_sim |
|
The sample also requires a public CoAP server IP address or URL available on the Internet.
When built for a board target with the */ns
variant, the sample is configured to compile and run as a non-secure application with Cortex-M Security Extensions enabled.
Therefore, it automatically includes Trusted Firmware-M that prepares the required peripherals and secure services to be available for the application.
Overview
The CoAP Client sample performs the following actions:
Connect to the configured public CoAP test server (specified by the Kconfig option CONFIG_COAP_SERVER_HOSTNAME).
Send periodic GET request for a test resource (specified by the Kconfig option CONFIG_COAP_RESOURCE) that is available on the server.
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.
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. To configure other resources, use the Kconfig option CONFIG_COAP_RESOURCE.
Configuration
See Configuring and building for information about how to permanently or temporarily change the configuration.
Configuration options
Check and configure the following Kconfig options in the coap_client/prj.conf
file:
- CONFIG_COAP_RESOURCE - CoAP resource configuration
This option sets the CoAP resource. Default is Californium observable resource.
- CONFIG_COAP_SERVER_HOSTNAME - CoAP server hostname
This option sets the CoAP server hostname. Default is
californium.eclipseprojects.io
.
- CONFIG_COAP_SERVER_PORT - CoAP server port
This option sets the port for the CoAP server. Default is
5683
.
Configuring Wi-Fi access point credentials
This sample uses the Wi-Fi credentials library to manage Wi-Fi credentials. Before the sample can connect to a Wi-Fi network, you must configure at least one credential set.
Once you have flashed your device with this sample, connect to your device’s UART interface and add credentials using the following command:
wifi_cred add NetworkSSID SecurityMode (OPEN, WPA2-PSK, WPA2-PSK-SHA256, WPA3-SAE) NetworkPassword
Where NetworkSSID is replaced with the SSID of the Wi-Fi access point you want your device to connect to, and NetworkPassword is its password.
If you are not sure which security mode to use, enable the CONFIG_NET_L2_WIFI_SHELL
Kconfig option and use the wifi scan
command to display a list of all accessible networks along with their corresponding security modes.
Then either reboot the device or use the wifi_cred auto_connect
command to manually trigger a connection attempt.
From now on, these credentials will be automatically used when the configured network is reachable.
When building as firmware image for a non-secure board target, the Wi-Fi credentials backend will be set to PSA using TF-M.
See the Wi-Fi: Shell sample document for more details on the wifi_cred
command.
Wi-Fi static credential options
If you want to configure the credentials statically, set the CONFIG_WIFI_CREDENTIALS_STATIC
Kconfig option to y
.
Important
Do not use static credentials in production environments.
Other options for statically configuring your Wi-Fi credentials:
CONFIG_WIFI_CREDENTIALS_STATIC
- This option enables static Wi-Fi configuration.CONFIG_WIFI_CREDENTIALS_STATIC_SSID
- Wi-Fi SSID.CONFIG_WIFI_CREDENTIALS_STATIC_PASSWORD
- Wi-Fi password.CONFIG_WIFI_CREDENTIALS_STATIC_TYPE_OPEN
- Wi-Fi network uses no password.CONFIG_WIFI_CREDENTIALS_STATIC_TYPE_PSK
- Wi-Fi network uses a password and PSK security (default).CONFIG_WIFI_CREDENTIALS_STATIC_TYPE_PSK_SHA256
- Wi-Fi network uses a password and PSK-256 security.CONFIG_WIFI_CREDENTIALS_STATIC_TYPE_SAE
- Wi-Fi network uses a password and SAE security.
Sending traces over UART on an nRF91 Series DK
To send modem traces over UART on an nRF91 Series DK, configuration must be added for the UART device in the devicetree and Kconfig. This is done by adding the modem trace UART snippet when building and programming.
Use the Cellular Monitor app for capturing and analyzing modem traces.
TF-M logging must use the same UART as the application. For more details, see shared TF-M logging.
Building and running
This sample can be found under samples/net/coap_client
in the nRF Connect SDK folder structure.
When built as firmware image for a board target with the */ns
variant, the sample has Cortex-M Security Extensions (CMSE) enabled and separates the firmware between Non-Secure Processing Environment (NSPE) and Secure Processing Environment (SPE).
Because of this, it automatically includes the Trusted Firmware-M (TF-M).
To read more about CMSE, see Processing environments.
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 programming the sample to your development kit, complete the following steps to test it:
Connect the kit to the computer using a USB cable. The kit is assigned a COM port (Windows) or ttyACM device (Linux), which is visible in the Device Manager.
Connect to the kit with a terminal emulator (for example, nRF Connect Serial Terminal). See Testing and optimization for the required settings and steps.
Power on or reset the kit.
Observe that the following output is displayed in the terminal:
The CoAP client sample started
Observe that the discovered IP address of the public CoAP server is displayed on the terminal emulator.
Observe that your DK sends periodic CoAP GET requests to the configured server for a configured resource after it gets LTE connection.
Observe that the sample either displays the response data received from the server or indicates a timeout 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 GET request sent sent to californium.eclipseprojects.io, resource: obs
CoAP response: code: 0x45, payload: 15:29:45
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 responsespayload: 15:39:40
- the actual message payload (current time in UTC format) from the resource that is queried in this sample
References
Dependencies
This sample uses the following Zephyr libraries: