How to Implement LEACH Routing in NS2
To implement the LEACH (Low-Energy Adaptive Clustering Hierarchy) is a famous hierarchical routing protocol that created for the wireless sensor networks (WSNs) to extend the network lifetime by reducing energy consumption. This routing works by dynamically choosing the cluster heads which is aggregate data from the sensor nodes and send it to the base station. Cluster heads rotate to dispense energy consumption between all nodes.
The simulation NS2 does not have built-in support for the LEACH routing. However, it can be executed using custom agents and the modules in NS2. Below is a procedure that implementing the LEACH routing in NS2.
Steps to Implement LEACH in NS2
- Set Up NS2 Environment
- Make sure NS2 is installed and appropriately set up on the system.
- Install the mit/uAMPS module that delivers the LEACH-specific executions for the simulation NS2. These module can be discovered in external repositories such as these provided by the universities or researchers.
- Understand LEACH Protocol
- Cluster Formation: LEACH protocol randomly choose the cluster heads from the nodes in the network. Cluster heads transmit their status, and other nodes attach the closest cluster head.
- Data Aggregation: Non-cluster head nodes are send their data to the cluster head that aggregates the data and forwards it to the base station.
- Cluster Head Rotation: To prevent the draining the energy of the cluster heads, LEACH protocol rotates the role of cluster head between the nodes in every round.
- Install LEACH Protocol for NS2
- If we don’t have already installed the LEACH module, then we follow the given steps to append the mit/uAMPS module to the NS2 environment:
- Download the uAMPS patch for NS2 in LEACH implementation from repositories such as the MIT/UAMPS project or other sources.
- Apply the patch and recompile NS2.
- If we don’t have already installed the LEACH module, then we follow the given steps to append the mit/uAMPS module to the NS2 environment:
After installing the LEACH module, the LEACH functionality will be obtainable in the NS2 simulation.
Sample Script to Implement LEACH in NS2
The following is a specimen OTcl script that mimics a basic LEACH-based wireless sensor network in NS2.
- Set up the Wireless Sensor Network
# Create the simulator object
set ns [new Simulator]
# Open the trace file for recording data
set tracefile [open out.tr w]
$ns trace-all $tracefile
# Define the finish procedure
proc finish {} {
global ns tracefile
$ns flush-trace
close $tracefile
exec nam out.nam &
exit 0
}
# Create the wireless channel and set up parameters for the WSN
set chan [new Channel/WirelessChannel]
$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 -channel $chan -topoInstance $topo \
-agentTrace ON -routerTrace ON -macTrace OFF -movementTrace OFF
# Create a topology object
set topo [new Topography]
# Define the topology boundary (1000m x 1000m)
$topo load_flatgrid 1000 1000
# Define the number of sensor nodes
set num_nodes 10
# Create sensor nodes
for {set i 0} {$i < $num_nodes} {incr i} {
set node($i) [$ns node]
$node($i) random-motion 0 ;# Disable node mobility (static WSN)
}
# Set the initial node positions (randomly for simplicity)
$node(0) set X_ 100
$node(0) set Y_ 300
$node(1) set X_ 500
$node(1) set Y_ 700
$node(2) set X_ 800
$node(2) set Y_ 200
$node(3) set X_ 400
$node(3) set Y_ 600
$node(4) set X_ 600
$node(4) set Y_ 300
$node(5) set X_ 700
$node(5) set Y_ 500
$node(6) set X_ 900
$node(6) set Y_ 700
$node(7) set X_ 300
$node(7) set Y_ 800
$node(8) set X_ 500
$node(8) set Y_ 100
$node(9) set X_ 200
$node(9) set Y_ 500
# Set up a base station (sink) at a specific position (e.g., node 0 is base station)
set baseStation $node(0)
$baseStation setdest 500 500 0
# Configure the LEACH protocol
for {set i 0} {$i < $num_nodes} {incr i} {
$node($i) set energyModel EnergyModel
$node($i) set initialEnergy 100.0 ;# 100 units of energy per node
$node($i) set txPower 0.5 ;# Transmission power
$node($i) set rxPower 0.2 ;# Reception power
}
# Start the LEACH routing process
$ns at 0.0 “[$baseStation agent] start”
# Stop the simulation after 50 seconds
$ns at 50.0 “finish”
# Run the simulation
$ns run
Explanation of the Script:
- Wireless Network Configuration: The wireless network is set up with the 802.11 MAC, a TwoRayGround propagation model, and the static nodes. This mimics a usual WSN situation.
- Node Configuration: The nodes are set up to use the LEACH protocol as its routing algorithm (-adhocRouting LEACH). This sensor nodes that positioned statically in a 1000×1000 grid.
- Energy Model: Every sensor node is given primary energy level of the 100 units. Power consumption for transferring and receiving is set to 0.5 and 0.2 units, correspondily.
- Base Station: The node 0 is created as the base station (sink) in which all the aggregated data from the cluster heads will send.
- Cluster Head Selection: LEACH routing will automatically manage the dynamic selection of the cluster heads and data aggregation.
- Simulation Duration: The simulation runs for 50 seconds.
Running the Simulation
- We can save the script as leach_simulation.tcl.
- Run the simulation using NS2:
ns leach_simulation.tcl
- The simulation will create a Network Animator (NAM) file (out.nam) and a trace file (out.tr). We can use the NAM to visualize the sensor network and how the LEACH protocol schematises the nodes into clusters and forwards data.
Analysing the Results
- Energy Efficiency: LEACH is created to reduce energy consumption. Evaluate the trace file to manage the energy usage of every node and the total energy consumption.
- Cluster Formation: We can use the NAM tool to visualize how cluster heads are chosen and how data is transferred to the base station via those cluster heads.
- Packet Delivery: Test the trace file to establish the packet delivery ratio that shows how well the data is aggregated and transmitted to the base station.
Enhancements
- Energy Optimization: Test with various primary energy levels or power consumption values to monitor how the network performs.
- Cluster Head Rotation: Learn how often the cluster heads are rotated, and how this effects the energy consumption and the network performance.
- Node Failure: Launch the node failures or minimize the energy levels of particular nodes during the simulation to observe how the LEACH protocol adjusts.
- Scalability: Enlarge the number of nodes in the network to learn how LEACH scales with larger WSNs.
We applied a sequential procedure to LEACH Routing, which was then implemented and evaluated using the simulation tool ns2. If you want further details regarding this, we will offered.
Obtain expert guidance on LEACH Routing in NS2 from our top developers. We have a wealth of project ideas and are committed to steering you in the right direction with timely delivery. Our team specializes in all nodes and modules customized to meet your project requirements.