2022-07-12 22:41:36 -04:00
|
|
|
| Supported Targets | ESP32 | ESP32-C3 | ESP32-S2 | ESP32-S3 |
|
2022-05-20 05:50:08 -04:00
|
|
|
| ----------------- | ----- | -------- | -------- | -------- |
|
|
|
|
|
2018-06-28 03:46:10 -04:00
|
|
|
|
|
|
|
# CoAP client example
|
|
|
|
|
|
|
|
(See the README.md file in the upper level 'examples' directory for more information about examples.)
|
2019-04-13 07:15:28 -04:00
|
|
|
This CoAP client example is very simplified adaptation of one of the
|
|
|
|
[libcoap](https://github.com/obgm/libcoap) examples.
|
2018-06-28 03:46:10 -04:00
|
|
|
|
2019-04-13 07:15:28 -04:00
|
|
|
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](https://tools.ietf.org/html/rfc7641).
|
2018-06-28 03:46:10 -04:00
|
|
|
|
2019-04-13 07:15:28 -04:00
|
|
|
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.
|
2018-06-28 03:46:10 -04:00
|
|
|
|
2019-04-13 07:15:28 -04:00
|
|
|
If the URI is prefixed with coap+tcp://, then the CoAP will try to use TCP for the communication.
|
|
|
|
|
|
|
|
The Constrained Application Protocol (CoAP) is a specialized web transfer protocol for use with
|
2022-05-20 05:50:08 -04:00
|
|
|
constrained nodes and constrained networks in the Internet of Things.
|
2019-04-13 07:15:28 -04:00
|
|
|
The protocol is designed for machine-to-machine (M2M) applications such as smart energy and
|
|
|
|
building automation.
|
|
|
|
|
|
|
|
Please refer to [RFC7252](https://www.rfc-editor.org/rfc/pdfrfc/rfc7252.txt.pdf) for more details.
|
2018-06-28 03:46:10 -04:00
|
|
|
|
|
|
|
## How to use example
|
|
|
|
|
|
|
|
### Configure the project
|
|
|
|
|
|
|
|
```
|
2019-08-01 23:31:20 -04:00
|
|
|
idf.py menuconfig
|
2018-06-28 03:46:10 -04:00
|
|
|
```
|
|
|
|
|
2019-04-13 07:15:28 -04:00
|
|
|
Example Connection Configuration --->
|
2022-07-27 06:47:46 -04:00
|
|
|
* Set WiFi SSID
|
|
|
|
* Set WiFi Password
|
2019-04-13 07:15:28 -04:00
|
|
|
Component config --->
|
2019-07-31 06:52:49 -04:00
|
|
|
CoAP Configuration --->
|
|
|
|
* Set encryption method definition, PSK (default) or PKI
|
|
|
|
* Enable CoAP debugging if required
|
2022-07-27 06:47:46 -04:00
|
|
|
* Disable CoAP using TCP if this is not required (TCP needed for TLS)
|
|
|
|
* Disable CoAP server functionality to reduce code size
|
|
|
|
Example CoAP Client Configuration --->
|
|
|
|
* Set CoAP Target Uri
|
|
|
|
* If PSK, Set CoAP Preshared Key to use in connection to the server
|
|
|
|
* If PSK, Set CoAP PSK Client identity (username)
|
2018-06-28 03:46:10 -04:00
|
|
|
|
|
|
|
### Build and Flash
|
|
|
|
|
|
|
|
Build the project and flash it to the board, then run monitor tool to view serial output:
|
|
|
|
|
|
|
|
```
|
2019-04-13 07:15:28 -04:00
|
|
|
idf.py build
|
2019-08-01 23:31:20 -04:00
|
|
|
idf.py -p PORT flash monitor
|
2018-06-28 03:46:10 -04:00
|
|
|
```
|
|
|
|
|
|
|
|
(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
|
2019-03-08 15:22:17 -05:00
|
|
|
Prerequisite: we startup a CoAP server on coap server example,
|
2022-05-20 05:50:08 -04:00
|
|
|
or use the default of coap://californium.eclipseprojects.io.
|
2018-06-28 03:46:10 -04:00
|
|
|
|
2022-05-20 05:50:08 -04:00
|
|
|
and you could receive data from CoAP server if succeed,
|
2018-06-28 03:46:10 -04:00
|
|
|
such as the following log:
|
|
|
|
|
|
|
|
```
|
|
|
|
...
|
2021-03-02 10:32:07 -05:00
|
|
|
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=35.185.40.182
|
2019-03-08 15:22:17 -05:00
|
|
|
Received:
|
2021-07-19 04:47:27 -04:00
|
|
|
****************************************************************
|
|
|
|
CoAP RFC 7252 Cf 3.0.0-SNAPSHOT
|
|
|
|
****************************************************************
|
2019-03-08 15:22:17 -05:00
|
|
|
This server is using the Eclipse Californium (Cf) CoAP framework
|
|
|
|
published under EPL+EDL: http://www.eclipse.org/californium/
|
|
|
|
|
2021-07-19 04:47:27 -04:00
|
|
|
(c) 2014-2020 Institute for Pervasive Computing, ETH Zurich and others
|
|
|
|
****************************************************************
|
2018-06-28 03:46:10 -04:00
|
|
|
...
|
|
|
|
```
|
|
|
|
|
2019-03-08 15:22:17 -05:00
|
|
|
## libcoap Documentation
|
2021-03-02 10:32:07 -05:00
|
|
|
This can be found at [libcoap Documentation](https://libcoap.net/documentation.html).
|
|
|
|
The current API is 4.3.0.
|
|
|
|
|
|
|
|
## libcoap Specific Issues
|
|
|
|
These can be raised at [libcoap Issues](https://github.com/obgm/libcoap/issues).
|
2019-03-08 15:22:17 -05:00
|
|
|
|
2018-06-28 03:46:10 -04:00
|
|
|
## Troubleshooting
|
2019-04-13 07:15:28 -04:00
|
|
|
* Please make sure Target Url includes valid `host`, optional `port`,
|
2022-07-27 06:47:46 -04:00
|
|
|
optional `path`, and begins with `coap://`, `coaps://`, `coap+tcp://` or `coaps+tcp://`
|
|
|
|
(not all hosts support TCP/TLS including coap+tcp://californium.eclipseprojects.io).
|
2019-03-08 15:22:17 -05:00
|
|
|
|
2022-07-27 06:47:46 -04:00
|
|
|
* CoAP logging can be enabled by running 'idf.py menuconfig -> Component config -> CoAP Configuration -> Enable CoAP debugging'
|
|
|
|
and setting appropriate log level. If Mbed TLS logging is required, this needs to be configured separately under mbedTLS
|
|
|
|
Component Configuration and the CoAP logging level set to mbedTLS.
|