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

How to Implement 3D Wireless Sensor Modeling in ns3

To implement 3D wireless sensor modeling in ns3, we need to follow several steps. First, we need to set-up a simulation in which sensor nodes are placed in a three-dimensional space and then we have to analyze their interactions. Below given steps will guide on how to implement 3D Wireless sensor modeling:

Step-by-step guide on how to implement

Step 1: Set Up the ns3 Environment

  1. Install ns3: Make sure that ns3 is installed on the system.

sudo apt-get update

sudo apt-get install ns3

Create a New ns-3 Project: Create a directory for the new project within the ns3 workspace.

cd ns3

mkdir scratch/3d-wireless-sensor-modeling

Step 2: Define the 3D Mobility Model

  1. Create a New Simulation Script: Create a new script in scratch directory to implement the simulation scenario.

// 3d-wireless-sensor-modeling.cc

#include “ns3/core-module.h”

#include “ns3/network-module.h”

#include “ns3/internet-module.h”

#include “ns3/point-to-point-module.h”

#include “ns3/applications-module.h”

#include “ns3/mobility-module.h”

#include “ns3/wifi-module.h”

using namespace ns3;

NS_LOG_COMPONENT_DEFINE(“3DWirelessSensorModeling”);

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

CommandLine cmd;

cmd.Parse(argc, argv);

NodeContainer nodes;

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

// Configure WiFi

WifiHelper wifi;

wifi.SetStandard(WIFI_PHY_STANDARD_80211b);

WifiMacHelper wifiMac;

YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default();

YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default();

wifiPhy.SetChannel(wifiChannel.Create());

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

NetDeviceContainer devices = wifi.Install(wifiPhy, wifiMac, nodes);

// Set up mobility model

MobilityHelper mobility;

mobility.SetPositionAllocator(“ns3::RandomBoxPositionAllocator”,

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

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

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

 

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

mobility.Install(nodes);

// Install the internet stack

InternetStackHelper stack;

stack.Install(nodes);

Ipv4AddressHelper address;

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

Ipv4InterfaceContainer interfaces = address.Assign(devices);

// Install applications (e.g., OnOff applications)

OnOffHelper onOffHelper(“ns3::UdpSocketFactory”, Address());

onOffHelper.SetAttribute(“OnTime”, StringValue(“ns3::ConstantRandomVariable[Constant=1]”));

onOffHelper.SetAttribute(“OffTime”, StringValue(“ns3::ConstantRandomVariable[Constant=0]”));

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

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

Ipv4Address addr = node->GetObject<Ipv4>()->GetAddress(1, 0).GetLocal();

AddressValue remoteAddress(InetSocketAddress(addr, 8080));

onOffHelper.SetAttribute(“Remote”, remoteAddress);

ApplicationContainer app = onOffHelper.Install(node);

app.Start(Seconds(1.0));

app.Stop(Seconds(10.0));

}

// Enable tracing

AsciiTraceHelper ascii;

wifiPhy.EnableAsciiAll(ascii.CreateFileStream(“3d-wireless-sensor-modeling.tr”));

wifiPhy.EnablePcapAll(“3d-wireless-sensor-modeling”);

Simulator::Stop(Seconds(10.0));

Simulator::Run();

Simulator::Destroy();

return 0;

}

Compile the Script: Compile the script using the waf build system.

./waf build

Run the Simulation: Run the simulation script and observe the results.

./waf –run scratch/3d-wireless-sensor-modeling

Step 3: Set Up the 3D Mobility Model

  1. Define the 3D Position Allocator: Use the RandomBoxPositionAllocator to allocate random positions in a 3D space.

mobility.SetPositionAllocator(“ns3::RandomBoxPositionAllocator”,

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

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

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

Assign the Mobility Model: Use the ConstantPositionMobilityModel for static nodes or another mobility model if you want the nodes to move.

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

mobility.Install(nodes);

Step 4: Configure WiFi and Applications

  1. Configure WiFi: Set up the WiFi devices and the Ad-hoc MAC layer

WifiHelper wifi;

wifi.SetStandard(WIFI_PHY_STANDARD_80211b);

WifiMacHelper wifiMac;

YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default();

YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default();

wifiPhy.SetChannel(wifiChannel.Create());

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

NetDeviceContainer devices = wifi.Install(wifiPhy, wifiMac, nodes);

Install Internet Stack: Install the internet stack on the nodes.

InternetStackHelper stack;

stack.Install(nodes);

Ipv4AddressHelper address;

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

Ipv4InterfaceContainer interfaces = address.Assign(devices);

Set Up Applications: Use OnOffHelper or other application helpers to generate traffic in the network.

OnOffHelper onOffHelper(“ns3::UdpSocketFactory”, Address());

onOffHelper.SetAttribute(“OnTime”, StringValue(“ns3::ConstantRandomVariable[Constant=1]”));

onOffHelper.SetAttribute(“OffTime”, StringValue(“ns3::ConstantRandomVariable[Constant=0]”));

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

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

Ipv4Address addr = node->GetObject<Ipv4>()->GetAddress(1, 0).GetLocal();

AddressValue remoteAddress(InetSocketAddress(addr, 8080));

onOffHelper.SetAttribute(“Remote”, remoteAddress);

ApplicationContainer app = onOffHelper.Install(node);

app.Start(Seconds(1.0));

app.Stop(Seconds(10.0));

}

Step 5: Enable Tracing and Run the Simulation

  1. Enable Tracing: Enable tracing to collect data for analysis.

AsciiTraceHelper ascii;

wifiPhy.EnableAsciiAll(ascii.CreateFileStream(“3d-wireless-sensor-modeling.tr”));

wifiPhy.EnablePcapAll(“3d-wireless-sensor-modeling”);

Run the Simulation: Set the simulation stop time and run it.

Simulator::Stop(Seconds(10.0));

Simulator::Run();

Simulator::Destroy();

At last, we had concluded the steps and scripts for implementing the 3D Wireless Sensor modeling in ns3 by defining the mobility model, setting up 3D mobility model, configuring Wi-fi and applications, enable tracing and running the simulation for analyzing the output.

Connect with us for implementation of  3D wireless sensor modelling for your projects , we have needed resources to guide you.