Java Solaris Communities Partners My Sun Sun Store United States Worldwide
»   Welcome and Introduction
»  A Short Tour of a Sun SPOT
»   The Ectoplasmic Bouncing Ball Demo
» Loading and Running the Air Text Demo
»   Changing the Air Text Demo
» Basestation in Action
    Send-Data and HTTP Demos
   
 

Sun SPOT Quick Start Tutorial

Send-Data and HTTP Demos

 
 

In this module you see how a Sun SPOT can communicate sensor information by radio to an application running on your personal computer or laptop (SendDataDemo) and also how a Sun SPOT can interact over the Internet with a website (HTTPDemo).

Finding the Send-Data and HTTP Demos

The SendDataDemo and HTTPDemo applications are located under Sun/SunSPOT/sdk/Demos.

SendDataDemo transmits periodic sensor readings measured on Sun SPOTs, either physical or emulated, to an application on your computer that displays transmitted values. SendDataDemo consists of three parts. The first is mandatory. The second and third are interchangeable.

  • SendDataDemo-onSPOT – periodically samples the built-in light sensor on a Sun SPOT, physical or emulated, and broadcasts sensor values over the radio.

  • SendDataDemo-onDesktop – runs on a host computer, listening for sensor-sample broadcasts, and prints received values.

  • SendDataDemo-GUIonDesktop – runs on a host computer, listening for sensor-sample broadcasts, and displays them graphically.

HTTPDemo sends predetermined messages to the Twitter.com website.

Running the SendDataDemo

First prepare a Sun SPOT to report its light sensor values by building and running the SendDataDemo:

  1. Connect a Sun SPOT to your host computer with a USB cable.

  2. From a command window in the sdk/Demos/SendDataDemo/SendDataDemo-onSPOT directory, run the following command. This compiles the sensor sampling application and loads it into the Sun SPOT:

    ant deploy

  3. When deployment is complete, disconnect the Sun SPOT and reset it by pressing the control button on the end. After the SPOT reboots, it enters a loop where it reads the light sensor, broadcasts that reading, and goes to sleep for ten seconds before the next sample. The Sun SPOT flashes its rightmost LED to visually indicate a sampling event. You notice the SPOT going into deep sleep between consecutive samples if it is not running other threads that use the CPU, radio, or other resources.

Now launch the host applications to print and plot light sensor readings from the Sun SPOT:

  1. From another command window, in the sdk/Demos/SendDataDemo/SendDataDemo-onDesktop directory, run the following command:

    ant host-run

  2. After many lines of Ant messages, the host application connects through the basestation radio to the Sun SPOT, and prints the transmitted light sensor readings.

  3. Move the Sun SPOT around the room into dark and well-lighted areas and see how the printed values change.

  4. Quit SendDataDemo-onDesktop by typing Control-C, or the interrupt sequence for your computer.

  5. Change directory to sdk/Demos/SendDataDemo/SendDataDemo-GUIonDesktop and run the same command as before:

    ant host-run

  6. This time a graphical window appears. Wait for values to start displaying and move the Sun SPOT around the room again. Transmitted light sensor values are graphed against time.

You might be interested in modifying the program to sample and transmit temperature readings instead of, or in addition to, light sensor readings. Also you could increase the sleep period between samples (to preserve battery power), change the X axis intervals of SendDataDemo-GUIonDesktop, and graph the overnight readings.

Running the HTTP Demo

Twitter is the name of a short-message posting service on the Internet, and also the name of the website. HTTPDemo is a Sun SPOT application that sends a message to the Twitter website. The Sun SPOT is able to make HTTP connections on its own, using the socket protocol optionally provided by Java ME.

To compile and run the HTTP demo, follow these steps:

  1. Connect a Sun SPOT to your host computer with a USB cable.

  2. From a command window in the sdk/Demos/HTTPDemo directory, run the following command. This compiles the sensor sampling application and loads it into the Sun SPOT:

    ant deploy

  3. Disconnect the Sun SPOT, but do not reset it yet.

  4. Attach the Sun SPOT basestation to your host with a USB cable. HTTP networking requires the use of a basestation and special host application called the socket-proxy that acts as a relay between a TCP/IP connection (with Twitter.com) and a radio-stream connection to the Sun SPOT. Start the socket-proxy application:

    ant socket-proxy-gui

  5. Wait for a GUI window to open on your host computer. Check the box labelled I/O to enable logging of the data sent over the HTTP connection. Press the Start button to start the socket-proxy.

  6. Now reset the Sun SPOT used in Steps 1-3 by pressing the control button on its end.

  7. Monitor the Proxy Log in the GUI window from Step 5. You see the Sun SPOT retrieving the microblog that "sunspotdemo" just posted. This is done by an HTTP GET request. The Sun SPOT blinks its leftmost LED green when a GET request is in progress. After the message has been downloaded, you can display it by gently moving the SPOT back and forth in a direction perpendicular to the LED array. You can verify that the correct message is displayed by pointing your browser or web phone to http://twitter.com/sunspotdemo.

  8. Now shake the Sun SPOT along its Z-axis. In other words, hold it flat and move it up and down with force. If you shake it vigorously enough, the Sun SPOT quickly blinks its the 5th LED (from the left) three times, and posts a microblog. The last part of the text is chosen randomly from a set of pre-configured strings. The message is posted using an HTTP POST request. The Sun SPOT blinks its rightmost LED green when a POST request is in progress.

  9. In your browser or web phone, refresh the website http://twitter.com/sunspotdemo and note the time and originating IP address of your new message.

Try changing the message that gets posted to Twitter.com. The message code is in the DropMonitor class (inside src/org/sunspotworld/demo/HTTPDemo.java). If you have a Twitter screen-name, add it to the property ScreenNamesToMonitor (inside resources/META-INF/MANIFEST.MF) so when you run the demo, the Sun SPOT can fetch and display your last microblog entry.

What to do Next?

There are a number of other examples of Sun SPOT applications included in the Demos directory. Some work well in the Emulator, while others work best when run on a physical Sun SPOT. See the README.txt file in each demo for information on what the demo does and how to run the demo.

AirText – Displays words in the air as you wave the Sun SPOT back and forth. Best on a physical Sun SPOT.

BounceDemo – Illuminates LEDs to simulate a bouncing ball. Best on a physical Sun SPOT.

BuiltInSensorsDemo – Represents movement, temperature, and light. Best on a physical Sun SPOT.

DatabaseDemo – Records environmental conditions in an SQL database, which you can later query for reports.

RadioStrength – Displays strength of radio signals on the LEDs, useful for understanding how distance and transmit power affect the radio signal strength.

TelemetryDemo – Provides a framework for collecting data on a remote Sun SPOT and passing it over the radio to a host application for recording and display.

CodeSamples – This directory contains simple examples of how to use the radio and some of the basic Sun SPOT sensors, to help people learn how to program their Sun SPOTs.

  • AccelerometerSampleCode – Simple use of the accelerometer to measure tilt of the Sun SPOT.
  • GPIOToneGeneratorSampleCode – Illustrates use of the GPIO pin coupled with special driver software to implement a tone generator. Best on a physical Sun SPOT.
  • LEDSampleCode – Simple LED demo to show how to blink an LED on and off, move the lit LED from left to right, and pulse an LED from dim to bright.
  • LibraryExtensionSampleCode – This code illustrates how to add user code to the Sun SPOT library.
  • LightSensorSampleCode –
  • RadioBroadcastSampleCode – This simple demo shows how to use the radio to broadcast data to any listening Sun SPOT(s).
  • RadioInputStreamSampleCode – This example shows how to use the basic radio functionality.
  • SunSpotApplicationTemplate – Template project for a Sun SPOT application.
  • SunSpotHostApplicationTemplate – Template project for a Sun SPOT host application.
  • SwitchesSampleCode – This application illustrates both a simple use of waiting for a switch to change state, and a call back style of using the switches, as in the listener-notifier pattern used for conventional UI devices like the mouse buttons.
  • TemperatureSensorSampleCode – Illustrates use of the on board temperature Sensor by printing out the temperature level reading in various units.

Next: Sun SPOT Emulator Software Tutorial and Sun SPOT Resources on the Web.