The latest development version of this page may be more current than this released 4.0.0 version.

CoAP download

Browse source code on GitHub

Overview

This sample demonstrates the use of the CoAP client API to make GET requests to a CoAP server. If the data to be fetched is larger than a single CoAP packet, a blockwise transfer will be used to receive the data.

Once the transfer is complete, the sample prints the amount of data received and the time taken.

Requirements

Build and Running

Build the CoAP download sample application like this:

west build -b <board to use> samples/net/sockets/coap_download -- -DCONF_FILE=<config file to use>

The easiest way to run this sample application is to build and run it as a native_sim application. Some setup is required as described in Networking with native_sim board.

Download a CoAP server application, for example aiocoap (Python), or Eclipse Californium (Java) has a demo Simple File Server application.

Using aiocoap:

python -m pip install "aiocoap[all]"
mkdir file_root
echo "some test data" > file_root/test.txt
aiocoap-fileserver file_root

You can also change the name of the CoAP resource to request via Kconfig:

CONFIG_NET_SAMPLE_COAP_RESOURCE_PATH="resource_name_to_request"

Launch net-setup.sh in net-tools:

./net-setup.sh

Build and run the coap_download sample application for native_sim like this:

west build -b native_sim samples/net/sockets/coap_download
west build -t run

Sample output

[00:00:00.000,000] <inf> net_config: Initializing network
[00:00:00.000,000] <inf> net_config: IPv4 address: 192.0.2.1
[00:00:00.110,000] <inf> net_config: IPv6 address: 2001:db8::1
[00:00:00.110,000] <inf> net_config: IPv6 address: 2001:db8::1
[00:00:00.110,000] <inf> coap_download: Network L4 is connected
[00:00:00.110,000] <inf> coap_download: Starting CoAP download using IPv4
[00:00:00.180,000] <inf> coap_download: CoAP response, result_code=69, offset=0, len=100
[00:00:00.180,000] <inf> coap_download: CoAP download done, got 100 bytes in 70 ms
[00:00:00.180,000] <inf> coap_download: Starting CoAP download using IPv6
[00:00:00.300,000] <inf> coap_download: CoAP response, result_code=69, offset=0, len=100
[00:00:00.300,000] <inf> coap_download: CoAP download done, got 100 bytes in 120 ms

See also

COAP Library