## 5 IoT Platform Communication with IoT Sensors and Applications
### CoAP Versus HTTP
The dressing style in CoAP is like that in HTTP and extends to the URI structure, so to get the resource the URI address must be known in advance. Similar to an HTTP URI, a typical CoAP URI is shown in the following format:  
* coap://host[:port]/[path][?query]. CoAP does not inherent an authentication or encryption standard, so the user needs to use the Datagram Transport Layer Security (DTLS). A URI example when using DTLS is:  
* //insecure connection coap://ex.net:10/~status/val.xml
* //secure connection coaps://ex.net:10/~status/val.xml  
Since CoAP is lightweight and HTTP-like, the clients can communicate in the cloud, where proxies can also be used. The relationships between the endpoints can be established even at the sensor level. The origin servers in Figure 16 show the shared resources. As mentioned above, the proxies translate the CoAP to HTTP in order to forward client requests. The port that is used by CoAP must be under the support of a server offering resources. When DTLS has enabled the default CoAP, the port used is 5684, otherwise port 5683 is used. Figure 16 illustrates the architecture of the CoAP protocol. Figure 15: HTTP client and CoAP server interactions by layer via the HTTP-CoAP proxy. The two basic layers of CoAP are the request-response layer, which is responsible for sending and receiving RESTful queries, and the transactional layer, which handles the messages by utilizing one of the four CoAP message types. As mentioned previously, the four message types in CoAP are confirmable (CON), non-confirmable (NON), acknowledgement (ACK), and reset (RST). In the second layer, congestion control and multicasting are also included. The CoAP responses emulate HTTP also, where 2.01 stands for created, 2.02 for deleted, 2.04 for changed, 2.05 for content, 4.04 for (resource) not found, and 4.05 for method not allowed. There are seven major components of a CoAP system. First are the endpoints or the sources and destinations of a message. The proxies are the CoAP endpoints used to perform requests such as reducing the load of the network, accessing sleeping nodes, and providing a security layer. The client initiates a request and also receives the response, while the server is the destination of a request, after which a response is created. A mediator actor acts as a server and as a client to an origin server (a proxy is a mediator). The residence place of the given resource is called the origin server. The observers are clients that can register themselves using a modified GET message. An observer is connected to a resource, so if there are any changes to the state, the observer will be notified by the server. As seen in Figure 17, the CoAP message header is designed to achieve maximal efficiency and bandwidth preservation. With a four-byte-long header and request messages with 10- to 20-byte headers, CoAP message headers are typically 10 times shorter than the HTTP ones. The message type  
Figure 16: The architecture of CoAP. A proxy or intermediary maps the HTTP and CoAP protocols between origin servers and clients. Figure 17: CoAP message structure. identifiers (T) are set in each header alongside the related unique message-ID. Error or success signaling across the channels is performed using the code field. All other fields after the header are optional. In the CoAP message structure, the version is a 2-bit integer and by default is set to 1, meaning the future versions may differ. The message type is defined by a 2-bit identifier as follows: CON(0), ACK(2), RST(3). The token length is the length of a variable-length token field and the code is an 8-bit indicator of success, failure, and errors. The message-ID is a 16-bit unsigned integer used to detect duplicate messages, while the token takes from 0 up to 8 bytes and is used to associate requests with responses. There are some optional parameters for the requests and responses that may be added, such as the URI information, max-age, content, and Etags. The payload is optional as well, which can be data or a message of zero length [36].