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 Body Area Network In NS3

To implement a wireless body area network (WBAN) in ns-3 consists to simulate a network of wearable sensors that interact wirelessly. Here are the step by step procedures how to set up a WBAN in ns-3 environment.

Prerequisites

  • ns-3 installed on your system.
  • Basic understanding of ns-3 and C++ programming.

Steps to Implement WBAN in ns-3

  1. Install ns-3: Ensure you have ns-3 installed. You can download it from the official website and follow the installation instructions.

wget https://www.nsnam.org/release/ns-allinone-3.xx.tar.bz2

tar -xjf ns-allinone-3.xx.tar.bz2

cd ns-allinone-3.xx

./build.py –enable-examples –enable-tests

  1. Include Necessary Headers: Include the required headers for network modules, mobility, and internet.

#include “ns3/core-module.h”

#include “ns3/network-module.h”

#include “ns3/mobility-module.h”

#include “ns3/internet-module.h”

#include “ns3/wifi-module.h”

#include “ns3/applications-module.h”

  1. Create Network Topology: Create nodes representing sensors and a coordinator (e.g., a central device collecting data from sensors).

NodeContainer sensorNodes;

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

NodeContainer coordinatorNode;

coordinatorNode.Create(1); // Create 1 coordinator node

  1. Set Up Mobility Model: Configure the mobility model to simulate the placement of the sensors on a human body.

MobilityHelper mobility;

mobility.SetPositionAllocator(“ns3::GridPositionAllocator”,

                              “MinX”, DoubleValue(0.0),

                              “MinY”, DoubleValue(0.0),

                              “DeltaX”, DoubleValue(0.5),

                              “DeltaY”, DoubleValue(0.5),

                              “GridWidth”, UintegerValue(2),

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

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

mobility.Install(sensorNodes);

mobility.Install(coordinatorNode);

  1. Install WiFi Devices: Install WiFi devices on the sensor nodes and the coordinator.

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));

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

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

NetDeviceContainer coordinatorDevice = wifi.Install(wifiPhy, wifiMac, coordinatorNode);

  1. Install Internet Stack: Install the internet stack on the sensor nodes and the coordinator.

InternetStackHelper stack;

stack.Install(sensorNodes);

stack.Install(coordinatorNode);

  1. Assign IP Addresses: Assign IP addresses to the devices.

Ipv4AddressHelper address;

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

Ipv4InterfaceContainer sensorInterfaces = address.Assign(sensorDevices);

Ipv4InterfaceContainer coordinatorInterface = address.Assign(coordinatorDevice);

  1. Create Applications: Create and install applications to simulate data transmission from sensors to the coordinator.

uint16_t port = 9;

 

OnOffHelper onoff(“ns3::UdpSocketFactory”, Address(InetSocketAddress(coordinatorInterface.GetAddress(0), port)));

onoff.SetConstantRate(DataRate(“50kb/s”));

ApplicationContainer apps;

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

    apps.Add(onoff.Install(sensorNodes.Get(i)));

}

apps.Start(Seconds(1.0));

apps.Stop(Seconds(10.0));

PacketSinkHelper sink(“ns3::UdpSocketFactory”, Address(InetSocketAddress(Ipv4Address::GetAny(), port)));

apps = sink.Install(coordinatorNode.Get(0));

apps.Start(Seconds(0.0));

apps.Stop(Seconds(10.0));

  1. Run the Simulation: Finally, run the simulation and clean up.

Simulator::Stop(Seconds(10.0));

Simulator::Run();

Simulator::Destroy();

Example Complete Script

Here are the samples to complete the scripts for setup a basic WBAN in ns-3 environment.

#include “ns3/core-module.h”

#include “ns3/network-module.h”

#include “ns3/mobility-module.h”

#include “ns3/internet-module.h”

#include “ns3/wifi-module.h”

#include “ns3/applications-module.h”

using namespace ns3;

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

{

    NodeContainer sensorNodes;

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

    NodeContainer coordinatorNode;

    coordinatorNode.Create(1); // Create 1 coordinator node

    MobilityHelper mobility;

    mobility.SetPositionAllocator(“ns3::GridPositionAllocator”,

                                  “MinX”, DoubleValue(0.0),

                                  “MinY”, DoubleValue(0.0),

                                  “DeltaX”, DoubleValue(0.5),

                                  “DeltaY”, DoubleValue(0.5),

                                  “GridWidth”, UintegerValue(2),

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

 

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

    mobility.Install(sensorNodes);

    mobility.Install(coordinatorNode);

    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));

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

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

    NetDeviceContainer coordinatorDevice = wifi.Install(wifiPhy, wifiMac, coordinatorNode);

    InternetStackHelper stack;

    stack.Install(sensorNodes);

    stack.Install(coordinatorNode);

    Ipv4AddressHelper address;

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

    Ipv4InterfaceContainer sensorInterfaces = address.Assign(sensorDevices);

    Ipv4InterfaceContainer coordinatorInterface = address.Assign(coordinatorDevice);

 

    uint16_t port = 9;

    OnOffHelper onoff(“ns3::UdpSocketFactory”, Address(InetSocketAddress(coordinatorInterface.GetAddress(0), port)));

    onoff.SetConstantRate(DataRate(“50kb/s”));

    ApplicationContainer apps;

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

        apps.Add(onoff.Install(sensorNodes.Get(i)));

    }

    apps.Start(Seconds(1.0));

    apps.Stop(Seconds(10.0));

    PacketSinkHelper sink(“ns3::UdpSocketFactory”, Address(InetSocketAddress(Ipv4Address::GetAny(), port)));

    apps = sink.Install(coordinatorNode.Get(0));

    apps.Start(Seconds(0.0));

    apps.Stop(Seconds(10.0));

    Simulator::Stop(Seconds(10.0));

    Simulator::Run();

    Simulator::Destroy();

    return 0;

}

Running the Script

Compile and run the script using the following commands:

./waf configure –enable-examples

./waf build

./waf –run <script-name>

Finally, here we provide the general instructions to perform an analysis in wireless body are network in ns-3 environment. We also helpful to give additional information about wireless body area network.