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 Communication in NS3

To implement the wireless communication in ns-3 consists to setup a network with nodes that makes use of Wi-Fi for communication. Here are the procedures to make a basic wireless network using Wi-Fi in ns-3 environment.

Step-by-Step Guide to Implement Wireless Communication 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., wireless-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”

  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 nodes and configure Wi-Fi settings

NodeContainer wifiStaNodes;

wifiStaNodes.Create (2); // Create 2 station nodes (devices)

NodeContainer wifiApNode;

wifiApNode.Create (1); // Create 1 AP node

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

YansWifiChannelHelper channel = YansWifiChannelHelper::Default ();

YansWifiPhyHelper phy = YansWifiPhyHelper::Default ();

phy.SetChannel (channel.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 mac;

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

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

NetDeviceContainer staDevices = wifi.Install (phy, mac, wifiStaNodes);

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

NetDeviceContainer apDevices = wifi.Install (phy, mac, wifiApNode);

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

InternetStackHelper stack;

stack.Install (wifiApNode);

stack.Install (wifiStaNodes);

Ipv4AddressHelper address;

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

Ipv4InterfaceContainer staInterfaces = address.Assign (staDevices);

Ipv4InterfaceContainer apInterfaces = address.Assign (apDevices);

  1. Configure Mobility:
  • Set up mobility models for the AP and station nodes

MobilityHelper mobility;

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

mobility.Install (wifiApNode);

mobility.SetPositionAllocator (“ns3::GridPositionAllocator”,

                               “MinX”, DoubleValue (0.0),

                               “MinY”, DoubleValue (0.0),

                               “DeltaX”, DoubleValue (5.0),

                               “DeltaY”, DoubleValue (10.0),

                               “GridWidth”, UintegerValue (3),

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

mobility.SetMobilityModel (“ns3::RandomWalk2dMobilityModel”,

                           “Bounds”, RectangleValue (Rectangle (-50, 50, -50, 50)));

mobility.Install (wifiStaNodes);

  1. Install Applications:
  • Set up a UDP echo server on the AP node and a UDP echo client on one of the station nodes:

UdpEchoServerHelper echoServer (9);

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

serverApps.Start (Seconds (1.0));

serverApps.Stop (Seconds (10.0));

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

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

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

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

ApplicationContainer clientApps = echoClient.Install (wifiStaNodes.Get (0));

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 (wireless-simulation.cc):

Here is the sample to complete the script for wireless communication 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 wifiStaNodes;

  wifiStaNodes.Create (2); // Create 2 station nodes (devices)

 

  NodeContainer wifiApNode;

  wifiApNode.Create (1); // Create 1 AP node

  YansWifiChannelHelper channel = YansWifiChannelHelper::Default ();

  YansWifiPhyHelper phy = YansWifiPhyHelper::Default ();

  phy.SetChannel (channel.Create ());

  WifiHelper wifi;

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

  WifiMacHelper mac;

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

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

  NetDeviceContainer staDevices = wifi.Install (phy, mac, wifiStaNodes);

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

  NetDeviceContainer apDevices = wifi.Install (phy, mac, wifiApNode);

  InternetStackHelper stack;

  stack.Install (wifiApNode);

  stack.Install (wifiStaNodes);

  Ipv4AddressHelper address;

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

  Ipv4InterfaceContainer staInterfaces = address.Assign (staDevices);

  Ipv4InterfaceContainer apInterfaces = address.Assign (apDevices);

 

  MobilityHelper mobility;

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

  mobility.Install (wifiApNode);

  mobility.SetPositionAllocator (“ns3::GridPositionAllocator”,

                                 “MinX”, DoubleValue (0.0),

                                 “MinY”, DoubleValue (0.0),

                                 “DeltaX”, DoubleValue (5.0),

                                 “DeltaY”, DoubleValue (10.0),

                                 “GridWidth”, UintegerValue (3),

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

  mobility.SetMobilityModel (“ns3::RandomWalk2dMobilityModel”,

                             “Bounds”, RectangleValue (Rectangle (-50, 50, -50, 50)));

  mobility.Install (wifiStaNodes);

  UdpEchoServerHelper echoServer (9);

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

  serverApps.Start (Seconds (1.0));

  serverApps.Stop (Seconds (10.0));

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

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

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

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

  ApplicationContainer clientApps = echoClient.Install (wifiStaNodes.Get (0));

  clientApps.Start (Seconds (2.0));

  clientApps.Stop (Seconds (10.0));

  Simulator::Stop (Seconds (10.0));

  Simulator::Run ();

  Simulator::Destroy ();

  return 0;

}

Explanation:

Here we have explained the process for wireless communication that performs

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

At last, we discussed about implementation process and setup network configuration in wireless communication in ns-3 environment. We support any kinds of wireless communication networks.