Sunday 2 November 2014

AODV simulation for a Sensor network 802.15.4

           The reason to write this topic is many people asked me how to simulate sensor networks.
Obviously, authors of 802.15.4/Zigbee protocol developers on NS2 have given a sample examples. But, these examples do not run correctly, and give some kind of unknown error (at least I don’t know what errors mean).
Therefore, I have decided to test AODV using 802.15.4 MAC/PHY. Thus, if my tests work, I hope you can test your own routing protocols using this source code.

Alright, the TCL file is fairly simple. I briefly explain what means what. We first set simulation environment. We are going to deploy 500 nodes, in 1000×500 sqm area, simulation time is 500 seconds. And we are using 802.15.4 MAC/PHY and interface queue is 100. We also set simulator and files to trace the simulation.
1# Generated by Topology Generator for Network Simulator
2set val(chan)          Channel/WirelessChannel      ;# channel type
3set val(prop)          Propagation/TwoRayGround     ;# radio-propagation model
4set val(netif)         Phy/WirelessPhy/802_15_4     ;# network interface type
5set val(mac)           Mac/802_15_4                 ;# MAC type
6set val(ifq)           Queue/DropTail/PriQueue      ;# interface queue type
7set val(ll)            LL                           ;# link layer type
8set val(ant)           Antenna/OmniAntenna          ;# antenna model
9set val(ifqlen)        100                  ;# max packet in ifq
10set val(nn)            500              ;# number of mobilenodes
11set val(rp)            AODV             ;# protocol tye
12set val(x)             1000             ;# X dimension of topography
13set val(y)             500              ;# Y dimension of topography
14set val(stop)          500              ;# simulation period
15set val(energymodel)   EnergyModel          ;# Energy Model
16set val(initialenergy) 100              ;# value
17 
18set ns              [new Simulator]
19set tracefd         [open trace-aodv-802-15-4.tr w]
20set namtrace        [open nam-aodv-802-15-4.nam w]
21 
22$ns trace-all $tracefd
23$ns namtrace-all-wireless $namtrace $val(x) $val(y)
Let’s set radio transmission range to 40 meters, but this does not mean exactly 40 meters. The code below filters packet with receiving signal strength above “40 meters”.
1set dist(5m7.69113e-06
2set dist(9m2.37381e-06
3set dist(10m) 1.92278e-06
4set dist(11m) 1.58908e-06
5set dist(12m) 1.33527e-06
6set dist(13m) 1.13774e-06
7set dist(14m) 9.81011e-07
8set dist(15m) 8.54570e-07
9set dist(16m) 7.51087e-07
10set dist(20m) 4.80696e-07
11set dist(25m) 3.07645e-07
12set dist(30m) 2.13643e-07
13set dist(35m) 1.56962e-07
14set dist(40m) 1.20174e-07
15Phy/WirelessPhy set CSThresh_ $dist(40m)
16Phy/WirelessPhy set RXThresh_ $dist(40m)
And lets set topography as flat, deploy nodes randomly in an area of 1000 x 500 sqm.
1# set up topography object
2set topo       [new Topography]
3$topo load_flatgrid $val(x) $val(y)
4 
5create-god $val(nn)
6 
7# configure the nodes
8$ns node-config -adhocRouting $val(rp)
9            -llType $val(ll)
10             -macType $val(mac)
11             -ifqType $val(ifq)
12             -ifqLen $val(ifqlen)
13             -antType $val(ant)
14             -propType $val(prop)
15             -phyType $val(netif)
16             -channel [new $val(chan)]
17             -topoInstance $topo
18             -agentTrace ON
19             -routerTrace ON
20             -macTrace  OFF
21             -movementTrace OFF
22             -energyModel $val(energymodel)
23             -initialEnergy $val(initialenergy)
24             -rxPower 35.28e-3
25             -txPower 31.32e-3
26         -idlePower 712e-6
27         -sleepPower 144e-9
28 
29             #-IncomingErrProc MultistateErrorProc
30             #-OutgoingErrProc MultistateErrorProc
31 
32for {set i 0} {$i < $val(nn) } { incr i } {
33        set mnode_($i) [$ns node]
34}
35 
36for {set i 1} {$i < $val(nn) } { incr i } {
37    $mnode_($i) set X_ [ expr {$val(x) * rand()} ]
38    $mnode_($i) set Y_ [ expr {$val(y) * rand()} ]
39    $mnode_($i) set Z_ 0
40}
And we are goig to deploy sink node in the center of area, i.e. at [500, 250].
1# Position of Sink
2$mnode_(0) set X_ [ expr {$val(x)/2} ]
3$mnode_(0) set Y_ [ expr {$val(y)/2} ]
4$mnode_(0) set Z_ 0.0
5$mnode_(0) label "Sink" 

The code below is useful how big the nodes are going to be shown in NAM (network animator), thus it does not have meaning in real simulation.
1for {set i 0} {$i < $val(nn)} { incr i } {
2    $ns initial_node_pos $mnode_($i) 10
3}
Finally, we have deployed nodes, and remained important thing is establish connection. We are going to use UDP protocol with CBR (constant bit rate, interval (interval_) is set to 2 seconds)
1#Setup a UDP connection
2set udp [new Agent/UDP]
3$ns attach-agent $mnode_(10) $udp
4 
5set sink [new Agent/Null]
6$ns attach-agent $mnode_(0) $sink
7 
8$ns connect $udp $sink
9$udp set fid_ 2
10 
11#Setup a CBR over UDP connection
12set cbr [new Application/Traffic/CBR]
13$cbr attach-agent $udp
14$cbr set type_ CBR
15$cbr set packet_size_ 50
16$cbr set rate_ 0.1Mb
17$cbr set interval_ 2
18#$cbr set random_ false
19 
20$ns at 5.0 "$cbr start"
21$ns at [expr $val(stop) - 5] "$cbr stop"
22 
23# Telling nodes when the simulation ends
24for {set i 0} {$i < $val(nn) } { incr i } {
25    $ns at $val(stop) "$mnode_($i) reset;"
26}
27 
28# ending nam and the simulation
29$ns at $val(stop) "$ns nam-end-wireless $val(stop)"
30$ns at $val(stop) "stop"
31$ns at [expr $val(stop) + 0.01] "puts "end simulation"; $ns halt"
32proc stop {} {
33    global ns tracefd namtrace
34    $ns flush-trace
35    close $tracefd
36    close $namtrace
37}
38 
39$ns run
We have finished writing sample AODV TCL script, we can run it
1ns aodv_802_15_4.tcl
Download whole source code here : aodv_802_15_4.tcl . If you find any problem with that, leave comment here. If you want to test your own routing protocol simply change $val(rp) AODV with your own.

No comments:

Post a Comment