Lab Manual Pervasive Computing
Lab Manual Pervasive Computing
Lab Manual
SENG-621 Pervasive Computing
Lab 1
Lab Objective:
Go to project menu and give name to save cupcarbon project ( you can choose any directory to save
the projects)
2
Basic Examples 3
SENG – 621: Pervasive Computing
BS(SWE) Part-IV [Morning]
Example 1: Hello World
This example shows how a sensor can display a message “Hello World”. The following
steps show how to do this:
Step 1. Create a new project: this can be done either by clicking on the “New
project” icon of the toolbar or on the menu Project New project. Choose the
name (example: helloworld) and the place where you want to save your project.
Inside this folder, 1 file (helloworld.cup) and 8 other directories will be created.
The content of each directory is given in the following:
a. config: it contains the simulation parameters file, the building list file, the
marker list file and two other directories (sensor and sensor_radios) that
contains the list of sensor nodes (one file by sensor node) and the list of
the radio modules of each sensor.
b. gps: it contains the list of routes
c. logs: it contains the log file
d. results: it contains the simulation results (a csv file)
e. scripts: it contains the SenScript files of the project
f. netevents: it contains the natural event files
g. tmp, network: are used by the simulator
Step 2. Add a new sensor node on the map: click either on the Add Sensor icon of
the toolbar or from the menu bar (Add Add Sensor Node). Then, click on the
map where you want to add the sensor node. Another click will lead to another
new sensor node and so on. To stop adding sensor nodes, just click on the right
button of the mouse. You can also click on the icon of the toolbar, or by
typing on the escape [esc] button of the keyboard.
3
Basic Examples 4
SENG – 621: Pervasive Computing
BS(SWE) Part-IV [Morning]
Step 3. Open the SenScript Window: the SenScript window can be opened by clicking on the
icon of the toolbar or from the menu Simulation SenScript Window.
Step 4. Write the script: add the following script (1) in the text area part of the SenScript
window:
loop
print "Hello W orld" stop
Add the name hello of this script in the File name field (2), then click on the Save button (3) just in the
left part of this field. This will create a file hello.csc in the directory scripts.
Step 5. Assign the SenScript file to the sensor node: Select the sensor node on the map (1).
4
Basic Examples 5
SENG – 621: Pervasive Computing
BS(SWE) Part-IV [Morning]
Go to Device Parameters in the left part of the main window (2). Then, select the hello.csc
file in the field Script file (3). And then, click on the apply button just in the right (4).
Step 6. Run the simulation: For this example, there is no need to parameterize the simulation, just click on
the run simulation button in the toolbar or in the simulation parameters menu in the left.
5
Basic Examples 6
SENG – 621: Pervasive Computing
BS(SWE) Part-IV [Morning]
Step 7. Simulation results: in this example the simulation results shows a Hello World message displayed
by the sensor.
loop
set a 7
set b 8 set x a+b
print a "+" b "=" x
stop
The simulation result will display 7 + 8 = 15.
6
Basic Examples 7
SENG – 621: Pervasive Computing
BS(SWE) Part-IV [Morning]
Lab 2
Lab Objective
1. Getting familiar with if-else and for statements in cupcarbon simulator
2. Understanding use of mark, delay and blink functions
loop
mark 1
stop
The simulation result will shows a sensor marked with a yellow green center.
Then, use the same steps of Example 1, with the following script:
loop else
rand mark 0
x end
if(x<0.5 delay 1000
)
mark 1
7
Basic Examples 8
SENG – 621: Pervasive Computing
BS(SWE) Part-IV [Morning]
The simulation results show marked and unmarked sensor nodes that change the marking state during
the simulation.
8
SENG – 621: Pervasive Computing
Basic Examples 9
BS(SWE) Part-IV [Morning]
In this example, the simulation will stop only if it reaches the simulation time. To stop
manually the simulation, just click on the button .
Example 3: Blinking and LEDs
Blinking
Use Example 3 with the following script:
loop mark 1
delay 1000
mark 0
delay 1000
Loop
for i 0 15
led 13 i
delay 1000
end
Note that the command led 13 0 is the same as mark 0 and the command led 13 1 is the
same as mark 1. The other colors are between the values 2 and 14. The number 13
means that this LED will be connected to the pin 13 of the considered embedded card. It
is not considered in the simulation.
Lab Tasks
Write a senscript to display table of 10 with delay of one second and marking the node when table is
printed.
Script:
___________________________________________
___________________________________________
___________________________________________
___________________________________________
___________________________________________
___________________________________________
___________________________________________
___________________________________________
___________________________________________
SENG – 621: Pervasive Computing
Basic Examples 10
BS(SWE) Part-IV [Morning]
Lab 3
Lab Objective:
1. Getting Familiar with Unicasting, multicasting and broadcasting modes of transmission
2. Using sensor nodes as routers (intermediate node), to relay the data to receiver sensor
node.
The following example will show a sensor node that will send each second 0 and 1 to another sensor node.
The receiver will be marked if it receives 1 (mark 1) and unmarked if it receives 0 (mark 0).
Step 1. Create a new project: this can be done either by clicking on the “New project” icon of the toolbar
or on the menu Project New project.
Step 2. Add two sensor nodes on the map: click either on the Add Sensor icon of the toolbar or from the
menu bar (Add Add Sensor Node). Then, click on the map where you want to add the sensor nodes so that
they can communicate (i.e., There exists a link between the two sensor nodes).
Step 3. Write the SenScript of the transmitter (sensor node 1): The SenScript of the transmitter can be
obtained directly from the SenScript window by clicking on the menu button Transmitter (Version 1) which
must be completed by adding the id of the receiver (i.e., 2) in the command send. Save the file with the name
transmitter.
loop
send 1 2
delay 1000
send 0 2
delay 1000
Step 4. Write the SenScript of the receiver (sensor node 2): As for the first script, The SenScript of the
receiver can be obtained directly from the SenScript window by clicking on the menu button Receiver (Version
1). Save the file with the name receiver.
loop
receive v
mark v
SENG – 621: Pervasive Computing
Basic Examples 11
BS(SWE) Part-IV [Morning]
Step 5. Assign the SenScript file to the sensor nodes: Select the sensor node 1 on the map (1). Go to Device
Parameters in the left part of the main window (2). Then, select the transmitter.csc file in the field Script file
(3). And then, click on the apply button just in the right (4). Do the same procedure for the second sensor node
by choosing the SenScript file receiver.csc. After doing this, the center of each sensor node will be colored in
orange and the name of the assigned SenScript file will be displayed on the sensor node in gray color.
Step 6. Configure the simulation parameters: To visualize the result of the simulation, in this example one
assign to the simulation speed the value 500 ms (1/2 second) and to the arrow speed the value 1000 ms (1
second). The arrows correspond to the send messages.
you can also turn on the acknowledgement messages by checing ACK checkbox in simulation parameters
The red arrow shows the sent messages. The value in the middle of the arrow represents the sent message. If
the transmitter is sending A and B instead of 1 and 0, then the scripts must be rewritten as follows:
Transmitter 2:
Loop
send "A" 2
delay 1000
send "B" 2
delay 1000
Receiver 2:
SENG – 621: Pervasive Computing
Basic Examples 12
BS(SWE) Part-IV [Morning]
Loop
receive v
if(v=="A")
mark 1
else
mark 0
end
The codes of the receivers are same. That is to say, one will assign the same SenScript file for each receiver. No
need to create different scripts for each sensor node since they have the same script. We need just to remove
in the script of the transmitter the id of the receiver. The script will be as follows:
Loop
send "A" *
delay 1000
send "B" *
delay 1000
Simulate …
Once the button apply is pressed, one remark, for the corresponding sensor nodes, that between
the brackets situated in the right of the name in the center, the value is changed from 0 to 12. This
Loop
send "A" 0 12
delay 1000
send "B" 0 12
delay 1000
The value 0 of this script means that the message will be sent for sensor nodes having the MY address given in
the followed number. In this example, this number is equal to 12.
3, and the second router is the sensor node 3, which will route the received messages to the sensor node 4
(the receiver). The code for receiver and transmitter are same as in previous examples (Example 1) while the
codes of the routers are given as follows”
The transmitter:
atget id id id = the identifier of the sensor node (in the example id=1)
loop
data p id "A" p = "1#A"
send p send "1#A" in a broadcast mode
delay 1000 wait for 1 second
data p id "B" p = "1#B"
send p send "1#B"
delay 1000 wait 1 second
The router, this script will be assigned to the sensor node 2 and 3:
Loop
receive rp read the received message and assign it to rp
rdata rp rid v rid = 3 and v=A
if(v=="A") if v==A
mark 1 the sensor node will be marked
else otherwise,
mark 0 it will be unmarked
end
SENG – 621: Pervasive Computing
Basic Examples 15
BS(SWE) Part-IV [Morning]
Lab 4
Lab objective:
Reading values of weather sensor and gas sensors.
Adding and sensing mobile nodes in the network.
Let first create the first sensor node that sends each 100 milliseconds the value of its sensing unit. Let call its
script sensor. Let increase its sensing unit so that to obtain the following result:
Then, the second sensor node will just await for messages received from the first one and be marked or not
according to the received message.
Before adding a mobile, first, we start with adding a route. To do this, we will add two markers and then select
both of them. Then we will type many times on the key ‘u’ of the keyboard until to obtain the following result:
The route must be saved using the button Save in the Marker Parameters view.
SENG – 621: Pervasive Computing
Basic Examples 16
BS(SWE) Part-IV [Morning]
Now, we can add a mobile and assign it the previously created route. This is done using
the field GPS file inthe Device Parameters view.
Once done, the mobile will have a center with the orange color. Then, the project is ready for
simulation.
For the simulation, change the value of the simulation speed to 0 ms and the Arrow Speed to 50 ms.
Simulate….
Transmitter:
loop Receiver:
areadsensor v loop
if(v!="X") receive y
print v print y
rdata v a b c if(y>20)
send c 2 mark 1
end else
delay 1000 mark 0
end
SENG – 621: Pervasive Computing
Basic Examples 17
BS(SWE) Part-IV [Morning]
The simulation results will show something like the following figures where the sensor node is unmarked after
receiving a value less than 20 and it is marker after receiving the value 23 which is greater than 20.
Unmarked sensor node after receiving the value 14<20 Marked sensor node after receiving the value 23>20
It is also possible to write the received values in a file during the simulation using the command
printfile as follows:
loop
receive y
print y
time t get the current simulation time
printfile t y print in a file the current simulation time and the received value x
if(y>20)
mark 1
else
mark 0
end
The obtained file has the same name as the executing sensor node and it is located in the directory results.
SENG – 621: Pervasive Computing
Basic Examples 18
BS(SWE) Part-IV [Morning]
Lab 5
Lab objective
Getting information about neighboring nodes
Getting information regarding radio parameters (channel Id, My ID, Radio module) of the sensor.
Changing transmission power levels of the sensor nodes.
1. The Network ID
2. The Channel
3. The MY address
loop
atget nid v1
atget ch v2
atget my v3
print "network id=" v1 " channel id=" v2 " my id is=" v3
delay 1000
The first sensor node will send A and B each second and the other sensor nodes will display the
received message (i.e., A and B). After each transmission of the couple A and B we will change the
percentage of the power of the sending message. In the beginning we will put 100% and then we
switch to 60%. The script of this situation is written as follows:
atpl 100
send "A"
delay 1000
atpl 60
send "b"
delay 1000
The script of the receiver:
loop
receive x
print x
100% 60%
1. Display the Coordinates of each sensor node: the script that allows to display
the coordinates of a sensor node is given by:
SENG – 621: Pervasive Computing
Basic Examples 20
BS(SWE) Part-IV [Morning]
loop
getpos x
print x
stop
Assign this script to each sensor node and simulate. The result will be as follows:
To recuperate each coordinate separately, use the command rdata. One can use also the comma
nd getpos2 that allows to recuperate the coordinates into 2 separated variable as follows:
loop
getpos2 x y
print x y
stop
Obtaining the list of the neighbors: To obtain the list of the neighbors of the sensor S1 for
example, we can use two commands. The first one is atnd x and the second one is atnd x y. The
first command allows to assign to x the number of neighbors and the second one allows to assign
to x the number of neighbors and to y the list of the identifiers of the neighbors separated by #. In
the following we present two scripts using each of them each of these commands and the results of
their simulation.
Script 1 (atnd x): this will display the number of the neighbors of S1.
loop
atnd x
print x
stop
Simulation result:
SENG – 621: Pervasive Computing
Basic Examples 21
BS(SWE) Part-IV [Morning]
Script 2 (atnd x y): this will display each second the identifier of each neighbor (for good visualization, you
must set the value of the simulation speed to 1000):
loop
atnd n v
for i 0 n
vget x v i
print x
delay 1000
end
stop
Simulation result:
In the following, we will display each second the distance with each neighbor