This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| en:iot-open:practical:hardware:sut:esp32:iot_6 [2024/05/01 16:27] – created pczekalski | en:iot-open:practical:hardware:sut:esp32:iot_6 [2024/06/29 18:52] (current) – [Prerequisites] pczekalski | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== IOT6: Connecting to the CoAP service ====== | ||
| + | The following scenario will show you how to connect to the existing CoAP server. The server can be either an implementation on another node as present in the scenario [[en: | ||
| + | A CoAP service is an endpoint that provides information. It is UDP-based. To connect to the service, you need to know its coap URI (or coaps, for secure connections). | ||
| + | |||
| + | ===== Prerequisites ===== | ||
| + | To implement this scenario, it is necessary to get familiar with at least one of the following scenarios first: | ||
| + | * [[en: | ||
| + | * [[en: | ||
| + | * [[en: | ||
| + | * [[en: | ||
| + | and obligatory: | ||
| + | * [[[en: | ||
| + | |||
| + | There are many implementations of the CoAP protocol, but we will use the following library: | ||
| + | <code ini> | ||
| + | lib_deps = | ||
| + | hirotakaster/ | ||
| + | </ | ||
| + | |||
| + | ===== Suggested Readings and Knowledge Resources ===== | ||
| + | * [[en: | ||
| + | * [[en: | ||
| + | * [[en: | ||
| + | * [[en: | ||
| + | * [[en: | ||
| + | |||
| + | ===== Hands-on Lab Scenario ===== | ||
| + | Note—this scenario can be used in tandem with [[[en: | ||
| + | |||
| + | You can also choose to use an existing service (e.g., NodeRed implementation). Note that the service must be present in the WiFi network to which you connect your node. | ||
| + | |||
| + | ==== Task to be implemented ==== | ||
| + | Connect to the " | ||
| + | Connect to the CoAP server using the given URI. The server' | ||
| + | |||
| + | If you choose to use the CoAP service implemented already in your network as a network service, refer to the technical documentation of the infrastructure to find its URL and credentials. | ||
| + | |||
| + | ==== Start ==== | ||
| + | Check if you can clearly see a full display (of your choice) in your video stream. Book a device and create a dummy Arduino file with '' | ||
| + | Implement a connection to the " | ||
| + | |||
| + | === Step 1 === | ||
| + | Refer to the hardware documentation and ensure an understanding of the network infrastructure you're interfacing with.\\ | ||
| + | Implement the code to display on the selected device.\\ | ||
| + | Connect to the WiFi in the STA mode (as a client) and ensure the connection is OK and you got an IP from the DHCP server.\\ | ||
| + | It is essential to note and present (using a display of your choice) the node's IP address, as you will later need to refer to it with a client to use your service. | ||
| + | |||
| + | === Step 2 === | ||
| + | Include the WiFi UDP and CoAP implementation libraries headers in your code: | ||
| + | <code c> | ||
| + | #include < | ||
| + | #include < | ||
| + | </ | ||
| + | WiFi UDP is part of the Arduino for the ESP32 framework, so you do not need to add it explicitly to the '' | ||
| + | === Step 3 === | ||
| + | Declare an IP address of the CoAP server endpoint. Adjust '' | ||
| + | <code c> | ||
| + | #define CoAPport 5683 | ||
| + | #define CoAPpath "< | ||
| + | IPAddress coapExtSeviceIP(a, | ||
| + | </ | ||
| + | <note tip>The '' | ||
| + | < | ||
| + | coap:// | ||
| + | </ | ||
| + | then your '' | ||
| + | <code c> | ||
| + | #define CoAPpath " | ||
| + | </ | ||
| + | === Step 4 === | ||
| + | Declate communication objects: | ||
| + | <code c> | ||
| + | WiFiUDP udp; //UDP Communication class | ||
| + | Coap coap(udp); | ||
| + | </ | ||
| + | |||
| + | === Step 5 === | ||
| + | Declare function prototypes (not necessary if you implement them in the correct order): | ||
| + | <code c> | ||
| + | void callback_response(CoapPacket & | ||
| + | </ | ||
| + | |||
| + | === Step 5 === | ||
| + | Implement response handler for '' | ||
| + | <code c> | ||
| + | void callback_response(CoapPacket & | ||
| + | char p[packet.payloadlen + 1]; | ||
| + | memcpy(p, packet.payload, | ||
| + | p[packet.payloadlen] = NULL; | ||
| + | //Add your code to represent information on the selected display | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | === Step 6 === | ||
| + | Register response callback and start CoAP client: | ||
| + | <code c> | ||
| + | coap.response(callback_response); | ||
| + | coap.start(); | ||
| + | </ | ||
| + | |||
| + | === Step 7 === | ||
| + | Make a call (GET) request to the service. Remember to provide the correct URI part.\\ | ||
| + | Mind that the last argument of the function is a " | ||
| + | The laboratory technical documentation provides a list of the services, their URIs (including paths), methods, and IP ports. | ||
| + | <code c> | ||
| + | int msgid = coap.get(coapExtSeviceIP, | ||
| + | </ | ||
| + | |||
| + | === Step 8 === | ||
| + | Process CoAP services in the '' | ||
| + | <code c> | ||
| + | delay(1000); | ||
| + | coap.loop(); | ||
| + | </ | ||
| + | < | ||
| + | |||
| + | ==== Result validation ==== | ||
| + | You should be able to connect to WiFi and a CoAP service of your choice (either another node or a software service present in the network): depending on whether you're fully remote or able to access our networks with an additional device, you need to implement a CoAP server on another laboratory node (as present in the scenario [[[en: | ||
| + | |||
| + | ===== FAQ ===== | ||
| + | **How do I implement a server to my CoAP client?**: If you're fully remote, there are two options: | ||
| + | * you need to implement a service (CoAP server) on your own - use another laboratory node and scenario [[[en: | ||
| + | * or you can use a service available in the network (refer to the node's technical documentation for integration services). | ||
| + | If you're on a laptop or mobile within the WiFi network range to which the laboratory nodes are connected, you can connect to it with your laptop and use any CoAP client/ | ||
| + | |||
| + | < | ||
| + | <code bash> | ||
| + | ~$ coap-server | ||
| + | </ | ||
| + | then you can check its availability with the following command: | ||
| + | <code bash> | ||
| + | ~$ coap-client -m get coap:// | ||
| + | This is a test server made with libcoap (see https:// | ||
| + | Copyright (C) 2010--2019 Olaf Bergmann < | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | <WRAP noprint> | ||
| + | ===== Project information ===== | ||
| + | {{: | ||
| + | This Intellectual Output was implemented under the Erasmus+ KA2.\\ | ||
| + | Project IOT-OPEN.EU Reloaded – Education-based strengthening of the European universities, | ||
| + | Project number: 2022-1-PL01-KA220-HED-000085090. | ||
| + | |||
| + | **__Erasmus+ Disclaimer__**\\ | ||
| + | This project has been funded with support from the European Commission. \\ | ||
| + | This publication reflects the views of only the author, and the Commission cannot be held responsible for any use that may be made of the information contained therein. | ||
| + | |||
| + | **__Copyright Notice__**\\ | ||
| + | This content was created by the IOT-OPEN.EU Reloaded consortium, 2022, | ||
| + | The content is Copyrighted and distributed under CC BY-NC [[https:// | ||
| + | <figure label> | ||
| + | {{: | ||
| + | </ | ||
| + | |||
| + | </ | ||