Elmurod A. Talipov
14Apr/09Off

Sensor Network 802.15.4 AODV Simulation

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.

# Generated by Topology Generator for Network Simulator (c) Elmurod Talipov
set val(chan)          Channel/WirelessChannel      ;# channel type
set val(prop)          Propagation/TwoRayGround     ;# radio-propagation model
set val(netif)         Phy/WirelessPhy/802_15_4     ;# network interface type
set val(mac)           Mac/802_15_4                 ;# MAC type
set val(ifq)           Queue/DropTail/PriQueue      ;# interface queue type
set val(ll)            LL                           ;# link layer type
set val(ant)           Antenna/OmniAntenna          ;# antenna model
set val(ifqlen)        100	         	    ;# max packet in ifq
set val(nn)            500			    ;# number of mobilenodes
set val(rp)            AODV			    ;# protocol tye
set val(x)             1000			    ;# X dimension of topography
set val(y)             500			    ;# Y dimension of topography
set val(stop)          500			    ;# simulation period
set val(energymodel)   EnergyModel		    ;# Energy Model
set val(initialenergy) 100			    ;# value

set ns        		[new Simulator]
set tracefd       	[open trace-aodv-802-15-4.tr w]
set namtrace      	[open nam-aodv-802-15-4.nam w]

$ns trace-all $tracefd
$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(5m)  7.69113e-06
set dist(9m)  2.37381e-06
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 up topography object
set topo       [new Topography]
$topo load_flatgrid $val(x) $val(y)

create-god $val(nn)

# configure the nodes
$ns node-config -adhocRouting $val(rp) \
            -llType $val(ll) \
             -macType $val(mac) \
             -ifqType $val(ifq) \
             -ifqLen $val(ifqlen) \
             -antType $val(ant) \
             -propType $val(prop) \
             -phyType $val(netif) \
             -channel [new $val(chan)] \
             -topoInstance $topo \
             -agentTrace ON \
             -routerTrace ON \
             -macTrace  OFF \
             -movementTrace OFF \
             -energyModel $val(energymodel) \
             -initialEnergy $val(initialenergy) \
             -rxPower 35.28e-3 \
             -txPower 31.32e-3 \
	     -idlePower 712e-6 \
	     -sleepPower 144e-9

             #-IncomingErrProc MultistateErrorProc \
             #-OutgoingErrProc MultistateErrorProc

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()} ]
	$mnode_($i) set Z_ 0
}

And we are goig to deploy sink node in the center of area, i.e. at [500, 250].

# Position of Sink
$mnode_(0) set X_ [ expr {$val(x)/2} ]
$mnode_(0) set Y_ [ expr {$val(y)/2} ]
$mnode_(0) set Z_ 0.0
$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.

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)

#Setup a UDP connection
set udp [new Agent/UDP]
$ns attach-agent $mnode_(10) $udp

set sink [new Agent/Null]
$ns attach-agent $mnode_(0) $sink

$ns connect $udp $sink
$udp set fid_ 2

#Setup a CBR over UDP connection
set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp
$cbr set type_ CBR
$cbr set packet_size_ 50
$cbr set rate_ 0.1Mb
$cbr set interval_ 2
#$cbr set random_ false

$ns at 5.0 "$cbr start"
$ns at [expr $val(stop) - 5] "$cbr stop"

# Telling nodes when the simulation ends
for {set i 0} {$i < $val(nn) } { incr i } {
    $ns at $val(stop) "$mnode_($i) reset;"
}

# ending nam and the simulation
$ns at $val(stop) "$ns nam-end-wireless $val(stop)"
$ns at $val(stop) "stop"
$ns at [expr $val(stop) + 0.01] "puts \"end simulation\"; $ns halt"
proc stop {} {
    global ns tracefd namtrace
    $ns flush-trace
    close $tracefd
    close $namtrace
}

$ns run

We have finished writing sample AODV TCL script, we can run it

 ns aodv_802_15_4.tcl 

NAM gives me following deployment result.

nam-aodv-802-15-4nam

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.

Comments (36) Trackbacks (3)
  1. Hello,
    Thank you for your example it’s very helpful,well when I’m trying the example I got this output

    num_nodes is set 500
    INITIALIZE THE LIST xListHead
    channel.cc:sendUp – Calc highestAntennaZ_ and distCST_
    highestAntennaZ_ = 1.5, distCST_ = 58.7
    SORTING LISTS …DONE!
    end simulation

    ======
    Please need your advice
    Thanks advanced

  2. This is only going to show that your simulation run successfully. But your experiment result in *.tr (trace) file, you have to analyze to get information which you need. And, you can see the *.nam (network animation) file to see how nodes are deployed, and how packets has been transfered. Don’t forget this just animation it only gives you hint about your simulation.

  3. Hi,
    Thanks for your support for new bees… well when i run the script i saw the simulation environment and when i run it i can’t be bale to see any packets like as we normally see in the wired network… what should I do in this case. Also If i need to change MAC to S-MAC or 802.11 then what should I do?
    Thanks

  4. I am sure the code works fine. I have checked that through trace file.About changing mac protocol you may do it
    set val(mac) SMac/802.15.4 (or something like that) but I am not sure S-MAC is implemented in NS2. Read the WPAN_ZBR_pub.pdf file located in $NS_ROOT/ns-2.**/wpan/.

  5. i just have couple of queries related to Zigbee routing approach.

    1. Zigbee uses modified aodv protocol which uses routing metric as bidirectional link quality instead of hop count metric.is this modified aodv available as open source code in Ns2? If not, is it possible to obtain the RSSI value of a node from the lower layer of 802_15_4.?

    2.While setting the transmission range for a node, what does the CS_Thresh and RX_Thresh values signify?

    Thanks.

  6. (1) I am not sure if the modified AODV available, but Zheng (Zigbee on NS2 developer) states that the ZBR, which is modified version of AODV is no more available on release. I assume that it is hard to find that version you need.
    And obtaining RSSI value is possible, you just need few tricks. Look at the file $NS_ROOT/ns-2.**/wpan/p802_15_4phy.cc

    (2) These parameters indicate Signal Strength threshold in decibel. RX_Thresh means that the packets with below this threshold are dropped. CS_Threshold is carrier sensing I guess.

  7. i want to detect congestion depending on the queue length in AODv. coulsd you help me to do so

  8. while comparing the 802.11 with 802.15.4 what i observed for a 3 hops scenario that the drop rate is higher in 802.15.4 as compared to 802.11. Also in 802.11 multiple sources sends to same desitination simultaneously but herei n 802.15.4 its not a case ? Also overhead confirmation packets are seems more as compared to 802.11?
    I am waiting for ur response in this regard.

  9. Atif, I haven’t done that comparison. Probably you are right, what I have found out was that in same condition 802.15.4 had higher packet loss. I can only assume that happens due to the unreliable link quality of 802.15.4, but it might also be a bug in the code.

  10. if we are using 802.15.4.. how we can specifically assign nodes a FFD or RFDs?…. sink act as FFD? and what about sources and relay nodes are they RFDs?
    How we adjust this parameter while using 802.15.4 as MAC protocol and using AODV as routing agent.
    Thanks

  11. In NS2, to the best of my knowledge, you can’t specify FFD or RFD. Sink always acts FFD. Since you can’t specify FFD or RFD, all acts as FFD, thus they can generate and realy packets. Originally, only FFDs can relay packets.

  12. Anotehr thing i have noticed while simulating the AODV with 802.15.4 using tcp and udp as traffic agents that no two sources simultaneously sends data to same relay node where as this was not a case in 802.11 using the same routing agent and traffic agents?

  13. I have a compiling problem in making a a new agent, How can I put object file (object file which is created after compiling .cc file) into the “make file”, where should I paste that?

    I tried to compile the sample file but the system stucks.

    Pls help me on this and If you have a step by step tutorial how to modify make file, pls mail me.

    Your help means alot to me.

  14. Salemalaykom,
    I execut your script but i have obtein this msg:num_nodes is set 500
    INITIALIZE THE LIST xListHead
    channel.cc:sendUp – Calc highestAntennaZ_ and distCST_
    highestAntennaZ_ = 1.5, distCST_ = 58.7
    SORTING LISTS …DONE!
    end simulation
    can you Please help me, I need your advice
    And I need to have the mail for Atif because i work for the comparaisant the AODV 802.15.4 and Aodv 802.11.
    Thanks advanced

  15. sir

    i have used yr code. i got nam and trace file. and i also changed the routing protocols in to DSR and DSDV. it works fine. i want to execute directed diffusion .but this program not working what are the changes i have to do. pl help me sir. its very urgent.
    i never forgot yr help

  16. i want to discard the node that has not enough energy.
    Do u know how i can get remaining energy of nodes?
    how i can discard the node?
    how i can compute sensing area of the node?

  17. Hello,
    I am new in Ns2 (only a week) but not in 802.15.4. After running some simulations and analyzing the trace files, I have some questions about the model implemented in ns-2.34. I am interested in simulate a beacon-enabled and multihop network. I do not know if the model does not work properly or if I do not know how to use it. The points are the followings:
    – Passive Scan: Does not work. But with some modification in the code, I have solve the problem.
    – Packet forwarding: If an intermediate node has to forward a packet to a parent node and has not enough time to it in the current CAP, the packet is not dropped but it is not transmitted in the following CAP. I think that the model work OK with BO=SO, but when it is used SO<BO (BO=7 and SO=2 is a good superframe configuration for commercial applications) there are a lot of errors.
    – Node synchronization: If a node losses the synchronization with its parent, nothing happens.
    I would be delighted to receive your comments about these points. Have you had the same behaviour with the data forwarding?

    Best regards,
    Miguel.

  18. hi sir..
    I’m balashunmugaraja.. now I’m doing ME- CSE in India.. currently I’m doing project in AODV with improve the performance with battery power,bandwidth and congestion avoidance… plz help me to do this project.. if you have any source code .. plz help me.. I’m waiting for ur reply
    thanks&regards
    bala

  19. Guys I have been busy for several weeks, could get back to you.

    To Atif. putting (.cc) object file is usually under NS-ROOT (ns-2.**/). Find aodv.cc and put your file names as it aodv, it should work. And don’t forget to modify following files:

    $NS_ROOT/Makefile
    $NS_ROOT/queue/priqueue.cc
    $NS_ROOT/common/packet.h
    $NS_ROOT/trace/cmu-trace.h
    $NS_ROOT/trace/cmu-trace.cc
    $NS_ROOT/tcl/lib/ns-packet.tcl
    $NS_ROOT/tcl/lib/ns-lib.tcl
    $NS_ROOT/tcl/lib/ns-agent.tcl
    $NS_ROOT/tcl/lib/ns-mobilenode.tcl

    About how to modify. I can’t bring it here, refer some tutorials.

    To Kanna. The code does not work is DD (direct diffusion) therefore you should find another script. It’s available on authors website, i believe.

    To Balashunmugaraja: I can’t help you doing your project, but if you find something difficult. Ask me question I’ll try to help.

  20. Hello,

    I am trying to implement a weak energy harvesting aware protocol using this model as the basis. So I need to make provisions for adding energy to nodes at regular intervals of time (or whenever the node energy falls). Also I require different nodes to have different initial energies. What will I be required to do to achieve this? Also if the .cc and .h files have to be modified, guide me as to which files in particular will have to be tweaked and where? I would really appreciate any guidance and support.

    Thank You.

  21. Hi,

    I am new to creating own routing protocol using C++ in ns2 and i want to clear some basic queries

    1.if i am modifying a existing cc file, should i have to provide
    make depend
    make commands.
    or
    make clean
    make
    or
    ./configure
    make clean
    make

    2. Say if i want to print routing table of any node that i specify in my tcl script to my trace file,please provide your inputs?

    3.Say i want to print some variables to be displayed in my konsole window, is it enough to give printf statements??

    4.I wanted to make use of the energy model in ns2 to use residual energy of the node as routing metric and also wanted to check while forwarding to a node,whether its residual energy is atleast 10% of initial energy assigned?
    Could you please provide some pointers here?

    Thanks & Regards,
    Jayavignesh

  22. Sir, can you help me to use rmst filter in a tcl file. It wil be very kind of you

  23. Rishab, you can do that…

    modify following files in $NS-ROOT\ns-2.xx\mobile\
    energy-model.cc
    energy-model.h

    Jayavignesh,

    1. just “make” is enough..
    2. is it question? goto to “NS2: Printing Routing Table in AODV” post on my blog.
    3. yes
    4. do as I explained Rishab

    Dushyanta

    I don’t know what is rmst filter… Elaborate your question..

  24. Sir, I want to simulate a wsn topology by using transport layer protocols like RMST, PSFQ, STCP which are the protocols used instead of tcp in wsn. But I am able to used them in ns2. RMST is a protocol under diffusion3 in ns2 directory but no example to use it . Can you help me?

  25. @ jayavignesh
    I am also implementing an energy aware network. Maybe we can get in touch and help each other. My email is mailrish@gmail.com

  26. Hello
    i am doing my final project about object tracking in wsn with ns2. i want to apply a hierarcy tree in Zigbee for object tracking .i’m a beginner here. i should exchange aodv routing to apply my new one. but i dont know which script should modify. need some help. myheki@gmail.com

  27. Assalem Allaykom,
    Sir
    I want to simulate a star topology in beaconless mode in IEEE802.15.4 to study the performance for this norme for the throughput, energy, delay and Paket delivery ratio, so I need to varies the trafic load an payload size values in my TCL script in ns2.31 but I can’t and I don’t know what is the step for this can you sir help me or any one because I need it to final my work or to send me an example for the TCl script to put the trafic load and payload size.
    Best regard
    Djamila bendjamil2005@yahoo.fr

  28. sir,
    i have ewxecuted DD sucessfully. i got nam and trace file. can you help me to analyse the trace file. please

  29. Hi sir,

    i’m very new in Ns2 this is my first time to run Ns2. i have tried to run the source code that you provide, but it is not working, and it show error:
    Num_Nodes is set 500
    Invalid command name ” phy/wireless/phy/802_15_4″
    while executing
    ” phy/wireless/phy/802_15_4 create o18″
    invoke from within
    .
    .
    .
    and many more…
    thanks

    Regards,
    Tra

  30. hi sir,

    i have planned to do my project on wireless sensor networks simulation in ns2……
    but i cant able get an clear tutorial for sensor networks simulation in ns2……

    can anyone help me with sample code or a link to a tutorial…. where my probs can be solved…

    thanks,
    Santhosh

  31. hello sir,

    I tried with this code sir, but i cant able to get the nam output……
    but there were no errors…………….

  32. Hi,
    I m doing localization based on RSSI values in Wireless Network.
    So, Please tell me,
    How to calculate the RSSI values at each node ?

  33. Hi,

    Thanks for the previous replies.
    I am working on a geographic energy efficient forwarding(routing) cross layer approach for WSN as per 802.15.4 MAC/PHY standard.
    I have a requirement to simulate using discrete power levels in ns2 where nodes will be configured with default power level.An on-demand requirement for node to transmit the data, it will broadcast at default power level to find the forwarding neighbors, if no neighbors reply within a timer value, then it has to dynamically adapt its transmission power and retransmit to find any forwarding node replying to it.
    Please provide me with some pointers how to handle this dynamic adjustments of power value. What files need to changed in PHY layer in ns2? How to track the Pt_ variable in my Routing code? Any sample code/pseudocode to explain this would be of immense help!!

    Thanks,
    Jayavignesh

  34. Sorry for everyone, please do not post questions here as comment. I do hardly check these comments. Write me e-mail, or post your comment to newest NS2 related post.

    Santosh, if your NAM does not work then it was made something wrong during installation of ns2.

    Gaurav, I have not tried getting RSSI. I have to check.

    Jayavignesh. you should check NS-ROOT/ns-??.??/wpan/. There is a file p802_15_4phy.cc, there you will find power control related code.

  35. isnt there any debug program for ns2 module? are we should always do “make clean” or “make” if we want to add new module in c++ files??

  36. Hi all,
    Just two simple questions:

    1. where can I specify the transmit power signal and modify it ?

    2. Where can I specify the height of the transmit and receive antennas?

    Thanks in advance.