## 1 Introduction
However, more powerful networks and devices could still use complex libraries or APIs such as those listed above. Typical WSNs nodes are battery-powered and often deployed in unattended environment, therefore software applications should be optimized concerning lowest energy consumption and reliable execution. The reduction of energy consumption is mainly achieved using radio duty cycling protocols. Further reduction can be reached through effective memory management. This allows saving CPU cycles and reducing the code complexity. Both aspects are critical for lowering the energy consumed by the CPU processing and for reducing the risks of failures during execution. The optimization process of our CoAP implementation is focused in developing an efficient and safe use of memory. The results obtained in our tests prove the importance that memory management has on performance and resource consumption. This paper focuses on severely constrained WSNs nodes featuring few kilobytes of memory and CPUs with reduced computational capabilities. Although some actual technology offers more powerful nodes, working with these constrained nodes help us to fix a lower bound to the resources that could be used by our implementation. In that way, we ensure that our design choices can deliver a highly optimized implementation suitable for any application domain. CoAP has been already implemented in the most popular OSs for WSNs such as Contiki [16] and TinyOS [15, 17]. In particular, an implementation based on the Libcoap library has been released in the latest distribution of TinyOS. We refer to this implementation as CoapBlip. However, we detected some weaknesses in its design that may have a negative impact on the resource consumption and functioning of sensor nodes. Furthermore, being an adaptation of an external library its code does not result optimized for TinyOS. The implementation presented in [17] has been developed on top of a 6 LoWPAN implementation that is no longer supported, and is affected by various limitations [18, 19]. Therefore, the existing CoAP implementations for TinyOS would not allow either the functionalities or the performance offered by this protocol to be fully realized. To overcome this problem, we design and implement our original implementation of CoAP for TinyOS, which we refer to as TinyCoAP ([[http://sourceforge.net/projects/tinycoap/](http://sourceforge.net/projects/tinycoap/)]([http://sourceforge.net/projects/tinycoap/](http://sourceforge.net/projects/tinycoap/))). The CoAP definition developed by the CoRE work group is still under standardization, and its use in WSNs has been studied in several works [15, 16, 17, 20, 21, 22]. However, none of these papers contain an extensive evaluation of CoAP. In particular, the reliability mechanism provided by the protocol has not yet been evaluated. Although a comparison between CoAP and HTTP has been made in [22], it does not consider the possibility of using HTTP with transport protocols different from TCP. In our tests, we evaluate the performance of an HTTP server using UDP and persistent TCP connections. In fact, a fair comparison between CoAP and HTTP should at least include the above-mentioned possibilities in order to minimize the effects that TCP has on the HTTP performance, otherwise a comparison between CoAP and HTTP would result in an evaluation of UDP and TCP. In this paper, we provide several original contributions. First, we present and evaluate our original implementation of a CoAP library native for TinyOS. Then, we provide a comprehensive analysis of the functioning of CoAP including an evaluation of the reliability mechanism. The analysis is conducted by carrying out an extensive evaluation of CoAP performance in a real WSN. In particular,we compare the performance of TinyCoAP with those of HTTP and CoapBlip. The evaluation of each solution is performed in terms of energy, latency, and memory occupation. The effects that high request rates have on performance have been also evaluated.