OVMS3-idf/examples/protocols/coap_client/README.md
Jon Shallow 1aaec808da Add DTLS support to libcoap using MbedTLS
This update supports DTLS, TLS is a future TODO

components/coap/CMakeLists.txt:
components/coap/component.mk:

Add in the new files that have to be built
Replace libcoap/src/coap_notls.c with libcoap/src/coap_mbedtls.c

components/coap/libcoap:

Update the version to include the current version for supporting MbedTLS

components/coap/port/coap_debug.c:
components/coap/port/coap_mbedtls.c:
components/coap/port/include/coap/coap_dtls.h:

New port files for DTLS

components/coap/port/include/coap_config_posix.h:

Include building with MbedTLS

examples/protocols/coap_client/README.md:
examples/protocols/coap_client/main/CMakeLists.txt:
examples/protocols/coap_client/main/Kconfig.projbuild:
examples/protocols/coap_client/main/coap_client_example_main.c:
examples/protocols/coap_client/main/component.mk:

Update CoAP client to support DTLS

examples/protocols/coap_client/main/coap_ca.pem
examples/protocols/coap_client/main/coap_client.crt
examples/protocols/coap_client/main/coap_client.key

New PKI Certs for CoAP client (copied from wpa2_enterprise example)

examples/protocols/coap_server/README.md:
examples/protocols/coap_server/main/CMakeLists.txt:
examples/protocols/coap_server/main/Kconfig.projbuild:
examples/protocols/coap_server/main/coap_server_example_main.c:
examples/protocols/coap_server/main/component.mk:

Update CoAP server to support DTLS
Change "no data" to "Hello World!" to prevent confusion

examples/protocols/coap_server/main/coap_ca.pem
examples/protocols/coap_server/main/coap_server.crt
examples/protocols/coap_server/main/coap_server.key

New PKI Certs for CoAP server (copied from wpa2_enterprise example)

Closes https://github.com/espressif/esp-idf/pull/3345
Closes https://github.com/espressif/esp-idf/issues/1379
2019-08-06 10:37:40 +05:30

4 KiB

CoAP client example

(See the README.md file in the upper level 'examples' directory for more information about examples.) This CoAP client example is very simplified adaptation of one of the libcoap examples.

CoAP client example will connect your ESP32 device to a CoAP server, send off a GET request and fetch the response data from CoAP server. The client can be extended to PUT / POST / DELETE requests, as well as supporting the Observer extensions RFC7641.

If the URI is prefixed with coaps:// instead of coap://, then the CoAP client will attempt to use the DTLS protocol using the defined Pre-Shared Keys(PSK) or Public Key Infrastructure (PKI) which the CoAP server needs to know about.

If the URI is prefixed with coap+tcp://, then the CoAP will try to use TCP for the communication.

NOTE: coaps+tcp:// is not currently supported, even though both libcoap and MbedTLS support it.

The Constrained Application Protocol (CoAP) is a specialized web transfer protocol for use with constrained nodes and constrained networks in the Internet of Things.
The protocol is designed for machine-to-machine (M2M) applications such as smart energy and building automation.

Please refer to RFC7252 for more details.

How to use example

Configure the project

idf.py menuconfig

Example Connection Configuration --->

  • Set WiFi SSID under Example Configuration
  • Set WiFi Password under Example Configuration Example CoAP Client Configuration --->
  • Set CoAP Target Uri
  • Set encryption method definitions (None, PSK or PKI)
  • If PSK Set CoAP Preshared Key to use in connection to the server
  • If PSK Set CoAP PSK Client identity (username) Enable CoAP debugging if required Component config ---> mbedTLS ---> [] Enable mbedtls certificate expiry check TLS Key Exchange Methods ---> [] Enable pre-shared-key ciphersuites [] Enable PSK based ciphersuite modes [] Support DTLS protocol (all versions)

Build and Flash

Build the project and flash it to the board, then run monitor tool to view serial output:

idf.py build
idf.py -p PORT flash monitor

(To exit the serial monitor, type Ctrl-].)

See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.

Example Output

Prerequisite: we startup a CoAP server on coap server example, or use the default of coap://californium.eclipse.org.

and you could receive data from CoAP server if succeed,
such as the following log:

...
I (332) wifi: mode : sta (30:ae:a4:04:1b:7c)
I (1672) wifi: n:11 0, o:1 0, ap:255 255, sta:11 0, prof:1
I (1672) wifi: state: init -> auth (b0)
I (1682) wifi: state: auth -> assoc (0)
I (1692) wifi: state: assoc -> run (10)
I (1692) wifi: connected with huawei_cw, channel 11
I (1692) wifi: pm start, type: 1

I (2582) event: sta ip: 192.168.3.89, mask: 255.255.255.0, gw: 192.168.3.1
I (2582) CoAP_client: Connected to AP
I (2582) CoAP_client: DNS lookup succeeded. IP=104.196.15.150
Received:
************************************************************
CoAP RFC 7252                              Cf 2.0.0-SNAPSHOT
************************************************************
This server is using the Eclipse Californium (Cf) CoAP framework
published under EPL+EDL: http://www.eclipse.org/californium/

(c) 2014, 2015, 2016 Institute for Pervasive Computing, ETH Zurich and others
************************************************************
...

libcoap Documentation

This can be found at https://libcoap.net/doc/reference/4.2.0/

Troubleshooting

  • Please make sure Target Url includes valid host, optional port, optional path, and begins with coap://, coaps:// or coap+tcp:// for a coap server that supports TCP (not all do including coap+tcp://californium.eclipse.org).

  • CoAP logging can be enabled by running 'make menuconfig' and enable debugging

  • Encryption (MbedTLS) can be enabled by running 'make menuconfig' and enable debugging