To simulate Low-Energy Adaptive Clustering Hierarchy (LEACH) routing projects using NS2, we will require installing the LEACH protocol into NS2. LEACH is a hierarchical clustering-based routing protocol created for wireless sensor networks to reduce energy consumption. It forms clusters and randomly chooses cluster heads to evenly deliver the energy load between the sensor nodes.
The simulation platform NS2 does not come with a built-in implementation of LEACH, however there are LEACH protocol executions obtainable, which we can combine into NS2. Here’s how we can simulate LEACH routing projects using NS2:
Steps to Simulate LEACH Routing in NS2
- Install NS2 with LEACH Support
We require downloading and installing a changed version of NS2, which encompasses the LEACH protocol. There are numerous versions available online. A general method is to utilize NS2 with Mannasim patches that support LEACH and other WSN protocols.
- Install NS2 by following the official guidelines from NS2 Website.
- We require searching for and applying patches, which contain LEACH for NS2, or download a version of NS2 that has LEACH pre-installed, like Mannasim.
- Install LEACH Protocol in NS2
If we have a simple version of NS2 without LEACH then we follow these steps:
- Download the LEACH patch for NS2 from a reliable source.
- Implement the patch to NS2 by changing the NS2 source code, then rebuild NS2 by executing make clean and make in the NS2 directory.
- Create a TCL Script for LEACH Simulation
When we have LEACH combined into NS2, we can begin by making a basic TCL script, which describes the network topology, sensor nodes, and traffic sources.
Example TCL Script for LEACH Simulation:
# Create a simulator instance
set ns [new Simulator]
# Define a trace file and nam file for analysis and visualization
set tracefile [open “leach_trace.tr” w]
set namfile [open “leach_simulation.nam” w]
$ns trace-all $tracefile
$ns namtrace-all $namfile
# Create a topology object for wireless sensor networks
set topo [new Topography]
$topo load_flatgrid 500 500
# Set up the network parameters for wireless communication
create-god 100 # Number of sensor nodes
$ns node-config -adhocRouting LEACH \
-llType LL \
-macType Mac/802_11 \
-ifqType Queue/DropTail/PriQueue \
-ifqLen 50 \
-antType Antenna/OmniAntenna \
-propType Propagation/TwoRayGround \
-phyType Phy/WirelessPhy \
-channelType Channel/WirelessChannel \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF \
-movementTrace OFF
# Create sensor nodes in the network
for {set i 0} {$i < 100} {incr i} {
set node_($i) [$ns node]
$node_($i) random-motion 0 ;# Disable random node movement
}
# Define a UDP agent and attach it to node 0 (source)
set udp0 [new Agent/UDP]
$ns attach-agent $node_(0) $udp0
# Define a traffic generator (CBR) for the UDP agent
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 512
$cbr0 set rate_ 1Mb
$cbr0 attach-agent $udp0
# Attach a UDP sink to the last node (destination)
set udp1 [new Agent/UDP]
$ns attach-agent $node_(99) $udp1
# Connect the source to the destination
$ns connect $udp0 $udp1
# Schedule the traffic
$ns at 1.0 “$cbr0 start”
$ns at 10.0 “$cbr0 stop”
# Set up the finish procedure
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exit 0
}
# Schedule the end of the simulation
$ns at 11.0 “finish”
# Run the simulation
$ns run
- Run the LEACH Simulation
After saving the above script as leach_simulation.tcl then we can execute it with NS2:
ns leach_simulation.tcl
- Visualize the Simulation with NAM
To envision how the LEACH protocol operates, open the generated .nam file in the NAM tool:
nam leach_simulation.nam
- Analyse the Trace File
The trace file (leach_trace.tr) will have records of the simulation that can be examined for:
- Energy consumption
- Packet delivery ratio
- End-to-end delay
- Number of cluster head selections
- Modify the Simulation Parameters
- Number of Nodes: We can alter the amount of nodes to monitor how LEACH scales.
- Cluster Head Selection: Change the energy thresholds or selection frequency to estimate how the protocol adjusts to energy usage.
- Node Mobility: Even though LEACH is created for stationary nodes, we can test with node mobility.
Optional Enhancements:
- Energy Analysis: We can replicate diverse energy models for nodes to learn how LEACH saves energy.
- Performance Comparison: Replicate other WSN routing protocols like PEGASIS or TEEN to relate performance parameters such as energy efficiency, throughput, and delay.
We have showcased the valuable information on how to execute and analyse the Leach Routing projects within NS2 simulation environment and how to enhance the energy analysis and performance of these project. If needed, we will provide the specific insights on this topic through another manual.
All you need to do is just share your Low-Energy Adaptive Clustering Hierarchy (LEACH) routing project detail to us we provide you with top expert’s simulation help.