How to Implement Mobile Sensing in NS2
To implement the Mobile Sensing using NS2 (Network Simulator 2), which encompasses simulating a network in which the mobile devices or nodes (such as smartphones or vehicles) are gather and distribute the data whereas moving via a particular environment. This sensing networks can use for a kind of applications like environmental monitoring, traffic analysis, or health monitoring. In this procedure, we show on how to executing Mobile Sensing in NS2:
Step-by-Step Implementation:
- Set up the Simulation Environment
Firstly, configure the simulation environment by describing the wireless channel, propagation model, network interface, and mobility patterns for the mobile nodes.
Example TCL Script for Basic Setup:
# Create a simulator instance
set ns [new Simulator]
# Define the wireless environment
set val(chan) Channel/WirelessChannel ;# Wireless channel type
set val(prop) Propagation/TwoRayGround ;# Propagation model
set val(netif) Phy/WirelessPhy ;# Network interface type
set val(mac) Mac/802_11 ;# MAC protocol
set val(ifq) Queue/DropTail/PriQueue ;# Interface queue type
set val(ll) LL ;# Link layer type
set val(ant) Antenna/OmniAntenna ;# Antenna model
set val(x) 1000 ;# X dimension of topography (simulation area)
set val(y) 1000 ;# Y dimension of topography
# Create the topography
create-god 10 ;# Number of nodes in the network
- Create Mobile Sensing Nodes
In mobile sensing, the nodes (e.g., smartphones, vehicles, or robots) are move whereas gathering and transferring data. This simulation platform NS2 permits to make a mobile nodes and describe its mobility using a predefined or custom mobility model.
Example of Creating Mobile Nodes:
# Create mobile nodes
for {set i 0} {$i < 10} {incr i} {
set node_($i) [$ns node]
$node_($i) set X_ [expr $i * 100] ;# Initial position (X-axis)
$node_($i) set Y_ [expr $i * 50] ;# Initial position (Y-axis)
}
# Set up mobility for the nodes
for {set i 0} {$i < 10} {incr i} {
$ns_ at 1.0 “$node_($i) setdest [expr rand() * 1000] [expr rand() * 1000] 10”
# Each node moves at 10 m/s towards a random destination within 1000×1000 area
}
Above sample, mobile nodes are made and allocated random positions. The setdest command is used to stipulate the movement of the nodes, and replicating mobile sensing.
- Implement a Mobility Model
The simulation tool NS2 supports numerous mobility models like the Random Waypoint Model or the Pursue Mobility Model. For the mobile sensing, we can be used the Random Waypoint Model that nodes are randomly move to various ends including pauses among the movements.
Example of Using Random Waypoint Model:
# Random Waypoint mobility model for each node
for {set i 0} {$i < 10} {incr i} {
$node_($i) setdest [expr rand() * $val(x)] [expr rand() * $val(y)] [expr 5 + rand() * 10]
# Nodes move with a speed between 5 to 15 m/s towards random destinations
}
The mobility model describes how nodes are move in the simulation area, and reflecting the dynamics of the mobile sensing networks.
- Generate Sensing Data Traffic
In mobile sensing, every single node gathers and transfers data to a base station, other nodes, or a centralized server. We can replicate it by making the traffic sources at each mobile node using UDP, TCP, or CBR (Constant Bit Rate) applications.
Example of Sensing Data Traffic Using UDP:
# Create a UDP agent for sending sensed data from node 0
set udp [new Agent/UDP]
$ns attach-agent $node_(0) $udp
# Create a traffic generator to simulate sensed data (e.g., temperature data)
set cbr [new Application/Traffic/CBR]
$cbr set packet_size_ 512 ;# Packet size in bytes
$cbr set rate_ 0.5Mb ;# 0.5 Mbps transmission rate
$cbr attach-agent $udp
# Set a null agent (receiver) at another node
set null [new Agent/Null]
$ns attach-agent $node_(9) $null
$ns connect $udp $null
# Start and stop the sensing data traffic
$ns at 2.0 “$cbr start”
$ns at 15.0 “$cbr stop”
In this instance, the node 0 transfers the sensing data (like environmental data) to node 9 at a constant bit rate.
- Implement a Routing Protocol
This mobile sensing networks depends on the routing protocols to transfer the data among mobile nodes and the base station. We can be used AODV (Ad-hoc On-Demand Distance Vector), DSR (Dynamic Source Routing), or any other routing protocol are appropriate for mobile ad-hoc networks (MANETs).
Example of Using AODV Routing Protocol:
# Set up the AODV routing protocol
set val(rp) AODV
for {set i 0} {$i < 10} {incr i} {
$node_($i) set ragent [$val(rp)]
}
It permits the mobile nodes to actively find routes to the end nodes as they move in the simulation area.
- Multihop Data Transmission
In this mobile sensing networks, the data are gathered by one node could require to be sent by other nodes (multihop) that to attain the base station or sink node. The simulation environment NS2 automatically manages the multihop communication if the routing protocol supports this (e.g., AODV or DSR).
- Simulate Data Processing and Aggregation (Optional)
Some mobile sensing applications are behaves in-network data processing or aggregation to minimise the amount of data transferred. We can replicate it by changing the traffic rate or packet size actively.
Example of Data Aggregation:
# Simulate data aggregation by adjusting the packet size or rate
$cbr set packet_size_ 1024 ;# Increased packet size to simulate aggregated data
It minimise the number of packets transmitted by aggregating smaller packets into the larger ones.
- Simulate Energy Consumption (Optional)
The mobile sensing devices are frequently battery-powered, thus it is necessary to mimic energy consumption. The tool NS2 has a built-in energy model, which can be used to trace the energy usage of each mobile node while communication.
Example of Using the Energy Model:
# Set up the energy model for each node
for {set i 0} {$i < 10} {incr i} {
$node_($i) set energyModel_ EnergyModel
$node_($i) set initialEnergy_ 100.0 ;# Initial energy in Joules
}
It will trace the energy consumption for each node as they move and communicate through the simulation.
- Monitor and Trace the Simulation
Allow tracing to observe the behaviour of the mobile sensing network, like packet delivery, latency, and energy consumption.
Enable Trace Files for Analysis:
# Enable tracing for the simulation
set tracefile [open “mobile_sensing_trace.tr” w]
$ns trace-all $tracefile
The trace file will encompass data regarding the packet transmissions, receptions, routing decisions, and energy consumption for future analysis.
- Run the Simulation
Ultimately, run the simulation then set the simulation duration.
# Set the end time of the simulation
$ns at 20.0 “finish”
proc finish {} {
global ns tracefile
$ns flush-trace
close $tracefile
exit 0
}
# Run the simulation
$ns run
We can be examined the trace file to measure the performance of the mobile sensing network, after running the simulation.
Example Complete TCL Script for Mobile Sensing
# Create a simulator instance
set ns [new Simulator]
# Define network parameters
set val(chan) Channel/WirelessChannel
set val(prop) Propagation/TwoRayGround
set val(netif) Phy/WirelessPhy
set val(mac) Mac/802_11
set val(ifq) Queue/DropTail/PriQueue
set val(ll) LL
set val(ant) Antenna/OmniAntenna
set val(x) 1000
set val(y) 1000
# Create mobile nodes
for {set i 0} {$i < 10} {incr i} {
set node_($i) [$ns node]
$node_($i) set X_ [expr $i * 100]
$node_($i) set Y_ [expr $i * 50]
}
# Set mobility for the nodes using Random Waypoint Model
for {set i 0} {$i < 10} {incr i} {
$ns_ at 1.0 “$node_($i) setdest [expr rand() * 1000] [expr rand() * 1000] [expr 5 + rand() * 10]”
}
# Set up AODV routing protocol
set val(rp) AODV
for {set i 0} {$i < 10} {incr i} {
$node_($i) set ragent [$val(rp)]
}
# Create UDP agent for sensing data
set udp [new Agent/UDP]
$ns attach-agent $node_(0) $udp
# Create CBR traffic generator
set cbr [new Application/Traffic/CBR]
$cbr set packet_size_ 512
$cbr set rate_ 0.5Mb
$cbr attach-agent $udp
# Create null agent as the receiver
set null [new Agent/Null]
$ns attach-agent $node_(9) $null
$ns connect $udp $null
# Start sensing data traffic
$ns at 2.0 “$cbr start”
$ns at 15.0 “$cbr stop”
# Enable tracing
set tracefile [open “mobile_sensing_trace.tr” w]
$ns trace-all $tracefile
# End the simulation
$ns at 20.0 “finish”
proc finish {} {
global ns tracefile
$ns flush-trace
close $tracefile
exit 0
}
# Run the simulation
$ns run
At the end, above approach and examples definitely helps you to obtain the idea on how to execute and setup the Mobile sensing in NS2 tool. Also we will offer further informations on this topic based on your requirements.
Hit up ns2project.com for top-notch Mobile Sensing solutions for your projects. Our team of developers is here to offer you the best support. We stay updated with the latest research methods to ensure you get outstanding results for your projects.