Ns3 Projects for B.E/B.Tech M.E/M.Tech PhD Scholars.  Phone-Number:9790238391   E-mail: ns3simulation@gmail.com

How To Implement Wireless Sensor Network in Ns3

To implement a wireless sensor network (WSN) in ns-3 consists of to make a network sensor node that interact with each other wirelessly. Here are the procedures to set up an elementary WSN using ns-3.

Step-by-Step Guide to Implement a Wireless Sensor Network in ns-3

  1. Install ns-3:
    • Ensure ns-3 is installed. Follow the installation instructions for ns-3 if you haven’t done so already.
  2. Create a Simulation Script:
    • Create a new script file, e.g., wsn-simulation.cc.
  3. Include Necessary Headers:

#include “ns3/core-module.h”

#include “ns3/network-module.h”

#include “ns3/internet-module.h”

#include “ns3/mobility-module.h”

#include “ns3/wifi-module.h”

#include “ns3/applications-module.h”

#include “ns3/energy-module.h”

#include “ns3/simple-wireless-module.h”

  1. Define the Main Function:

using namespace ns3;

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

{

  CommandLine cmd;

  cmd.Parse (argc, argv);

  1. Set Up the Network Topology:
  • Create sensor nodes and configure Wi-Fi settings

NodeContainer sensorNodes;

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

NodeContainer sinkNode;

sinkNode.Create (1); // Create 1 sink node

  1. Configure Wi-Fi Channel and PHY Layer:
  • Set up the Wi-Fi channel and physical layer:

YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();

YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();

wifiPhy.SetChannel (wifiChannel.Create ());

  1. Configure Wi-Fi MAC Layer:
  • Set up the MAC layer and install Wi-Fi devices on nodes:

WifiHelper wifi;

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

WifiMacHelper wifiMac;

Ssid ssid = Ssid (“ns-3-ssid”);

 

wifiMac.SetType (“ns3::StaWifiMac”, “Ssid”, SsidValue (ssid), “ActiveProbing”, BooleanValue (false));

NetDeviceContainer sensorDevices = wifi.Install (wifiPhy, wifiMac, sensorNodes);

wifiMac.SetType (“ns3::ApWifiMac”, “Ssid”, SsidValue (ssid));

NetDeviceContainer sinkDevice = wifi.Install (wifiPhy, wifiMac, sinkNode);

  1. Install Internet Stack:
  • Install the Internet stack on all nodes

InternetStackHelper stack;

stack.Install (sensorNodes);

stack.Install (sinkNode);

Ipv4AddressHelper address;

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

Ipv4InterfaceContainer sensorInterfaces = address.Assign (sensorDevices);

Ipv4InterfaceContainer sinkInterface = address.Assign (sinkDevice);

  1. Configure Mobility:
  • Set up mobility models for the sensor nodes and sink node:

MobilityHelper mobility;

mobility.SetPositionAllocator (“ns3::GridPositionAllocator”,

                               “MinX”, DoubleValue (0.0),

                               “MinY”, DoubleValue (0.0),

                               “DeltaX”, DoubleValue (5.0),

                               “DeltaY”, DoubleValue (5.0),

                               “GridWidth”, UintegerValue (5),

                               “LayoutType”, StringValue (“RowFirst”));

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

mobility.Install (sensorNodes);

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

mobility.Install (sinkNode);

  1. Install Applications:
  • Set up a UDP echo server on the sink node and a UDP echo client on the sensor nodes to simulate data collection:

UdpEchoServerHelper echoServer (9);

ApplicationContainer serverApps = echoServer.Install (sinkNode.Get (0));

serverApps.Start (Seconds (1.0));

serverApps.Stop (Seconds (10.0));

UdpEchoClientHelper echoClient (sinkInterface.GetAddress (0), 9);

echoClient.SetAttribute (“MaxPackets”, UintegerValue (1));

echoClient.SetAttribute (“Interval”, TimeValue (Seconds (1.0)));

echoClient.SetAttribute (“PacketSize”, UintegerValue (1024));

ApplicationContainer clientApps = echoClient.Install (sensorNodes);

clientApps.Start (Seconds (2.0));

clientApps.Stop (Seconds (10.0));

  1. Run the Simulation:
  • Define the simulation stop time and start the simulator:

Simulator::Stop (Seconds (10.0));

Simulator::Run ();

Simulator::Destroy ();

return 0;

Example Complete Script (wsn-simulation.cc):

Here, we provide the sample snippet to complete the script for wireless sensor network simulation in ns-3 environment.

#include “ns3/core-module.h”

#include “ns3/network-module.h”

#include “ns3/internet-module.h”

#include “ns3/mobility-module.h”

#include “ns3/wifi-module.h”

#include “ns3/applications-module.h”

using namespace ns3;

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

{

  CommandLine cmd;

  cmd.Parse (argc, argv);

 

  NodeContainer sensorNodes;

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

  NodeContainer sinkNode;

  sinkNode.Create (1); // Create 1 sink node

  YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();

  YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();

  wifiPhy.SetChannel (wifiChannel.Create ());

  WifiHelper wifi;

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

  WifiMacHelper wifiMac;

  Ssid ssid = Ssid (“ns-3-ssid”);

  wifiMac.SetType (“ns3::StaWifiMac”, “Ssid”, SsidValue (ssid), “ActiveProbing”, BooleanValue (false));

  NetDeviceContainer sensorDevices = wifi.Install (wifiPhy, wifiMac, sensorNodes);

  wifiMac.SetType (“ns3::ApWifiMac”, “Ssid”, SsidValue (ssid));

  NetDeviceContainer sinkDevice = wifi.Install (wifiPhy, wifiMac, sinkNode);

  InternetStackHelper stack;

  stack.Install (sensorNodes);

  stack.Install (sinkNode);

  Ipv4AddressHelper address;

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

  Ipv4InterfaceContainer sensorInterfaces = address.Assign (sensorDevices);

  Ipv4InterfaceContainer sinkInterface = address.Assign (sinkDevice);

  MobilityHelper mobility;

  mobility.SetPositionAllocator (“ns3::GridPositionAllocator”,

                                 “MinX”, DoubleValue (0.0),

                                 “MinY”, DoubleValue (0.0),

                                 “DeltaX”, DoubleValue (5.0),

                                 “DeltaY”, DoubleValue (5.0),

                                 “GridWidth”, UintegerValue (5),

                                 “LayoutType”, StringValue (“RowFirst”));

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

  mobility.Install (sensorNodes);

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

  mobility.Install (sinkNode);

  UdpEchoServerHelper echoServer (9);

  ApplicationContainer serverApps = echoServer.Install (sinkNode.Get (0));

  serverApps.Start (Seconds (1.0));

  serverApps.Stop (Seconds (10.0));

  UdpEchoClientHelper echoClient (sinkInterface.GetAddress (0), 9);

  echoClient.SetAttribute (“MaxPackets”, UintegerValue (1));

  echoClient.SetAttribute (“Interval”, TimeValue (Seconds (1.0)));

  echoClient.SetAttribute (“PacketSize”, UintegerValue (1024));

  ApplicationContainer clientApps = echoClient.Install (sensorNodes);

  clientApps.Start (Seconds (2.0));

  clientApps.Stop (Seconds (10.0));

  Simulator::Stop (Seconds (10.0));

  Simulator::Run ();

  Simulator::Destroy ();

  return 0;

}

Explanation:

The given below are the process for wireless sensor network in ns-3 environment that are

  1. Network Configuration:
    • Nodes are created for the sensor nodes and the sink node.
    • The Wi-Fi channel and physical layer (PHY) are configured using YansWifiChannelHelper and YansWifiPhyHelper.
    • The Wi-Fi MAC layer is set up for both sensor nodes and the sink node, and the Wi-Fi devices are installed on the nodes.
  2. Internet Stack:
    • The Internet stack is installed on all nodes, and IP addresses are assigned to the network devices.
  3. Mobility:
    • The mobility model is set for the sensor nodes and the sink node using ConstantPositionMobilityModel and GridPositionAllocator.
  4. Applications:
    • A UDP echo server application is installed on the sink node.
    • UDP echo client applications are installed on the sensor nodes to simulate data collection.
  5. Running the Simulation:
    • The simulation is run for a specified duration, and then the simulator is destroyed to clean up.

Lastly we discussed how to implement and simulate the wireless sensor network in ns-3 environment , we support and provide additional information about wireless sensor network.