To implement the wireless communication in ns-3 consists to set-up a network with nodes that makes use of Wi-Fi for communication. We do all types of performance analysis related to your project. Here are the procedures to make a simple wireless network using the Wi-Fi in ns-3 environment.
Step-by-Step Guide to Implement Wireless Communication in ns-3
- Install ns-3:
- Ensure ns-3 is installed. Follow the installation instructions for ns-3 if you haven’t done so already.
- Create a Simulation Script:
- Create a new script file, e.g., wireless-simulation.cc.
- 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”
- Define the Main Function:
using namespace ns3;
int main (int argc, char *argv[])
{
CommandLine cmd;
cmd.Parse (argc, argv);
- 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
- 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 ());
- 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);
- 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);
- 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);
- 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));
- 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):
At this point we provide the sample to complete the script for wireless 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 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:
The given below are the process for wireless in ns-3 environment
- 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.
- Internet Stack:
- The Internet stack is installed on all nodes, and IP addresses are assigned to the network devices.
- Mobility:
- The mobility model is set for the AP and STA nodes using ConstantPositionMobility Model and RandomWalk2dMobilityModel.
- 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.
- Running the Simulation:
- The simulation is run for a specified duration, and then the simulator is destroyed to clean up.
Finally, here we discussed about the implementation process for wireless in ns-3 environment and we provide any kind of information about wireless.