How to Start Leach Routing Projects Using NS3

To start a Low-Energy Adaptive Clustering Hierarchy (LEACH) routing project using NS3 which encompasses to execute or replicate a hierarchical routing protocol that are created for wireless sensor networks (WSNs). LEACH is an energy-efficient protocol, which classifies nodes to clusters including one node to perform like a cluster head, in the course of interaction minimizing energy consumption.

Below is a structured approach on how we start and simulate the LEACH Routing Projects in NS3:

Steps to Start LEACH Routing Projects in NS3

  1. Understand LEACH Routing

Key Features of LEACH:

  1. Clustering:
    • Nodes are gathered to clusters, and each cluster contains a Cluster Head (CH).
  2. Data Aggregation:
    • From cluster members, Cluster Head accumulate information then combined it, and send it to the base station.
  3. Dynamic CH Selection:
    • CHs are occasionally turned to deliver the energy consumption.

Objectives of LEACH:

  • In WSNs, reduce energy consumption.
  • Extend the network lifetime by means of equalizing node energy usage.
  1. Set Up NS3

Install NS3:

  • Go to nsnam.org then we can download and install NS3.
  • Confirm installation:

./waf –run scratch/test-example

Required Modules:

  • internet: It supports for basic network functionalities.
  • wifi: For wireless interaction.
  • energy: For designing node energy consumption, this module is used.
  1. Plan Your LEACH Simulation

Define Objectives:

  • To replicate the LEACH routing within a WSN.
  • Estimate energy efficiency, cluster formation, and network lifetime.

Example Topology:

  • A sensor network including one base station (BS) and numerous sensor nodes:

Base Station (BS)

|

Cluster Heads (CHs)

|

Cluster Members

Metrics to Evaluate:

  • Energy Consumption: We need to assess the energy usage for interaction.
  • Network Lifetime: Measure duration while waiting for the initial node fails.
  • Data Delivery Ratio: We compute the rate of effectively delivered packets.
  1. Implement LEACH Routing

While NS3 doesn’t natively support a built-in LEACH execution then we can make a custom application replicating their behavior.

4.1 Create a LEACH Application

LEACH Application Code:

#include “ns3/core-module.h”

#include “ns3/network-module.h”

#include “ns3/internet-module.h”

#include “ns3/wifi-module.h”

#include “ns3/energy-module.h”

#include “ns3/mobility-module.h”

using namespace ns3;

class LeachRoutingApp : public Application {

public:

LeachRoutingApp();

virtual ~LeachRoutingApp();

void Setup(NodeContainer nodes, Ptr<Node> baseStation, double clusterChangeTime);

private:

virtual void StartApplication();

virtual void StopApplication();

void SelectClusterHeads();

void CommunicateWithBaseStation();

NodeContainer m_nodes;

Ptr<Node>     m_baseStation;

double        m_clusterChangeTime;

std::set<uint32_t> m_clusterHeads; // IDs of current cluster heads

};

LeachRoutingApp::LeachRoutingApp() : m_baseStation(nullptr), m_clusterChangeTime(0) {}

LeachRoutingApp::~LeachRoutingApp() {}

void LeachRoutingApp::Setup(NodeContainer nodes, Ptr<Node> baseStation, double clusterChangeTime) {

m_nodes = nodes;

m_baseStation = baseStation;

m_clusterChangeTime = clusterChangeTime;

}

void LeachRoutingApp::StartApplication() {

SelectClusterHeads();

Simulator::Schedule(Seconds(m_clusterChangeTime), &LeachRoutingApp::SelectClusterHeads, this);

}

void LeachRoutingApp::StopApplication() {}

void LeachRoutingApp::SelectClusterHeads() {

m_clusterHeads.clear();

for (uint32_t i = 0; i < m_nodes.GetN(); ++i) {

Ptr<Node> node = m_nodes.Get(i);

double randomValue = UniformVariable().GetValue();

if (randomValue < 0.1) { // Example threshold for CH selection

m_clusterHeads.insert(node->GetId());

}

}

CommunicateWithBaseStation();

}

void LeachRoutingApp::CommunicateWithBaseStation() {

for (auto it = m_clusterHeads.begin(); it != m_clusterHeads.end(); ++it) {

Ptr<Node> clusterHead = m_nodes.Get(*it);

// Simulate communication between cluster head and base station

NS_LOG_INFO(“Cluster Head ” << clusterHead->GetId() << ” communicating with Base Station”);

}

}

4.2 Use the LEACH Application in a Simulation

Main Simulation Script:

int main(int argc, char *argv[]) {

NodeContainer sensorNodes;

sensorNodes.Create(10); // 10 sensor nodes

Ptr<Node> baseStation = CreateObject<Node>(); // Base station

// Set up mobility

MobilityHelper mobility;

mobility.SetMobilityModel(“ns3::ConstantPositionMobilityModel”);

mobility.Install(sensorNodes);

Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator>();

positionAlloc->Add(Vector(50, 50, 0)); // Base station at (50, 50)

mobility.SetPositionAllocator(positionAlloc);

mobility.Install(baseStation);

// Set up energy model

BasicEnergySourceHelper energySourceHelper;

energySourceHelper.Set(“BasicEnergySourceInitialEnergyJ”, DoubleValue(10.0));

EnergySourceContainer energySources = energySourceHelper.Install(sensorNodes);

// Add WiFi communication

WifiHelper wifi;

YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default();

YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default();

wifiPhy.SetChannel(wifiChannel.Create());

WifiMacHelper wifiMac;

wifi.SetRemoteStationManager(“ns3::ConstantRateWifiManager”);

wifi.Install(wifiPhy, wifiMac, sensorNodes);

// Install Internet stack

InternetStackHelper stack;

stack.Install(sensorNodes);

stack.Install(baseStation);

// Run LEACH application

Ptr<LeachRoutingApp> leachApp = CreateObject<LeachRoutingApp>();

leachApp->Setup(sensorNodes, baseStation, 10.0); // Cluster change every 10 seconds

baseStation->AddApplication(leachApp);

leachApp->SetStartTime(Seconds(1.0));

leachApp->SetStopTime(Seconds(50.0));

Simulator::Run();

Simulator::Destroy();

return 0;

}

  1. Simulate and Debug

Enable Logging:

export NS_LOG=LeachRoutingApp=level_all

./waf –run scratch/leach-routing

Capture Packets with PCAP:

Examine the network traffic to utilize PCAP tracing:

wifiPhy.EnablePcapAll(“leach-routing”);

  1. Evaluate Performance

Metrics to Measure:

  • Energy Consumption: Monitor the energy usage of nodes.
  • Network Lifetime: Estimate the duration until the initial node or a rate of nodes reduces its energy.
  • Data Delivery Ratio: Assess rate of effectively distributed packets to total packets transmitted.

Use FlowMonitor:

FlowMonitorHelper flowMonitor;

Ptr<FlowMonitor> monitor = flowMonitor.InstallAll();

monitor->SerializeToXmlFile(“leach-performance.xml”, true, true);

  1. Advanced Features
  • Dynamic Node Deployment:
    • We need to insert the mobility models to replicate moving sensor nodes.

MobilityHelper mobility;

mobility.SetMobilityModel(“ns3::RandomWaypointMobilityModel”);

mobility.Install(sensorNodes);

  • Optimize CH Selection:
    • For CH selection, we can utilise energy levels, distance to BS, or other factors.
  • Fault Tolerance:
    • Mimic node failures and then monitor the influence over cluster formation.
  1. Document and Visualize

Document Implementation:

  • Clearly describe goals, techniques, and outcomes.

Visualize Results:

  • Envision the simulation to utilize NetAnim tool.
  • We want to plot the performance indicators such as energy consumption or network lifetime to utilize tools such as Matplotlib or Gnuplot.

To conclude, we had exhibited the detailed procedure with sample coding to implement and simulate the LEACH Routing Projects using the simulation tool NS3. Further insights regarding this project will be also made available, if required.

Our researchers will help you pick the perfect topic so you can ace your grades. When you’re ready to kick off your Leach Routing Projects with the NS3 tool, rely on the phdprojects.org team to support you at every step. We take a systematic method to make sure you get straightforward explanations throughout the entire process.  Enjoy personalized help from us to ensure you get top-notch project results delivered right on schedule. We focus on wireless sensor networks (WSNs) and customize everything to fit your research needs.