CoAP TCP Client
Overview
This sample demonstrates how to use the CoAP over TCP client API to communicate with a CoAP server using reliable transports as specified in RFC 8323.
This demo assumes that the platform of choice has networking support, some adjustments to the configuration may be needed.
The sample shows:
Connecting to a CoAP TCP server
Waiting for CSM exchange completion using event callbacks
Sending GET requests over TCP
Using Ping/Pong signals with event-based waiting
Graceful session release
Event callback for server signals (Release, Abort)
Requirements
Networking with the host system, Networking with native_sim board
Network connection between the board and host running a CoAP server
A CoAP server with TCP support, such as aiocoap
Building and Running
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.
A CoAP server with TCP support must be running on the host. Using
aiocoap:
python -m pip install "aiocoap[all]"
mkdir file_root
echo "Hello from CoAP TCP!" > file_root/test
aiocoap-fileserver --bind "coap+tcp://[::]:5683" file_root
Launch net-setup.sh in net-tools:
./net-setup.sh
Build and run the sample:
west build -b native_sim samples/net/sockets/coap_client_tcp
west build -t run
Configuration
The server address and port can be modified via Kconfig and in src/main.c:
CONFIG_NET_CONFIG_PEER_IPV4_ADDR: Server IP addressPEER_PORT: Server port (default: 5683)RESOURCE_PATH: Resource path to request (default: “test”)
The following Kconfig options are relevant:
CONFIG_COAP_OVER_RELIABLE_TRANSPORT: Enable CoAP over TCP supportCONFIG_COAP_CLIENT_TCP: Enable CoAP TCP clientCONFIG_COAP_CLIENT_MESSAGE_SIZE: Maximum message size