How to Begin Implement a PEGASIS Protocol in NS3

To create a Power-Efficient Gathering in Sensor Information Systems (PEGASIS) is a chain-based hierarchical routing protocol for wireless sensor networks (WSNs). PEGASIS has contains the energy efficiency through organizing nodes in chains and reducing the communication distance for data aggregation. It executes the PEGASIS for ns3 needs generating a custom routing protocol then it is not natively helps for ns3.

Here’s how to begin:

Steps to Begin Implement a PEGASIS Protocol in NS3

  1. Understand PEGASIS Protocol
  • Key Features:
    • It forms a node for chain in which every node has communicates use the closest neighbor.
    • Data is aggregated with the chain and transmitted for base station through one designated node (leader).
    • Rotates the leader for balance the energy usage.
  • Simulation Goals:
    • Estimate the energy efficiency, throughput, and latency in a WSN.
    • Validate the PEGASIS below various network sizes and topologies.
  1. Set Up ns-3 Environment
  1. Install ns-3:

git clone https://gitlab.com/nsnam/ns-3-dev.git

cd ns-3-dev

./build.py

  1. Verify the installation:

./ns3 run hello-simulator

  1. Plan the Network Topology
  • Components:
    • Organized in a field for sensor nodes.
    • Data collection for base station.
  • Topology:
    • Arbitrary placement for nodes.
    • Nodes form a chain in dynamically for during the replication.
  1. Implement PEGASIS
  2. Define the Protocol Logic
  1. Node Chain Formation:
    • Greedy procedures use to connect the nodes according to the minimum distance.
    • Sample: Open the node closest for the base station and improve the nearest unvisited node in the chain.
  2. Leader Selection:
    • Alternate the leader between nodes for balanced energy consumption.
    • Monitor the energy levels for prevent the leader exhaustion.
  3. Data Aggregation:
    • Nodes are aggregate the data received from neighbours and sending the chain.
  4. Transmit to Base Station:
    • The leader transmits the aggregated data for the base station.
  1. Create a Custom Routing Protocol

Encompass the Ipv4RoutingProtocol class in ns3 for execute the PEGASIS routing behavior.

Sample Skeleton Code:

#include “ns3/core-module.h”

#include “ns3/network-module.h”

#include “ns3/internet-module.h”

#include “ns3/mobility-module.h”

#include “ns3/ipv4-routing-protocol.h”

#include <vector>

class PegasisRouting : public ns3::Ipv4RoutingProtocol {

public:

PegasisRouting();

virtual ~PegasisRouting();

// Override routing methods

Ptr<Ipv4Route> RouteOutput(Ptr<Packet> packet, const Ipv4Header &header,

Ptr<NetDevice> oif, Socket::SocketErrno &sockerr) override;

bool RouteInput(Ptr<const Packet> packet, const Ipv4Header &header,

Ptr<const NetDevice> idev, UnicastForwardCallback ucb,

MulticastForwardCallback mcb, LocalDeliverCallback lcb,

ErrorCallback ecb) override;

void CreateChain();

void SelectLeader();

void AggregateData();

void TransmitToBaseStation();

private:

std::vector<ns3::Ptr<ns3::Node>> chain;

uint32_t leaderIndex;

};

  1. Integrate the Protocol
  1. Set Up the Routing Helper:

class PegasisRoutingHelper : public ns3::Ipv4RoutingHelper {

public:

Ptr<Ipv4RoutingProtocol> Create(Ptr<Node> node) const override {

return CreateObject<PegasisRouting>();

}

};

  1. Use the Helper in Simulation:

PegasisRoutingHelper pegasisRouting;

InternetStackHelper internet;

internet.SetRoutingHelper(pegasisRouting);

internet.Install(nodes);

  1. Set Up the Simulation
  2. Define Nodes and Placement

ns3::NodeContainer nodes;

nodes.Create(20);  // 20 sensor nodes

// Set up mobility

ns3::MobilityHelper mobility;

mobility.SetPositionAllocator(“ns3::RandomRectanglePositionAllocator”,

“X”, ns3::StringValue(“ns3::UniformRandomVariable[Min=0.0|Max=500.0]”),

“Y”, ns3::StringValue(“ns3::UniformRandomVariable[Min=0.0|Max=500.0]”));

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

mobility.Install(nodes);

  1. Set Up Communication

// Install network devices

ns3::WifiHelper wifi;

wifi.SetStandard(ns3::WIFI_PHY_STANDARD_80211b);

ns3::WifiMacHelper mac;

mac.SetType(“ns3::AdhocWifiMac”);

ns3::WifiPhyHelper phy = ns3::WifiPhyHelper::Default();

phy.SetChannel(ns3::YansWifiChannelHelper::Default().Create());

ns3::NetDeviceContainer devices = wifi.Install(phy, mac, nodes);

// Assign IP addresses

ns3::Ipv4AddressHelper address;

address.SetBase(“10.1.1.0”, “255.255.255.0”);

address.Assign(devices);

  1. Generate Traffic

// UDP Application: Nodes send data to the leader

ns3::OnOffHelper onOff(“ns3::UdpSocketFactory”,

ns3::Address(ns3::InetSocketAddress(Ipv4Address(“10.1.1.1”), 9)));

onOff.SetAttribute(“DataRate”, ns3::StringValue(“1Mbps”));

onOff.SetAttribute(“PacketSize”, ns3::UintegerValue(512));

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

ns3::ApplicationContainer app = onOff.Install(nodes.Get(i));

app.Start(ns3::Seconds(1.0));

app.Stop(ns3::Seconds(20.0));

}

  1. Run the Simulation

ns3::Simulator::Run();

ns3::Simulator::Destroy();

  1. Analyze Results

Metrics:

  • Energy Efficiency:
    • It calculates the energy consumed through every node.
  • Packet Delivery Ratio (PDR):
    • Ratio of successfully has delivered the packets for the total number of transmitting.
  • Latency:
    • It takes the duration for packets to reach the base station.

Tracing and Visualization:

  • Ensure the tracing for packet analysis.pcap

ns3::AsciiTraceHelper ascii;

phy.EnableAsciiAll(ascii.CreateFileStream(“pegasis.tr”));

phy.EnablePcapAll(“pegasis”);

  • NetAnim for envision the use this tool:

./waf –run “pegasis-simulation –vis”

  1. Iterate and Enhance
  • Advanced Features:
    • Enhance the mobility for sensor nodes.
    • It contains the dynamic energy usage designs.
  • Scenarios:
    • Validate by different numbers of nodes and field sizes.
    • Launch the obstacles or interference.
  • Comparison:
    • It replicates the LEACH or other routing protocols for evaluation.

We understood the basic to advanced implementation process on how the Power-Efficient Collecting in Sensor Information Systems will enhance the energy efficiency in the transmitted data over the network using the ns3 tool. We also distribute how the PEGASIS will perform in other simulation tools.