RESTful Protocol and CoAP
RESTful Protocol and CoAP
The SOA style (see Figure 2) is structured around three key architectural (*) A Perspective on the Future of
components: (i) service provider, (ii) service consumer, and (iii) service Middleware-based Software Engineering,
registry. In SOA-based environments, the Service-Oriented Middleware Valérie Issarny, Mauro Caporuscio,
Nikolaos Georgantas, Workshop on the
(SOM) is in charge of enabling the deployment of services and
Future of Software Engineering : FOSE
coordination among the three key conceptual elements that 2007, 2007, Minneapolis, United States.
characterize the SOA style. pp.244-258, 2007,
https://hal.inria.fr/inria-00415919
Popularity of service oriented computing is mainly due to its Web
Service instantiation.
REST stands for REpresentational State Transfer. REST is web standards based architecture and uses HTTP Protocol
for data communication. It revolves around resource where every component is a resource and a resource is accessed
by a common interface using HTTP standard methods. REST was first introduced by Roy Fielding in 2000.
In REST architecture, a REST Server simply provides access to resources and REST client accesses and presents the
resources. Here each resource is identified by URIs/ global IDs. REST uses various representations to represent a
resource like text, JSON and XML. Now a days JSON is the most popular format being used in web services.
Even though REST is heavily influenced by the Web-Technology, in theory the REST architecture style is not bound
to HTTP. However, HTTP is the only relevant instance of the REST. For this reason this article describes REST
implemented by using HTTP. Often this is called RESTful HTTP.
The idea behind RESTful HTTP is to use the existing features and capabilities of the WEB. REST does not invent new
technologies, components or services. RESTful HTTP defines the principles and constrains to use the existing WEB-
Standards in a better way.
For the next exercise, you must choice the software environment you are using:
Your virtual machine with linux
Your native machine with Windows and Cygwin to enable shell script programming
J.-Y. Tigli
Université de Nice – Sophia Antipolis
1
930, Route des Colles – B.P. 145 - 06903 Sophia Antipolis Cedex – France
Tél : +33 (0)4 92 96 50 50 – Fax : +33 (0)4 92 96 50 55
http://rainbow.i3s.unice.fr
J.-Y. Tigli, I3S – University of Nice Sophia Antipolis, 2016-2017
Middleware for Internet of Things – RESTful protocol and CoAP
«curl» is a tool to transfer data from or to a server, using one of the supported protocols (DICT, FILE, FTP, FTPS,
GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMBS, SMTP,
SMTPS, TELNET and TFTP). The command is designed to work without user interaction.
curl http://curl.haxx.se/docs/httpscripting.html
You can find more information about “curl” in these man pages and tutorials.
All the documentation about the corresponding RESTFul API is on http://openweathermap.org/api. More details
and example are available on http://openweathermap.org/current. To use these services you must first sign up to
get your own key on http://openweathermap.org/price. Your key is then the value of the “appid” parameter in the
requests to the web services (ex. http://api.openweathermap.org/data/2.5/weather?id=2172797&appid=<YOUR
KEY HERE>).
Exercise 2 : “Jq” is a lightweight and flexible command-line JSON processor. It’s available for all OS like linux
and windows on https://stedolan.github.io/jq/download/. In this last case you can use it as a shell command
using Cygwin. Don’t forget to copy jq.exe in the bin directory of Cygwin. In the case of linux you can use “sudo
apt-get install jq” command.
Write some shell scripts using “curl” and “jq” that get temperature (Celsius), pressure (hPa), wind direction
and speed, humidity and the weather for a given city. Basic filters that can be defined with jp are like these
(https://stedolan.github.io/jq/manual/#Basicfilters).
Be careful:
If you need to add parameters in your URL, you must split url and the parameter in a «curl» call. Parameter
must appear after –d option and before the url..
If you’re interested in how to install it you can look at the website of the project. Here we are going to use it to
manage some Zwave ,NOcean sensors, etc. They are popular standards for connected devices in smart homes. For
this tutorial, one openhab server will be installed in the tutorial network with a set of Zwave devices (some details
will be given by the teacher during the tutorial).
J.-Y. Tigli
Université de Nice – Sophia Antipolis
2
930, Route des Colles – B.P. 145 - 06903 Sophia Antipolis Cedex – France
Tél : +33 (0)4 92 96 50 50 – Fax : +33 (0)4 92 96 50 55
http://rainbow.i3s.unice.fr
J.-Y. Tigli, I3S – University of Nice Sophia Antipolis, 2016-2017
Middleware for Internet of Things – RESTful protocol and CoAP
The sensors and actuators are reachable at the addresses http://192.168.1.187:8080/items/<name>, where <name> is
the name of the device like for example “zwave_device_xxx”.
Exercise 3 : Test your access to the Zwave sensors and actuators through the provided web services, using online
commands. What is the protocol to exchange data?
J.-Y. Tigli
Université de Nice – Sophia Antipolis
3
930, Route des Colles – B.P. 145 - 06903 Sophia Antipolis Cedex – France
Tél : +33 (0)4 92 96 50 50 – Fax : +33 (0)4 92 96 50 55
http://rainbow.i3s.unice.fr
J.-Y. Tigli, I3S – University of Nice Sophia Antipolis, 2016-2017
Middleware for Internet of Things – RESTful protocol and CoAP
Be careful:
In the case of the remote electrical plug you must send a POST / HTTP commands with data for the
command on the body of the message. To do that, find an example on the web on how to use
HttpWebRequest to send a POST.
The Constrained Application Protocol (CoAP) is a RESTful web transfer protocol for resource-constrained networks
and nodes. CoAP is designed to easily translate to HTTP for simplified integration with the web, while also meeting
specialized requirements such as multicast support, very low overhead, and simplicity. The CoRE group has proposed
the following features for CoAP: RESTful protocol design minimizing the complexity of mapping with HTTP, Low
header overhead and parsing complexity, URI and content-type support, Support for the discovery of resources
provided by known CoAP services.
0 1 2 3
01234567890123456789012345678901
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| 1 | 0 | 1 | GET=1 | MID=0x7d34 |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| 11 | 11 | "temperature" (11 B) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
0 1 2 3
01234567890123456789012345678901
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| 1 | 2 | 0 | 2.05=69 | MID=0x7d34 |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| "22.3 C" (6 B) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
J.-Y. Tigli
Université de Nice – Sophia Antipolis
4
930, Route des Colles – B.P. 145 - 06903 Sophia Antipolis Cedex – France
Tél : +33 (0)4 92 96 50 50 – Fax : +33 (0)4 92 96 50 55
http://rainbow.i3s.unice.fr
J.-Y. Tigli, I3S – University of Nice Sophia Antipolis, 2016-2017
Middleware for Internet of Things – RESTful protocol and CoAP
CoAP.NET 1.1.0 is a nugget package (https://www.nuget.org/packages/CoAP/), thus you can install the library for
you project in Visual Studio using the package manager console (see http://docs.nuget.org/consume/package-
manager-console to proceed).
Exercise 8 :
The “CoAP Simple Service.zip” file available on the course web site, show you a simple development of a CoAP client
and server that exchange “Hello World” message.
Modify this code to:
Test your client to connect a remote CoAP server (with another URL than localhost)
Create a second resource that manage a temperature value (integer) that we can change and read from a
CoAP server
J.-Y. Tigli
Université de Nice – Sophia Antipolis
5
930, Route des Colles – B.P. 145 - 06903 Sophia Antipolis Cedex – France
Tél : +33 (0)4 92 96 50 50 – Fax : +33 (0)4 92 96 50 55
http://rainbow.i3s.unice.fr
J.-Y. Tigli, I3S – University of Nice Sophia Antipolis, 2016-2017
Middleware for Internet of Things – RESTful protocol and CoAP
Exercise 9 : Using Windows Communication Foundation (WCF) and CoAP.Net, write a Gateway that share a
resource between a CoAP Service and a HTTP REST Service.
J.-Y. Tigli
Université de Nice – Sophia Antipolis
6
930, Route des Colles – B.P. 145 - 06903 Sophia Antipolis Cedex – France
Tél : +33 (0)4 92 96 50 50 – Fax : +33 (0)4 92 96 50 55
http://rainbow.i3s.unice.fr