nes-proj/examples/rest-example/README

54 lines
3.0 KiB
Plaintext

Open a terminal and go to "examples/rest-example/" directory.
MAIN EXAMPLE:
rest-server-example.c : A RESTful server example showing how to use the REST layer to develop server-side applications (possible to run it over either COAP or HTTP)
To use COAP as the underlying application protocol, one should define WITH_COAP = 1 in rest-example/Makefile. Otherwise, HTTP is used.
Look at the source code to see which resources are available. (check the RESOURCE macros in the code).
Each resource has a handler function which is called by the REST layer to serve the request.
(i.e. "helloworld" resource has a handler function named "helloworld_handler" which is called when a web service request is received for "helloworld" resource.)
To run REST examples in COOJA under Linux
--------------------------------------------
Accessing the server from outside:
Start COOJA and load the simulation "rest-server-example.csc" by the following command.
> make TARGET=cooja rest-server-example.csc
After loading the cooja file, open another another terminal pointing to the same directory and connect to the COOJA simulation using tunslip6:
>make connect-router-cooja
Now you need to use a COAP or HTTP client to interact with the COOJA nodes running REST code.
In this setting, two servers are available:
IP addresses are aaaa::0212:7402:0002:0202 and aaaa::0212:7403:0003:0303. COAP uses 61616, whereas HTTP uses 8080 port in default configuration.
HTTP Examples
You can use curl as an http client to interact with the COOJA motes running REST code.
>curl -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/helloworld #get helloworld plain text
>curl -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/led?color=green -d mode=off -i #turn off the green led
>curl -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/.well-known/core -i
>curl -X POST -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/helloworld #method not allowed
Accessing the server inside the sensor network:
(Note: Provided only for COAP implementation)
Start COOJA and load the simulation "coap-client-server-example.csc" by the following command.
> make TARGET=cooja coap-client-server-example.csc
coap-client-server-example.csc : Runs rest-server-example.c as the server (over COAP) (IP:aaaa::0212:7401:0001:0101)
in one node and coap-client-example.c as the client (IP: aaaa::0212:7402:0002:0202) in another node.
Client periodically accesses resources of server and prints the payload.
To run REST server on real nodes under Linux
--------------------------------------------
1. Program the nodes with the rest-server-example
> make TARGET=sky rest-server-example.upload
2. Disconnect the nodes and program one node with the RPL border router
> (cd ../ipv6/rpl-border-router && make TARGET=sky border-router.upload)
3. Connect to the border router using tunslip6:
> make connect-router
4. Reconnect the motes, reboot them and note their IP addresses.
5. Remaining parts are the same with the COOJA example. (i.e. if it is a COAP Server, it is available at <NODE_IP_ADDR>:61616)