doc

looneyvillestaticSoftware and s/w Development

Aug 15, 2012 (4 years and 8 months ago)

249 views

Lab 1
a
: Becoming familiar with the Java
-
programmable
Sun SPOT technology.

Introduction:



The goal of this lab is to introduce you to Sun’s new Sun SPOT (which stands for Small
Programmable Object Technology) wireless sensor network technology. Unlike

some common

motes
that run TinyOS and require applications to be
written in NesC, Sun’s new sensor network devices

are
programmed in Java.

For people with experience with
Java, learning to program applications for the Sun
SPOTs should be relatively easy.

Sun makes use of familiar techniques in the design of the SPOT API ;
communication between motes, for example, should be reasonably intuitive for anyone that has used
Java sockets before. The Sun SPOT software comes with a number of fun example applica
tions that
show off some of what the motes are capable of. We will explore a couple of these, learning how to
deploy applications on the SPOTs as we go, and ultimately start looking at the code to see how the
programs work.

NOTE:

The
s
e

lab
s assume

access

to a computer with the SPOT SDK pre
-
installed and configured, as well
as the
NetBeans

IDE with the SPOT modules working. If you are in Dr. Bulusu’s Sensor Network class at
PSU this should be the case.

This has been updated for use with Blue SPOT SDK and

NetBeans

6.5.

Let’s get started.

1.

Check your equipment
.

You should have been provided with a Sun SPOT developme
nt kit. This
includes two free
-
range SPOTs

and a basestation. You can tell the basestation apart from the
free
-
range SPOTs by the fact that it

does not have a detachable semi
-
transparent plastic cover
on top and is not as tall off the table

(a basestation is basically just a free
-
range SPOT without a
batt
ery or

sensor board; a free
-
range SPOT can be used as a basestation if so desired
)
. You wil
l
also need

at least one USB cable to connect

your SPOTs
to the computer.


2.

Open
NetBeans
.

Sun has made programming and deploying applications to the SPOTs
extremely simple by incorpora
ting the functionality into
NetBeans, the Integrated Development
Enviro
nment (IDE) that Sun offers with their free version of the Java
Development Kit (JDK). The
NetBeans modules that allow fo
r this come with the Sun SPOT SDK
. In this lab, we will be
learning how to
work with the SPOTs through
NetBeans. You may already hav
e another favorite
IDE of your o
wn, or even already dislike
NetBeans for whatever reason, however we will make
use of it in these labs to take advantage of the ease of developing and deploying Sun

SPOT
applications that it provides.

a. Double
-
click the
N
etBeans icon on your desktop to start it.


3.

“Short tour of a Sun SPOT”
.
When you first open
NetBeans, you should see a tab open called

Sun SPOTs I
nfo”, with a heading that reads:
“Welcome to the World of Sun Small
Programmable Objec
t Technology
-

Sun SPO
Ts.


If you don’t see this tab open,
you can bring it up by clicking the icon that looks like a SPOT (fifth from the left) at the top of
NetBeans

directly below the menu bar. A

little farther down the page

(from the heading)
, you
will see a link to the
S
un SPOT Quick Start Tutorial
. Click this.
You will be taken to a page that
reads: “Before You Begin Tutorials.”
You don’t need to read this, just continue on by following a
link to the Quick Start Tutorial.
From there click on either the link at the en
d of the introduction
or on the sidebar to the “short tour of a Sun SPOT.” Quickly read over this page. This will get
your familiar with the basics of the SPOTs. Try taking the lids off and locating the switches next
to the LED’s (under the lid) and the

control (power) button. The tour will teach you about
turning a SPOT on, how to reset a SPOT, and how to interpret the power/activity LED.


The Ectoplasmic Bouncing Ball Demo


Now that you’ve got the basics, let’s see what these things can do. The SPO
Ts come preloaded with a
very cool demo that utilizes the on
-
board three
-
axis ac
celerometer, one of several
sensors that the free
-
range SPOTs are equipped with, and the eight tri
-
color LED’s under the lid. This demo is also covered in
the next section of
the Quick Start Tutorial. You are welcome to refer the tutorial in addition to

or
instead of

the steps below (if anything is unclear, the tutorial has pictures).

1.

Turn the SPOT on.

Most likely in the last step you pressed the power button on one of the
mo
tes at some point and noticed a few of the LED’s on top (there are eight total) light up. If not,
turn one of the free
-
range SPOTs on now. The two lights on the ends will be red, and there will
be a blue

(possibly green or red instead) light

somewhere tha
t acts like a bouncing ba
ll if you tilt
or shake the SPOT

back and forth.
NOTE: if your SPOT doesn’t respond and refuses to power
-
up, it may not be charged. In this case, request a new kit (you can also use the USB cable to
plug it into the computer and c
harge it, but it may take a minute or two before it will have
enough power for the demo to run)


2.

Play with the bouncing ball.

Take a moment to get a feel for the bouncing ball application.
Shake the SPOT furiously to get the ball bouncing between the two

ends at lightning speed and
then hold it still and wat
ch the ball gradually lose velocity

and settle back down to a final resting
place. When the ball is on one end, try tilting the mote gradually just until the ball falls and
watch it bounce back a few
times (most likely ending up on the other end; though sometimes if
you just barely tilt it, the ball will go back to where it started). Try to get the ball to stop in the
very middle (without spending too much time trying). Once you’ve had enough fun, g
o on to
the next step.


3.

Introduce the second SPOT
.

If you haven’t already, start up the second free
-
range SPOT, which
should also have the b
ouncing ball application loaded
. This time you will see a ball that is green.
Now turn both motes off (hold the c
ontrol button for a couple seconds; if you just press it
without holding, it will reset and not shut down) and then turn the one that previously had the
green ball back on again. Notice that it will now have a blue ball. This means that the SPOTs,
true t
o the spirit of wireless sensor networks, are aware of each others’ presence.
NOTE: if you
are doing this lab in close proximity to other groups, the colors may not match what is
described here since all the motes will be communicating and trying to assig
n themselves a
unique color at startup. There is a maximum of three motes (and thus three unique colors) per
self
-
assigning group, though.


4.

Break down the walls.

Make sure both your free
-
range SPOT
s are on. Now go ahead and take
both the lids off. If yo
u were wondering earlier what the control switches do, the answer is: it
depends on what the application developer wants them to. It just so happens that the
developers at Sun were nice enough to make
the
bouncing ball demo even cooler by letting us
use t
he control switches to share multiple balls in one arena that spans more than one SPOT. If
that wording is confusing to you, try it yourself and see what happens.


a.

Press one of the control switches. The red light on that end of the mote will start to
b
link. While that light is still blinking, press a switch on another mote. The two
“walls” should disappear. Now you can have both balls going back and forth
between SPOTs! Unless you’re really good, the balls will probably quickly combine
into one. Yo
u can even put the walls back up and isolate both balls o
n one SPOT
, or
take all the walls out altogether.


b.

Play with this for awhile, taking walls down and passing balls back and forth
between multiple motes.

Conclusion:

The point of the lab so far was

to get you familiar with the basics and layout of the Sun
SPOTs and hopefully get you excited about working with them by giving you a glimpse at some of the
fun, cool things that can be done with them.





Lab 1b
: Application Deployment and the SPOT Base
station

Learning to Deploy Applications: The Telemetry Demo


Now that we’ve played around with the application that comes on the SPOTs, let’s see how to put
something else on there. Sun's “Quick Start Tutorial” makes use of the AirText demo to demonstr
ate
deployment. We have chosen, however, to use a different demo, called Telemetry, for this purpose.
You will learn not only how to build and deploy applications with
NetBeans
, but you will get to see an
example where the basestation relays data from th
e free
-
range SPOTs to an application running on a
PC. The Telemetry program receives data from a SPOT's accelerometer and reports it in a graph with
some adjustable parameters.

1.

Open, build, and deploy.


Changing the current application on your Sun SPOTs

couldn’t be
much easier. First we
will install the application on

the free
-
range SPOT. Next, we will build
and run a Sun SPOT host Java desktop
program on the PC that reports sensor data returned
from the SPOT. All you have to do is open a project, cle
an and build it, and deploy it on the
SPOT.

a.

Open.


The project we are going to open and then put on the SPOTs next is called
Telemetry
-
OnSPOT
. To open this project, you can
either click the link on the “Sun
SPOTs info” tab

(if you closed this already, it
can be reopened by clicking the icon
that looks like a SPOT on the toolbar just beneath the menus and about five icons
from the left),
or go to File
-
>Open Project

and at the open file dialog select the
Telemetry
-
OnSPOT project folder from inside the Teleme
tryDemo folder which is in
the Demos directory. If you get lost, the absolute directory (on Windows) should be

C:
\
Program Files
\
Sun
\
Sun SPOT
\
Demos
\
TelemetryDemo
\
Telemetry
-
OnSPOT. Make
sure yo
u have the “open as main project” box checked.

b.

Build.

You’ll notice that Telemetry
-
OnSPOT now appears in the Project Explorer on
the left
-
side of
NetBeans
. You can expand the project and view the source files that
the application consists of in the Pro
ject Explorer if you’d like.
Right click on the
Telemetry
-
OnSPOT project (the root of the tree if you did any expanding) and
select “Clean and Build Project.”


This command can also be run from the “Build”
menu or with shift+F11. You will notice that out
put from the build goes in the box
that takes up approximately the lower fourth of the
NetBeans

window. The last line
of output from the build should be

“BUILD SUCCESSFUL”

followed by how long the
operation took.

c.

Deploy.


Now that the application is bui
lt, it’s time to put it on a SPOT! First make
sure you have a SPOT plugged into the USB cable and that the cable is connected to
the computer. Then,
right click on the Telemetry
-
OnSPOT project and select
“Deploy to Sun SPOT.”

After a few seconds and sev
eral lines of output, the output
should once again conclude with
“BUILD SUCCESSFUL.”

If
NetBeans

appears to
pause in the middle of the deployment, tap the reset button on the SPOT you are
deploying to, and the operation should immediately continue. Your S
POT is now
running the free
-
range portion of the Telemetry demo.
Note: When you first
connect a SPOT to the PC, Windows may bring up the “Found New Hardware”
dialog. This is for the installation of the Sun

SPOT USB drivers. You should be
okay just clicki
ng “next” several times until it finishes (it doesn't matter what you
select for the first choice) and then telling it to “continue anyway” when it
complains that the driver is not windows
-
certified. You may have to do this again
later when Windows encoun
ters a new SPOT it has not seen before.


2.

Set up the basestation.

By now you should have one SPOT successfully running the
Telemetry
-
OnSPOT application. You will see the left
-
most LED lit up red, and the one next to
it will flash yellow occasionally. Nex
t, you need to get the basestation set up so that when
you run the OnDesktop Telemetry application, it will be able to receive sensor data from the
free
-
range SPOT you just deployed

onto. A very convenient tool called the

SPOT Manager
and allows you to ea
sily manage your SDK installation and the state that your SPOTs are in
without having to use the command line. The file to open the SPOT Manager should be on
your desktop.
Go to your desktop and double
-
click on “SPOTManager.jnlp.”

You should
see a Java p
rogram launch. If you haven't already,
plug

the USB cable

into the basestation

to connect it to the PC. At the top of the “Sun SPOTs” tab of the SPOT Manager you will see
“Select a SPOT” and then a pull
-
down list that should display the IEEE address of
the your
basestation. If you don’t see anything in that box, hit “refresh.” Now we need to get your
basestation running as a shared basestation, which is the mode necessary to interface a
SPOT host a
pplication with free
-
range SPOTs. To do this,
click on
the basestation button
towards the bottom of the “Sun SPOTs” tab and select “start shared basestation.”
You
should see a second window pop
-
up with basestation output. If everything went right, the
last line of output you will see in this new window is “[
TraceRouteServer]starting.” This will
stay open until you stop running a shared basestation.


NOTE: If when you attempt to start
a shared basestation an exception is reported in the window that appears (or
you see
an
y
other incorrect output
), it may be n
ecessary to open a terminal window (command
prompt on Windows), navigate to any SPOT project folder, and run the command “ant
selectbasestation run”.

After running this command, you will need to ctrl+c out and then
try starting a shared basestation again.



3.

Build and run the OnDesktop project.

It is time to build and run the desktop Java
application. By now, you should be able to figure out how to do this.
Open the Telemetry
-
OnDesktop project, make sure it is set as your
main

project, right click on the
project and
select “Clean and Build Project,” and, assuming that your build was successful, right click
on the project again and choose “Run Project.”

A window should appear that looks like a
graph with some buttons and check
-
boxes at the bottom. You wi
ll also notice that the red
light on your free
-
range SPOT is now green, indicating that it is talking to the desktop
application and will be the source of accelerometer data once you tell it to “collect data.”
Congratulations! You have successfully deploy
ed both parts of your first SPOT/Desktop
application.


4.

Test the Telemetry demo
.

Now that you have the Telemetry demo on a SPOT and running
on your PC, let’s see what it does. Hit the “collect data” button at the bottom of the
desktop program and watch a
s the data from the SPOT's accelerometer is reported on the
graph. Play with the Telemetry demo for a while if you like, trying different things and
exploring the functionality provided. Pretty cool, huh?

Over the Air Deployment


So far you’ve been int
roduced to the standard way of deploying applications. It is also possible to
deploy a new application to a remote SPOT by using over
-
the
-
air deployment. We will now briefly
describe how to do this. Which application you deploy is not important, however
, the AirText demo
would be a good choice. With this demo, a SPOT appears to write a message in the air when vigorously
shaken back and forth in a relatively dark room.

NOTE:

OTA Deployment is also covered in Sun’s Quick
Start Tutorial under the “The Base
station in Action” section; this may be useful to refer to if any
problems arise.

1.

Enable
the
OTA

command server
.

First select a free
-
range SPOT that you wish to deploy the
new application to. Connect this to the computer with the USB cable. Open the SPO
T
Manager (or, if already open, hit refresh) and
note the SPOT’s IEEE address

(starts with
0014.4F01). The latter two 4
-
digit parts of the IEEE address can also be found under the lip
of the SPOT. You will need to know this address for the deployment.
C
lick on the OTA
Command Server button on the Sun SPOTs tab of the SPOT Manager and select “Enable
OTA Command Server”
.
This will allow you to deploy a new application to this SPOT using
OTA deployment.

2.

Build project
.
In
NetBeans
, open the application yo
u want to deploy (recommended is
AirText found in the Demos folder).

Once opened, right
-
click on the project name and select
“clean and build”. This will ensure the application has been built and is ready to be
deployed.

3.

Connect basestation and deploy
.

Now we are ready to deploy the project over
-
the
-
air
using the basestation.
Connect the basestation

to the computer with the USB cable (the
free
-
range SPOT should no longer be connected).
Open a terminal or command prompt
and navigate to the project folde
r of the application you just built

(on a typical Windows
install using the AirText demo, you would type “cd C:
\
Program
Files
\
Sun
\
SunSPOT
\
Demos
\
AirText” followed by a return). At this point you may want to
issue an “ant startbasestation” command to ensure

your basestation is in the correct mode,
though this is probably not necessary.
To deploy the application, type
:


“ant
-
DremoteId=0014.4F01.xxxx.xxxx deploy”

where xxxx.xxxx is the last part of the
remote SPOT’s IEEE addres
s, printed under the lip of the SPOT
. If all goes well, you will see
a progress indicator as the new application is sent to the remote SPOT and output that ends
with “BUILD SUCCESSFUL”. If you deployed the AirText demo, reset the remote SPOT and
start sw
inging it back and forth in the air; you should see a message start to appear (though
it may be hard to read at first as you adjust your swing rate).

Conclusion
:
After completing this section, you should know how to build and deploy projects to the
SPOTs,

both to connected SPOTs through
NetBeans

and with over
-
the
-
air deployment,
as well as how to
run
SPOT desktop host

applications in NetBeans
, and how to start and stop a shared basestation.
Hopefully by now are you excited about working with the Sun SPOTs

and can't wait to start writing your
own applications.


HOMEWORK:


While deploying the telemetry demo, you probably witnessed mayhem in class as people discovered
that their desktop host application was communicating with another group’s remote SPOT inst
ead of
their own
. Your assignment is to examine the code for the Telemetry demo and modify it so that your
desktop application only communicates with a specific remote SPOT. You may do this however you
like, however it can be done without changing the re
mote SPOT application at all (you need only
modify the OnDesktop part).