http://anrg.usc.edu/contiki/api.php?action=feedcontributions&user=Muni&feedformat=atomContiki - User contributions [en]2024-03-29T13:36:41ZUser contributionsMediaWiki 1.26.2http://anrg.usc.edu/contiki/index.php?title=EE_652_projects&diff=1663EE 652 projects2014-12-18T06:29:39Z<p>Muni: /* Backpressure Control Protocol on IPv6 stack of Contiki - Mrunal & Chhavi */</p>
<hr />
<div>EE 652 2014 Projects<br />
<br />
== Scheduling algorithms for IEEE 802.15.4e networks - Pedro ==<br />
<br />
'''Repository''': http://neptune.usc.edu:8081/pdasilva/tsch-schedulers/<br />
<br />
'''Description''':<br />
<br />
''How to compile'': gcc -std=gnu99 -o Scheduling util/*.c graphs/*.c mcc/*.c tasa/*.c main.c<br />
<br />
''How to execute'': ./Scheduling <sink_id> <algorithm><br />
<br />
where: <sink_id> = Sink identification (starting at 0) and <algorithm> = 0 if MCC and 1 if TASA<br />
<br />
''Output'': you will find files ext_schedule.h and topology.c, which should be used according to the project report<br />
<br />
The execution will consider data/prr55.txt file as input for PRR statistics. You need to create the file with tree description before running TASA. You can easily do that first running MCC for a given sink id (e.g. 3) and then running TASA for the same sink id.<br />
<br />
== Backpressure Control Protocol on IPv6 stack of Contiki - Mrunal and Chhavi ==<br />
<br />
'''Repository''': https://github.com/chhavikapoor/EE652_Final<br />
<br />
Use the branch final_project from the repository.<br />
<br />
Follow the README-BCP in the final_project repository to simulate the implementation of BCP on IPv6 stack in COOJA.<br />
<br />
'''Description''': <br />
<br />
BCP is an implementation of dynamic backpressure routing in which the routing and forwarding decisions are made on per packet basis that takes into consideration the backpressure weight of each of its neighbors. The ubiquitous use of TCP/IP protocol suite in web applications, peer to peer networking over the internet etc motivated us to implement the Backpressure Collection Protocol on the IPv6 Stack of Contiki. We implemented a functional version of BCP on the IPv6 stack of Contiki OS.<br />
In our implementation, BCP Sink by default has the node_id = 1.<br />
<br />
== Modifications to RPL for Mobility - Pratyush Deshpande, Gopi Marella and Abhilash Hegde ==<br />
<br />
'''Repository''': https://github.com/pratyush18/contiki-new.git<br />
<br />
'''Description''':<br />
<br />
Routing Protocol for Low power and lossy networks (RPL) has been recently adopted IETF routing protocol standard for low power wireless sensor networks and Internet of Things applications. Originally RPL is designed for static networks with no support for mobility. But, several IoT applications involve mobile nodes and thus there is a need to modify RPL for supporting mobile node scenarios. In RPL, routing takes place by formationof Destination Oriented Acyclic Graph (DODAG). Several control messages like DODAG Information Objects (DIOs), DODAG Information Solicitation and DODAG Advertisement Objects (DAOs) are exchanged for the DODAG formation. These control messages propagate throughout the network and collaboratively work to form the DODAG. The <br />
control messages in RPL are controlled by several timers. These control message timers need to be modified for mobile node scenarios. The timers need to be optimized based on mobility of nodes in the network. In this paper we have modified the existing implementation of RPL protocol in Contiki Operating system to improve its performance for scenarios where mobile nodes are involved. We have also developed a test-bed for simulating mobile nodes in COOJA simulator and evaluating performance metrics like Packet delivery ratio, Power consumption and Average latency per packet.</div>Munihttp://anrg.usc.edu/contiki/index.php?title=EE_652_projects&diff=1660EE 652 projects2014-12-17T01:49:01Z<p>Muni: </p>
<hr />
<div>EE 652 2014 Projects<br />
<br />
== Pedro - Scheduling algorithms for IEEE 802.15.4e networks ==<br />
<br />
'''Repository''': http://neptune.usc.edu:8081/pdasilva/tsch-schedulers/<br />
<br />
'''Description''':<br />
<br />
''How to compile'': gcc -std=gnu99 -o Scheduling util/*.c graphs/*.c mcc/*.c tasa/*.c main.c<br />
<br />
''How to execute'': ./Scheduling <sink_id> <algorithm><br />
<br />
where: <sink_id> = Sink identification (starting at 0) and <algorithm> = 0 if MCC and 1 if TASA<br />
<br />
''Output'': you will find files ext_schedule.h and topology.c, which should be used according to the project report<br />
<br />
The execution will consider data/prr55.txt file as input for PRR statistics. You need to create the file with tree description before running TASA. You can easily do that first running MCC for a given sink id (e.g. 3) and then running TASA for the same sink id.<br />
<br />
== Backpressure Control Protocol on IPv6 stack of Contiki - Mrunal & Chhavi ==<br />
<br />
'''Repository''': https://github.com/chhavikapoor/EE652_Final<br />
<br />
Use the branch final_project from the repository.<br />
<br />
Follow the README-BCP in the final_project repository to simulate the implementation of BCP on IPv6 stack in COOJA.<br />
<br />
'''Description''': <br />
<br />
BCP is an implementation of dynamic backpressure routing in which the routing and forwarding decisions are made on per packet basis that takes into consideration the backpressure weight of each of its neighbors. The ubiquitous use of TCP/IP protocol suite in web applications, peer to peer networking over the internet etc motivated us to implement the Backpressure Collection Protocol on the IPv6 Stack of Contiki. We implemented a functional version of BCP on the IPv6 stack of Contiki OS.<br />
In our implementation, BCP Sink by default has the node_id = 1.</div>Munihttp://anrg.usc.edu/contiki/index.php?title=REST_example_running_on_Cooja_and_Sky_motes&diff=1130REST example running on Cooja and Sky motes2014-11-08T21:51:14Z<p>Muni: /* Introduction */</p>
<hr />
<div>[[Contiki_tutorials | Back to Contiki Tutorials]]<br />
<br />
__TOC__<br />
<br />
<br />
==Introduction==<br />
<br />
REST (Representational State Transfer) is an architectural style consisting of coordinated set of architectural constraints APPLIED to components, connectors and data elements within a distributed hypermedia system. It is one in which web services are viewed as resources and can be uniquely identified by their URLs. A web service can be characterized as RESTful if they conform to architectural constraints like client-server, stateless, cacheable, layered system and uniform Interface. The basic REST design principle uses the HTTP or COAP protocol methods for typical CRUD operations:<br />
* POST - Create a resource<br />
* GET - Retrieve a resource<br />
* PUT – Update a resource<br />
* DELETE - Delete a resource<br />
There are various resources that are available at the server. Each resource at the server has a handler function which the REST layer calls to serve the request by the client. The REST server sends the response back to the client with the contents of the resource requested.<br />
<br />
==You Will Learn==<br />
<br />
How to use REST layer to develop server-side applications like COAP or HTTP and run it on COOJA and using T-mote sky.<br />
<br />
==Relavant Files==<br />
<br />
* /contiki-2.7/apps/rest-common - It contains the codes that are mentioned as target in the rest-example.<br />
* /contiki-2.7/apps/rest-coap - It contains codes for codes mentioned as target in rest-common. <br />
* /contiki-2.7/apps/rest-http - It contains codes for codes mentioned as target in rest-common.<br />
* /contiki-2.7/examples/rest-example - It has the main example of the REST server side and COAP client side codes that are to be uploaded on COOJA and real motes.<br />
<br />
==Understanding The Code==<br />
<br />
===rest-server-example.c===<br />
<br />
The REST server has light, led, toggle, helloworld and discover resources. Each resource has a resource handler function which is called when the client requests for that resource and the resource handler function sends the response back to the client. For example, when a HTTP client sends a request to the REST server for light resource, the REST server calls the light_handler function which reads the light sensor values from the mote and sends the light sensor values along with a simple etag back to the client as shown below. <br />
<br />
<source lang="c"><br />
/*A simple getter example. Returns the reading from light sensor with a simple etag*/<br />
RESOURCE(light, METHOD_GET, "light");<br />
void<br />
light_handler(REQUEST* request, RESPONSE* response)<br />
{<br />
read_light_sensor(&light_photosynthetic, &light_solar);<br />
sprintf(temp,"%u;%u", light_photosynthetic, light_solar);<br />
<br />
char etag[4] = "ABCD";<br />
rest_set_header_content_type(response, TEXT_PLAIN);<br />
rest_set_header_etag(response, etag, sizeof(etag));<br />
rest_set_response_payload(response, temp, strlen(temp));<br />
}<br />
</source><br />
<br />
===coap-client-example.c===<br />
The COAP client establishes a connection with the server on the COAP port 61616 and sets the et timer to a particular value. Everytime the et timer is expired, the send_data(void) function is called. When it recieves the response from the server for its request, the handle_incoming_data() function is called as shown in the code snippet within the process. <br />
<br />
<source lang="c"><br />
etimer_set(&et, 5 * CLOCK_SECOND);<br />
while(1) {<br />
PROCESS_YIELD();<br />
if (etimer_expired(&et)) {<br />
send_data();<br />
etimer_reset(&et);<br />
} else if (ev == tcpip_event) {<br />
handle_incoming_data();<br />
}<br />
}<br />
</source><br />
<br />
The COAP client runs a timer which when resets, the client randomly selects a service_id (resource) using random_rand() function and sends the request to the REST server as seen below in the send_data(void) function.<br />
<br />
<source lang="c"><br />
int data_size = 0;<br />
int service_id = random_rand() % NUMBER_OF_URLS;<br />
coap_packet_t* request = (coap_packet_t*)allocate_buffer(sizeof(coap_packet_t));<br />
init_packet(request);<br />
<br />
coap_set_method(request, COAP_GET);<br />
request->tid = xact_id++;<br />
request->type = MESSAGE_TYPE_CON;<br />
coap_set_header_uri(request, service_urls[service_id]);<br />
</source><br />
<br />
<br />
When the server response returns back to the client, it runs the handle_incoming_data() function which takes the packet, parses the message and prints the payload that is it receives from the response. It can be seen in the code below<br />
<br />
<source lang="c"><br />
static void<br />
handle_incoming_data()<br />
{<br />
PRINTF("Incoming packet size: %u \n", (uint16_t)uip_datalen());<br />
if (init_buffer(COAP_DATA_BUFF_SIZE)) {<br />
if (uip_newdata()) {<br />
coap_packet_t* response = (coap_packet_t*)allocate_buffer(sizeof(coap_packet_t));<br />
if (response) {<br />
parse_message(response, uip_appdata, uip_datalen());<br />
response_handler(response);<br />
}<br />
}<br />
delete_buffer();<br />
}<br />
}<br />
</source><br />
<br />
==Run the REST-Example on COOJA==<br />
<br />
<br />
===HTTP Example===<br />
<br />
<br />
1. Open the Makefile in the /contiki-2.7/examples/rest-example folder. For HTTP server, make sure the WITH_COAP = 0 in the Makefile.<br />
<br />
<br />
2. Open the terminal. Go to the /contiki-2.7/examples/rest-example folder. Run the following command given below:<br />
<br />
<code><nowiki> make TARGET=cooja rest-server-example.csc </nowiki></code><br />
<br />
This will open a COOJA terminal and loads the rest-server-example code for simulation. The Network will look as shown:<br />
<br />
[[File:Networkhttp.PNG]] <br />
<br />
<br />
3. Open another terminal. Go to the same directory and connect the COOJA simulation to the router.<br />
<br />
<code><nowiki> make connect-router-cooja </nowiki></code><br />
<br />
<br />
4. Start the Simulation on the COOJA and test the connectivity by pinging the server. The IP addresses of the servers are aaaa::0212:7402:0002:0202 and aaaa::0212:7403:0003:0303.<br />
<br />
<code><nowiki> ping6 aaaa::0212:7402:0002:0202 </nowiki></code><br />
<br />
<code><nowiki> ping6 aaaa::0212:7403:0003:0303 </nowiki></code><br />
<br />
<br />
5. Use curl as a HTTP client we interact with the COOJA motes that are running the REST code. The default HTTP client runs on 8080 port number.<br />
<br />
<code><nowiki> curl -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/helloworld #get helloworld plain text </nowiki></code><br />
<br />
<code><nowiki> curl -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/led?color=green -d mode=off -i #turn off the green led </nowiki></code><br />
<br />
<code><nowiki> curl -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/.well-known/core -i </nowiki></code><br />
<br />
<code><nowiki> curl -X POST -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/helloworld #method not allowed </nowiki></code><br />
<br />
<br />
===COAP Example===<br />
<br />
<br />
1. Open the Makefile in the /contiki-2.7/examples/rest-example folder. For COAP server, make sure the WITH_COAP = 1 in the Makefile.<br />
<br />
2. Open the terminal. Go to the /contiki-2.7/examples/rest-example folder. Run the following command given below:<br />
<br />
<code><nowiki> make TARGET=cooja coap-client-server-example.csc </nowiki></code><br />
<br />
This opens the COOJA terminal which runs rest-server-example.c over COAP in one node having IP address aaaa::0212:7401:0001:0101 and coap-client-example.c in the another node having IP address aaaa::0212:7402:0002:0202. COAP uses 61616 port number as default. The client in the example periodically accesses the resources of the server and prints the payload which can be seen on the mote output. The Network looks as shown below:<br />
<br />
[[File:Coapnetwork.PNG]]<br />
<br />
3.Start the Simulation and observe the mote output. The Output is diplayed as shown in the below window.<br />
<br />
[[File:Coapmoteout.PNG]]<br />
<br />
==Run the REST-Example on T-mote Sky==<br />
<br />
<br />
1. Go to the /contiki-2.7/examples/rest-example folder. Connect the motes and program them with rest-server-example.<br />
<br />
<code><nowiki> make TARGET=sky rest-server-example.upload </nowiki></code><br />
<br />
<br />
2. Disconnect the motes and load one other mote with the RPL border router to connect to the rest-server.<br />
<br />
<code><nowiki> cd ../ipv6/rpl-border-router </nowiki></code><br />
<br />
<code><nowiki> make TARGET=sky border-router.upload </nowiki></code><br />
<br />
<br />
3. Connect the REST-server to the border router using tunslip6.<br />
<br />
<code><nowiki> make connect-router </nowiki></code><br />
<br />
<br />
4. Open new terminal window for each mote and execute the following command and reset the motes. The IP addresses of these motes get printed after the motes are reset.<br />
<br />
<code><nowiki> make login TARGET=sky MOTE=2 #Shows the prints for first mote </nowiki></code><br />
<br />
<code><nowiki> make login TARGET=sky MOTE=3 #For second mote and so on. </nowiki></code><br />
<br />
<br />
5. Ping the motes using the IP addresses we get from step 4. As highlighted below<br />
<br />
<code><nowiki> ping6 <IPv6 Address of the MOTE> </nowiki></code><br />
<br />
[[File:IPaddrmote.PNG]]<br />
<br />
<br />
6. If WITH_COAP = 0 i.e. HTTP Server, use the HTTP Client to connect to the REST server. When WITH_COAP = 1. i.e. COAP Server, load the coap-client-example.c to the motes and connect to the REST server (same as COOJA Example).<br />
<br />
==General Issues You Might Face==<br />
<br />
<br />
1. To use curl as the HTTP client you need to install curl by running the following command in the terminal:<br />
<br />
<code><nowiki> sudo apt-get install curl </nowiki></code><br />
<br />
2. The memory on the Tmote sky is not big enough to fit in the HTTP example on REST server. Thus while running it on real motes we get the error “region text is full”. To overcome this problem, instead of RPL to decide the route, we can use static routes. We can also reduce the size by changing the Makefile of the border router by setting the WITH_WEBSERVER = 0. <br />
<br />
3. Generally the COAP Client is used instead of HTTP client for running over REST Server.<br />
<br />
<br />
==References==<br />
<br />
<br />
1. REST-Example Github page: https://github.com/contiki-os/contiki/tree/master/examples/rest-example<br />
<br />
2. A Low-Power CoAP for Contiki - Matthias Kovatsch, Simon Duquennoy, Adam Dunkels<br />
<br />
<br />
[[Contiki_tutorials | Back to Contiki Tutorials]]</div>Munihttp://anrg.usc.edu/contiki/index.php?title=REST_example_running_on_Cooja_and_Sky_motes&diff=1119REST example running on Cooja and Sky motes2014-11-08T20:33:47Z<p>Muni: /* Run the REST-Example on T-mote Sky */</p>
<hr />
<div>[[Contiki_tutorials | Back to Contiki Tutorials]]<br />
<br />
__TOC__<br />
<br />
<br />
==Introduction==<br />
<br />
REST (Representational State Transfer) is an architectural style consisting of coordinated set of architectural constraints APPLIED to components, connectors and data elements within a distributed hypermedia system. It is one in which web services are viewed as resources and can be uniquely identified by their URLs. A web service can be characterized as RESTful if they conform to architectural constraints like client-server, stateless, cacheable, layered system and uniform Interface. The basic REST design principle uses the HTTP or COAP protocol methods for typical CRUD operations:<br />
POST - Create a resource<br />
GET - Retrieve a resource<br />
PUT – Update a resource<br />
DELETE - Delete a resource<br />
There are various resources that are available at the server. Each resource at the server has a handler function which the REST layer calls to serve the request by the client. The REST server sends the response back to the client with the contents of the resource requested.<br />
<br />
==You Will Learn==<br />
<br />
How to use REST layer to develop server-side applications like COAP or HTTP and run it on COOJA and using T-mote sky.<br />
<br />
==Relavant Files==<br />
<br />
* /contiki-2.7/apps/rest-common - It contains the codes that are mentioned as target in the rest-example.<br />
* /contiki-2.7/apps/rest-coap - It contains codes for codes mentioned as target in rest-common. <br />
* /contiki-2.7/apps/rest-http - It contains codes for codes mentioned as target in rest-common.<br />
* /contiki-2.7/examples/rest-example - It has the main example of the REST server side and COAP client side codes that are to be uploaded on COOJA and real motes.<br />
<br />
==Understanding The Code==<br />
<br />
===rest-server-example.c===<br />
<br />
The REST server has light, led, toggle, helloworld and discover resources. Each resource has a resource handler function which is called when the client requests for that resource and the resource handler function sends the response back to the client. For example, when a HTTP client sends a request to the REST server for light resource, the REST server calls the light_handler function which reads the light sensor values from the mote and sends the light sensor values along with a simple etag back to the client as shown below. <br />
<br />
<source lang="c"><br />
/*A simple getter example. Returns the reading from light sensor with a simple etag*/<br />
RESOURCE(light, METHOD_GET, "light");<br />
void<br />
light_handler(REQUEST* request, RESPONSE* response)<br />
{<br />
read_light_sensor(&light_photosynthetic, &light_solar);<br />
sprintf(temp,"%u;%u", light_photosynthetic, light_solar);<br />
<br />
char etag[4] = "ABCD";<br />
rest_set_header_content_type(response, TEXT_PLAIN);<br />
rest_set_header_etag(response, etag, sizeof(etag));<br />
rest_set_response_payload(response, temp, strlen(temp));<br />
}<br />
</source><br />
<br />
===coap-client-example.c===<br />
The COAP client establishes a connection with the server on the COAP port 61616 and sets the et timer to a particular value. Everytime the et timer is expired, the send_data(void) function is called. When it recieves the response from the server for its request, the handle_incoming_data() function is called as shown in the code snippet within the process. <br />
<br />
<source lang="c"><br />
etimer_set(&et, 5 * CLOCK_SECOND);<br />
while(1) {<br />
PROCESS_YIELD();<br />
if (etimer_expired(&et)) {<br />
send_data();<br />
etimer_reset(&et);<br />
} else if (ev == tcpip_event) {<br />
handle_incoming_data();<br />
}<br />
}<br />
</source><br />
<br />
The COAP client runs a timer which when resets, the client randomly selects a service_id (resource) using random_rand() function and sends the request to the REST server as seen below in the send_data(void) function.<br />
<br />
<source lang="c"><br />
int data_size = 0;<br />
int service_id = random_rand() % NUMBER_OF_URLS;<br />
coap_packet_t* request = (coap_packet_t*)allocate_buffer(sizeof(coap_packet_t));<br />
init_packet(request);<br />
<br />
coap_set_method(request, COAP_GET);<br />
request->tid = xact_id++;<br />
request->type = MESSAGE_TYPE_CON;<br />
coap_set_header_uri(request, service_urls[service_id]);<br />
</source><br />
<br />
<br />
When the server response returns back to the client, it runs the handle_incoming_data() function which takes the packet, parses the message and prints the payload that is it receives from the response. It can be seen in the code below<br />
<br />
<source lang="c"><br />
static void<br />
handle_incoming_data()<br />
{<br />
PRINTF("Incoming packet size: %u \n", (uint16_t)uip_datalen());<br />
if (init_buffer(COAP_DATA_BUFF_SIZE)) {<br />
if (uip_newdata()) {<br />
coap_packet_t* response = (coap_packet_t*)allocate_buffer(sizeof(coap_packet_t));<br />
if (response) {<br />
parse_message(response, uip_appdata, uip_datalen());<br />
response_handler(response);<br />
}<br />
}<br />
delete_buffer();<br />
}<br />
}<br />
</source><br />
<br />
==Run the REST-Example on COOJA==<br />
<br />
<br />
===HTTP Example===<br />
<br />
<br />
1. Open the Makefile in the /contiki-2.7/examples/rest-example folder. For HTTP server, make sure the WITH_COAP = 0 in the Makefile.<br />
<br />
<br />
2. Open the terminal. Go to the /contiki-2.7/examples/rest-example folder. Run the following command given below:<br />
<br />
<code><nowiki> make TARGET=cooja rest-server-example.csc </nowiki></code><br />
<br />
This will open a COOJA terminal and loads the rest-server-example code for simulation. The Network will look as shown:<br />
<br />
[[File:Networkhttp.PNG]] <br />
<br />
<br />
3. Open another terminal. Go to the same directory and connect the COOJA simulation to the router.<br />
<br />
<code><nowiki> make connect-router-cooja </nowiki></code><br />
<br />
<br />
4. Start the Simulation on the COOJA and test the connectivity by pinging the server. The IP addresses of the servers are aaaa::0212:7402:0002:0202 and aaaa::0212:7403:0003:0303.<br />
<br />
<code><nowiki> ping6 aaaa::0212:7402:0002:0202 </nowiki></code><br />
<br />
<code><nowiki> ping6 aaaa::0212:7403:0003:0303 </nowiki></code><br />
<br />
<br />
5. Use curl as a HTTP client we interact with the COOJA motes that are running the REST code. The default HTTP client runs on 8080 port number.<br />
<br />
<code><nowiki> curl -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/helloworld #get helloworld plain text </nowiki></code><br />
<br />
<code><nowiki> curl -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/led?color=green -d mode=off -i #turn off the green led </nowiki></code><br />
<br />
<code><nowiki> curl -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/.well-known/core -i </nowiki></code><br />
<br />
<code><nowiki> curl -X POST -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/helloworld #method not allowed </nowiki></code><br />
<br />
<br />
===COAP Example===<br />
<br />
<br />
1. Open the Makefile in the /contiki-2.7/examples/rest-example folder. For COAP server, make sure the WITH_COAP = 1 in the Makefile.<br />
<br />
2. Open the terminal. Go to the /contiki-2.7/examples/rest-example folder. Run the following command given below:<br />
<br />
<code><nowiki> make TARGET=cooja coap-client-server-example.csc </nowiki></code><br />
<br />
This opens the COOJA terminal which runs rest-server-example.c over COAP in one node having IP address aaaa::0212:7401:0001:0101 and coap-client-example.c in the another node having IP address aaaa::0212:7402:0002:0202. COAP uses 61616 port number as default. The client in the example periodically accesses the resources of the server and prints the payload which can be seen on the mote output. The Network looks as shown below:<br />
<br />
[[File:Coapnetwork.PNG]]<br />
<br />
3.Start the Simulation and observe the mote output. The Output is diplayed as shown in the below window.<br />
<br />
[[File:Coapmoteout.PNG]]<br />
<br />
==Run the REST-Example on T-mote Sky==<br />
<br />
<br />
1. Go to the /contiki-2.7/examples/rest-example folder. Connect the motes and program them with rest-server-example.<br />
<br />
<code><nowiki> make TARGET=sky rest-server-example.upload </nowiki></code><br />
<br />
<br />
2. Disconnect the motes and load one other mote with the RPL border router to connect to the rest-server.<br />
<br />
<code><nowiki> cd ../ipv6/rpl-border-router </nowiki></code><br />
<br />
<code><nowiki> make TARGET=sky border-router.upload </nowiki></code><br />
<br />
<br />
3. Connect the REST-server to the border router using tunslip6.<br />
<br />
<code><nowiki> make connect-router </nowiki></code><br />
<br />
<br />
4. Open new terminal window for each mote and execute the following command and reset the motes. The IP addresses of these motes get printed after the motes are reset.<br />
<br />
<code><nowiki> make login TARGET=sky MOTE=2 #Shows the prints for first mote </nowiki></code><br />
<br />
<code><nowiki> make login TARGET=sky MOTE=3 #For second mote and so on. </nowiki></code><br />
<br />
<br />
5. Ping the motes using the IP addresses we get from step 4. As highlighted below<br />
<br />
<code><nowiki> ping6 <IPv6 Address of the MOTE> </nowiki></code><br />
<br />
[[File:IPaddrmote.PNG]]<br />
<br />
<br />
6. If WITH_COAP = 0 i.e. HTTP Server, use the HTTP Client to connect to the REST server. When WITH_COAP = 1. i.e. COAP Server, load the coap-client-example.c to the motes and connect to the REST server (same as COOJA Example).<br />
<br />
==General Issues You Might Face==<br />
<br />
<br />
1. To use curl as the HTTP client you need to install curl by running the following command in the terminal:<br />
<br />
<code><nowiki> sudo apt-get install curl </nowiki></code><br />
<br />
2. The memory on the Tmote sky is not big enough to fit in the HTTP example on REST server. Thus while running it on real motes we get the error “region text is full”. To overcome this problem, instead of RPL to decide the route, we can use static routes. We can also reduce the size by changing the Makefile of the border router by setting the WITH_WEBSERVER = 0. <br />
<br />
3. Generally the COAP Client is used instead of HTTP client for running over REST Server.<br />
<br />
<br />
==References==<br />
<br />
<br />
1. REST-Example Github page: https://github.com/contiki-os/contiki/tree/master/examples/rest-example<br />
<br />
2. A Low-Power CoAP for Contiki - Matthias Kovatsch, Simon Duquennoy, Adam Dunkels<br />
<br />
<br />
[[Contiki_tutorials | Back to Contiki Tutorials]]</div>Munihttp://anrg.usc.edu/contiki/index.php?title=REST_example_running_on_Cooja_and_Sky_motes&diff=1116REST example running on Cooja and Sky motes2014-11-08T20:30:51Z<p>Muni: </p>
<hr />
<div>[[Contiki_tutorials | Back to Contiki Tutorials]]<br />
<br />
__TOC__<br />
<br />
<br />
==Introduction==<br />
<br />
REST (Representational State Transfer) is an architectural style consisting of coordinated set of architectural constraints APPLIED to components, connectors and data elements within a distributed hypermedia system. It is one in which web services are viewed as resources and can be uniquely identified by their URLs. A web service can be characterized as RESTful if they conform to architectural constraints like client-server, stateless, cacheable, layered system and uniform Interface. The basic REST design principle uses the HTTP or COAP protocol methods for typical CRUD operations:<br />
POST - Create a resource<br />
GET - Retrieve a resource<br />
PUT – Update a resource<br />
DELETE - Delete a resource<br />
There are various resources that are available at the server. Each resource at the server has a handler function which the REST layer calls to serve the request by the client. The REST server sends the response back to the client with the contents of the resource requested.<br />
<br />
==You Will Learn==<br />
<br />
How to use REST layer to develop server-side applications like COAP or HTTP and run it on COOJA and using T-mote sky.<br />
<br />
==Relavant Files==<br />
<br />
* /contiki-2.7/apps/rest-common - It contains the codes that are mentioned as target in the rest-example.<br />
* /contiki-2.7/apps/rest-coap - It contains codes for codes mentioned as target in rest-common. <br />
* /contiki-2.7/apps/rest-http - It contains codes for codes mentioned as target in rest-common.<br />
* /contiki-2.7/examples/rest-example - It has the main example of the REST server side and COAP client side codes that are to be uploaded on COOJA and real motes.<br />
<br />
==Understanding The Code==<br />
<br />
===rest-server-example.c===<br />
<br />
The REST server has light, led, toggle, helloworld and discover resources. Each resource has a resource handler function which is called when the client requests for that resource and the resource handler function sends the response back to the client. For example, when a HTTP client sends a request to the REST server for light resource, the REST server calls the light_handler function which reads the light sensor values from the mote and sends the light sensor values along with a simple etag back to the client as shown below. <br />
<br />
<source lang="c"><br />
/*A simple getter example. Returns the reading from light sensor with a simple etag*/<br />
RESOURCE(light, METHOD_GET, "light");<br />
void<br />
light_handler(REQUEST* request, RESPONSE* response)<br />
{<br />
read_light_sensor(&light_photosynthetic, &light_solar);<br />
sprintf(temp,"%u;%u", light_photosynthetic, light_solar);<br />
<br />
char etag[4] = "ABCD";<br />
rest_set_header_content_type(response, TEXT_PLAIN);<br />
rest_set_header_etag(response, etag, sizeof(etag));<br />
rest_set_response_payload(response, temp, strlen(temp));<br />
}<br />
</source><br />
<br />
===coap-client-example.c===<br />
The COAP client establishes a connection with the server on the COAP port 61616 and sets the et timer to a particular value. Everytime the et timer is expired, the send_data(void) function is called. When it recieves the response from the server for its request, the handle_incoming_data() function is called as shown in the code snippet within the process. <br />
<br />
<source lang="c"><br />
etimer_set(&et, 5 * CLOCK_SECOND);<br />
while(1) {<br />
PROCESS_YIELD();<br />
if (etimer_expired(&et)) {<br />
send_data();<br />
etimer_reset(&et);<br />
} else if (ev == tcpip_event) {<br />
handle_incoming_data();<br />
}<br />
}<br />
</source><br />
<br />
The COAP client runs a timer which when resets, the client randomly selects a service_id (resource) using random_rand() function and sends the request to the REST server as seen below in the send_data(void) function.<br />
<br />
<source lang="c"><br />
int data_size = 0;<br />
int service_id = random_rand() % NUMBER_OF_URLS;<br />
coap_packet_t* request = (coap_packet_t*)allocate_buffer(sizeof(coap_packet_t));<br />
init_packet(request);<br />
<br />
coap_set_method(request, COAP_GET);<br />
request->tid = xact_id++;<br />
request->type = MESSAGE_TYPE_CON;<br />
coap_set_header_uri(request, service_urls[service_id]);<br />
</source><br />
<br />
<br />
When the server response returns back to the client, it runs the handle_incoming_data() function which takes the packet, parses the message and prints the payload that is it receives from the response. It can be seen in the code below<br />
<br />
<source lang="c"><br />
static void<br />
handle_incoming_data()<br />
{<br />
PRINTF("Incoming packet size: %u \n", (uint16_t)uip_datalen());<br />
if (init_buffer(COAP_DATA_BUFF_SIZE)) {<br />
if (uip_newdata()) {<br />
coap_packet_t* response = (coap_packet_t*)allocate_buffer(sizeof(coap_packet_t));<br />
if (response) {<br />
parse_message(response, uip_appdata, uip_datalen());<br />
response_handler(response);<br />
}<br />
}<br />
delete_buffer();<br />
}<br />
}<br />
</source><br />
<br />
==Run the REST-Example on COOJA==<br />
<br />
<br />
===HTTP Example===<br />
<br />
<br />
1. Open the Makefile in the /contiki-2.7/examples/rest-example folder. For HTTP server, make sure the WITH_COAP = 0 in the Makefile.<br />
<br />
<br />
2. Open the terminal. Go to the /contiki-2.7/examples/rest-example folder. Run the following command given below:<br />
<br />
<code><nowiki> make TARGET=cooja rest-server-example.csc </nowiki></code><br />
<br />
This will open a COOJA terminal and loads the rest-server-example code for simulation. The Network will look as shown:<br />
<br />
[[File:Networkhttp.PNG]] <br />
<br />
<br />
3. Open another terminal. Go to the same directory and connect the COOJA simulation to the router.<br />
<br />
<code><nowiki> make connect-router-cooja </nowiki></code><br />
<br />
<br />
4. Start the Simulation on the COOJA and test the connectivity by pinging the server. The IP addresses of the servers are aaaa::0212:7402:0002:0202 and aaaa::0212:7403:0003:0303.<br />
<br />
<code><nowiki> ping6 aaaa::0212:7402:0002:0202 </nowiki></code><br />
<br />
<code><nowiki> ping6 aaaa::0212:7403:0003:0303 </nowiki></code><br />
<br />
<br />
5. Use curl as a HTTP client we interact with the COOJA motes that are running the REST code. The default HTTP client runs on 8080 port number.<br />
<br />
<code><nowiki> curl -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/helloworld #get helloworld plain text </nowiki></code><br />
<br />
<code><nowiki> curl -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/led?color=green -d mode=off -i #turn off the green led </nowiki></code><br />
<br />
<code><nowiki> curl -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/.well-known/core -i </nowiki></code><br />
<br />
<code><nowiki> curl -X POST -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/helloworld #method not allowed </nowiki></code><br />
<br />
<br />
===COAP Example===<br />
<br />
<br />
1. Open the Makefile in the /contiki-2.7/examples/rest-example folder. For COAP server, make sure the WITH_COAP = 1 in the Makefile.<br />
<br />
2. Open the terminal. Go to the /contiki-2.7/examples/rest-example folder. Run the following command given below:<br />
<br />
<code><nowiki> make TARGET=cooja coap-client-server-example.csc </nowiki></code><br />
<br />
This opens the COOJA terminal which runs rest-server-example.c over COAP in one node having IP address aaaa::0212:7401:0001:0101 and coap-client-example.c in the another node having IP address aaaa::0212:7402:0002:0202. COAP uses 61616 port number as default. The client in the example periodically accesses the resources of the server and prints the payload which can be seen on the mote output. The Network looks as shown below:<br />
<br />
[[File:Coapnetwork.PNG]]<br />
<br />
3.Start the Simulation and observe the mote output. The Output is diplayed as shown in the below window.<br />
<br />
[[File:Coapmoteout.PNG]]<br />
<br />
==Run the REST-Example on T-mote Sky==<br />
<br />
<br />
1. Go to the /contiki-2.7/examples/rest-example folder. Connect the motes and program them with rest-server-example.<br />
<br />
<code><nowiki> make TARGET=sky rest-server-example.upload </nowiki></code><br />
<br />
<br />
2. Disconnect the motes and load one other mote with the RPL border router to connect to the rest-server.<br />
<br />
<code><nowiki> cd ../ipv6/rpl-border-router </nowiki></code><br />
<br />
<code><nowiki> make TARGET=sky border-router.upload </nowiki></code><br />
<br />
<br />
3. Connect the REST-server to the border router using tunslip6.<br />
<br />
<code><nowiki> make connect-router </nowiki></code><br />
<br />
<br />
4. Open new terminal window for each mote and execute the following command and reset the motes. The IP addresses of these motes get printed after the motes are reset.<br />
<br />
<code><nowiki> make login TARGET=sky MOTE=2 #Shows the prints for first mote </nowiki></code><br />
<br />
<code><nowiki> make login TARGET=sky MOTE=3 #For second mote and so on. </nowiki></code><br />
<br />
<br />
5. Ping the motes using the IP addresses we get from step 4. As shown below<br />
<br />
<code><nowiki> ping6 <IPv6 Address of the MOTE> </nowiki></code><br />
<br />
[[File:IPaddrmote.PNG]]<br />
<br />
<br />
6. If WITH_COAP = 0 i.e. HTTP Server, use the HTTP Client to connect to the REST server. When WITH_COAP = 1. i.e. COAP Server, load the coap-client-example.c to the motes and connect to the REST server (same as COOJA Example).<br />
<br />
<br />
==General Issues You Might Face==<br />
<br />
<br />
1. To use curl as the HTTP client you need to install curl by running the following command in the terminal:<br />
<br />
<code><nowiki> sudo apt-get install curl </nowiki></code><br />
<br />
2. The memory on the Tmote sky is not big enough to fit in the HTTP example on REST server. Thus while running it on real motes we get the error “region text is full”. To overcome this problem, instead of RPL to decide the route, we can use static routes. We can also reduce the size by changing the Makefile of the border router by setting the WITH_WEBSERVER = 0. <br />
<br />
3. Generally the COAP Client is used instead of HTTP client for running over REST Server.<br />
<br />
<br />
==References==<br />
<br />
<br />
1. REST-Example Github page: https://github.com/contiki-os/contiki/tree/master/examples/rest-example<br />
<br />
2. A Low-Power CoAP for Contiki - Matthias Kovatsch, Simon Duquennoy, Adam Dunkels<br />
<br />
<br />
[[Contiki_tutorials | Back to Contiki Tutorials]]</div>Munihttp://anrg.usc.edu/contiki/index.php?title=File:IPaddrmote.PNG&diff=1115File:IPaddrmote.PNG2014-11-08T20:28:59Z<p>Muni: </p>
<hr />
<div></div>Munihttp://anrg.usc.edu/contiki/index.php?title=REST_example_running_on_Cooja_and_Sky_motes&diff=1098REST example running on Cooja and Sky motes2014-11-08T19:51:46Z<p>Muni: </p>
<hr />
<div>[[Contiki_tutorials | Back to Contiki Tutorials]]<br />
<br />
__TOC__<br />
<br />
<br />
==Introduction==<br />
<br />
REST (Representational State Transfer) is an architectural style consisting of coordinated set of architectural constraints APPLIED to components, connectors and data elements within a distributed hypermedia system. It is one in which web services are viewed as resources and can be uniquely identified by their URLs. A web service can be characterized as RESTful if they conform to architectural constraints like client-server, stateless, cacheable, layered system and uniform Interface. The basic REST design principle uses the HTTP or COAP protocol methods for typical CRUD operations:<br />
POST - Create a resource<br />
GET - Retrieve a resource<br />
PUT – Update a resource<br />
DELETE - Delete a resource<br />
There are various resources that are available at the server. Each resource at the server has a handler function which the REST layer calls to serve the request by the client. The REST server sends the response back to the client with the contents of the resource requested.<br />
<br />
==You Will Learn==<br />
<br />
How to use REST layer to develop server-side applications like COAP or HTTP and run it on COOJA and using T-mote sky.<br />
<br />
==Relavant Files==<br />
<br />
* /contiki-2.7/apps/rest-common - It contains the codes that are mentioned as target in the rest-example.<br />
* /contiki-2.7/apps/rest-coap - It contains codes for codes mentioned as target in rest-common. <br />
* /contiki-2.7/apps/rest-http - It contains codes for codes mentioned as target in rest-common.<br />
* /contiki-2.7/examples/rest-example - It has the main example of the REST server side and COAP client side codes that are to be uploaded on COOJA and real motes.<br />
<br />
==Understanding The Code==<br />
<br />
===rest-server-example.c===<br />
<br />
The REST server has light, led, toggle, helloworld and discover resources. Each resource has a resource handler function which is called when the client requests for that resource and the resource handler function sends the response back to the client. For example, when a HTTP client sends a request to the REST server for light resource, the REST server calls the light_handler function which reads the light sensor values from the mote and sends the light sensor values along with a simple etag back to the client as shown below. <br />
<br />
<source lang="c"><br />
/*A simple getter example. Returns the reading from light sensor with a simple etag*/<br />
RESOURCE(light, METHOD_GET, "light");<br />
void<br />
light_handler(REQUEST* request, RESPONSE* response)<br />
{<br />
read_light_sensor(&light_photosynthetic, &light_solar);<br />
sprintf(temp,"%u;%u", light_photosynthetic, light_solar);<br />
<br />
char etag[4] = "ABCD";<br />
rest_set_header_content_type(response, TEXT_PLAIN);<br />
rest_set_header_etag(response, etag, sizeof(etag));<br />
rest_set_response_payload(response, temp, strlen(temp));<br />
}<br />
</source><br />
<br />
===coap-client-example.c===<br />
The COAP client establishes a connection with the server on the COAP port 61616 and sets the et timer to a particular value. Everytime the et timer is expired, the send_data(void) function is called. When it recieves the response from the server for its request, the handle_incoming_data() function is called as shown in the code snippet within the process. <br />
<br />
<source lang="c"><br />
etimer_set(&et, 5 * CLOCK_SECOND);<br />
while(1) {<br />
PROCESS_YIELD();<br />
if (etimer_expired(&et)) {<br />
send_data();<br />
etimer_reset(&et);<br />
} else if (ev == tcpip_event) {<br />
handle_incoming_data();<br />
}<br />
}<br />
</source><br />
<br />
The COAP client runs a timer which when resets, the client randomly selects a service_id (resource) using random_rand() function and sends the request to the REST server as seen below in the send_data(void) function.<br />
<br />
<source lang="c"><br />
int data_size = 0;<br />
int service_id = random_rand() % NUMBER_OF_URLS;<br />
coap_packet_t* request = (coap_packet_t*)allocate_buffer(sizeof(coap_packet_t));<br />
init_packet(request);<br />
<br />
coap_set_method(request, COAP_GET);<br />
request->tid = xact_id++;<br />
request->type = MESSAGE_TYPE_CON;<br />
coap_set_header_uri(request, service_urls[service_id]);<br />
</source><br />
<br />
<br />
When the server response returns back to the client, it runs the handle_incoming_data() function which takes the packet, parses the message and prints the payload that is it receives from the response. It can be seen in the code below<br />
<br />
<source lang="c"><br />
static void<br />
handle_incoming_data()<br />
{<br />
PRINTF("Incoming packet size: %u \n", (uint16_t)uip_datalen());<br />
if (init_buffer(COAP_DATA_BUFF_SIZE)) {<br />
if (uip_newdata()) {<br />
coap_packet_t* response = (coap_packet_t*)allocate_buffer(sizeof(coap_packet_t));<br />
if (response) {<br />
parse_message(response, uip_appdata, uip_datalen());<br />
response_handler(response);<br />
}<br />
}<br />
delete_buffer();<br />
}<br />
}<br />
</source><br />
<br />
==Run the REST-Example on COOJA==<br />
<br />
<br />
===HTTP Example===<br />
<br />
<br />
1. Open the Makefile in the /contiki-2.7/examples/rest-example folder. For HTTP server, make sure the WITH_COAP = 0 in the Makefile.<br />
<br />
<br />
2. Open the terminal. Go to the /contiki-2.7/examples/rest-example folder. Run the following command given below:<br />
<br />
<code><nowiki> make TARGET=cooja rest-server-example.csc </nowiki></code><br />
<br />
This will open a COOJA terminal and loads the rest-server-example code for simulation. The Network will look as shown:<br />
<br />
[[File:Networkhttp.PNG]] <br />
<br />
<br />
3. Open another terminal. Go to the same directory and connect the COOJA simulation to the router.<br />
<br />
<code><nowiki> make connect-router-cooja </nowiki></code><br />
<br />
<br />
4. Start the Simulation on the COOJA and test the connectivity by pinging the server. The IP addresses of the servers are aaaa::0212:7402:0002:0202 and aaaa::0212:7403:0003:0303.<br />
<br />
<code><nowiki> ping6 aaaa::0212:7402:0002:0202 </nowiki></code><br />
<br />
<code><nowiki> ping6 aaaa::0212:7403:0003:0303 </nowiki></code><br />
<br />
<br />
5. Use curl as a HTTP client we interact with the COOJA motes that are running the REST code. The default HTTP client runs on 8080 port number.<br />
<br />
<code><nowiki> curl -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/helloworld #get helloworld plain text </nowiki></code><br />
<br />
<code><nowiki> curl -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/led?color=green -d mode=off -i #turn off the green led </nowiki></code><br />
<br />
<code><nowiki> curl -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/.well-known/core -i </nowiki></code><br />
<br />
<code><nowiki> curl -X POST -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/helloworld #method not allowed </nowiki></code><br />
<br />
<br />
===COAP Example===<br />
<br />
<br />
1. Open the Makefile in the /contiki-2.7/examples/rest-example folder. For COAP server, make sure the WITH_COAP = 1 in the Makefile.<br />
<br />
2. Open the terminal. Go to the /contiki-2.7/examples/rest-example folder. Run the following command given below:<br />
<br />
<code><nowiki> make TARGET=cooja coap-client-server-example.csc </nowiki></code><br />
<br />
This opens the COOJA terminal which runs rest-server-example.c over COAP in one node having IP address aaaa::0212:7401:0001:0101 and coap-client-example.c in the another node having IP address aaaa::0212:7402:0002:0202. COAP uses 61616 port number as default. The client in the example periodically accesses the resources of the server and prints the payload which can be seen on the mote output. The Network looks as shown below:<br />
<br />
[[File:Coapnetwork.PNG]]<br />
<br />
3.Start the Simulation and observe the mote output. The Output is diplayed as shown in the below window.<br />
<br />
[[File:Coapmoteout.PNG]]<br />
<br />
==Run the REST-Example on T-mote Sky==<br />
<br />
<br />
1. Go to the /contiki-2.7/examples/rest-example folder. Connect the motes and program them with rest-server-example.<br />
<br />
<code><nowiki> make TARGET=sky rest-server-example.upload </nowiki></code><br />
<br />
<br />
2. Disconnect the motes and load one other mote with the RPL border router to connect to the rest-server.<br />
<br />
<code><nowiki> cd ../ipv6/rpl-border-router </nowiki></code><br />
<br />
<code><nowiki> make TARGET=sky border-router.upload </nowiki></code><br />
<br />
<br />
3. Connect the REST-server to the border router using tunslip6.<br />
<br />
<code><nowiki> make connect-router </nowiki></code><br />
<br />
<br />
4. Open new terminal window for each mote and execute the following command and reset the motes. The IP addresses of these motes get printed after the motes are reset.<br />
<br />
<code><nowiki> make login TARGET=sky MOTE=2 #Shows the prints for first mote </nowiki></code><br />
<br />
<code><nowiki> make login TARGET=sky MOTE=3 #For second mote and so on. </nowiki></code><br />
<br />
<br />
5. Ping the motes using the IP addresses we get from step 4.<br />
<br />
<code><nowiki> ping6 <IPv6 Address of the MOTE> </nowiki></code><br />
<br />
<br />
6. If WITH_COAP = 0 i.e. HTTP Server, use the HTTP Client to connect to the REST server. When WITH_COAP = 1. i.e. COAP Server, load the coap-client-example.c to the motes and connect to the REST server (same as COOJA Example).<br />
<br />
<br />
==General Issues You Might Face==<br />
<br />
<br />
1. To use curl as the HTTP client you need to install curl by running the following command in the terminal:<br />
<br />
<code><nowiki> sudo apt-get install curl </nowiki></code><br />
<br />
2. The memory on the Tmote sky is not big enough to fit in the HTTP example on REST server. Thus while running it on real motes we get the error “region text is full”. To overcome this problem, instead of RPL to decide the route, we can use static routes. We can also reduce the size by changing the Makefile of the border router by setting the WITH_WEBSERVER = 0. <br />
<br />
3. Generally the COAP Client is used instead of HTTP client for running over REST Server.<br />
<br />
<br />
==References==<br />
<br />
<br />
1. REST-Example Github page: https://github.com/contiki-os/contiki/tree/master/examples/rest-example<br />
<br />
2. A Low-Power CoAP for Contiki - Matthias Kovatsch, Simon Duquennoy, Adam Dunkels<br />
<br />
<br />
[[Contiki_tutorials | Back to Contiki Tutorials]]</div>Munihttp://anrg.usc.edu/contiki/index.php?title=REST_example_running_on_Cooja_and_Sky_motes&diff=1097REST example running on Cooja and Sky motes2014-11-08T19:51:03Z<p>Muni: </p>
<hr />
<div>[[Contiki_tutorials | Back to Contiki Tutorials]]<br />
<br />
__TOC__<br />
<br />
<br />
==Introduction==<br />
<br />
REST (Representational State Transfer) is an architectural style consisting of coordinated set of architectural constraints APPLIED to components, connectors and data elements within a distributed hypermedia system. It is one in which web services are viewed as resources and can be uniquely identified by their URLs. A web service can be characterized as RESTful if they conform to architectural constraints like client-server, stateless, cacheable, layered system and uniform Interface. The basic REST design principle uses the HTTP or COAP protocol methods for typical CRUD operations:<br />
POST - Create a resource<br />
GET - Retrieve a resource<br />
PUT – Update a resource<br />
DELETE - Delete a resource<br />
There are various resources that are available at the server. Each resource at the server has a handler function which the REST layer calls to serve the request by the client. The REST server sends the response back to the client with the contents of the resource requested.<br />
<br />
==You Will Learn==<br />
<br />
How to use REST layer to develop server-side applications like COAP or HTTP and run it on COOJA and using T-mote sky.<br />
<br />
==Relavant Files==<br />
<br />
* /contiki-2.7/apps/rest-common - It contains the codes that are mentioned as target in the rest-example.<br />
* /contiki-2.7/apps/rest-coap - It contains codes for codes mentioned as target in rest-common. <br />
* /contiki-2.7/apps/rest-http - It contains codes for codes mentioned as target in rest-common.<br />
* /contiki-2.7/examples/rest-example - It has the main example of the REST server side and COAP client side codes that are to be uploaded on COOJA and real motes.<br />
<br />
==Understanding The Code==<br />
<br />
===rest-server-example.c===<br />
<br />
The REST server has light, led, toggle, helloworld and discover resources. Each resource has a resource handler function which is called when the client requests for that resource and the resource handler function sends the response back to the client. For example, when a HTTP client sends a request to the REST server for light resource, the REST server calls the light_handler function which reads the light sensor values from the mote and sends the light sensor values along with a simple etag back to the client as shown below. <br />
<br />
<source lang="c"><br />
/*A simple getter example. Returns the reading from light sensor with a simple etag*/<br />
RESOURCE(light, METHOD_GET, "light");<br />
void<br />
light_handler(REQUEST* request, RESPONSE* response)<br />
{<br />
read_light_sensor(&light_photosynthetic, &light_solar);<br />
sprintf(temp,"%u;%u", light_photosynthetic, light_solar);<br />
<br />
char etag[4] = "ABCD";<br />
rest_set_header_content_type(response, TEXT_PLAIN);<br />
rest_set_header_etag(response, etag, sizeof(etag));<br />
rest_set_response_payload(response, temp, strlen(temp));<br />
}<br />
</source><br />
<br />
===coap-client-example.c===<br />
The COAP client establishes a connection with the server on the COAP port 61616 and sets the et timer to a particular value. Everytime the et timer is expired, the send_data(void) function is called. When it recieves the response from the server for its request, the handle_incoming_data() function is called as shown in the code snippet within the process. <br />
<br />
<source lang="c"><br />
etimer_set(&et, 5 * CLOCK_SECOND);<br />
while(1) {<br />
PROCESS_YIELD();<br />
if (etimer_expired(&et)) {<br />
send_data();<br />
etimer_reset(&et);<br />
} else if (ev == tcpip_event) {<br />
handle_incoming_data();<br />
}<br />
}<br />
</source><br />
<br />
The COAP client runs a timer which when resets, the client randomly selects a service_id (resource) using random_rand() function and sends the request to the REST server as seen below in the send_data(void) function.<br />
<br />
<source lang="c"><br />
int data_size = 0;<br />
int service_id = random_rand() % NUMBER_OF_URLS;<br />
coap_packet_t* request = (coap_packet_t*)allocate_buffer(sizeof(coap_packet_t));<br />
init_packet(request);<br />
<br />
coap_set_method(request, COAP_GET);<br />
request->tid = xact_id++;<br />
request->type = MESSAGE_TYPE_CON;<br />
coap_set_header_uri(request, service_urls[service_id]);<br />
</source><br />
<br />
<br />
When the server response returns back to the client, it runs the handle_incoming_data() function which takes the packet, parses the message and prints the payload that is it receives from the response. It can be seen in the code below<br />
<br />
<source lang="c"><br />
static void<br />
handle_incoming_data()<br />
{<br />
PRINTF("Incoming packet size: %u \n", (uint16_t)uip_datalen());<br />
if (init_buffer(COAP_DATA_BUFF_SIZE)) {<br />
if (uip_newdata()) {<br />
coap_packet_t* response = (coap_packet_t*)allocate_buffer(sizeof(coap_packet_t));<br />
if (response) {<br />
parse_message(response, uip_appdata, uip_datalen());<br />
response_handler(response);<br />
}<br />
}<br />
delete_buffer();<br />
}<br />
}<br />
</source><br />
<br />
==Run the REST-Example on COOJA==<br />
<br />
<br />
===HTTP Example===<br />
<br />
<br />
1. Open the Makefile in the /contiki-2.7/examples/rest-example folder. For HTTP server, make sure the WITH_COAP = 0 in the Makefile.<br />
<br />
<br />
2. Open the terminal. Go to the /contiki-2.7/examples/rest-example folder. Run the following command given below:<br />
<br />
<code><nowiki> make TARGET=cooja rest-server-example.csc </nowiki></code><br />
<br />
This will open a COOJA terminal and loads the rest-server-example code for simulation. The Network will look as shown:<br />
<br />
[[File:Networkhttp.PNG]] <br />
<br />
<br />
3. Open another terminal. Go to the same directory and connect the COOJA simulation to the router.<br />
<br />
<code><nowiki> make connect-router-cooja </nowiki></code><br />
<br />
<br />
4. Start the Simulation on the COOJA and test the connectivity by pinging the server. The IP addresses of the servers are aaaa::0212:7402:0002:0202 and aaaa::0212:7403:0003:0303.<br />
<br />
<code><nowiki> ping6 aaaa::0212:7402:0002:0202 </nowiki></code><br />
<br />
<code><nowiki> ping6 aaaa::0212:7403:0003:0303 </nowiki></code><br />
<br />
<br />
5. Use curl as a HTTP client we interact with the COOJA motes that are running the REST code. The default HTTP client runs on 8080 port number.<br />
<br />
<code><nowiki> curl -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/helloworld #get helloworld plain text </nowiki></code><br />
<br />
<code><nowiki> curl -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/led?color=green -d mode=off -i #turn off the green led </nowiki></code><br />
<br />
<code><nowiki> curl -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/.well-known/core -i </nowiki></code><br />
<br />
<code><nowiki> curl -X POST -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/helloworld #method not allowed </nowiki></code><br />
<br />
<br />
===COAP Example===<br />
<br />
<br />
1. Open the Makefile in the /contiki-2.7/examples/rest-example folder. For COAP server, make sure the WITH_COAP = 1 in the Makefile.<br />
<br />
2. Open the terminal. Go to the /contiki-2.7/examples/rest-example folder. Run the following command given below:<br />
<br />
<code><nowiki> make TARGET=cooja coap-client-server-example.csc </nowiki></code><br />
<br />
This opens the COOJA terminal which runs rest-server-example.c over COAP in one node having IP address aaaa::0212:7401:0001:0101 and coap-client-example.c in the another node having IP address aaaa::0212:7402:0002:0202. COAP uses 61616 port number as default. The client in the example periodically accesses the resources of the server and prints the payload which can be seen on the mote output. The Network looks as shown below:<br />
<br />
[[File:Coapnetwork.PNG]]<br />
<br />
3.Start the Simulation and observe the mote output. The Output is diplayed as shown in the below window.<br />
<br />
[[File:Coapmoteout.PNG]]<br />
<br />
==Run the REST-Example on T-mote Sky==<br />
<br />
<br />
1. Go to the /contiki-2.7/examples/rest-example folder. Connect the motes and program them with rest-server-example.<br />
<br />
<code><nowiki> make TARGET=sky rest-server-example.upload </nowiki></code><br />
<br />
<br />
2. Disconnect the motes and load one other mote with the RPL border router to connect to the rest-server.<br />
<br />
<code><nowiki> cd ../ipv6/rpl-border-router </nowiki></code><br />
<br />
<code><nowiki> make TARGET=sky border-router.upload </nowiki></code><br />
<br />
<br />
3. Connect the REST-server to the border router using tunslip6.<br />
<br />
<code><nowiki> make connect-router </nowiki></code><br />
<br />
<br />
4. Open new terminal window for each mote and execute the following command and reset the motes. The IP addresses of these motes get printed after the motes are reset.<br />
<br />
<code><nowiki> make login TARGET=sky MOTE=2 #Shows the prints for first mote </nowiki></code><br />
<br />
<code><nowiki> make login TARGET=sky MOTE=3 #For second mote and so on. </nowiki></code><br />
<br />
<br />
5. Ping the motes using the IP addresses we get from step 4.<br />
<br />
<code><nowiki> ping6 <IPv6 Address of the MOTE> </nowiki></code><br />
<br />
<br />
6. If WITH_COAP = 0 i.e. HTTP Server, use the HTTP Client to connect to the REST server. When WITH_COAP = 1. i.e. COAP Server, load the coap-client-example.c to the motes and connect to the REST server (same as COOJA Example).<br />
<br />
<br />
==General Issues You Might Face==<br />
<br />
<br />
1. To use curl as the HTTP client you need to install curl by running the following command in the terminal:<br />
<br />
<code><nowiki> sudo apt-get install curl </nowiki></code><br />
<br />
2. The memory on the Tmote sky is not big enough to fit in the HTTP example on REST server. Thus while running it on real motes we get the error “region text is full”. To overcome this problem, instead of RPL to decide the route, we can use static routes. We can also reduce the size by changing the Makefile of the border router by setting the WITH_WEBSERVER = 0. <br />
<br />
3. Generally the COAP Client is used instead of HTTP client for running over REST Server.<br />
<br />
<br />
==References==<br />
<br />
<br />
1. REST-Example Github page: https://github.com/contiki-os/contiki/tree/master/examples/rest-example<br />
<br />
2. A Low-Power CoAP for Contiki - Matthias Kovatsch, Simon Duquennoy, Adam Dunkels<br />
<br />
[[Contiki_tutorials | Back to Contiki Tutorials]]</div>Munihttp://anrg.usc.edu/contiki/index.php?title=REST_example_running_on_Cooja_and_Sky_motes&diff=1096REST example running on Cooja and Sky motes2014-11-08T19:48:29Z<p>Muni: /* Relavant Directories */</p>
<hr />
<div>==Introduction==<br />
<br />
REST (Representational State Transfer) is an architectural style consisting of coordinated set of architectural constraints APPLIED to components, connectors and data elements within a distributed hypermedia system. It is one in which web services are viewed as resources and can be uniquely identified by their URLs. A web service can be characterized as RESTful if they conform to architectural constraints like client-server, stateless, cacheable, layered system and uniform Interface. The basic REST design principle uses the HTTP or COAP protocol methods for typical CRUD operations:<br />
POST - Create a resource<br />
GET - Retrieve a resource<br />
PUT – Update a resource<br />
DELETE - Delete a resource<br />
There are various resources that are available at the server. Each resource at the server has a handler function which the REST layer calls to serve the request by the client. The REST server sends the response back to the client with the contents of the resource requested.<br />
<br />
==You Will Learn==<br />
<br />
How to use REST layer to develop server-side applications like COAP or HTTP and run it on COOJA and using T-mote sky.<br />
<br />
==Relavant Files==<br />
<br />
* /contiki-2.7/apps/rest-common - It contains the codes that are mentioned as target in the rest-example.<br />
* /contiki-2.7/apps/rest-coap - It contains codes for codes mentioned as target in rest-common. <br />
* /contiki-2.7/apps/rest-http - It contains codes for codes mentioned as target in rest-common.<br />
* /contiki-2.7/examples/rest-example - It has the main example of the REST server side and COAP client side codes that are to be uploaded on COOJA and real motes.<br />
<br />
==Understanding The Code==<br />
<br />
===rest-server-example.c===<br />
<br />
The REST server has light, led, toggle, helloworld and discover resources. Each resource has a resource handler function which is called when the client requests for that resource and the resource handler function sends the response back to the client. For example, when a HTTP client sends a request to the REST server for light resource, the REST server calls the light_handler function which reads the light sensor values from the mote and sends the light sensor values along with a simple etag back to the client as shown below. <br />
<br />
<source lang="c"><br />
/*A simple getter example. Returns the reading from light sensor with a simple etag*/<br />
RESOURCE(light, METHOD_GET, "light");<br />
void<br />
light_handler(REQUEST* request, RESPONSE* response)<br />
{<br />
read_light_sensor(&light_photosynthetic, &light_solar);<br />
sprintf(temp,"%u;%u", light_photosynthetic, light_solar);<br />
<br />
char etag[4] = "ABCD";<br />
rest_set_header_content_type(response, TEXT_PLAIN);<br />
rest_set_header_etag(response, etag, sizeof(etag));<br />
rest_set_response_payload(response, temp, strlen(temp));<br />
}<br />
</source><br />
<br />
===coap-client-example.c===<br />
The COAP client establishes a connection with the server on the COAP port 61616 and sets the et timer to a particular value. Everytime the et timer is expired, the send_data(void) function is called. When it recieves the response from the server for its request, the handle_incoming_data() function is called as shown in the code snippet within the process. <br />
<br />
<source lang="c"><br />
etimer_set(&et, 5 * CLOCK_SECOND);<br />
while(1) {<br />
PROCESS_YIELD();<br />
if (etimer_expired(&et)) {<br />
send_data();<br />
etimer_reset(&et);<br />
} else if (ev == tcpip_event) {<br />
handle_incoming_data();<br />
}<br />
}<br />
</source><br />
<br />
The COAP client runs a timer which when resets, the client randomly selects a service_id (resource) using random_rand() function and sends the request to the REST server as seen below in the send_data(void) function.<br />
<br />
<source lang="c"><br />
int data_size = 0;<br />
int service_id = random_rand() % NUMBER_OF_URLS;<br />
coap_packet_t* request = (coap_packet_t*)allocate_buffer(sizeof(coap_packet_t));<br />
init_packet(request);<br />
<br />
coap_set_method(request, COAP_GET);<br />
request->tid = xact_id++;<br />
request->type = MESSAGE_TYPE_CON;<br />
coap_set_header_uri(request, service_urls[service_id]);<br />
</source><br />
<br />
<br />
When the server response returns back to the client, it runs the handle_incoming_data() function which takes the packet, parses the message and prints the payload that is it receives from the response. It can be seen in the code below<br />
<br />
<source lang="c"><br />
static void<br />
handle_incoming_data()<br />
{<br />
PRINTF("Incoming packet size: %u \n", (uint16_t)uip_datalen());<br />
if (init_buffer(COAP_DATA_BUFF_SIZE)) {<br />
if (uip_newdata()) {<br />
coap_packet_t* response = (coap_packet_t*)allocate_buffer(sizeof(coap_packet_t));<br />
if (response) {<br />
parse_message(response, uip_appdata, uip_datalen());<br />
response_handler(response);<br />
}<br />
}<br />
delete_buffer();<br />
}<br />
}<br />
</source><br />
<br />
==Run the REST-Example on COOJA==<br />
<br />
<br />
===HTTP Example===<br />
<br />
<br />
1. Open the Makefile in the /contiki-2.7/examples/rest-example folder. For HTTP server, make sure the WITH_COAP = 0 in the Makefile.<br />
<br />
<br />
2. Open the terminal. Go to the /contiki-2.7/examples/rest-example folder. Run the following command given below:<br />
<br />
<code><nowiki> make TARGET=cooja rest-server-example.csc </nowiki></code><br />
<br />
This will open a COOJA terminal and loads the rest-server-example code for simulation. The Network will look as shown:<br />
<br />
[[File:Networkhttp.PNG]] <br />
<br />
<br />
3. Open another terminal. Go to the same directory and connect the COOJA simulation to the router.<br />
<br />
<code><nowiki> make connect-router-cooja </nowiki></code><br />
<br />
<br />
4. Start the Simulation on the COOJA and test the connectivity by pinging the server. The IP addresses of the servers are aaaa::0212:7402:0002:0202 and aaaa::0212:7403:0003:0303.<br />
<br />
<code><nowiki> ping6 aaaa::0212:7402:0002:0202 </nowiki></code><br />
<br />
<code><nowiki> ping6 aaaa::0212:7403:0003:0303 </nowiki></code><br />
<br />
<br />
5. Use curl as a HTTP client we interact with the COOJA motes that are running the REST code. The default HTTP client runs on 8080 port number.<br />
<br />
<code><nowiki> curl -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/helloworld #get helloworld plain text </nowiki></code><br />
<br />
<code><nowiki> curl -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/led?color=green -d mode=off -i #turn off the green led </nowiki></code><br />
<br />
<code><nowiki> curl -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/.well-known/core -i </nowiki></code><br />
<br />
<code><nowiki> curl -X POST -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/helloworld #method not allowed </nowiki></code><br />
<br />
<br />
===COAP Example===<br />
<br />
<br />
1. Open the Makefile in the /contiki-2.7/examples/rest-example folder. For COAP server, make sure the WITH_COAP = 1 in the Makefile.<br />
<br />
2. Open the terminal. Go to the /contiki-2.7/examples/rest-example folder. Run the following command given below:<br />
<br />
<code><nowiki> make TARGET=cooja coap-client-server-example.csc </nowiki></code><br />
<br />
This opens the COOJA terminal which runs rest-server-example.c over COAP in one node having IP address aaaa::0212:7401:0001:0101 and coap-client-example.c in the another node having IP address aaaa::0212:7402:0002:0202. COAP uses 61616 port number as default. The client in the example periodically accesses the resources of the server and prints the payload which can be seen on the mote output. The Network looks as shown below:<br />
<br />
[[File:Coapnetwork.PNG]]<br />
<br />
3.Start the Simulation and observe the mote output. The Output is diplayed as shown in the below window.<br />
<br />
[[File:Coapmoteout.PNG]]<br />
<br />
==Run the REST-Example on T-mote Sky==<br />
<br />
<br />
1. Go to the /contiki-2.7/examples/rest-example folder. Connect the motes and program them with rest-server-example.<br />
<br />
<code><nowiki> make TARGET=sky rest-server-example.upload </nowiki></code><br />
<br />
<br />
2. Disconnect the motes and load one other mote with the RPL border router to connect to the rest-server.<br />
<br />
<code><nowiki> cd ../ipv6/rpl-border-router </nowiki></code><br />
<br />
<code><nowiki> make TARGET=sky border-router.upload </nowiki></code><br />
<br />
<br />
3. Connect the REST-server to the border router using tunslip6.<br />
<br />
<code><nowiki> make connect-router </nowiki></code><br />
<br />
<br />
4. Open new terminal window for each mote and execute the following command and reset the motes. The IP addresses of these motes get printed after the motes are reset.<br />
<br />
<code><nowiki> make login TARGET=sky MOTE=2 #Shows the prints for first mote </nowiki></code><br />
<br />
<code><nowiki> make login TARGET=sky MOTE=3 #For second mote and so on. </nowiki></code><br />
<br />
<br />
5. Ping the motes using the IP addresses we get from step 4.<br />
<br />
<code><nowiki> ping6 <IPv6 Address of the MOTE> </nowiki></code><br />
<br />
<br />
6. If WITH_COAP = 0 i.e. HTTP Server, use the HTTP Client to connect to the REST server. When WITH_COAP = 1. i.e. COAP Server, load the coap-client-example.c to the motes and connect to the REST server (same as COOJA Example).<br />
<br />
<br />
==General Issues You Might Face==<br />
<br />
<br />
1. To use curl as the HTTP client you need to install curl by running the following command in the terminal:<br />
<br />
<code><nowiki> sudo apt-get install curl </nowiki></code><br />
<br />
2. The memory on the Tmote sky is not big enough to fit in the HTTP example on REST server. Thus while running it on real motes we get the error “region text is full”. To overcome this problem, instead of RPL to decide the route, we can use static routes. We can also reduce the size by changing the Makefile of the border router by setting the WITH_WEBSERVER = 0. <br />
<br />
3. Generally the COAP Client is used instead of HTTP client for running over REST Server.<br />
<br />
<br />
==References==<br />
<br />
<br />
1. REST-Example Github page: https://github.com/contiki-os/contiki/tree/master/examples/rest-example<br />
<br />
2. A Low-Power CoAP for Contiki - Matthias Kovatsch, Simon Duquennoy, Adam Dunkels</div>Munihttp://anrg.usc.edu/contiki/index.php?title=REST_example_running_on_Cooja_and_Sky_motes&diff=1095REST example running on Cooja and Sky motes2014-11-08T19:48:08Z<p>Muni: /* Introduction */</p>
<hr />
<div>==Introduction==<br />
<br />
REST (Representational State Transfer) is an architectural style consisting of coordinated set of architectural constraints APPLIED to components, connectors and data elements within a distributed hypermedia system. It is one in which web services are viewed as resources and can be uniquely identified by their URLs. A web service can be characterized as RESTful if they conform to architectural constraints like client-server, stateless, cacheable, layered system and uniform Interface. The basic REST design principle uses the HTTP or COAP protocol methods for typical CRUD operations:<br />
POST - Create a resource<br />
GET - Retrieve a resource<br />
PUT – Update a resource<br />
DELETE - Delete a resource<br />
There are various resources that are available at the server. Each resource at the server has a handler function which the REST layer calls to serve the request by the client. The REST server sends the response back to the client with the contents of the resource requested.<br />
<br />
==You Will Learn==<br />
<br />
How to use REST layer to develop server-side applications like COAP or HTTP and run it on COOJA and using T-mote sky.<br />
<br />
==Relavant Directories==<br />
<br />
* /contiki-2.7/apps/rest-common - It contains the codes that are mentioned as target in the rest-example.<br />
* /contiki-2.7/apps/rest-coap - It contains codes for codes mentioned as target in rest-common. <br />
* /contiki-2.7/apps/rest-http - It contains codes for codes mentioned as target in rest-common.<br />
* /contiki-2.7/examples/rest-example - It has the main example of the REST server side and COAP client side codes that are to be uploaded on COOJA and real motes.<br />
<br />
==Understanding The Code==<br />
<br />
===rest-server-example.c===<br />
<br />
The REST server has light, led, toggle, helloworld and discover resources. Each resource has a resource handler function which is called when the client requests for that resource and the resource handler function sends the response back to the client. For example, when a HTTP client sends a request to the REST server for light resource, the REST server calls the light_handler function which reads the light sensor values from the mote and sends the light sensor values along with a simple etag back to the client as shown below. <br />
<br />
<source lang="c"><br />
/*A simple getter example. Returns the reading from light sensor with a simple etag*/<br />
RESOURCE(light, METHOD_GET, "light");<br />
void<br />
light_handler(REQUEST* request, RESPONSE* response)<br />
{<br />
read_light_sensor(&light_photosynthetic, &light_solar);<br />
sprintf(temp,"%u;%u", light_photosynthetic, light_solar);<br />
<br />
char etag[4] = "ABCD";<br />
rest_set_header_content_type(response, TEXT_PLAIN);<br />
rest_set_header_etag(response, etag, sizeof(etag));<br />
rest_set_response_payload(response, temp, strlen(temp));<br />
}<br />
</source><br />
<br />
===coap-client-example.c===<br />
The COAP client establishes a connection with the server on the COAP port 61616 and sets the et timer to a particular value. Everytime the et timer is expired, the send_data(void) function is called. When it recieves the response from the server for its request, the handle_incoming_data() function is called as shown in the code snippet within the process. <br />
<br />
<source lang="c"><br />
etimer_set(&et, 5 * CLOCK_SECOND);<br />
while(1) {<br />
PROCESS_YIELD();<br />
if (etimer_expired(&et)) {<br />
send_data();<br />
etimer_reset(&et);<br />
} else if (ev == tcpip_event) {<br />
handle_incoming_data();<br />
}<br />
}<br />
</source><br />
<br />
The COAP client runs a timer which when resets, the client randomly selects a service_id (resource) using random_rand() function and sends the request to the REST server as seen below in the send_data(void) function.<br />
<br />
<source lang="c"><br />
int data_size = 0;<br />
int service_id = random_rand() % NUMBER_OF_URLS;<br />
coap_packet_t* request = (coap_packet_t*)allocate_buffer(sizeof(coap_packet_t));<br />
init_packet(request);<br />
<br />
coap_set_method(request, COAP_GET);<br />
request->tid = xact_id++;<br />
request->type = MESSAGE_TYPE_CON;<br />
coap_set_header_uri(request, service_urls[service_id]);<br />
</source><br />
<br />
<br />
When the server response returns back to the client, it runs the handle_incoming_data() function which takes the packet, parses the message and prints the payload that is it receives from the response. It can be seen in the code below<br />
<br />
<source lang="c"><br />
static void<br />
handle_incoming_data()<br />
{<br />
PRINTF("Incoming packet size: %u \n", (uint16_t)uip_datalen());<br />
if (init_buffer(COAP_DATA_BUFF_SIZE)) {<br />
if (uip_newdata()) {<br />
coap_packet_t* response = (coap_packet_t*)allocate_buffer(sizeof(coap_packet_t));<br />
if (response) {<br />
parse_message(response, uip_appdata, uip_datalen());<br />
response_handler(response);<br />
}<br />
}<br />
delete_buffer();<br />
}<br />
}<br />
</source><br />
<br />
==Run the REST-Example on COOJA==<br />
<br />
<br />
===HTTP Example===<br />
<br />
<br />
1. Open the Makefile in the /contiki-2.7/examples/rest-example folder. For HTTP server, make sure the WITH_COAP = 0 in the Makefile.<br />
<br />
<br />
2. Open the terminal. Go to the /contiki-2.7/examples/rest-example folder. Run the following command given below:<br />
<br />
<code><nowiki> make TARGET=cooja rest-server-example.csc </nowiki></code><br />
<br />
This will open a COOJA terminal and loads the rest-server-example code for simulation. The Network will look as shown:<br />
<br />
[[File:Networkhttp.PNG]] <br />
<br />
<br />
3. Open another terminal. Go to the same directory and connect the COOJA simulation to the router.<br />
<br />
<code><nowiki> make connect-router-cooja </nowiki></code><br />
<br />
<br />
4. Start the Simulation on the COOJA and test the connectivity by pinging the server. The IP addresses of the servers are aaaa::0212:7402:0002:0202 and aaaa::0212:7403:0003:0303.<br />
<br />
<code><nowiki> ping6 aaaa::0212:7402:0002:0202 </nowiki></code><br />
<br />
<code><nowiki> ping6 aaaa::0212:7403:0003:0303 </nowiki></code><br />
<br />
<br />
5. Use curl as a HTTP client we interact with the COOJA motes that are running the REST code. The default HTTP client runs on 8080 port number.<br />
<br />
<code><nowiki> curl -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/helloworld #get helloworld plain text </nowiki></code><br />
<br />
<code><nowiki> curl -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/led?color=green -d mode=off -i #turn off the green led </nowiki></code><br />
<br />
<code><nowiki> curl -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/.well-known/core -i </nowiki></code><br />
<br />
<code><nowiki> curl -X POST -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/helloworld #method not allowed </nowiki></code><br />
<br />
<br />
===COAP Example===<br />
<br />
<br />
1. Open the Makefile in the /contiki-2.7/examples/rest-example folder. For COAP server, make sure the WITH_COAP = 1 in the Makefile.<br />
<br />
2. Open the terminal. Go to the /contiki-2.7/examples/rest-example folder. Run the following command given below:<br />
<br />
<code><nowiki> make TARGET=cooja coap-client-server-example.csc </nowiki></code><br />
<br />
This opens the COOJA terminal which runs rest-server-example.c over COAP in one node having IP address aaaa::0212:7401:0001:0101 and coap-client-example.c in the another node having IP address aaaa::0212:7402:0002:0202. COAP uses 61616 port number as default. The client in the example periodically accesses the resources of the server and prints the payload which can be seen on the mote output. The Network looks as shown below:<br />
<br />
[[File:Coapnetwork.PNG]]<br />
<br />
3.Start the Simulation and observe the mote output. The Output is diplayed as shown in the below window.<br />
<br />
[[File:Coapmoteout.PNG]]<br />
<br />
==Run the REST-Example on T-mote Sky==<br />
<br />
<br />
1. Go to the /contiki-2.7/examples/rest-example folder. Connect the motes and program them with rest-server-example.<br />
<br />
<code><nowiki> make TARGET=sky rest-server-example.upload </nowiki></code><br />
<br />
<br />
2. Disconnect the motes and load one other mote with the RPL border router to connect to the rest-server.<br />
<br />
<code><nowiki> cd ../ipv6/rpl-border-router </nowiki></code><br />
<br />
<code><nowiki> make TARGET=sky border-router.upload </nowiki></code><br />
<br />
<br />
3. Connect the REST-server to the border router using tunslip6.<br />
<br />
<code><nowiki> make connect-router </nowiki></code><br />
<br />
<br />
4. Open new terminal window for each mote and execute the following command and reset the motes. The IP addresses of these motes get printed after the motes are reset.<br />
<br />
<code><nowiki> make login TARGET=sky MOTE=2 #Shows the prints for first mote </nowiki></code><br />
<br />
<code><nowiki> make login TARGET=sky MOTE=3 #For second mote and so on. </nowiki></code><br />
<br />
<br />
5. Ping the motes using the IP addresses we get from step 4.<br />
<br />
<code><nowiki> ping6 <IPv6 Address of the MOTE> </nowiki></code><br />
<br />
<br />
6. If WITH_COAP = 0 i.e. HTTP Server, use the HTTP Client to connect to the REST server. When WITH_COAP = 1. i.e. COAP Server, load the coap-client-example.c to the motes and connect to the REST server (same as COOJA Example).<br />
<br />
<br />
==General Issues You Might Face==<br />
<br />
<br />
1. To use curl as the HTTP client you need to install curl by running the following command in the terminal:<br />
<br />
<code><nowiki> sudo apt-get install curl </nowiki></code><br />
<br />
2. The memory on the Tmote sky is not big enough to fit in the HTTP example on REST server. Thus while running it on real motes we get the error “region text is full”. To overcome this problem, instead of RPL to decide the route, we can use static routes. We can also reduce the size by changing the Makefile of the border router by setting the WITH_WEBSERVER = 0. <br />
<br />
3. Generally the COAP Client is used instead of HTTP client for running over REST Server.<br />
<br />
<br />
==References==<br />
<br />
<br />
1. REST-Example Github page: https://github.com/contiki-os/contiki/tree/master/examples/rest-example<br />
<br />
2. A Low-Power CoAP for Contiki - Matthias Kovatsch, Simon Duquennoy, Adam Dunkels</div>Munihttp://anrg.usc.edu/contiki/index.php?title=REST_example_running_on_Cooja_and_Sky_motes&diff=1036REST example running on Cooja and Sky motes2014-11-08T05:07:55Z<p>Muni: /* COAP Example */</p>
<hr />
<div>==Introduction==<br />
<br />
REST (Representational State Transfer) is an architectural style consisting of coordinated set of architectural constraints applied to components, connectors and data elements within a distributed hypermedia system. The REST architectural style is applied to development of web services. A web service can be characterized as RESTful if they conform to architectural constraints like client-server, stateless, cacheable, layered system and uniform Interface. A REST architecture style is one in which web services are viewed as resources and can be uniquely identified by their URLs. RESTful architecture can be run over HTTP or COAP. The basic REST design principle uses the HTTP protocol methods for typical CRUD operations:<br />
* POST - Create a resource<br />
* GET - Retrieve a resource<br />
* PUT – Update a resource<br />
* DELETE - Delete a resource<br />
Each resource has a handler function which the REST layer calls to serve the request by the client.<br />
<br />
==You Will Learn==<br />
<br />
How to use REST layer to develop server-side applications like COAP or HTTP and run it on COOJA and using T-mote sky.<br />
<br />
==Relavant Directories==<br />
<br />
* /contiki-2.7/apps/rest-common - It contains the codes that are mentioned as target in the rest-example.<br />
* /contiki-2.7/apps/rest-coap - It contains codes for codes mentioned as target in rest-common. <br />
* /contiki-2.7/apps/rest-http - It contains codes for codes mentioned as target in rest-common.<br />
* /contiki-2.7/examples/rest-example - It has the main example of the REST server side and COAP client side codes that are to be uploaded on COOJA and real motes.<br />
<br />
==Understanding The Code==<br />
<br />
===rest-server-example.c===<br />
<br />
The REST server has light, led, toggle, helloworld and discover resources. Each resource has a resource handler function which is called when the client requests for that resource and the resource handler function sends the response back to the client. For example, when a HTTP client sends a request to the REST server for light resource, the REST server calls the light_handler function which reads the light sensor values from the mote and sends the light sensor values along with a simple etag back to the client as shown below. <br />
<br />
<source lang="c"><br />
/*A simple getter example. Returns the reading from light sensor with a simple etag*/<br />
RESOURCE(light, METHOD_GET, "light");<br />
void<br />
light_handler(REQUEST* request, RESPONSE* response)<br />
{<br />
read_light_sensor(&light_photosynthetic, &light_solar);<br />
sprintf(temp,"%u;%u", light_photosynthetic, light_solar);<br />
<br />
char etag[4] = "ABCD";<br />
rest_set_header_content_type(response, TEXT_PLAIN);<br />
rest_set_header_etag(response, etag, sizeof(etag));<br />
rest_set_response_payload(response, temp, strlen(temp));<br />
}<br />
</source><br />
<br />
===coap-client-example.c===<br />
The COAP client establishes a connection with the server on the COAP port 61616 and sets the et timer to a particular value. Everytime the et timer is expired, the send_data(void) function is called. When it recieves the response from the server for its request, the handle_incoming_data() function is called as shown in the code snippet within the process. <br />
<br />
<source lang="c"><br />
etimer_set(&et, 5 * CLOCK_SECOND);<br />
while(1) {<br />
PROCESS_YIELD();<br />
if (etimer_expired(&et)) {<br />
send_data();<br />
etimer_reset(&et);<br />
} else if (ev == tcpip_event) {<br />
handle_incoming_data();<br />
}<br />
}<br />
</source><br />
<br />
The COAP client runs a timer which when resets, the client randomly selects a service_id (resource) using random_rand() function and sends the request to the REST server as seen below in the send_data(void) function.<br />
<br />
<source lang="c"><br />
int data_size = 0;<br />
int service_id = random_rand() % NUMBER_OF_URLS;<br />
coap_packet_t* request = (coap_packet_t*)allocate_buffer(sizeof(coap_packet_t));<br />
init_packet(request);<br />
<br />
coap_set_method(request, COAP_GET);<br />
request->tid = xact_id++;<br />
request->type = MESSAGE_TYPE_CON;<br />
coap_set_header_uri(request, service_urls[service_id]);<br />
</source><br />
<br />
<br />
When the server response returns back to the client, it runs the handle_incoming_data() function which takes the packet, parses the message and prints the payload that is it receives from the response. It can be seen in the code below<br />
<br />
<source lang="c"><br />
static void<br />
handle_incoming_data()<br />
{<br />
PRINTF("Incoming packet size: %u \n", (uint16_t)uip_datalen());<br />
if (init_buffer(COAP_DATA_BUFF_SIZE)) {<br />
if (uip_newdata()) {<br />
coap_packet_t* response = (coap_packet_t*)allocate_buffer(sizeof(coap_packet_t));<br />
if (response) {<br />
parse_message(response, uip_appdata, uip_datalen());<br />
response_handler(response);<br />
}<br />
}<br />
delete_buffer();<br />
}<br />
}<br />
</source><br />
<br />
==Run the REST-Example on COOJA==<br />
<br />
<br />
===HTTP Example===<br />
<br />
<br />
1. Open the Makefile in the /contiki-2.7/examples/rest-example folder. For HTTP server, make sure the WITH_COAP = 0 in the Makefile.<br />
<br />
<br />
2. Open the terminal. Go to the /contiki-2.7/examples/rest-example folder. Run the following command given below:<br />
<br />
<code><nowiki> make TARGET=cooja rest-server-example.csc </nowiki></code><br />
<br />
This will open a COOJA terminal and loads the rest-server-example code for simulation. The Network will look as shown:<br />
<br />
[[File:Networkhttp.PNG]] <br />
<br />
<br />
3. Open another terminal. Go to the same directory and connect the COOJA simulation to the router.<br />
<br />
<code><nowiki> make connect-router-cooja </nowiki></code><br />
<br />
<br />
4. Start the Simulation on the COOJA and test the connectivity by pinging the server. The IP addresses of the servers are aaaa::0212:7402:0002:0202 and aaaa::0212:7403:0003:0303.<br />
<br />
<code><nowiki> ping6 aaaa::0212:7402:0002:0202 </nowiki></code><br />
<br />
<code><nowiki> ping6 aaaa::0212:7403:0003:0303 </nowiki></code><br />
<br />
<br />
5. Use curl as a HTTP client we interact with the COOJA motes that are running the REST code. The default HTTP client runs on 8080 port number.<br />
<br />
<code><nowiki> curl -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/helloworld #get helloworld plain text </nowiki></code><br />
<br />
<code><nowiki> curl -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/led?color=green -d mode=off -i #turn off the green led </nowiki></code><br />
<br />
<code><nowiki> curl -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/.well-known/core -i </nowiki></code><br />
<br />
<code><nowiki> curl -X POST -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/helloworld #method not allowed </nowiki></code><br />
<br />
<br />
===COAP Example===<br />
<br />
<br />
1. Open the Makefile in the /contiki-2.7/examples/rest-example folder. For COAP server, make sure the WITH_COAP = 1 in the Makefile.<br />
<br />
2. Open the terminal. Go to the /contiki-2.7/examples/rest-example folder. Run the following command given below:<br />
<br />
<code><nowiki> make TARGET=cooja coap-client-server-example.csc </nowiki></code><br />
<br />
This opens the COOJA terminal which runs rest-server-example.c over COAP in one node having IP address aaaa::0212:7401:0001:0101 and coap-client-example.c in the another node having IP address aaaa::0212:7402:0002:0202. COAP uses 61616 port number as default. The client in the example periodically accesses the resources of the server and prints the payload which can be seen on the mote output. The Network looks as shown below:<br />
<br />
[[File:Coapnetwork.PNG]]<br />
<br />
3.Start the Simulation and observe the mote output. The Output is diplayed as shown in the below window.<br />
<br />
[[File:Coapmoteout.PNG]]<br />
<br />
==Run the REST-Example on T-mote Sky==<br />
<br />
<br />
1. Go to the /contiki-2.7/examples/rest-example folder. Connect the motes and program them with rest-server-example.<br />
<br />
<code><nowiki> make TARGET=sky rest-server-example.upload </nowiki></code><br />
<br />
<br />
2. Disconnect the motes and load one other mote with the RPL border router to connect to the rest-server.<br />
<br />
<code><nowiki> cd ../ipv6/rpl-border-router </nowiki></code><br />
<br />
<code><nowiki> make TARGET=sky border-router.upload </nowiki></code><br />
<br />
<br />
3. Connect the REST-server to the border router using tunslip6.<br />
<br />
<code><nowiki> make connect-router </nowiki></code><br />
<br />
<br />
4. Open new terminal window for each mote and execute the following command and reset the motes. The IP addresses of these motes get printed after the motes are reset.<br />
<br />
<code><nowiki> make login TARGET=sky MOTE=2 #Shows the prints for first mote </nowiki></code><br />
<br />
<code><nowiki> make login TARGET=sky MOTE=3 #For second mote and so on. </nowiki></code><br />
<br />
<br />
5. Ping the motes using the IP addresses we get from step 4.<br />
<br />
<code><nowiki> ping6 <IPv6 Address of the MOTE> </nowiki></code><br />
<br />
<br />
6. If WITH_COAP = 0 i.e. HTTP Server, use the HTTP Client to connect to the REST server. When WITH_COAP = 1. i.e. COAP Server, load the coap-client-example.c to the motes and connect to the REST server (same as COOJA Example).<br />
<br />
<br />
==General Issues You Might Face==<br />
<br />
<br />
1. To use curl as the HTTP client you need to install curl by running the following command in the terminal:<br />
<br />
<code><nowiki> sudo apt-get install curl </nowiki></code><br />
<br />
2. The memory on the Tmote sky is not big enough to fit in the HTTP example on REST server. Thus while running it on real motes we get the error “region text is full”. To overcome this problem, instead of RPL to decide the route, we can use static routes. We can also reduce the size by changing the Makefile of the border router by setting the WITH_WEBSERVER = 0. <br />
<br />
3. Generally the COAP Client is used instead of HTTP client for running over REST Server.<br />
<br />
<br />
==References==<br />
<br />
<br />
1. REST-Example Github page: https://github.com/contiki-os/contiki/tree/master/examples/rest-example<br />
<br />
2. A Low-Power CoAP for Contiki - Matthias Kovatsch, Simon Duquennoy, Adam Dunkels</div>Munihttp://anrg.usc.edu/contiki/index.php?title=REST_example_running_on_Cooja_and_Sky_motes&diff=1035REST example running on Cooja and Sky motes2014-11-08T05:06:39Z<p>Muni: /* COAP Example */</p>
<hr />
<div>==Introduction==<br />
<br />
REST (Representational State Transfer) is an architectural style consisting of coordinated set of architectural constraints applied to components, connectors and data elements within a distributed hypermedia system. The REST architectural style is applied to development of web services. A web service can be characterized as RESTful if they conform to architectural constraints like client-server, stateless, cacheable, layered system and uniform Interface. A REST architecture style is one in which web services are viewed as resources and can be uniquely identified by their URLs. RESTful architecture can be run over HTTP or COAP. The basic REST design principle uses the HTTP protocol methods for typical CRUD operations:<br />
* POST - Create a resource<br />
* GET - Retrieve a resource<br />
* PUT – Update a resource<br />
* DELETE - Delete a resource<br />
Each resource has a handler function which the REST layer calls to serve the request by the client.<br />
<br />
==You Will Learn==<br />
<br />
How to use REST layer to develop server-side applications like COAP or HTTP and run it on COOJA and using T-mote sky.<br />
<br />
==Relavant Directories==<br />
<br />
* /contiki-2.7/apps/rest-common - It contains the codes that are mentioned as target in the rest-example.<br />
* /contiki-2.7/apps/rest-coap - It contains codes for codes mentioned as target in rest-common. <br />
* /contiki-2.7/apps/rest-http - It contains codes for codes mentioned as target in rest-common.<br />
* /contiki-2.7/examples/rest-example - It has the main example of the REST server side and COAP client side codes that are to be uploaded on COOJA and real motes.<br />
<br />
==Understanding The Code==<br />
<br />
===rest-server-example.c===<br />
<br />
The REST server has light, led, toggle, helloworld and discover resources. Each resource has a resource handler function which is called when the client requests for that resource and the resource handler function sends the response back to the client. For example, when a HTTP client sends a request to the REST server for light resource, the REST server calls the light_handler function which reads the light sensor values from the mote and sends the light sensor values along with a simple etag back to the client as shown below. <br />
<br />
<source lang="c"><br />
/*A simple getter example. Returns the reading from light sensor with a simple etag*/<br />
RESOURCE(light, METHOD_GET, "light");<br />
void<br />
light_handler(REQUEST* request, RESPONSE* response)<br />
{<br />
read_light_sensor(&light_photosynthetic, &light_solar);<br />
sprintf(temp,"%u;%u", light_photosynthetic, light_solar);<br />
<br />
char etag[4] = "ABCD";<br />
rest_set_header_content_type(response, TEXT_PLAIN);<br />
rest_set_header_etag(response, etag, sizeof(etag));<br />
rest_set_response_payload(response, temp, strlen(temp));<br />
}<br />
</source><br />
<br />
===coap-client-example.c===<br />
The COAP client establishes a connection with the server on the COAP port 61616 and sets the et timer to a particular value. Everytime the et timer is expired, the send_data(void) function is called. When it recieves the response from the server for its request, the handle_incoming_data() function is called as shown in the code snippet within the process. <br />
<br />
<source lang="c"><br />
etimer_set(&et, 5 * CLOCK_SECOND);<br />
while(1) {<br />
PROCESS_YIELD();<br />
if (etimer_expired(&et)) {<br />
send_data();<br />
etimer_reset(&et);<br />
} else if (ev == tcpip_event) {<br />
handle_incoming_data();<br />
}<br />
}<br />
</source><br />
<br />
The COAP client runs a timer which when resets, the client randomly selects a service_id (resource) using random_rand() function and sends the request to the REST server as seen below in the send_data(void) function.<br />
<br />
<source lang="c"><br />
int data_size = 0;<br />
int service_id = random_rand() % NUMBER_OF_URLS;<br />
coap_packet_t* request = (coap_packet_t*)allocate_buffer(sizeof(coap_packet_t));<br />
init_packet(request);<br />
<br />
coap_set_method(request, COAP_GET);<br />
request->tid = xact_id++;<br />
request->type = MESSAGE_TYPE_CON;<br />
coap_set_header_uri(request, service_urls[service_id]);<br />
</source><br />
<br />
<br />
When the server response returns back to the client, it runs the handle_incoming_data() function which takes the packet, parses the message and prints the payload that is it receives from the response. It can be seen in the code below<br />
<br />
<source lang="c"><br />
static void<br />
handle_incoming_data()<br />
{<br />
PRINTF("Incoming packet size: %u \n", (uint16_t)uip_datalen());<br />
if (init_buffer(COAP_DATA_BUFF_SIZE)) {<br />
if (uip_newdata()) {<br />
coap_packet_t* response = (coap_packet_t*)allocate_buffer(sizeof(coap_packet_t));<br />
if (response) {<br />
parse_message(response, uip_appdata, uip_datalen());<br />
response_handler(response);<br />
}<br />
}<br />
delete_buffer();<br />
}<br />
}<br />
</source><br />
<br />
==Run the REST-Example on COOJA==<br />
<br />
<br />
===HTTP Example===<br />
<br />
<br />
1. Open the Makefile in the /contiki-2.7/examples/rest-example folder. For HTTP server, make sure the WITH_COAP = 0 in the Makefile.<br />
<br />
<br />
2. Open the terminal. Go to the /contiki-2.7/examples/rest-example folder. Run the following command given below:<br />
<br />
<code><nowiki> make TARGET=cooja rest-server-example.csc </nowiki></code><br />
<br />
This will open a COOJA terminal and loads the rest-server-example code for simulation. The Network will look as shown:<br />
<br />
[[File:Networkhttp.PNG]] <br />
<br />
<br />
3. Open another terminal. Go to the same directory and connect the COOJA simulation to the router.<br />
<br />
<code><nowiki> make connect-router-cooja </nowiki></code><br />
<br />
<br />
4. Start the Simulation on the COOJA and test the connectivity by pinging the server. The IP addresses of the servers are aaaa::0212:7402:0002:0202 and aaaa::0212:7403:0003:0303.<br />
<br />
<code><nowiki> ping6 aaaa::0212:7402:0002:0202 </nowiki></code><br />
<br />
<code><nowiki> ping6 aaaa::0212:7403:0003:0303 </nowiki></code><br />
<br />
<br />
5. Use curl as a HTTP client we interact with the COOJA motes that are running the REST code. The default HTTP client runs on 8080 port number.<br />
<br />
<code><nowiki> curl -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/helloworld #get helloworld plain text </nowiki></code><br />
<br />
<code><nowiki> curl -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/led?color=green -d mode=off -i #turn off the green led </nowiki></code><br />
<br />
<code><nowiki> curl -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/.well-known/core -i </nowiki></code><br />
<br />
<code><nowiki> curl -X POST -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/helloworld #method not allowed </nowiki></code><br />
<br />
<br />
===COAP Example===<br />
<br />
<br />
1. Open the Makefile in the /contiki-2.7/examples/rest-example folder. For COAP server, make sure the WITH_COAP = 1 in the Makefile.<br />
<br />
2. Open the terminal. Go to the /contiki-2.7/examples/rest-example folder. Run the following command given below:<br />
<br />
<code><nowiki> make TARGET=cooja coap-client-server-example.csc </nowiki></code><br />
<br />
This opens the COOJA terminal which runs rest-server-example.c over COAP in one node having IP address aaaa::0212:7401:0001:0101 and coap-client-example.c in the another node having IP address aaaa::0212:7402:0002:0202. COAP uses 61616 port number as default. The client in the example periodically accesses the resources of the server and prints the payload which can be seen on the mote output. The Network looks as shown below:<br />
<br />
[[File:Coapnetwork.PNG]]<br />
<br />
3.Start the Simulation and observe the mote output.<br />
<br />
[[File:Coapmoteout.PNG]]<br />
<br />
==Run the REST-Example on T-mote Sky==<br />
<br />
<br />
1. Go to the /contiki-2.7/examples/rest-example folder. Connect the motes and program them with rest-server-example.<br />
<br />
<code><nowiki> make TARGET=sky rest-server-example.upload </nowiki></code><br />
<br />
<br />
2. Disconnect the motes and load one other mote with the RPL border router to connect to the rest-server.<br />
<br />
<code><nowiki> cd ../ipv6/rpl-border-router </nowiki></code><br />
<br />
<code><nowiki> make TARGET=sky border-router.upload </nowiki></code><br />
<br />
<br />
3. Connect the REST-server to the border router using tunslip6.<br />
<br />
<code><nowiki> make connect-router </nowiki></code><br />
<br />
<br />
4. Open new terminal window for each mote and execute the following command and reset the motes. The IP addresses of these motes get printed after the motes are reset.<br />
<br />
<code><nowiki> make login TARGET=sky MOTE=2 #Shows the prints for first mote </nowiki></code><br />
<br />
<code><nowiki> make login TARGET=sky MOTE=3 #For second mote and so on. </nowiki></code><br />
<br />
<br />
5. Ping the motes using the IP addresses we get from step 4.<br />
<br />
<code><nowiki> ping6 <IPv6 Address of the MOTE> </nowiki></code><br />
<br />
<br />
6. If WITH_COAP = 0 i.e. HTTP Server, use the HTTP Client to connect to the REST server. When WITH_COAP = 1. i.e. COAP Server, load the coap-client-example.c to the motes and connect to the REST server (same as COOJA Example).<br />
<br />
<br />
==General Issues You Might Face==<br />
<br />
<br />
1. To use curl as the HTTP client you need to install curl by running the following command in the terminal:<br />
<br />
<code><nowiki> sudo apt-get install curl </nowiki></code><br />
<br />
2. The memory on the Tmote sky is not big enough to fit in the HTTP example on REST server. Thus while running it on real motes we get the error “region text is full”. To overcome this problem, instead of RPL to decide the route, we can use static routes. We can also reduce the size by changing the Makefile of the border router by setting the WITH_WEBSERVER = 0. <br />
<br />
3. Generally the COAP Client is used instead of HTTP client for running over REST Server.<br />
<br />
<br />
==References==<br />
<br />
<br />
1. REST-Example Github page: https://github.com/contiki-os/contiki/tree/master/examples/rest-example<br />
<br />
2. A Low-Power CoAP for Contiki - Matthias Kovatsch, Simon Duquennoy, Adam Dunkels</div>Munihttp://anrg.usc.edu/contiki/index.php?title=File:Coapmoteout.PNG&diff=1034File:Coapmoteout.PNG2014-11-08T05:05:58Z<p>Muni: </p>
<hr />
<div></div>Munihttp://anrg.usc.edu/contiki/index.php?title=REST_example_running_on_Cooja_and_Sky_motes&diff=1026REST example running on Cooja and Sky motes2014-11-08T04:57:20Z<p>Muni: /* COAP Example */</p>
<hr />
<div>==Introduction==<br />
<br />
REST (Representational State Transfer) is an architectural style consisting of coordinated set of architectural constraints applied to components, connectors and data elements within a distributed hypermedia system. The REST architectural style is applied to development of web services. A web service can be characterized as RESTful if they conform to architectural constraints like client-server, stateless, cacheable, layered system and uniform Interface. A REST architecture style is one in which web services are viewed as resources and can be uniquely identified by their URLs. RESTful architecture can be run over HTTP or COAP. The basic REST design principle uses the HTTP protocol methods for typical CRUD operations:<br />
* POST - Create a resource<br />
* GET - Retrieve a resource<br />
* PUT – Update a resource<br />
* DELETE - Delete a resource<br />
Each resource has a handler function which the REST layer calls to serve the request by the client.<br />
<br />
==You Will Learn==<br />
<br />
How to use REST layer to develop server-side applications like COAP or HTTP and run it on COOJA and using T-mote sky.<br />
<br />
==Relavant Directories==<br />
<br />
* /contiki-2.7/apps/rest-common - It contains the codes that are mentioned as target in the rest-example.<br />
* /contiki-2.7/apps/rest-coap - It contains codes for codes mentioned as target in rest-common. <br />
* /contiki-2.7/apps/rest-http - It contains codes for codes mentioned as target in rest-common.<br />
* /contiki-2.7/examples/rest-example - It has the main example of the REST server side and COAP client side codes that are to be uploaded on COOJA and real motes.<br />
<br />
==Understanding The Code==<br />
<br />
===rest-server-example.c===<br />
<br />
The REST server has light, led, toggle, helloworld and discover resources. Each resource has a resource handler function which is called when the client requests for that resource and the resource handler function sends the response back to the client. For example, when a HTTP client sends a request to the REST server for light resource, the REST server calls the light_handler function which reads the light sensor values from the mote and sends the light sensor values along with a simple etag back to the client as shown below. <br />
<br />
<source lang="c"><br />
/*A simple getter example. Returns the reading from light sensor with a simple etag*/<br />
RESOURCE(light, METHOD_GET, "light");<br />
void<br />
light_handler(REQUEST* request, RESPONSE* response)<br />
{<br />
read_light_sensor(&light_photosynthetic, &light_solar);<br />
sprintf(temp,"%u;%u", light_photosynthetic, light_solar);<br />
<br />
char etag[4] = "ABCD";<br />
rest_set_header_content_type(response, TEXT_PLAIN);<br />
rest_set_header_etag(response, etag, sizeof(etag));<br />
rest_set_response_payload(response, temp, strlen(temp));<br />
}<br />
</source><br />
<br />
===coap-client-example.c===<br />
The COAP client establishes a connection with the server on the COAP port 61616 and sets the et timer to a particular value. Everytime the et timer is expired, the send_data(void) function is called. When it recieves the response from the server for its request, the handle_incoming_data() function is called as shown in the code snippet within the process. <br />
<br />
<source lang="c"><br />
etimer_set(&et, 5 * CLOCK_SECOND);<br />
while(1) {<br />
PROCESS_YIELD();<br />
if (etimer_expired(&et)) {<br />
send_data();<br />
etimer_reset(&et);<br />
} else if (ev == tcpip_event) {<br />
handle_incoming_data();<br />
}<br />
}<br />
</source><br />
<br />
The COAP client runs a timer which when resets, the client randomly selects a service_id (resource) using random_rand() function and sends the request to the REST server as seen below in the send_data(void) function.<br />
<br />
<source lang="c"><br />
int data_size = 0;<br />
int service_id = random_rand() % NUMBER_OF_URLS;<br />
coap_packet_t* request = (coap_packet_t*)allocate_buffer(sizeof(coap_packet_t));<br />
init_packet(request);<br />
<br />
coap_set_method(request, COAP_GET);<br />
request->tid = xact_id++;<br />
request->type = MESSAGE_TYPE_CON;<br />
coap_set_header_uri(request, service_urls[service_id]);<br />
</source><br />
<br />
<br />
When the server response returns back to the client, it runs the handle_incoming_data() function which takes the packet, parses the message and prints the payload that is it receives from the response. It can be seen in the code below<br />
<br />
<source lang="c"><br />
static void<br />
handle_incoming_data()<br />
{<br />
PRINTF("Incoming packet size: %u \n", (uint16_t)uip_datalen());<br />
if (init_buffer(COAP_DATA_BUFF_SIZE)) {<br />
if (uip_newdata()) {<br />
coap_packet_t* response = (coap_packet_t*)allocate_buffer(sizeof(coap_packet_t));<br />
if (response) {<br />
parse_message(response, uip_appdata, uip_datalen());<br />
response_handler(response);<br />
}<br />
}<br />
delete_buffer();<br />
}<br />
}<br />
</source><br />
<br />
==Run the REST-Example on COOJA==<br />
<br />
<br />
===HTTP Example===<br />
<br />
<br />
1. Open the Makefile in the /contiki-2.7/examples/rest-example folder. For HTTP server, make sure the WITH_COAP = 0 in the Makefile.<br />
<br />
<br />
2. Open the terminal. Go to the /contiki-2.7/examples/rest-example folder. Run the following command given below:<br />
<br />
<code><nowiki> make TARGET=cooja rest-server-example.csc </nowiki></code><br />
<br />
This will open a COOJA terminal and loads the rest-server-example code for simulation. The Network will look as shown:<br />
<br />
[[File:Networkhttp.PNG]] <br />
<br />
<br />
3. Open another terminal. Go to the same directory and connect the COOJA simulation to the router.<br />
<br />
<code><nowiki> make connect-router-cooja </nowiki></code><br />
<br />
<br />
4. Start the Simulation on the COOJA and test the connectivity by pinging the server. The IP addresses of the servers are aaaa::0212:7402:0002:0202 and aaaa::0212:7403:0003:0303.<br />
<br />
<code><nowiki> ping6 aaaa::0212:7402:0002:0202 </nowiki></code><br />
<br />
<code><nowiki> ping6 aaaa::0212:7403:0003:0303 </nowiki></code><br />
<br />
<br />
5. Use curl as a HTTP client we interact with the COOJA motes that are running the REST code. The default HTTP client runs on 8080 port number.<br />
<br />
<code><nowiki> curl -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/helloworld #get helloworld plain text </nowiki></code><br />
<br />
<code><nowiki> curl -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/led?color=green -d mode=off -i #turn off the green led </nowiki></code><br />
<br />
<code><nowiki> curl -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/.well-known/core -i </nowiki></code><br />
<br />
<code><nowiki> curl -X POST -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/helloworld #method not allowed </nowiki></code><br />
<br />
<br />
===COAP Example===<br />
<br />
<br />
1. Open the Makefile in the /contiki-2.7/examples/rest-example folder. For COAP server, make sure the WITH_COAP = 1 in the Makefile.<br />
<br />
2. Open the terminal. Go to the /contiki-2.7/examples/rest-example folder. Run the following command given below:<br />
<br />
<code><nowiki> make TARGET=cooja coap-client-server-example.csc </nowiki></code><br />
<br />
This opens the COOJA terminal which runs rest-server-example.c over COAP in one node having IP address aaaa::0212:7401:0001:0101 and coap-client-example.c in the another node having IP address aaaa::0212:7402:0002:0202. COAP uses 61616 port number as default. The client in the example periodically accesses the resources of the server and prints the payload which can be seen on the mote output. The Network looks as shown below:<br />
<br />
[[File:Coapnetwork.PNG]]<br />
<br />
3.Start the Simulation and observe the mote output.<br />
<br />
==Run the REST-Example on T-mote Sky==<br />
<br />
<br />
1. Go to the /contiki-2.7/examples/rest-example folder. Connect the motes and program them with rest-server-example.<br />
<br />
<code><nowiki> make TARGET=sky rest-server-example.upload </nowiki></code><br />
<br />
<br />
2. Disconnect the motes and load one other mote with the RPL border router to connect to the rest-server.<br />
<br />
<code><nowiki> cd ../ipv6/rpl-border-router </nowiki></code><br />
<br />
<code><nowiki> make TARGET=sky border-router.upload </nowiki></code><br />
<br />
<br />
3. Connect the REST-server to the border router using tunslip6.<br />
<br />
<code><nowiki> make connect-router </nowiki></code><br />
<br />
<br />
4. Open new terminal window for each mote and execute the following command and reset the motes. The IP addresses of these motes get printed after the motes are reset.<br />
<br />
<code><nowiki> make login TARGET=sky MOTE=2 #Shows the prints for first mote </nowiki></code><br />
<br />
<code><nowiki> make login TARGET=sky MOTE=3 #For second mote and so on. </nowiki></code><br />
<br />
<br />
5. Ping the motes using the IP addresses we get from step 4.<br />
<br />
<code><nowiki> ping6 <IPv6 Address of the MOTE> </nowiki></code><br />
<br />
<br />
6. If WITH_COAP = 0 i.e. HTTP Server, use the HTTP Client to connect to the REST server. When WITH_COAP = 1. i.e. COAP Server, load the coap-client-example.c to the motes and connect to the REST server (same as COOJA Example).<br />
<br />
<br />
==General Issues You Might Face==<br />
<br />
<br />
1. To use curl as the HTTP client you need to install curl by running the following command in the terminal:<br />
<br />
<code><nowiki> sudo apt-get install curl </nowiki></code><br />
<br />
2. The memory on the Tmote sky is not big enough to fit in the HTTP example on REST server. Thus while running it on real motes we get the error “region text is full”. To overcome this problem, instead of RPL to decide the route, we can use static routes. We can also reduce the size by changing the Makefile of the border router by setting the WITH_WEBSERVER = 0. <br />
<br />
3. Generally the COAP Client is used instead of HTTP client for running over REST Server.<br />
<br />
<br />
==References==<br />
<br />
<br />
1. REST-Example Github page: https://github.com/contiki-os/contiki/tree/master/examples/rest-example<br />
<br />
2. A Low-Power CoAP for Contiki - Matthias Kovatsch, Simon Duquennoy, Adam Dunkels</div>Munihttp://anrg.usc.edu/contiki/index.php?title=File:Networkhttp.PNG&diff=1025File:Networkhttp.PNG2014-11-08T04:53:05Z<p>Muni: Muni uploaded a new version of &quot;File:Networkhttp.PNG&quot;</p>
<hr />
<div></div>Munihttp://anrg.usc.edu/contiki/index.php?title=REST_example_running_on_Cooja_and_Sky_motes&diff=1023REST example running on Cooja and Sky motes2014-11-08T03:56:16Z<p>Muni: </p>
<hr />
<div>==Introduction==<br />
<br />
REST (Representational State Transfer) is an architectural style consisting of coordinated set of architectural constraints applied to components, connectors and data elements within a distributed hypermedia system. The REST architectural style is applied to development of web services. A web service can be characterized as RESTful if they conform to architectural constraints like client-server, stateless, cacheable, layered system and uniform Interface. A REST architecture style is one in which web services are viewed as resources and can be uniquely identified by their URLs. RESTful architecture can be run over HTTP or COAP. The basic REST design principle uses the HTTP protocol methods for typical CRUD operations:<br />
* POST - Create a resource<br />
* GET - Retrieve a resource<br />
* PUT – Update a resource<br />
* DELETE - Delete a resource<br />
Each resource has a handler function which the REST layer calls to serve the request by the client.<br />
<br />
==You Will Learn==<br />
<br />
How to use REST layer to develop server-side applications like COAP or HTTP and run it on COOJA and using T-mote sky.<br />
<br />
==Relavant Directories==<br />
<br />
* /contiki-2.7/apps/rest-common - It contains the codes that are mentioned as target in the rest-example.<br />
* /contiki-2.7/apps/rest-coap - It contains codes for codes mentioned as target in rest-common. <br />
* /contiki-2.7/apps/rest-http - It contains codes for codes mentioned as target in rest-common.<br />
* /contiki-2.7/examples/rest-example - It has the main example of the REST server side and COAP client side codes that are to be uploaded on COOJA and real motes.<br />
<br />
==Understanding The Code==<br />
<br />
===rest-server-example.c===<br />
<br />
The REST server has light, led, toggle, helloworld and discover resources. Each resource has a resource handler function which is called when the client requests for that resource and the resource handler function sends the response back to the client. For example, when a HTTP client sends a request to the REST server for light resource, the REST server calls the light_handler function which reads the light sensor values from the mote and sends the light sensor values along with a simple etag back to the client as shown below. <br />
<br />
<source lang="c"><br />
/*A simple getter example. Returns the reading from light sensor with a simple etag*/<br />
RESOURCE(light, METHOD_GET, "light");<br />
void<br />
light_handler(REQUEST* request, RESPONSE* response)<br />
{<br />
read_light_sensor(&light_photosynthetic, &light_solar);<br />
sprintf(temp,"%u;%u", light_photosynthetic, light_solar);<br />
<br />
char etag[4] = "ABCD";<br />
rest_set_header_content_type(response, TEXT_PLAIN);<br />
rest_set_header_etag(response, etag, sizeof(etag));<br />
rest_set_response_payload(response, temp, strlen(temp));<br />
}<br />
</source><br />
<br />
===coap-client-example.c===<br />
The COAP client establishes a connection with the server on the COAP port 61616 and sets the et timer to a particular value. Everytime the et timer is expired, the send_data(void) function is called. When it recieves the response from the server for its request, the handle_incoming_data() function is called as shown in the code snippet within the process. <br />
<br />
<source lang="c"><br />
etimer_set(&et, 5 * CLOCK_SECOND);<br />
while(1) {<br />
PROCESS_YIELD();<br />
if (etimer_expired(&et)) {<br />
send_data();<br />
etimer_reset(&et);<br />
} else if (ev == tcpip_event) {<br />
handle_incoming_data();<br />
}<br />
}<br />
</source><br />
<br />
The COAP client runs a timer which when resets, the client randomly selects a service_id (resource) using random_rand() function and sends the request to the REST server as seen below in the send_data(void) function.<br />
<br />
<source lang="c"><br />
int data_size = 0;<br />
int service_id = random_rand() % NUMBER_OF_URLS;<br />
coap_packet_t* request = (coap_packet_t*)allocate_buffer(sizeof(coap_packet_t));<br />
init_packet(request);<br />
<br />
coap_set_method(request, COAP_GET);<br />
request->tid = xact_id++;<br />
request->type = MESSAGE_TYPE_CON;<br />
coap_set_header_uri(request, service_urls[service_id]);<br />
</source><br />
<br />
<br />
When the server response returns back to the client, it runs the handle_incoming_data() function which takes the packet, parses the message and prints the payload that is it receives from the response. It can be seen in the code below<br />
<br />
<source lang="c"><br />
static void<br />
handle_incoming_data()<br />
{<br />
PRINTF("Incoming packet size: %u \n", (uint16_t)uip_datalen());<br />
if (init_buffer(COAP_DATA_BUFF_SIZE)) {<br />
if (uip_newdata()) {<br />
coap_packet_t* response = (coap_packet_t*)allocate_buffer(sizeof(coap_packet_t));<br />
if (response) {<br />
parse_message(response, uip_appdata, uip_datalen());<br />
response_handler(response);<br />
}<br />
}<br />
delete_buffer();<br />
}<br />
}<br />
</source><br />
<br />
==Run the REST-Example on COOJA==<br />
<br />
<br />
===HTTP Example===<br />
<br />
<br />
1. Open the Makefile in the /contiki-2.7/examples/rest-example folder. For HTTP server, make sure the WITH_COAP = 0 in the Makefile.<br />
<br />
<br />
2. Open the terminal. Go to the /contiki-2.7/examples/rest-example folder. Run the following command given below:<br />
<br />
<code><nowiki> make TARGET=cooja rest-server-example.csc </nowiki></code><br />
<br />
This will open a COOJA terminal and loads the rest-server-example code for simulation. The Network will look as shown:<br />
<br />
[[File:Networkhttp.PNG]] <br />
<br />
<br />
3. Open another terminal. Go to the same directory and connect the COOJA simulation to the router.<br />
<br />
<code><nowiki> make connect-router-cooja </nowiki></code><br />
<br />
<br />
4. Start the Simulation on the COOJA and test the connectivity by pinging the server. The IP addresses of the servers are aaaa::0212:7402:0002:0202 and aaaa::0212:7403:0003:0303.<br />
<br />
<code><nowiki> ping6 aaaa::0212:7402:0002:0202 </nowiki></code><br />
<br />
<code><nowiki> ping6 aaaa::0212:7403:0003:0303 </nowiki></code><br />
<br />
<br />
5. Use curl as a HTTP client we interact with the COOJA motes that are running the REST code. The default HTTP client runs on 8080 port number.<br />
<br />
<code><nowiki> curl -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/helloworld #get helloworld plain text </nowiki></code><br />
<br />
<code><nowiki> curl -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/led?color=green -d mode=off -i #turn off the green led </nowiki></code><br />
<br />
<code><nowiki> curl -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/.well-known/core -i </nowiki></code><br />
<br />
<code><nowiki> curl -X POST -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/helloworld #method not allowed </nowiki></code><br />
<br />
<br />
===COAP Example===<br />
<br />
<br />
1. Open the Makefile in the /contiki-2.7/examples/rest-example folder. For HTTP server, make sure the WITH_COAP = 1 in the Makefile.<br />
<br />
2. Open the terminal. Go to the /contiki-2.7/examples/rest-example folder. Run the following command given below:<br />
<br />
<code><nowiki> make TARGET=cooja coap-client-server-example.csc </nowiki></code><br />
<br />
This opens the COOJA terminal which runs rest-server-example.c over COAP in one node having IP address aaaa::0212:7401:0001:0101 and coap-client-example.c in the another node having IP address aaaa::0212:7402:0002:0202. COAP uses 61616 port number as default. The client in the example periodically accesses the resources of the server and prints the payload which can be seen on the mote output. The Network looks as shown below:<br />
<br />
[[File:Coapnetwork.PNG]]<br />
<br />
3.Start the Simulation and observe the mote output.<br />
<br />
<br />
==Run the REST-Example on T-mote Sky==<br />
<br />
<br />
1. Go to the /contiki-2.7/examples/rest-example folder. Connect the motes and program them with rest-server-example.<br />
<br />
<code><nowiki> make TARGET=sky rest-server-example.upload </nowiki></code><br />
<br />
<br />
2. Disconnect the motes and load one other mote with the RPL border router to connect to the rest-server.<br />
<br />
<code><nowiki> cd ../ipv6/rpl-border-router </nowiki></code><br />
<br />
<code><nowiki> make TARGET=sky border-router.upload </nowiki></code><br />
<br />
<br />
3. Connect the REST-server to the border router using tunslip6.<br />
<br />
<code><nowiki> make connect-router </nowiki></code><br />
<br />
<br />
4. Open new terminal window for each mote and execute the following command and reset the motes. The IP addresses of these motes get printed after the motes are reset.<br />
<br />
<code><nowiki> make login TARGET=sky MOTE=2 #Shows the prints for first mote </nowiki></code><br />
<br />
<code><nowiki> make login TARGET=sky MOTE=3 #For second mote and so on. </nowiki></code><br />
<br />
<br />
5. Ping the motes using the IP addresses we get from step 4.<br />
<br />
<code><nowiki> ping6 <IPv6 Address of the MOTE> </nowiki></code><br />
<br />
<br />
6. If WITH_COAP = 0 i.e. HTTP Server, use the HTTP Client to connect to the REST server. When WITH_COAP = 1. i.e. COAP Server, load the coap-client-example.c to the motes and connect to the REST server (same as COOJA Example).<br />
<br />
<br />
==General Issues You Might Face==<br />
<br />
<br />
1. To use curl as the HTTP client you need to install curl by running the following command in the terminal:<br />
<br />
<code><nowiki> sudo apt-get install curl </nowiki></code><br />
<br />
2. The memory on the Tmote sky is not big enough to fit in the HTTP example on REST server. Thus while running it on real motes we get the error “region text is full”. To overcome this problem, instead of RPL to decide the route, we can use static routes. We can also reduce the size by changing the Makefile of the border router by setting the WITH_WEBSERVER = 0. <br />
<br />
3. Generally the COAP Client is used instead of HTTP client for running over REST Server.<br />
<br />
<br />
==References==<br />
<br />
<br />
1. REST-Example Github page: https://github.com/contiki-os/contiki/tree/master/examples/rest-example<br />
<br />
2. A Low-Power CoAP for Contiki - Matthias Kovatsch, Simon Duquennoy, Adam Dunkels</div>Munihttp://anrg.usc.edu/contiki/index.php?title=File:Coapnetwork.PNG&diff=1021File:Coapnetwork.PNG2014-11-08T03:51:34Z<p>Muni: </p>
<hr />
<div></div>Munihttp://anrg.usc.edu/contiki/index.php?title=File:Networkhttp.PNG&diff=1019File:Networkhttp.PNG2014-11-08T03:51:12Z<p>Muni: </p>
<hr />
<div></div>Munihttp://anrg.usc.edu/contiki/index.php?title=File:A3senddata.PNG&diff=1007File:A3senddata.PNG2014-11-08T03:02:26Z<p>Muni: Muni uploaded a new version of &quot;File:A3senddata.PNG&quot;</p>
<hr />
<div></div>Munihttp://anrg.usc.edu/contiki/index.php?title=File:A3recieve.PNG&diff=1006File:A3recieve.PNG2014-11-08T02:59:27Z<p>Muni: </p>
<hr />
<div></div>Munihttp://anrg.usc.edu/contiki/index.php?title=File:A3senddata.PNG&diff=1005File:A3senddata.PNG2014-11-08T02:58:28Z<p>Muni: </p>
<hr />
<div></div>Munihttp://anrg.usc.edu/contiki/index.php?title=File:A3timer.PNG&diff=1004File:A3timer.PNG2014-11-08T02:58:05Z<p>Muni: </p>
<hr />
<div></div>Munihttp://anrg.usc.edu/contiki/index.php?title=REST_example_running_on_Cooja_and_Sky_motes&diff=1003REST example running on Cooja and Sky motes2014-11-08T02:57:18Z<p>Muni: </p>
<hr />
<div>==Introduction==<br />
<br />
REST (Representational State Transfer) is an architectural style consisting of coordinated set of architectural constraints applied to components, connectors and data elements within a distributed hypermedia system. The REST architectural style is applied to development of web services. A web service can be characterized as RESTful if they conform to architectural constraints like client-server, stateless, cacheable, layered system and uniform Interface. A REST architecture style is one in which web services are viewed as resources and can be uniquely identified by their URLs. RESTful architecture can be run over HTTP or COAP. The basic REST design principle uses the HTTP protocol methods for typical CRUD operations:<br />
* POST - Create a resource<br />
* GET - Retrieve a resource<br />
* PUT – Update a resource<br />
* DELETE - Delete a resource<br />
Each resource has a handler function which the REST layer calls to serve the request by the client.<br />
<br />
==You Will Learn==<br />
<br />
How to use REST layer to develop server-side applications like COAP or HTTP and run it on COOJA and using T-mote sky.<br />
<br />
==Relavant Directories==<br />
<br />
* /contiki-2.7/apps/rest-common - It contains the codes that are mentioned as target in the rest-example.<br />
* /contiki-2.7/apps/rest-coap - It contains codes for codes mentioned as target in rest-common. <br />
* /contiki-2.7/apps/rest-http - It contains codes for codes mentioned as target in rest-common.<br />
* /contiki-2.7/examples/rest-example - It has the main example of the REST server side and COAP client side codes that are to be uploaded on COOJA and real motes.<br />
<br />
==Understanding The Code==<br />
<br />
===rest-server-example.c===<br />
<br />
The REST server has light, led, toggle, helloworld and discover resources. Each resource has a resource handler function which is called when the client requests for that resource and the resource handler function sends the response back to the client. For example, when a HTTP client sends a request to the REST server for light resource, the REST server calls the light_handler function which reads the light sensor values from the mote and sends the light sensor values along with a simple etag back to the client as shown below. <br />
<br />
[[File:a3handler.png]]<br />
<br />
===coap-client-example.c===<br />
The COAP client establishes a connection with the server on the COAP port 61616 and sets the et timer to a particular value. Everytime the et timer is expired, the send_data(void) function is called. When it recieves the response from the server for its request, the handle_incoming_data() function is called as shown in the code snippet below. <br />
<br />
[[File:a3timer.png]]<br />
<br />
The COAP client runs a timer which when resets, the client randomly selects a service_id (resource) using random_rand() function and sends the request to the REST server as seen below in the send_data(void).<br />
<br />
[[File:a3senddata.png]]<br />
<br />
When the server response returns back to the client, it runs the handle_incoming_data() function which takes the packet, parses the message and prints the payload that is it receives from the response. It can be seen in the code below<br />
<br />
[[File:a3recieve.png]]<br />
<br />
==Run the REST-Example on COOJA==<br />
<br />
<br />
===HTTP Example===<br />
<br />
<br />
1. Open the Makefile in the /contiki-2.7/examples/rest-example folder. For HTTP server, make sure the WITH_COAP = 0 in the Makefile.<br />
<br />
<br />
2. Open the terminal. Go to the /contiki-2.7/examples/rest-example folder. Run the following command given below:<br />
<br />
<code><nowiki> make TARGET=cooja rest-server-example.csc </nowiki></code><br />
<br />
This will open a COOJA terminal and loads the rest-server-example code for simulation. <br />
<br />
<br />
3. Open another terminal. Go to the same directory and connect the COOJA simulation to the router.<br />
<br />
<code><nowiki> make connect-router-cooja </nowiki></code><br />
<br />
<br />
4. Start the Simulation on the COOJA and test the connectivity by pinging the server. The IP addresses of the servers are aaaa::0212:7402:0002:0202 and aaaa::0212:7403:0003:0303.<br />
<br />
<code><nowiki> ping6 aaaa::0212:7402:0002:0202 </nowiki></code><br />
<br />
<code><nowiki> ping6 aaaa::0212:7403:0003:0303 </nowiki></code><br />
<br />
<br />
5. Use curl as a HTTP client we interact with the COOJA motes that are running the REST code. The default HTTP client runs on 8080 port number.<br />
<br />
<code><nowiki> curl -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/helloworld #get helloworld plain text </nowiki></code><br />
<br />
<code><nowiki> curl -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/led?color=green -d mode=off -i #turn off the green led </nowiki></code><br />
<br />
<code><nowiki> curl -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/.well-known/core -i </nowiki></code><br />
<br />
<code><nowiki> curl -X POST -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/helloworld #method not allowed </nowiki></code><br />
<br />
<br />
===COAP Example===<br />
<br />
<br />
1. Open the Makefile in the /contiki-2.7/examples/rest-example folder. For HTTP server, make sure the WITH_COAP = 1 in the Makefile.<br />
<br />
2. Open the terminal. Go to the /contiki-2.7/examples/rest-example folder. Run the following command given below:<br />
<br />
<code><nowiki> make TARGET=cooja coap-client-server-example.csc </nowiki></code><br />
<br />
This opens the COOJA terminal which runs rest-server-example.c over COAP in one node having IP address aaaa::0212:7401:0001:0101 and coap-client-example.c in the another node having IP address aaaa::0212:7402:0002:0202. COAP uses 61616 port number as default. The client in the example periodically accesses the resources of the server and prints the payload which can be seen on the mote output. <br />
<br />
3.Start the Simulation and observe the mote output.<br />
<br />
<br />
==Run the REST-Example on T-mote Sky==<br />
<br />
<br />
1. Go to the /contiki-2.7/examples/rest-example folder. Connect the motes and program them with rest-server-example.<br />
<br />
<code><nowiki> make TARGET=sky rest-server-example.upload </nowiki></code><br />
<br />
<br />
2. Disconnect the motes and load one other mote with the RPL border router to connect to the rest-server.<br />
<br />
<code><nowiki> cd ../ipv6/rpl-border-router </nowiki></code><br />
<br />
<code><nowiki> make TARGET=sky border-router.upload </nowiki></code><br />
<br />
<br />
3. Connect the REST-server to the border router using tunslip6.<br />
<br />
<code><nowiki> make connect-router </nowiki></code><br />
<br />
<br />
4. Open new terminal window for each mote and execute the following command and reset the motes. The IP addresses of these motes get printed after the motes are reset.<br />
<br />
<code><nowiki> make login TARGET=sky MOTE=2 #Shows the prints for first mote </nowiki></code><br />
<br />
<code><nowiki> make login TARGET=sky MOTE=3 #For second mote and so on. </nowiki></code><br />
<br />
<br />
5. Ping the motes using the IP addresses we get from step 4.<br />
<br />
<code><nowiki> ping6 <IPv6 Address of the MOTE> </nowiki></code><br />
<br />
<br />
6. If WITH_COAP = 0 i.e. HTTP Server, use the HTTP Client to connect to the REST server. When WITH_COAP = 1. i.e. COAP Server, load the coap-client-example.c to the motes and connect to the REST server (same as COOJA Example).<br />
<br />
<br />
==General Issues You Might Face==<br />
<br />
<br />
1. To use curl as the HTTP client you need to install curl by running the following command in the terminal:<br />
<br />
<code><nowiki> sudo apt-get install curl </nowiki></code><br />
<br />
2. The memory on the Tmote sky is not big enough to fit in the HTTP example on REST server. Thus while running it on real motes we get the error “region text is full”. To overcome this problem, instead of RPL to decide the route, we can use static routes. We can also reduce the size by changing the Makefile of the border router by setting the WITH_WEBSERVER = 0. <br />
<br />
3. Generally the COAP Client is used instead of HTTP client for running over REST Server.<br />
<br />
<br />
==References==<br />
<br />
<br />
1. REST-Example Github page: https://github.com/contiki-os/contiki/tree/master/examples/rest-example<br />
<br />
2. A Low-Power CoAP for Contiki - Matthias Kovatsch, Simon Duquennoy, Adam Dunkels</div>Munihttp://anrg.usc.edu/contiki/index.php?title=REST_example_running_on_Cooja_and_Sky_motes&diff=1002REST example running on Cooja and Sky motes2014-11-08T02:56:28Z<p>Muni: Created page with "==Introduction== REST (Representational State Transfer) is an architectural style consisting of coordinated set of architectural constraints applied to components, connectors..."</p>
<hr />
<div>==Introduction==<br />
<br />
REST (Representational State Transfer) is an architectural style consisting of coordinated set of architectural constraints applied to components, connectors and data elements within a distributed hypermedia system. The REST architectural style is applied to development of web services. A web service can be characterized as RESTful if they conform to architectural constraints like client-server, stateless, cacheable, layered system and uniform Interface. A REST architecture style is one in which web services are viewed as resources and can be uniquely identified by their URLs. RESTful architecture can be run over HTTP or COAP. The basic REST design principle uses the HTTP protocol methods for typical CRUD operations:<br />
* POST - Create a resource<br />
* GET - Retrieve a resource<br />
* PUT – Update a resource<br />
* DELETE - Delete a resource<br />
Each resource has a handler function which the REST layer calls to serve the request by the client.<br />
<br />
==You Will Learn==<br />
<br />
How to use REST layer to develop server-side applications like COAP or HTTP and run it on COOJA and using T-mote sky.<br />
<br />
==Relavant Directories==<br />
<br />
* /contiki-2.7/apps/rest-common - It contains the codes that are mentioned as target in the rest-example.<br />
* /contiki-2.7/apps/rest-coap - It contains codes for codes mentioned as target in rest-common. <br />
* /contiki-2.7/apps/rest-http - It contains codes for codes mentioned as target in rest-common.<br />
* /contiki-2.7/examples/rest-example - It has the main example of the REST server side and COAP client side codes that are to be uploaded on COOJA and real motes.<br />
<br />
==Understanding The Code==<br />
<br />
===rest-server-example.c===<br />
<br />
The REST server has light, led, toggle, helloworld and discover resources. Each resource has a resource handler function which is called when the client requests for that resource and the resource handler function sends the response back to the client. For example, when a HTTP client sends a request to the REST server for light resource, the REST server calls the light_handler function which reads the light sensor values from the mote and sends the light sensor values along with a simple etag back to the client as shown below. <br />
<br />
[[File:a3handler.png]]<br />
<br />
===coap-client-example.c===<br />
The COAP client establishes a connection with the server on the COAP port 61616 and sets the et timer to a particular value. Everytime the et timer is expired, the send_data(void) function is called. When it recieves the response from the server for its request, the handle_incoming_data() function is called as shown in the code snippet below. <br />
<br />
[[File:a3timer.png]]<br />
<br />
The COAP client runs a timer which when resets, the client randomly selects a service_id (resource) using random_rand() function and sends the request to the REST server as seen below in the send_data(void).<br />
<br />
[[File:a3senddata.png]]<br />
<br />
When the server response returns back to the client, it runs the handle_incoming_data() function which takes the packet, parses the message and prints the payload that is it receives from the response. It can be seen in the code below<br />
<br />
[[File:a3recieve.png]]<br />
<br />
==Run the REST-Example on COOJA==<br />
<br />
<br />
===HTTP Example===<br />
<br />
<br />
1. Open the Makefile in the /contiki-2.7/examples/rest-example folder. For HTTP server, make sure the WITH_COAP = 0 in the Makefile.<br />
<br />
<br />
2. Open the terminal. Go to the /contiki-2.7/examples/rest-example folder. Run the following command given below:<br />
<br />
<code><nowiki> make TARGET=cooja rest-server-example.csc </nowiki></code><br />
<br />
This will open a COOJA terminal and loads the rest-server-example code for simulation. <br />
<br />
<br />
3. Open another terminal. Go to the same directory and connect the COOJA simulation to the router.<br />
<br />
<code><nowiki> make connect-router-cooja </nowiki></code><br />
<br />
<br />
4. Start the Simulation on the COOJA and test the connectivity by pinging the server. The IP addresses of the servers are aaaa::0212:7402:0002:0202 and aaaa::0212:7403:0003:0303.<br />
<br />
<code><nowiki> ping6 aaaa::0212:7402:0002:0202 </nowiki></code><br />
<br />
<code><nowiki> ping6 aaaa::0212:7403:0003:0303 </nowiki></code><br />
<br />
<br />
5. Use curl as a HTTP client we interact with the COOJA motes that are running the REST code. The default HTTP client runs on 8080 port number.<br />
<br />
<code><nowiki> curl -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/helloworld #get helloworld plain text </nowiki></code><br />
<br />
<code><nowiki> curl -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/led?color=green -d mode=off -i #turn off the green led </nowiki></code><br />
<br />
<code><nowiki> curl -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/.well-known/core -i </nowiki></code><br />
<br />
<code><nowiki> curl -X POST -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/helloworld #method not allowed </nowiki></code><br />
<br />
<br />
===COAP Example===<br />
<br />
<br />
1. Open the Makefile in the /contiki-2.7/examples/rest-example folder. For HTTP server, make sure the WITH_COAP = 1 in the Makefile.<br />
<br />
<br />
2. Open the terminal. Go to the /contiki-2.7/examples/rest-example folder. Run the following command given below:<br />
<br />
<code><nowiki> make TARGET=cooja coap-client-server-example.csc </nowiki></code><br />
<br />
This opens the COOJA terminal which runs rest-server-example.c over COAP in one node having IP address aaaa::0212:7401:0001:0101 and coap-client-example.c in the another node having IP address aaaa::0212:7402:0002:0202. COAP uses 61616 port number as default. The client in the example periodically accesses the resources of the server and prints the payload which can be seen on the mote output. <br />
<br />
<br />
3.Start the Simulation and observe the mote output.<br />
<br />
<br />
==Run the REST-Example on T-mote Sky==<br />
<br />
<br />
1. Go to the /contiki-2.7/examples/rest-example folder. Connect the motes and program them with rest-server-example.<br />
<br />
<code><nowiki> make TARGET=sky rest-server-example.upload </nowiki></code><br />
<br />
<br />
2. Disconnect the motes and load one other mote with the RPL border router to connect to the rest-server.<br />
<br />
<code><nowiki> cd ../ipv6/rpl-border-router </nowiki></code><br />
<br />
<code><nowiki> make TARGET=sky border-router.upload </nowiki></code><br />
<br />
<br />
3. Connect the REST-server to the border router using tunslip6.<br />
<br />
<code><nowiki> make connect-router </nowiki></code><br />
<br />
<br />
4. Open new terminal window for each mote and execute the following command and reset the motes. The IP addresses of these motes get printed after the motes are reset.<br />
<br />
<code><nowiki> make login TARGET=sky MOTE=2 #Shows the prints for first mote </nowiki></code><br />
<br />
<code><nowiki> make login TARGET=sky MOTE=3 #For second mote and so on. </nowiki></code><br />
<br />
<br />
5. Ping the motes using the IP addresses we get from step 4.<br />
<br />
<code><nowiki> ping6 <IPv6 Address of the MOTE> </nowiki></code><br />
<br />
<br />
6. If WITH_COAP = 0 i.e. HTTP Server, use the HTTP Client to connect to the REST server. When WITH_COAP = 1. i.e. COAP Server, load the coap-client-example.c to the motes and connect to the REST server (same as COOJA Example).<br />
<br />
<br />
==General Issues You Might Face==<br />
<br />
<br />
1. To use curl as the HTTP client you need to install curl by running the following command in the terminal:<br />
<br />
<code><nowiki> sudo apt-get install curl </nowiki></code><br />
<br />
2. The memory on the Tmote sky is not big enough to fit in the HTTP example on REST server. Thus while running it on real motes we get the error “region text is full”. To overcome this problem, instead of RPL to decide the route, we can use static routes. We can also reduce the size by changing the Makefile of the border router by setting the WITH_WEBSERVER = 0. <br />
<br />
3. Generally the COAP Client is used instead of HTTP client for running over REST Server.<br />
<br />
<br />
==References==<br />
<br />
<br />
1. REST-Example Github page: https://github.com/contiki-os/contiki/tree/master/examples/rest-example<br />
<br />
2. A Low-Power CoAP for Contiki - Matthias Kovatsch, Simon Duquennoy, Adam Dunkels</div>Munihttp://anrg.usc.edu/contiki/index.php?title=File:A3handler.png&diff=1001File:A3handler.png2014-11-08T02:30:34Z<p>Muni: </p>
<hr />
<div></div>Munihttp://anrg.usc.edu/contiki/index.php?title=Contiki_tutorials&diff=458Contiki tutorials2014-10-24T22:18:06Z<p>Muni: </p>
<hr />
<div>[[Main_Page | Back to Main Page]]<br />
<br />
== List of Tutorials ==<br />
<br />
Completed<br />
# [[Installation]]<br />
# [[Hello World]]<br />
# [[Broadcast Example]]<br />
# [[Collect View]]<br />
# [[Contiki build system]]<br />
# [[Interfacing with Python]]<br />
# [[Sensor acquisition]] (light, temperature, humidity)<br />
<br />
Need review<br />
# [[Timers]] Tim, Leo<br />
# [[CFS-Coffee]] Kevin<br />
<br />
Starting<br />
# [[Tutornet]] Pedro, Kwame<br />
# [[Cooja Simulator]] (Getting started, debugging) Pedro<br />
# [[Protocols stack]]<br />
# [[CSMA]] Tim, Leo<br />
# [[RSS measurement]]<br />
# [[RPL objective function modification and simulation in cooja ]] Ashwini Telang<br />
# [[RPL UDP]] Jiahao Liang<br />
# [[MAC protocols in ContikiOS]] Pedro<br />
# [[RPL Border Router]] Chhavi<br />
# [[REST example running on Cooja and Sky motes]] Mrunal Muni <br />
<!--[[Processes]] Yash --><br />
<br />
<pre style="color: red">Be sure to include references in your tutorials, especially if you quote material from other sites!</pre></div>Munihttp://anrg.usc.edu/contiki/index.php?title=Contiki_tutorials&diff=457Contiki tutorials2014-10-24T22:17:34Z<p>Muni: </p>
<hr />
<div>[[Main_Page | Back to Main Page]]<br />
<br />
== List of Tutorials ==<br />
<br />
Completed<br />
# [[Installation]]<br />
# [[Hello World]]<br />
# [[Broadcast Example]]<br />
# [[Collect View]]<br />
# [[Contiki build system]]<br />
# [[Interfacing with Python]]<br />
# [[Sensor acquisition]] (light, temperature, humidity)<br />
<br />
Need review<br />
# [[Timers]] Tim, Leo<br />
# [[CFS-Coffee]] Kevin<br />
<br />
Starting<br />
# [[Tutornet]] Pedro, Kwame<br />
# [[Cooja Simulator]] (Getting started, debugging) Pedro<br />
# [[Protocols stack]]<br />
# [[CSMA]] Tim, Leo<br />
# [[RSS measurement]]<br />
# [[RPL objective function modification and simulation in cooja ]] Ashwini Telang<br />
# [[RPL UDP]] Jiahao Liang<br />
# [[MAC protocols in ContikiOS]] Pedro<br />
# [[RPL Border Router]] Chhavi<br />
# [[REST example running on Cooja and Sky motes]] Mrunal <br />
<!--[[Processes]] Yash --><br />
<br />
<pre style="color: red">Be sure to include references in your tutorials, especially if you quote material from other sites!</pre></div>Muni