To simulate Wireless Sensor Networks (WSN) projects in NS2 has needs to configure a network of sensor nodes that interact wirelessly to gather, transmit, and process information in a decentralized manner. Wireless Sensor Networks are generally utilized in applications like environmental monitoring, smart cities, healthcare, and IoT systems. NS2, with its support for wireless protocols and mobility, can replicate the communication and characteristics of sensor nodes. To get best simulation along with complete reasech services drop a message to phdprime.com we guarantee best services.
Here’s a step-by-step guide on how to simulate WSN projects using NS2.
Steps to Simulate Wireless Sensor Network Projects in NS2
- Install NS2
If you don’t have NS2 installed on the system, download it from the NS2 website. Follow the installation instructions depending on operating system (Linux is recommended).
- Basic Concept of Wireless Sensor Networks (WSNs)
A WSN consists of several key components:
- Sensor Nodes: Devices that sense environmental conditions and interact wirelessly.
- Sink/Base Station: A central node that collects data from the sensor nodes.
- Routing Protocols: Protocols that describe how data is transmitted among sensor nodes and the base station.
- Traffic Pattern: Data is often generated occasionally by the sensor nodes and routed to the base station.
- Setting up a Simple WSN in NS2
Example of a Simple WSN Simulation in NS2
In this instance, we will configure a wireless sensor network in which multiple sensor nodes interact with a sink (base station) using a wireless channel.
# Create an NS2 simulator instance
set ns [new Simulator]
# Define a wireless channel for the sensor nodes
set chan [new Channel/WirelessChannel]
# Configure wireless nodes with 802.11 MAC and Omni antenna
$ns node-config -adhocRouting AODV -llType LL -macType Mac/802_11 \
-ifqType Queue/DropTail/PriQueue -ifqLen 50 -antType Antenna/OmniAntenna \
-propType Propagation/TwoRayGround -phyType Phy/WirelessPhy \
-channelType Channel/WirelessChannel
# Create the sink (base station) node
set sink [$ns node]
$sink set X_ 200.0
$sink set Y_ 200.0
$sink set Z_ 0.0
# Create sensor nodes and set their positions
set num_sensors 4
for {set i 0} {$i < $num_sensors} {incr i} {
set node($i) [$ns node]
$node($i) set X_ [expr 100 + 50*$i]
$node($i) set Y_ [expr 150 + 30*$i]
$node($i) set Z_ 0.0
}
# Attach UDP agents to the sensor nodes for communication
for {set i 0} {$i < $num_sensors} {incr i} {
set udp($i) [new Agent/UDP]
set sinkAgent($i) [new Agent/Null]
$ns attach-agent $node($i) $udp($i)
$ns attach-agent $sink $sinkAgent($i)
$ns connect $udp($i) $sinkAgent($i)
}
# Set up Constant Bit Rate (CBR) traffic for data generation from sensors
for {set i 0} {$i < $num_sensors} {incr i} {
set cbr($i) [new Application/Traffic/CBR]
$cbr($i) set packetSize_ 512
$cbr($i) set interval_ 1.0 ;# Send one packet per second
$cbr($i) attach-agent $udp($i)
$ns at 1.0 “$cbr($i) start” ;# Start data generation at time 1.0s
}
# Enable tracing for analysis
set tracefile [open wsn_trace.tr w]
$ns trace-all $tracefile
# Finish procedure to end the simulation
proc finish {} {
global ns tracefile
$ns flush-trace
close $tracefile
exit 0
}
# Run the simulation for 10 seconds
$ns at 10.0 “finish”
$ns run
Explanation of the Script:
- Wireless Channel: A wireless channel is defined for sensor nodes to interact using 802.11 MAC and Omni antenna.
- Sink/Base Station: A sink node is generated at a central position in the network to gather data from sensor nodes.
- Sensor Nodes: Multiple sensor nodes are generated, and each is positioned using set X_ and set Y_ values.
- UDP Agents: A UDP agent is attached to each sensor node for transmitting data to the sink. The sink is configures to receive data using Null agents.
- CBR Traffic: Constant Bit Rate (CBR) traffic is created at each sensor node to replicate data sensing. The packet size is set to 512 bytes, and each node transmits a packet every second.
- Trace File: The simulation outputs a trace file (wsn_trace.tr) for evaluate of packet transmissions, receptions, and other events.
- Mobility in Wireless Sensor Networks
In some WSN scenarios, the sensor nodes or the sink be mobile. We can establish mobility using NS2’s mobility models. For instance, here’s how to move the sink node dynamically in the period of the simulation:
# Set initial position of the sink
$sink setdest 200.0 200.0 10.0 ;# Sink initially at (200, 200)
# Move the sink to a new position after 5 seconds
$ns at 5.0 “$sink setdest 300.0 300.0 10.0” ;# Move to (300, 300) at 10 m/s
- Energy Consumption in WSN
In WSNs, energy efficiency is vital because sensor nodes are usually a battery-powered. NS2 does not directly support energy models, however we can prolong it by adding an energy consumption model for each node. The energy model can monitor the energy consumed in the course of transmissions, receptions, and idle periods.
Example of Adding Energy Model:
# Configure energy model for sensor nodes
$ns node-config -energyModel EnergyModel -initialEnergy 100 -rxPower 0.5 -txPower 1.0 -idlePower 0.1
# Create sensor nodes with the energy model
for {set i 0} {$i < $num_sensors} {incr i} {
set node($i) [$ns node]
$node($i) set X_ [expr 100 + 50*$i]
$node($i) set Y_ [expr 150 + 30*$i]
$node($i) set Z_ 0.0
}
# You can check the energy level during simulation
$ns at 5.0 “$node(0) energy”
- Routing in Wireless Sensor Networks
Routing protocols in WSNs describe how sensor nodes transmit their data to the sink efficiently. Common routing protocols utilized in WSNs include:
- LEACH (Low-Energy Adaptive Clustering Hierarchy): A clustering-based protocol in which nodes are grouped into clusters with cluster heads.
- AODV (Ad hoc On-Demand Distance Vector): A dynamic routing protocol that identifies routes as needed.
- DSDV (Destination-Sequenced Distance-Vector): A proactive routing protocol.
In the example script, AODV is utilized as the default routing protocol.
To replicate a WSN with LEACH, we will need to install and execute a LEACH extension for NS2 (NS2 does not natively support LEACH).
- Data Aggregation in WSN
Data aggregation is utilized in WSNs to minimize redundancy and reduce data transmission. We can replicate data aggregation by adapting the traffic pattern so that intermediate nodes gather the data before forwarding it to the sink.
- Advanced WSN Scenarios
8.1. Multi-Hop WSN
In many WSNs, sensor nodes not able to interact directly with the sink because of distance. In such cases, the data is forwarded via intermediate sensor nodes.
# Example of multi-hop communication between sensor nodes and sink
$ns connect $udp(0) $sinkAgent(2) ;# Node 0 sends data to Node 2 (intermediate hop)
$ns connect $udp(2) $sinkAgent(3) ;# Node 2 sends data to sink (Node 3)
8.2. Cluster-Based WSN
In a cluster-based WSN, nodes are organized into clusters with cluster heads that gathers and forward data to the sink.
- Performance Metrics in WSN Simulation
After processing a WSN simulation, we need to measure the following parameters:
- Throughput: The rate of successfully routes the data to the sink.
- Energy Consumption: The amount of energy consumed by each sensor node.
- Packet Delivery Ratio (PDR): The percentage of packets successfully delivered to the sink.
- End-to-End Delay: The time taken for data to travel from a sensor node to the sink.
- Network Lifetime: The time until the first sensor node reduces its energy.
- Analysing the Trace File
The trace file (wsn_trace.tr) logs all the events in the course of the simulation that contain packet transmissions, receptions, and drops. We can parse the trace file to estimate parameters like throughput, packet loss, and delay.
Example: Calculating Throughput from Trace File
We can compose a simple script (e.g., using AWK or Python) to measure the trace file and calculate the throughput by counting the number of successfully received packets at the sink and dividing by the simulation time.
- Visualizing the Simulation in NAM
NS2 contains NAM (Network Animator) that enables you to envision the simulation and monitor on how packets are transmitted among nodes.
To allow NAM visualization, add the following lines to the script:
# Enable NAM trace file
set namfile [open wsn_simulation.nam w]
$ns namtrace-all $namfile
# Run NAM after the simulation finishes
proc finish {} {
global ns namfile
$ns flush-trace
close $namfile
exec nam wsn_simulation.nam &
exit 0
}
In the attainable guide will establish the simulation process that supports to simulate the Wireless Sensor Networks project and measure their performance NS2 tool. More details will be offered on this Wireless Sensor Networks in upcoming manual.