0% found this document useful (0 votes)
183 views19 pages

Coding Jarnir Ns2

The document describes the creation and communication model of a wireless sensor network simulation in NS2. It defines options such as the channel type, radio propagation model, network interface, MAC type and energy model. It then generates multiple sensor nodes within a 600x600 area, connects them using UDP and CBR, and simulates communication between the nodes for 10 seconds while tracking energy consumption.

Uploaded by

Rozita Rahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
183 views19 pages

Coding Jarnir Ns2

The document describes the creation and communication model of a wireless sensor network simulation in NS2. It defines options such as the channel type, radio propagation model, network interface, MAC type and energy model. It then generates multiple sensor nodes within a 600x600 area, connects them using UDP and CBR, and simulates communication between the nodes for 10 seconds while tracking energy consumption.

Uploaded by

Rozita Rahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

#*********************** MOBILIE ADHOC NETWORK *****************#

#****************** MOBILITY MODEL ***************************#

#**************************Multiple node Creation and communication model using


UDP (User Datagram Protocol)and CBR (Constant Bit Rate)*******************#
# Simulator Instance Creation
set ns [new Simulator]

#Fixing the co-ordinate of simulation area


set val(x) 500
set val(y) 500
# Define options
set val(chan) Channel/WirelessChannel ;# channel type
set val(prop) Propagation/TwoRayGround ;# radio-propagation model
set val(netif) Phy/WirelessPhy ;# network interface type
set val(mac) Mac/802_11 ;# 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) 50 ;# max packet in ifq
set val(nn) 2 ;# number of mobilenodes
set val(rp) AODV ;# routing protocol
set val(x) 500 ;# X dimension of topography
set val(y) 500 ;# Y dimension of topography
set val(stop) 10.0 ;# time of simulation end

# set up topography object


set topo [new Topography]
$topo load_flatgrid $val(x) $val(y)

# general operational descriptor- storing the hop details in the network


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) \
-channelType $val(chan) \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF \
-movementTrace ON

# Node Creation

for {set i 0} {$i < 10} {incr i} {

set node_($i) [$ns node]


$node_($i) color black

#******************************Defining Communication Between node0 and all


nodes ****************************
for {set i 1} {$i < 10} {incr i} {

# Defining a transport agent for sending


set udp [new Agent/UDP]

# Attaching transport agent to sender node


$ns attach-agent $node_($i) $udp

# Defining a transport agent for receiving


set null [new Agent/Null]

# Attaching transport agent to receiver node


$ns attach-agent $node_(0) $null

#Connecting sending and receiving transport agents


$ns connect $udp $null

#Defining Application instance


set cbr [new Application/Traffic/CBR]

# Attaching transport agent to application agent


$cbr attach-agent $udp

#Packet size in bytes and interval in seconds definition


$cbr set packetSize_ 512
$cbr set interval_ 0.1

# data packet generation starting time


$ns at 1.0 "$cbr start"
# data packet generation ending time
#$ns at 6.0 "$cbr stop"
}

#Filename: sample19.tcl

# ==========VEHICULAR ADHOC NETWORKS============


# Define options
set val(chan) Channel/WirelessChannel ;# channel type
set val(prop) Propagation/TwoRayGround ;# radio-propagation model
set val(netif1) Phy/WirelessPhy ;# network interface type
set val(netif2) Phy/WirelessPhy ;# network interface type
set val(mac) Mac/802_11 ;# 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) 50 ;# max packet in ifq
set val(nn) 46 ;# number of mobilenodes
set val(rp) AODV ;# routing protocol
set val(x) 200 ;# X dimension of topography
set val(y) 200 ;# Y dimension of topography
set val(stop) 10.0 ;# time of simulation end

# Simulator Instance Creation


set ns [new Simulator]

# set up topography object


set topo [new Topography]
$topo load_flatgrid $val(x) $val(y)
# general operational descriptor- storing the hop details in the network
create-god $val(nn)
# For model 'TwoRayGround'

set dist(20m) 4.80696e-07


set dist(26m) 2.84435e-07
set dist(27m) 2.63756e-07
set dist(28m) 2.45253e-07
set dist(25m) 3.07645e-07
set dist(30m) 2.13643e-07
set dist(35m) 1.56962e-07
set dist(50m) 7.69113e-08
set dist(75m) 3.41828e-08
set dist(60m) 5.34106e-08
set dist(70m) 3.92405e-08
# unity gain, omni-directional antennas
# set up the antennas to be centered in the node and 1.5 meters above it
Antenna/OmniAntenna set X_ 0
Antenna/OmniAntenna set Y_ 0
Antenna/OmniAntenna set Z_ 1.5
Antenna/OmniAntenna set Gt_ 1.0
Antenna/OmniAntenna set Gr_ 1.0
# Initialize the SharedMedia interface with parameters to make
# it work like the 914MHz Lucent WaveLAN DSSS radio interface

$val(netif1) set CPThresh_ 10.0


$val(netif1) set CSThresh_ $dist(70m)
$val(netif1) set RXThresh_ $dist(75m)
$val(netif1) set Rb_ 2*1e6
$val(netif1) set Pt_ 0.2818
$val(netif1) set freq_ 914e+6
$val(netif1) set L_ 1.0

$val(netif2) set CPThresh_ 10.0


$val(netif2) set CSThresh_ $dist(75m)
$val(netif2) set RXThresh_ $dist(75m)
$val(netif2) set Rb_ 2*1e6
$val(netif2) set Pt_ 0.2818
$val(netif2) set freq_ 914e+6
$val(netif2) set L_ 1.0
# set up topography object
set topo [new Topography]
$topo load_flatgrid $val(x) $val(y)

# Create God
set god_ [create-god $val(nn)]

set chan_1_ [new $val(chan)]

# configure node

$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(netif1) \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF \
-movementTrace ON \
-channel $chan_1_ \

for {set i 0} {$i < 1 } {incr i} {


set node_($i) [$ns node]
$node_($i) color black

$node_($i) random-motion 0 ;# disable random motion


}
$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(netif2) \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF \
-movementTrace ON \
-channel $chan_1_ \

for {set i 1} {$i < 4 } {incr i} {


set node_($i) [$ns node]
$node_($i) color black

}
Install sumo

$sudo apt-get update


$sudo apt-get install build-essential autoconf automake libxmu-dev default-jre
g++-4.4 libproj-dev libgdal-dev libxerces-c2-dev libfox-1.6-dev
$export CC=gcc-4.4 CXX=g++-4.4

$./configure

$make

$sudo make install

Execute the following command to run the MOVE.jar file


java -jar MOVE.jar (Java required)
AODV in Manet

#Filename: sample15.tcl

#**************AODV ROUTING PROTOCOL***************************#


# Simulator Instance Creation
set ns [new Simulator]

#Fixing the co-ordinate of simutaion area


set val(x) 500
set val(y) 500
# Define options
set val(chan) Channel/WirelessChannel ;# channel type
set val(prop) Propagation/TwoRayGround ;# radio-propagation model
set val(netif) Phy/WirelessPhy ;# network interface type
set val(mac) Mac/802_11 ;# 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) 50 ;# max packet in ifq
set val(nn) 10 ;# number of mobilenodes
set val(rp) AODV ;# routing protocol
set val(x) 500 ;# X dimension of topography
set val(y) 500 ;# Y dimension of topography
set val(stop) 10.0 ;# time of simulation end

# set up topography object


set topo [new Topography]
$topo load_flatgrid $val(x) $val(y)
# general operational descriptor- storing the hop details in the network
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) \
-channelType $val(chan) \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF \
-movementTrace ON

# Node Creation

for {set i 0} {$i < 10} {incr i} {

set node_($i) [$ns node]


$node_($i) color black

for {set i 0} {$i < 10} {incr i} {

$node_($i) set X_ [expr rand()*$val(x)]


$node_($i) set Y_ [expr rand()*$val(y)]
$node_($i) set Z_ 0

}
#******************************Defining Communication Between node0 and all
nodes ****************************8

for {set i 1} {$i < 10} {incr i} {


# Defining a transport agent for sending
set udp [new Agent/UDP]

# Attaching transport agent to sender node


$ns attach-agent $node_($i) $udp

# Defining a transport agent for receiving


set null [new Agent/Null]

# Attaching transport agent to receiver node


$ns attach-agent $node_(0) $null

#Connecting sending and receiving transport agents


$ns connect $udp $null

#Defining Application instance


set cbr [new Application/Traffic/CBR]

# Attaching transport agent to application agent


$cbr attach-agent $udp
#Packet size in bytes and interval in seconds definition
$cbr set packetSize_ 512
$cbr set interval_ 0.1

# data packet generation starting time


$ns at 1.0 "$cbr start"

# data packet generation ending time


#$ns at 6.0 "$cbr stop"

How to create Wireless Sensor Network


(WSN) in ns2
#Filename: sample18.tcl
#*******************SENSOR NETWORK ************************

#*********************ENERGY MODEL *********************88


#************Multiple node Creation and communication model using

UDP (User Datagram Protocol)and CBR (Constant Bit Rate)


***************************88
# Simulator Instance Creation
set ns [new Simulator]

#Fixing the co-ordinate of simulation area


set val(x) 600
set val(y) 600
# Define options
set val(chan) Channel/WirelessChannel ;# channel type
set val(prop) Propagation/TwoRayGround ;# radio-propagation model
set val(netif1) Phy/WirelessPhy ;# network interface type
set val(netif2) Phy/WirelessPhy ;# network interface type
set val(mac) Mac/802_11 ;# 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) 50 ;# max packet in ifq
set val(nn) 10 ;# number of mobilenodes
set val(rp) AODV ;# routing protocol
set val(x) 600 ;# X dimension of topography
set val(y) 600 ;# Y dimension of topography
set val(stop) 10.0 ;# time of simulation end
set val(energymodel) EnergyModel ;#Energy set up
# set up topography object
set topo [new Topography]
$topo load_flatgrid $val(x) $val(y)

# general operational descriptor- storing the hop details in the network


create-god $val(nn)

#Transmission range setup

#********************************** UNITY GAIN, 1.5m HEIGHT OMNI


DIRECTIONAL ANTENNA SET UP **************

Antenna/OmniAntenna set X_ 0
Antenna/OmniAntenna set Y_ 0
Antenna/OmniAntenna set Z_ 1.5
Antenna/OmniAntenna set Gt_ 1.0
Antenna/OmniAntenna set Gr_ 1.0

#********************************** SET UP COMMUNICATION AND SENSING


RANGE ***********************************

#default communication range 250m

# Initialize the SharedMedia interface with parameters to make


# it work like the 914MHz Lucent WaveLAN DSSS radio interface
$val(netif1) set CPThresh_ 10.0
$val(netif1) set CSThresh_ 2.28289e-11 ;#sensing range of 500m
$val(netif1) set RXThresh_ 2.28289e-11 ;#communication range of 500m
$val(netif1) set Rb_ 2*1e6
$val(netif1) set Pt_ 0.2818
$val(netif1) set freq_ 914e+6
$val(netif1) set L_ 1.0

# Initialize the SharedMedia interface with parameters to make


# it work like the 914MHz Lucent WaveLAN DSSS radio interface
$val(netif2) set CPThresh_ 10.0
$val(netif2) set CSThresh_ 8.91754e-10 ;#sensing range of 200m
$val(netif2) set RXThresh_ 8.91754e-10 ;#communication range of 200m
$val(netif2) set Rb_ 2*1e6
$val(netif2) set Pt_ 0.2818
$val(netif2) set freq_ 914e+6
$val(netif2) set L_ 1.0

# configure the first 5 nodes with transmission range of 500m

$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(netif1) \
-channelType $val(chan) \
-topoInstance $topo \
-energyModel $val(energymodel) \
-initialEnergy 10 \
-rxPower 0.5 \
-txPower 1.0 \
-idlePower 0.0 \
-sensePower 0.3 \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF \
-movementTrace ON

# Node Creation

set energy(0) 1000

$ns node-config -initialEnergy 1000 \


-rxPower 0.5 \
-txPower 1.0 \
-idlePower 0.0 \
-sensePower 0.3

set node_(0) [$ns node]


$node_(0) color black
# configure the remaining 5 nodes with transmission range of 200m

$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(netif2) \
-channelType $val(chan) \
-topoInstance $topo \
-energyModel $val(energymodel) \
-initialEnergy 10 \
-rxPower 0.5 \
-txPower 1.0 \
-idlePower 0.0 \
-sensePower 0.3 \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF \
-movementTrace ON
for {set i 1} {$i < 3} {incr i} {

set energy($i) [expr rand()*500]


$ns node-config -initialEnergy $energy($i) \
-rxPower 0.5 \
-txPower 1.0 \
-idlePower 0.0 \
-sensePower 0.3
set node_($i) [$ns node]
$node_($i) color black

How to use Loss Monitor in ns2

#Filename: sample13.tcl

#**************** Multiple node Creation and communication model using UDP (User
Datagram Protocol) and LOSS MONITIOR ***************************

#******************************Defining Communication Between node0 and all


nodes ****************************
puts $r "TIME\t\tNODEID\t\tRECEIVED_BYTES"
proc calling { } {

global node_ ns r lm

set now [$ns now]

set time 3.0


for {set i 1} {$i < 10} {incr i} {
set udp [new Agent/UDP]
$ns attach-agent $node_(0) $udp

set cbr [new Application/Traffic/CBR]


$cbr set packetSize_ 512
$cbr set interval_ 0.07
$cbr attach-agent $udp

set lm($i) [new Agent/LossMonitor]


$ns attach-agent $node_($i) $lm($i)

$ns connect $udp $lm($i)


$ns at $now "$cbr start"
$ns at [expr $now + $time] "$cbr stop"

}
}
proc data { } {

global node_ ns lm r

set now [$ns now]

for {set i 1} {$i < 10} {incr i} {

set rd [$lm($i) set bytes_]


puts $r "$now received_bytes of node $i = $rd"

}
}
#Filename: sample3.tcl

#***************Dynamic Wireless network **********************#

#******************Random Topology Creation********************#

#Run Time Argument

if {$argc != 1} {
error "\nCommand: ns sample3.tcl <no.of.nodes>\n\n "
}
set val(nn) [lindex $argv 0] ;# number of mobilenodes
#Random Location for nodes

for {set i 0} {$i < $val(nn)} {incr i} {

$node_($i) set X_ [expr rand()*$val(x)]


$node_($i) set Y_ [expr rand()*$val(y)]
$node_($i) set Z_ 0

}
#Filename: sample1.tcl

#TCL Tool Command Language

# Simulator Instance Creation


set ns [new Simulator]

#Fixing the co-ordinate of simutaion area


set val(x) 500
set val(y) 500
# Define options
set val(chan) Channel/WirelessChannel ;# channel type
set val(prop) Propagation/TwoRayGround ;# radio-propagation model

set val(netif) Phy/WirelessPhy ;# network interface type


set val(mac) Mac/802_11 ;# 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) 50 ;# max packet in ifq
set val(nn) 2 ;# number of mobilenodes
set val(rp) AODV ;# routing protocol
set val(x) 500 ;# X dimension of topography
set val(y) 400 ;# Y dimension of topography
set val(stop) 10.0 ;# time of simulation end

# set up topography object


set topo [new Topography]
$topo load_flatgrid $val(x) $val(y)

#Nam File Creation nam network animator


set namfile [open sample1.nam w]

#Tracing all the events and cofiguration


$ns namtrace-all-wireless $namfile $val(x) $val(y)

#Trace File creation


set tracefile [open sample1.tr w]

#Tracing all the events and cofiguration


$ns trace-all $tracefile
# general operational descriptor- storing the hop details in the network
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) \
-channelType $val(chan) \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF \
-movementTrace ON

# Node Creation
set node1 [$ns node]
# Initial color of the node
$node1 color black

#Location fixing for a single node


$node1 set X_ 200
$node1 set Y_ 100
$node1 set Z_ 0

set node2 [$ns node]


$node2 color black

$node2 set X_ 200


$node2 set Y_ 300
$node2 set Z_ 0
# Label and coloring
$ns at 0.1 "$node1 color blue"
$ns at 0.1 "$node1 label Node1"
$ns at 0.1 "$node2 label Node2"
#Size of the node
$ns initial_node_pos $node1 30
$ns initial_node_pos $node2 30
# ending nam and the simulation
$ns at $val(stop) "$ns nam-end-wireless $val(stop)"
$ns at $val(stop) "stop"

#Stopping the scheduler


$ns at 10.01 "puts \"end simulation\" ; $ns halt"
#$ns at 10.01 "$ns halt"
proc stop {} {
global namfile tracefile ns
$ns flush-trace
close $namfile
close $tracefile
#executing nam file
exec nam sample1.nam &
}

#Starting scheduler
$ns run

#############################################################
Execution:
ns sample1.tcl

You might also like