You are on page 1of 10

COMPUTER NETWORKS LABORATORY (IT8511) PROJECT

Simulation of LEACH (Low-Energy Adaptive Clustering Hierarchy) using


Network Simulator(NS2)

Bachelor of Technology (Information Technology)

DEPARTMENT OF INFORMATION TECHNOLOGY


MADRAS INSTITUTE OF TECHNOLOGY CAMPUS
ANNA UNIVERSITY
November 2014

Submitted by
FEROOZ KHAN.K.M(2012506015)
SIDDARTH.C.R(2012506047)

Abstract:
In wireless sensor networks (WSNs), due to the limitation of nodes energy, energy
efficiency is an important factor should be considered when the protocols are designing. As a
typical representative of hierarchical routing protocols, LEACH Protocol plays an important role.
LEACH protocol is intended to balance the energy consumption of the entire network and
extend the life of the network. Our aim is to simulate and show how leach protocol works in
this kind of environment.

Introduction :
Wireless sensor network (WSN) is a self-organized network composed by a large
number of micro sensors that are randomly deployed in monitoring regional through wireless
communication. With its wide application in military reconnaissance, medical aid, logistics
management, environmental monitoring, agriculture and other commercial areas, WSN has
become the furthermost technology in the field of communication and computer research.
Among the current researches, the clustering routing technology is the most widely influential.
Low-Energy Adaptive Clustering Hierarchy (LEACH) is a classical clustering routing in wireless
sensor networks. LEACH is the earliest proposed single-hop clustering routing protocol in WSN,
it can save network energy greatly compared with the non-cluster routing algorithm.

Tool Used:

Network Simulator 2 (NS2)

Protocol explanation:
LEACH (Low Energy Adaptive Clustering Hierarchy) is designed for sensor networks
where an end-user wants to remotely monitor the environment. In such a situation, the data
from the individual nodes must be sent to a central base station, often located far from the
sensor network, through which the end-user can access the data.
LEACH is a hierarchical protocol in which most nodes transmit to cluster heads, and the
cluster heads aggregate and compress the data and forward it to the base station (sink). Each
node uses a stochastic algorithm at each round to determine whether it will become a cluster
head in this round. LEACH assumes that each node has a radio powerful enough to directly
reach the base station or the nearest cluster head, but that using this radio at full power all the
time would waste energy.

Nodes that have been cluster heads cannot become cluster heads again for P rounds,
where P is the desired percentage of cluster heads. Thereafter, each node has a 1/P probability
of becoming a cluster head in each round. At the end of each round, each node that is not a
cluster head selects the closest cluster head and joins that cluster. The cluster head then
creates a schedule for each node in its cluster to transmit its data.
There are several desirable properties for protocols on these networks:

Use 100's - 1000's of nodes


Maximize system lifetime
Maximize network coverage
Use uniform, battery-operated nodes

Conventional network protocols, such as direct transmission, minimum transmission


energy, multi-hop routing, and clustering all have drawbacks that don't allow them to achieve
all the desirable properties. LEACH includes distributed cluster formation, local processing to
reduce global communication, and randomized rotation of the cluster-heads. Together, these
features allow LEACH to achieve the desired properties. Initial simulations show that LEACH is
an energy-efficient protocol that extends system lifetime.

Source Code (in NS2):


#===================================
#

Simulation parameters setup

#===================================
set val(chan) Channel/WirelessChannel ;# channel type
set val(prop) Propagation/TwoRayGround ;# radio-propagation model
set val(netif) Phy/WirelessPhy
set val(mac) Mac/802_11

;# network interface type


;# MAC type

set val(ifq) Queue/DropTail/PriQueue ;# interface queue type


set val(ll)

LL

;# link layer type

set val(ant) Antenna/OmniAntenna


set val(ifqlen) 50
set val(nn)

16

set val(rp)

DSDV

;# antenna model

;# max packet in ifq


;# number of mobilenodes
;# routing protocol

#===================================
#

Initialization

#===================================
#Create a ns simulator
set ns [new Simulator]

#Setup topography object


set topo

[new Topography]

$topo load_flatgrid $val(x) $val(y)


create-god $val(nn)

#Open the NS trace file


set tracefile [open out1.tr w]
$ns trace-all $tracefile

#Open the NAM trace file


set namfile [open out1.nam w]
$ns namtrace-all $namfile
$ns namtrace-all-wireless $namfile $val(x) $val(y)
set chan [new $val(chan)];#Create wireless channel

#===================================
#

Mobile node parameter setup

#===================================
$ns node-config -adhocRouting $val(rp) \
-llType
-macType

$val(ll) \
$val(mac) \

-ifqType

$val(ifq) \

-antType

$val(ant) \

-propType

$val(prop) \

-phyType

$val(netif) \

-channel

$chan \

-topoInstance $topo \

#===================================
#

Nodes Definition

#===================================
#Creation of nodes
set n0 [$ns node]
$n0 set X_ 437
$n0 set Y_ 301
$n0 set Z_ 0.0
$n0 color orange
$ns at 0.0 "$n0 color orange"
$ns initial_node_pos $n0 20
..

#===================================
#

Setting Destination address

#===================================
$ns at 0.5 "$n0 setdest 741.222 219.713 15.0"
$ns at 0.5 "$n1 setdest 661.911 293.578 15.0"
$ns at 0.0 "$n15 label Host"
..

#===================================
#

Creating cluster head node

#===================================

proc create_cluster_head_node {} {

global val ns_ node_ topo contador_nodos rng


Phy/WirelessPhy set Pt_ $val(pt_cluster_head)
$ns_ node-config -sensorNode ON \
-adhocRouting $val(rp) \
-llType $val(ll) \
.
-rxPower 0.3 \
-txPower 0.6 \
-initialEnergy 100.0 \
-movementTrace OFF
set node_($contador_nodos) [$ns_ node]
$node_($contador_nodos) random-motion 0
set x [$rng uniform 0.0 $val(x)]
set y [$rng uniform 0.0 $val(y)]
set interval [$rng uniform 0.0 1.0]
..
$app_($contador_nodos) attach-processing $processing_($contador_nodos)
$processing_($contador_nodos) node $node_($contador_nodos)
$ns_ at [expr $val(start) + 1 + $interval] "$app_($contador_nodos) start"
$ns_ at $val(stop) "$app_($contador_nodos) stop"
}

#======================================
# Creating agents, sinks and connecting them
#======================================
set tcp [new Agent/TCP]
$tcp set class_ 2
set sink [new Agent/TCPSink]
$ns attach-agent $n0 $tcp
$ns attach-agent $n1 $sink
$ns connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ns at 64.2 "$ftp start"
$ns at 68.0 "$ftp stop"
..
#===================================
#

Finish Procedure

#===================================
#Define a 'finish' procedure
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam out1.nam &
exit 0
}
$ns run

Screenshots:

Initial Formation

Formation after nodes have formed clusters

Data transfer from node to cluster head

Data transfer from cluster head to host(sink)

Conclusion:
Thus LEACH protocol is simulated in Network Simulator 2(NS2) and we could see how
LEACH protocol helps in balancing energy consumption of the whole network and extending the
network lifetime by balancing the energy consumption of these cluster heads.

You might also like