1
2# CoAP server example
3
4(See the README.md file in the upper level 'examples' directory for more information about examples.)
5This CoAP server example is very simplified adaptation of one of the
6[libcoap](https://github.com/obgm/libcoap) examples.
7
8CoAP server example will startup a daemon task, receive requests / data from CoAP client and transmit
9data to CoAP client.
10
11If the incoming request requests the use of DTLS (connecting to port 5684), then the CoAP server will
12try to establish a DTLS session using the previously defined Pre-Shared Key (PSK) - which
13must be the same as the one that the CoAP client is using, or Public Key Infrastructure (PKI) where
14the PKI information must match as requested.
15
16NOTE: Client sessions trying to use coaps+tcp:// are not currently supported, even though both
17libcoap and MbedTLS support it.
18
19The Constrained Application Protocol (CoAP) is a specialized web transfer protocol for use with
20constrained nodes and constrained networks in the Internet of Things.
21The protocol is designed for machine-to-machine (M2M) applications such as smart energy and
22building automation.
23
24Please refer to [RFC7252](https://www.rfc-editor.org/rfc/pdfrfc/rfc7252.txt.pdf) for more details.
25
26## How to use example
27
28### Configure the project
29
30```
31idf.py menuconfig
32```
33
34Example Connection Configuration  --->
35 * Set WiFi SSID under Example Configuration
36 * Set WiFi Password under Example Configuration
37Example CoAP Client Configuration  --->
38 * If PSK, Set CoAP Preshared Key to use in connection to the server
39Component config  --->
40  CoAP Configuration  --->
41    * Set encryption method definition, PSK (default) or PKI
42    * Enable CoAP debugging if required
43  High resolution timer (esp_timer)  --->
44    * Hardware timer to use for esp_timer - change if required (FRC2 for QEMU)
45
46
47### Build and Flash
48
49Build the project and flash it to the board, then run monitor tool to view serial output:
50
51```
52idf.py build
53idf.py -p PORT flash monitor
54```
55
56(To exit the serial monitor, type ``Ctrl-]``.)
57
58See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
59
60## Example Output
61current CoAP server would startup a daemon task,
62and the log is such as the following:
63
64```
65...
66I (332) wifi: mode : sta (30:ae:a4:04:1b:7c)
67I (1672) wifi: n:11 0, o:1 0, ap:255 255, sta:11 0, prof:1
68I (1672) wifi: state: init -> auth (b0)
69I (1682) wifi: state: auth -> assoc (0)
70I (1692) wifi: state: assoc -> run (10)
71I (1692) wifi: connected with huawei_cw, channel 11
72I (1692) wifi: pm start, type: 1
73
74I (2622) event: sta ip: 192.168.3.84, mask: 255.255.255.0, gw: 192.168.3.1
75I (2622) CoAP_server: Connected to AP
76...
77```
78
79If a CoAP client queries the `/Espressif` resource, CoAP server will return `"Hello World!"`
80until a CoAP client does a PUT with different data.
81
82## libcoap Documentation
83This can be found at [libcoap Documentation](https://libcoap.net/documentation.html).
84The current API is 4.3.0.
85
86## libcoap Specific Issues
87These can be raised at [libcoap Issues](https://github.com/obgm/libcoap/issues).
88
89## Troubleshooting
90* Please make sure CoAP client fetchs or puts data under path: `/Espressif` or
91fetches `/.well-known/core`
92
93* CoAP logging can be enabled by running 'idf.py menuconfig -> Component config -> CoAP Configuration' and setting appropriate log level
94