Exercise 4: LEDs as a web resourceExpected time: 15 minutes
Learning goals of this exercise
Create a new web application that exposes the LEDs as a web resource such that an HTTP GET to its URL returns the LED color setting and a PUT changes the LED color setting. A more detailed description of the service is here.
- Close any open files from previous exercises to avoid confusion and open HOLLEDController.java and WoTServer.java in Exercise4. Make Exercise4 your main project.
- HOLLEDController.java: Initialize myLEDs by doing a resource lookup (as shown in Flashlight.java from Exercise1) and modify the processRequest method to respond properly to GET and PUT requests.
- WoTServer.java: In startApp(), add another call to nas.registerApp registering an instance of HOLLEDController to handle the URL "/leds".
- 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 Exercise4 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 access and modify the LED settings on your SPOT at the URL http://<address>:<port>/spot-xxxx/leds using any HTTP client, e.g. Firefox or curl.
- Access LED setting
% curl --request GET "http://<address>:<port>/spot-xxxx/leds"
- Change the LED setting to yellow
% curl --request PUT --data "[255,255,0]" "http://<address>:<port>/spot-xxxx/leds"
- Besides using HTTP PUTs, you can also change the LED setting on the SPOT by pressing switch 2 to cycle through a small set of preselected colors.
- The graphic for your SPOT on the dashboard will start showing the LED setting in addition to battery status information, the SPOT's name and recent light readings.
This exercise showed how to expose the Sun SPOT led array as a web resource that responds to HTTP GETs and PUTs. Next, we'll add authentication to the PUTs so only authorized users can change the LED setting.