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 1000x500 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.
set val(chan) Channel / WirelessChannel ; |
set val(prop) Propagation / TwoRayGround ; |
set val(netif) Phy / WirelessPhy / 802_15_4 ; |
set val(mac) Mac / 802_15_4 ; |
set val(ifq) Queue / DropTail / PriQueue ; |
set val(ant) Antenna / OmniAntenna ; |
set val(energymodel) EnergyModel ; |
set val(initialenergy) 100 ; |
set tracefd [ open trace - aodv - 802 - 15 - 4.tr w] |
set namtrace [ open nam - aodv - 802 - 15 - 4.nam w] |
$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".
set dist( 10m ) 1.92278e - 06 |
set dist( 11m ) 1.58908e - 06 |
set dist( 12m ) 1.33527e - 06 |
set dist( 13m ) 1.13774e - 06 |
set dist( 14m ) 9.81011e - 07 |
set dist( 15m ) 8.54570e - 07 |
set dist( 16m ) 7.51087e - 07 |
set dist( 20m ) 4.80696e - 07 |
set dist( 25m ) 3.07645e - 07 |
set dist( 30m ) 2.13643e - 07 |
set dist( 35m ) 1.56962e - 07 |
set dist( 40m ) 1.20174e - 07 |
Phy / WirelessPhy set CSThresh_ $dist( 40m ) |
Phy / WirelessPhy set RXThresh_ $dist( 40m ) |
And lets set topography as flat, deploy nodes randomly in an area of 1000 x 500 sqm.
set topo [new Topography] |
$topo load_flatgrid $val(x) $val(y) |
$ns node - config - adhocRouting $val(rp) \ |
- channel [new $val(chan)] \ |
- energyModel $val(energymodel) \ |
- initialEnergy $val(initialenergy) \ |
for { set i 0 } {$i < $val(nn) } { incr i } { |
set mnode_($i) [$ns node] |
for { set i 1 } {$i < $val(nn) } { incr i } { |
$mnode_($i) set X_ [ expr {$val(x) * rand()} ] |
$mnode_($i) set Y_ [ expr {$val(y) * rand()} ] |
And we are goig to deploy sink node in the center of area, i.e. at [500, 250].
$mnode_( 0 ) set X_ [ expr {$val(x) / 2 } ] |
$mnode_( 0 ) set Y_ [ expr {$val(y) / 2 } ] |
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.
for { set i 0 } {$i < $val(nn)} { incr i } { |
$ns initial_node_pos $mnode_($i) 10 |
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)
$ns attach - agent $mnode_( 10 ) $udp |
set sink [new Agent / Null] |
$ns attach - agent $mnode_( 0 ) $sink |
set cbr [new Application / Traffic / CBR] |
$ns at [expr $val(stop) - 5 ] "$cbr stop" |
for { set i 0 } {$i < $val(nn) } { incr i } { |
$ns at $val(stop) "$mnode_($i) reset;" |
$ns at $val(stop) "$ns nam-end-wireless $val(stop)" |
$ns at [expr $val(stop) + 0.01 ] "puts \"end simulation\"; $ns halt" |
global ns tracefd namtrace |
We have finished writing sample AODV TCL script, we can run it
NAM gives me following deployment result.
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.
1 comment:
Hello,
While working on my PhD I am struggling to develop an intrusion detection system on top of zigbee motes, but have found with OPNET I need to be a member of the zigbee alliance to be able to communicate at the application layer. If I switch t NS/2 will I have the same problem? I need to be able to keep a neighborhood table and communicate between 1-hop nodes. Thank you, Audrey
Post a Comment