Exercise 3: Light sensor as a web resourceExpected time: 10 minutes
Learning goals of this exercise
Create a new web application that exposes the light sensor as a URL such that HTTP GETs to this URL return the built-in light sensor reading as a single integer. A more detailed description of the service is here.
- Close any open files from Exercise2 to avoid confusion and open HOLLightSensor.java and WoTServer.java in Exercise3. Make Exercise3 your main project.
- HOLLightSensor.java: Initialize lighSensor by doing a resource lookup (as shown in Flashlight.java from Exercise1) and modify the processRequest method to return the light sensor reading in respStr (again, refer to Flashlight.java for an example).
- WoTServer.java: In startApp(), add another call to nas.registerApp registering an instance of HOLLightSensor to handle the URL "/light".
- Connect your SPOT to the USB port (if it isn't already) and stop any previously running applications by resetting the Sun SPOT (don't power it off). Build, deploy and run Exercise3 on to the SPOT. In NetBeans, you can accomplish this by clicking the right button on the project name to bring up a context-menu and selecting "Run".
- After some time, the SPOT will discover the gateway machine and register itself with it.
At that point, you should be able to get light sensor readings from your SPOT at the URL http://<address>:<port>/spot-xxxx/light using any HTTP client, e.g. Firefox or curl.
% curl --request GET "http://<address>:<port>/spot-xxxx/light"
- The graphic for your SPOT on the dashboard will start showing recent light readings in addition to battery status information and the SPOT's name.
This exercise showed how to expose the Sun SPOT light sensor as a web resource that responds to HTTP GETs. Next, we'll create a service that responds to both GETs and PUTs.