guitarchanceΛογισμικό & κατασκευή λογ/κού

15 Αυγ 2012 (πριν από 4 χρόνια και 8 μήνες)

162 εμφανίσεις

Lab 3: SPOT Radio Communication

Background Reading:

SunSPOT Developer's Guide, pages 25

Before starting this lab, you should look over these pages of the
SPOT developer's guide which briefly cover the provided SPOT device libraries. Some of this
can be skimmed;
the idea is for you to get a feel for the SPOT API that you will be using to develop applications, and hopefully
pick up ways to do some of the most common tasks. Do pay close attention to the section on the radio
communication library, st
arting on page 29, though, as that is what this lab is concerned with.

Socket Programming Introduction:
. If y
ou haven't encountered Java socket programming before, it would be a good idea to go
through this tutorial that introduces the concepts and relevant parts of the Java API. The JavaWorld tutorial,
though good, only covers TCP sockets. For a brief coverage

of UDP socket programming in Java, check out these

Prelab Question:

What is the differ
ence between the Radiogram and the Radiostream protocols provided by
Sun for SPOT applications? Which standard networking protocol do each of these resemble?


Now that you are familiar with the SPOTs and know how to put applications on
them and start a shared
basestation, it is time to start developing applications of our own. For this exercise, we are concentrating on
learning about radio communication, which is how SPOTs talk to each other (and the basestation), and an
essential part
of almost every sensor network application. .

Radio Count: An Example of Using Radio Communication to Exchange Data
Between SPOTs.

For this lab, we are going to develop an application to be run on two SPOTs where each SPOT sends out
a number and the

other SPOT uses its LED's to display the number it receives. Each SPOT will constantly be
counting up, so you should see the displayed number (i.e. The color and LED's that you see illuminated)
changing. We will not be starting from scratch; instead, w
e will start with the RadioStrength demo provided by
Sun. When the RadioStrength demo is placed on two SPOTs, the LED's on each SPOT indicate the strength of
the signal that that SPOT is receiving.


Start new project.

Since we are starting from the Radio
Strength demo, we will just copy that project
and then modify the source file.
Open the RadioStrength project
, which is found in (no surprise) the
Demos folder.
Right click on the project and select “copy project.”

I recommend naming the new
project Rad
ioCount, but you are welcome to choose another name if you so desire.


Examine the code.

Open up the source file for the RadioStrength project in Netbeans, Each node has two primary tasks: it sends out a signal, and receives signals f
over the air, then using the LED's to display the strength of the received signal. In the program we are
creating, each SPOT performs two similar tasks: it counts up then sends out the current number, and
receives a number and then displays it (the o
ne received, not its own). Knowing this, identify the
methods that RadioStrength uses for each of its two main tasks, and postulate which will be used for
which task in RadioCount (Question 3.1).


3.1: What are the names of the methods that wil
l continuously perform the send and receive tasks in
RadioStrength, and which do you think will be rewritten to count/send, and which to receive/display
the number?


Replace the transmit loop.

In the method that represents the transmit loop (should be obvi
ous…) we
will count up and then send the number over the radio. The communication is achieved through the
use of the Radiogram protocol, which should seem clearly similar to UDP sockets in Java with
Datagrams. First a connection is opened with Connector.
open() and assigned to a DatagramConnection
object. Then the Datagram to be sent is constructed (in this case to contain the number that we have
counted to so far), and the send() method is invoked on our DatagramConnection instance to send the
Datagram o
ver the radio to be picked up by another SPOT. This is the code that should replace the
main “while(xmitDo)” loop in the transmit method for our RadioCount program:

while (xmitDo) {

try {

txConn = (DatagramConnection)"radiogram://broadcast:" +

Datagram xdg = txConn.newDatagram(txConn.getMaximumLength());

while (xmitDo) {

for(int i=0; i < 12; i++) {

for (int coun
t=0; count < 256; count++) {





pause(1000); // s
ome pause is needed, otherwise LED's won't be able to keep up




} catch (IOException ex) {

} finally {

if (txConn != null) {

try {


} catch (IOException ex) { }





Replace the receive loop

Now it’s time to replace the RadioStrength receive loop with one of our own.
The opening and

instantiating of the connection and the creation of a Radiogram object are similar to
the transmit loop. You will notice, however, that now our connection is declared specifically a
RadiogramConnection instead of just a DatagramConnection and that we are

dealing with Radiograms
in particular and not just any old Datagrams. Instead of sending, here we receive a Radiogram from our
connection and then read the number from it (notice how closely using Radiogram connections
resembles dealing with other Stream

types in Java, which include sockets) that we then send on to the
DisplayNumber() method to be displayed on the LED’s. Here is the code for the receive loop:

while (recvDo) {

try {

rcvConn = (RadiogramConnection)Connecto"radiogram://:" +

Radiogram rdg = (Radiogram)rcvConn.newDatagram(rcvConn.getMaximumLength());

while (recvDo) {



int num = rdg.readInt();

int index = rdg.readInt();

displayNumber(num, colorList[index]);


} catch (IOException ex) {

} finally {

if (rcvConn != null) {

try {


} catch (IOException ex) {}





Build and deploy RadioCount.

Once you’ve changed the code, go ahead and build the project, de
onto your two SPOTs, and watch the Radio Count program in action. You've just successfully created
and deployed your first Sun SPOT application of your own (though you were given the code)!

Now that you've got RadioCount running with two

motes counting for each other, what can you
do to personalize the behavior of your SPOTs? If other groups near you are at this stage as well, how can you
tell from whom each SPOT is receiving the number that they are displaying? Try to come up with a wa
y to be
sure. You could, for example, include something to identify SPOTs running your application in each Radiogram
sent out and then only display a number if the Radiogram that is received contains that identifier as well as the
counted number. Other i
deas include having your SPOTs always send a certain number or only numbers that
conform to a special pattern, and having your SPOT count down instead of up (which is really just sending
numbers that follow a different pattern).

: The goal of

this exercise and the RadioCount application was for you to gain confidence about
writing your own programs with the help of the demos and code pieces provided with the SPOT SDK and to
introduce you to using the SPOT radio communication library. When the

time comes to create your own
applications from scratch, you will be able to look back on this exercise to remember how to handle
communication between motes. Hopefully it will serve as a stepping stone to bigger and better things.