Wireless Sensor Network Simulation Using NetSim

Tarun Krishna T., Shashikant Suman and Pranav Viswanathan


IEEE 802.15.4-based wireless sensor networks (WSNs) have witnessed explosive growth in the recent past because of their position-independent sensing capabilities even in toxic and inaccessible regions to humans, the low cost of sensors, and a very long field lifetime given by their low power consumption. WSNs are formed by a large number of networked sensing nodes and it is sometimes complex to analytically model it. There are many simulation software for WSN, including NS 2, SENSE, NetSim, OPNET and OMNeT++.

This article is based on NetSim simulation. This simulator can be used to analyse data packet delivery, probability of packet being discarded and other parameters in wireless networks. It can also give you the simulated output in the form of graphs for analytical purposes. IEEE 802.15.4-based wireless sensor networks can support a maximum of 250 kbps for 2.4GHz bandwidth. Therefore WSNs are employed in those areas where a phenomenon (like intrusion detection or fire alarm) has to be sensed and the packets are generated at a very low data rate. However, the standard has limitations on network throughput as the load increases.

Fig. 1: Plot of improved throughput with modifications

Here, we will talk about the measures that can improve the performance of 802.15.4, thereby enabling support for high-bandwidth applications, such as video, and providing reliable quality of service (QoS) guarantees. Transporting such applications over a WSN will provide substantial benefits to diverse areas ranging from internal security to surgical medicine. As the load increases, the sensors’ attempt rate remain constant, resulting in more collisions leading to a decrease in performance. Attempt rate mainly depends on the backoff time that the sensor takes. Clearly, by increasing the backoff time, we can decrease the attempt rate, thereby improving the performance at higher loads. This can be done either by increasing the backoff multiplier or the backoff exponent limits (macMinBe and maxBE). Based on the above logic, we have modified these parameters in NetSim and ran simulations.

WSN simulation program
This program has been developed using NetSim’s development kit. It allows users to develop custom codes, simulate their models and statistically analyse performance metrics. WSN library contains ‘C’ source code for the primitives, and the configuration files are available as ‘xml’ files.

Throughput. Average rate of successful packet delivery. In terms of NetSim’s metrics, it is given by:

Throughput = (Frames Transmitted-Frames errored)/Simulation Time

Attempt rate. Number of backoffs over the time spent in backoffs (per node basis) is given by:

β=Number of backoffs or attempts/(Time spent in backoff + CCA)

Where CCA is clear channel assessment.

Attempt rate gives the number of attempts made by the sensor in a backoff slot, i.e., the probability that a sensor attempts in a backoff slot given that it has a packet. A backoff is followed by CCA and an attempt can be a success or failure. A successful attempt involves a frame transmission and a failed attempt involves CCA failure. Therefore the number of backoffs/attempts is the sum of failed CCA and frames transmitted:

(Number of backoffs=Frames Transmitted + Failed CCA).

Each packet transmission is followed by radio turnaround time, ACK packet and IFS, so time spent in backoff is the simulation time removing the packet transmission time, radio turnaround time, ACK, IFS, and the beacon transmission time:

Time spent in backoff + CCA = Simulation Time-Beacon Transmission Time-{Frames Transmitted*(Frame Transmission Time + Turnaround Time + ACK Time + IFS)}

Average of βi (set of sensors) gives the probability that a sensor attempts in a backoff slot.

Discard probability. Probability of packet being discarded:

Pdiscard = Frames discarded/Frames transmitted (ignoring retransmission)

Payload and plots. Payload at application layer is 4 bytes. Overheads are set according to the standard (20IP+13MAC+6PHY). Using the Metrics.txt files obtained from the simulations, graphs are plotted. Below is an example:

Simulations are done in different scenarios for various nodes. Using Python’s matplotlib module, you can plot a throughput plot.

Fig. 2: Cygwin/Mingw output
Fig. 2: Cygwin/Mingw output
Fig. 3: Building solution in Visual Studio
Fig. 3: Building solution in Visual Studio

From the plots, you can see that change in the backoff parameters (backoff multiplier and backoff exponent limits, i.e., macMinBE and MaxBE) leads to a higher throughput than with the 802.15.4 standard. As the attempt rate (β) increases, probability of a packet collision increases and so throughput decreases. Change in the backoff parameters decreases the attempt rate (β), as we notice in the plots, and therefore the collisions decrease leading to a higher throughput. This decrease in collisions is evident from the discard probability graphs.

These simulation results validate the theoretical analysis of improved throughput (see Fig. 1) with modifications to the 802.15.4 standard’s back-off parameters.

Software tools
Following software tools have been used to develop and test this program:

1. ‘C’ compiler. The project has been tested with Visual Studio 2005, Cygwin and Mingw.
2. Python 2.7 (http://www.python.org/download/)
3. Matplotlib 1.1.1 compatible with Python 2.7 (http://sourceforge.net/projects/matplotlib/files/matplotlib/matplotlib-1.1.1/)
4. Numpy 1.6.2 compatible with Python 2.7 (http://sourceforge.net/projects/numpy/files/)

Project folders (INSN_SampleProject). The project contains following folders:

Configuration_Files_Division/. ‘Configuration.xml’ has been divided into several parts so that the user can create a custom XML file from ‘C’ code (Main.c) using the following parts which don’t vary.

Dll/. All the dlls needed for running simulation are kept in this directory (taken from NetSim 6.1).

Documents/. This directory has all the documents corresponding to the project, and also some good reference documents to understand IEEE 802.15.4 standard.

Main.c. This ‘C’ code loads the dlls in Dll directory. It also loads the functions (Sample_WSN_Project_NetSim) required for simulation, and declares the function (scenario) for creating ‘Configuration.xml’ files into ‘/NetSim_Temp_Files’ directory.

PlotAll.py. For analysing the results after simulation, Python script has been used to plot the graphs. Numpy, fundamental package for scientific computing with Python, and Matplotlib, plotting library for Python, are used for plots.

Fig. 5: Running python.exe from command line

NetSim_Temp_Files/. This directory is used as a temporary directory for storing the configuration, trace and metrics files obtained in simulation.

Debug/. This directory is used by Visual Studio to create the exe file.

Output/. After each simulation, the trace files, metrics files and other files that are needed for analysis are copied to this folder automatically by the program.

(The various files contained in each of these folders have been given in the Readme.pdf file in ‘Document’ folder.)

Testing procedure
CYGWIN/MINGW. The procedure for building in ‘Cygwin’ is as follows:
1. Open ‘SampleProject/Main.c,’ modify the code as per your requirement and save it.
2. Open ‘Cygwin/MinGW’ terminal. Now open ‘SampleProject’ directory and run the following commands to compile and run the simulation:
(i) gcc Main.c -o Main.exe
(ii) ./Main.exe

You can refer to Fig. 2 for ‘Cygwin/MinGW’ output.

Visual Studio. For building with Visual Studio, proceed as follows:
1. Open ‘SampleProject.sln’ located in ‘SampleProject’ folder with Visual Studio 2005 or higher version.
2. Modify the parameters as needed in ‘SampleProject/Main.c’ and save it.
3. Build the project using the menu ‘Build→Build Solution’ or use the shortcut F7 (see Fig. 3).
4. Run the project using the menu ‘Debug→Start without Debugging’ or use the shortcut Ctrl+F5 (see Fig. 4).

Python. After running the simulation, use the Python script (PlotAll.py) to plot the graphs as described in WhitePaper.pdf. Note that the script saves the plots into output directory.

Python script can be run by double-clicking PlotAll.py file or you can run it via command line.

To run from command line, proceed as follows:
1. Open ‘WSN_SampleProject’ folder.
2. Check where ‘python.exe’ is present and use that path to run ‘PlotAll.py.’ For example, if ‘python.exe’ is located in ‘C:\Python27,’ use the path as shown in Fig. 5.

After each simulation, if needed, copy the plots and metrics files and save them in another folder before running the next simulation, as these files will get overwritten.

Now open ‘Output’ folder mentioned above. You will find the plots for beta, Pdiscard and throughput. These plots will give you the overall idea and status of the network.

Pranav Viswanathan, a B.Tech from IIT Madras, is a business manager at TETCOS. His interests include modeling and simulation, and wireless communication. Shashikant Suman, a B.Tech from IIT Kharagpur, is a senior developer at TETCOS. His interests include cognitive radio and wireless sensor networks. Tarun Krishna is a final-year student, IIT Madras, with interests in wireless sensor networks


Please enter your comment!
Please enter your name here