| 
									
										
										
										
											2018-06-28 15:46:10 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | # CoAP client example
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | (See the README.md file in the upper level 'examples' directory for more information about examples.) | 
					
						
							| 
									
										
										
										
											2019-04-13 12:15:28 +01:00
										 |  |  | This CoAP client example is very simplified adaptation of one of the | 
					
						
							|  |  |  | [libcoap](https://github.com/obgm/libcoap) examples. | 
					
						
							| 
									
										
										
										
											2018-06-28 15:46:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-13 12:15:28 +01: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 15:46:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-13 12:15:28 +01: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 15:46:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-13 12:15:28 +01:00
										 |  |  | 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](https://www.rfc-editor.org/rfc/pdfrfc/rfc7252.txt.pdf) for more details. | 
					
						
							| 
									
										
										
										
											2018-06-28 15:46:10 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | ## How to use example
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ### Configure the project
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2019-08-02 09:01:20 +05:30
										 |  |  | idf.py menuconfig | 
					
						
							| 
									
										
										
										
											2018-06-28 15:46:10 +08:00
										 |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-13 12:15:28 +01:00
										 |  |  | Example Connection Configuration  ---> | 
					
						
							|  |  |  |  * Set WiFi SSID under Example Configuration | 
					
						
							|  |  |  |  * Set WiFi Password under Example Configuration | 
					
						
							|  |  |  | Example CoAP Client Configuration  ---> | 
					
						
							|  |  |  |  * Set CoAP Target Uri | 
					
						
							| 
									
										
										
										
											2019-07-31 16:22:49 +05:30
										 |  |  |  * If PSK, Set CoAP Preshared Key to use in connection to the server | 
					
						
							|  |  |  |  * If PSK, Set CoAP PSK Client identity (username) | 
					
						
							| 
									
										
										
										
											2019-04-13 12:15:28 +01:00
										 |  |  | Component config  ---> | 
					
						
							| 
									
										
										
										
											2019-07-31 16:22:49 +05:30
										 |  |  |   CoAP Configuration  ---> | 
					
						
							|  |  |  |     * Set encryption method definition, PSK (default) or PKI | 
					
						
							|  |  |  |     * Enable CoAP debugging if required | 
					
						
							| 
									
										
										
										
											2018-06-28 15:46:10 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | ### Build and Flash
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Build the project and flash it to the board, then run monitor tool to view serial output: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2019-04-13 12:15:28 +01:00
										 |  |  | idf.py build | 
					
						
							| 
									
										
										
										
											2019-08-02 09:01:20 +05:30
										 |  |  | idf.py -p PORT flash monitor | 
					
						
							| 
									
										
										
										
											2018-06-28 15:46:10 +08: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 20:22:17 +00:00
										 |  |  | Prerequisite: we startup a CoAP server on coap server example, | 
					
						
							| 
									
										
										
										
											2021-07-19 14:17:27 +05:30
										 |  |  | or use the default of coaps://californium.eclipseprojects.io. | 
					
						
							| 
									
										
										
										
											2018-06-28 15:46:10 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | and you could receive data from CoAP server if succeed,   | 
					
						
							|  |  |  | such as the following log: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | ... | 
					
						
							| 
									
										
										
										
											2021-07-19 14:17:27 +05:30
										 |  |  | I (5104) esp_netif_handlers: example_connect: sta ip: 192.168.0.103, mask: 255.255.255.0, gw: 192.168.0.1 | 
					
						
							|  |  |  | I (5104) example_connect: Got IPv4 event: Interface "example_connect: sta" address: 192.168.0.103 | 
					
						
							|  |  |  | I (5604) example_connect: Got IPv6 event: Interface "example_connect: sta" address: fe80:0000:0000:0000:32ae:a4ff:fec5:3234, type: ESP_IP6_ADDR_IS_LINK_LOCAL | 
					
						
							|  |  |  | I (5604) example_connect: Connected to example_connect: sta | 
					
						
							|  |  |  | I (5614) example_connect: - IPv4 address: 192.168.0.103 | 
					
						
							|  |  |  | I (5614) example_connect: - IPv6 address: fe80:0000:0000:0000:32ae:a4ff:fec5:3234, type: ESP_IP6_ADDR_IS_LINK_LOCAL | 
					
						
							|  |  |  | I (6004) CoAP_client: DNS lookup succeeded. IP=35.185.40.182 | 
					
						
							| 
									
										
										
										
											2019-03-08 20:22:17 +00:00
										 |  |  | Received: | 
					
						
							| 
									
										
										
										
											2021-07-19 14:17:27 +05:30
										 |  |  | **************************************************************** | 
					
						
							|  |  |  | CoAP RFC 7252                                  Cf 3.0.0-SNAPSHOT | 
					
						
							|  |  |  | **************************************************************** | 
					
						
							| 
									
										
										
										
											2019-03-08 20:22:17 +00:00
										 |  |  | This server is using the Eclipse Californium (Cf) CoAP framework | 
					
						
							|  |  |  | published under EPL+EDL: http://www.eclipse.org/californium/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-19 14:17:27 +05:30
										 |  |  | (c) 2014-2020 Institute for Pervasive Computing, ETH Zurich and others | 
					
						
							|  |  |  | **************************************************************** | 
					
						
							| 
									
										
										
										
											2018-06-28 15:46:10 +08:00
										 |  |  | ... | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-08 20:22:17 +00:00
										 |  |  | ## libcoap Documentation
 | 
					
						
							|  |  |  | This can be found at https://libcoap.net/doc/reference/4.2.0/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-28 15:46:10 +08:00
										 |  |  | ## Troubleshooting
 | 
					
						
							| 
									
										
										
										
											2019-04-13 12:15:28 +01:00
										 |  |  | * 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 | 
					
						
							| 
									
										
										
										
											2021-07-19 14:17:27 +05:30
										 |  |  | (not all do including coap+tcp://californium.eclipseprojects.io). | 
					
						
							| 
									
										
										
										
											2019-03-08 20:22:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-31 16:22:49 +05:30
										 |  |  | * CoAP logging can be enabled by running 'idf.py menuconfig -> Component config -> CoAP Configuration' and setting appropriate log level |