Table of Contents

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 IOT5: Setting up a CoAP service or a software implementation present in the network.
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:

and obligatory:

There are many implementations of the CoAP protocol, but we will use the following library:

lib_deps = 
    hirotakaster/CoAP simple library@1.3.28

Suggested Readings and Knowledge Resources

Hands-on Lab Scenario

Note—this scenario can be used in tandem with IOT5: Setting up a CoAP service to build a client-server solution using two devices (CoAP server and CoAP client). You need to book two devices then and develop them in parallel. We suggest connecting them over the AP rather than setting up an access point on the ESP32 device.

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 “internal IoT” WiFI access point as presented in the scenario IOT2: Reading IP address of the WiFi—present connection status on display. Connect to the CoAP server using the given URI. The server's address must be present in the internal IoT network to which your device is connected.

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 void setup()… and void loop()….
Implement a connection to the “internal IoT” network as a client. Refer to the supervisor or the technical documentation on credentials (SSID, passphrase). We do not provide the exact code on how to connect to the WiFi as it is a part of IOT2: Reading IP address of the WiFi scenario.

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:

#include <WiFiUdp.h>
#include <coap-simple.h>

WiFi UDP is part of the Arduino for the ESP32 framework, so you do not need to add it explicitly to the platformio.ini file.

Step 3

Declare an IP address of the CoAP server endpoint. Adjust a, b, c d as needed for your context; see node's technical documentation for integration services details:

#define CoAPport 5683
#define CoAPpath "<here comes CoAP service path, without header, IP and port>"
IPAddress coapExtSeviceIP(a,b,c,d);
The CoAPpath is just a path part of the URI, e.g. if your CoAP service URI is:
coap://your_coap_server:9876/path_to_the_service/and_another_path

then your CoAPpath should be declared as:

#define CoAPpath "path_to_the_service/and_another_path"

Step 4

Declate communication objects:

WiFiUDP udp;      //UDP Communication class
Coap coap(udp);   //CoAP Communication class

Step 5

Declare function prototypes (not necessary if you implement them in the correct order):

void callback_response(CoapPacket &packet, IPAddress ip, int port); // CoAP client response callback

Step 5

Implement response handler for GET method:

void callback_response(CoapPacket &packet, IPAddress ip, int port) { 
  char p[packet.payloadlen + 1];
  memcpy(p, packet.payload, packet.payloadlen);
  p[packet.payloadlen] = NULL;
  //Add your code to represent information on the selected display
}

Step 6

Register response callback and start CoAP client:

  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 “path” part of the URI. The laboratory technical documentation provides a list of the services, their URIs (including paths), methods, and IP ports.

int msgid = coap.get(coapExtSeviceIP,CoAPport,CoAPpath);

Step 8

Process CoAP services in the void loop():

  delay(1000);
  coap.loop();
If you plan to make frequent requests, decrease the time above (delay()).

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 IOT5: Setting up a CoAP service) or use some other service (such as NodeRED or even command line): For self implementation, your CoAP service must be present in the same WiFi network as your CoAP client (laboratory node).

FAQ

How do I implement a server to my CoAP client?: If you're fully remote, there are two options:

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/server.

In Linux environments, you can use a dummy CoAP service that is installed along with libcoap package:
~$ coap-server

then you can check its availability with the following command:

~$ coap-client -m get coap://127.0.0.1
This is a test server made with libcoap (see https://libcoap.net)
Copyright (C) 2010--2019 Olaf Bergmann <bergmann@tzi.org> and others

Project information


This Intellectual Output was implemented under the Erasmus+ KA2.
Project IOT-OPEN.EU Reloaded – Education-based strengthening of the European universities, companies and labour force in the global IoT market.
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,2024.
The content is Copyrighted and distributed under CC BY-NC Creative Commons Licence, free for Non-Commercial use.