Applicable VersionsNetSim StandardNetSim Pro

Applicable Releasesv13

In this example, we explain the simulation of a Body Area Sensor Network/ IoT Healthcare application based on real data. 

Multiple sensors in the real BAN measure various health parameters which is saved to a log file (sample log file attached). An equivalent IoT network scenario is designed in NetSim, consisting of 11 sensor nodes, a 6LoWPAN Gateway, a router, and a server as shown below (sample configuration file attached - WBAN-IoT_v14.netsimexp for v14, WBAN_v13.netsim_exp for v13). The sensors are part of the BAN which connects to a server on the cloud. 

NetSim v14

NetSim v13.3

  • An application is configured for each sensor to periodically generate packets and forward it to the server via the 6LoWPAN Gateway
  • Sensors and Applications have been renamed suitably as per the log file used
  • The Application packet inter-arrival time has been set to 4 milliseconds as per the timestamps in the log file
  • The Wireshark option in the sensor general properties is set to Offline. This will create a *.pcap log file for each sensor node, which can be accessed from the results dashboard after the simulation

The CSV / XL file below shows the readings of the sensors. The first column has the timestamps for the subsequent 11 columns having values from 11 different sensors (measuring body's ECG, EEG, Airflow, .. etc) in the network.



NetSim Application module source codes are modified to read from the log file and update the packet payload during run time. In the inbuilt traffic generator, users can model different types of applications such as CBR, Custom, Voice, Video, Database, HTTP, etc. In those applications, NetSim adds a dummy payload (string "abcd...") to the packet as per the packet size. However, users can modify the packet payload to include specific information which can be read from a string, file, etc during the simulation. The following steps highlight the code modifications done:

STEP 1: Open NetSim source codes from Open Simulation->Workspace Options-> Open code button or from Help->Open Source code option.

STEP 2: Open copy_payload() function present in Application.c file inside Application project

STEP 3: Inside the function copy_payload(), make the modifications as given below (hightlighted in red) : 

void copy_payload(UINT8 real[],NetSim_PACKET* packet,unsigned int* payload,APP_INFO* info)
u_short i;
uint32_t key = 16;
size_t len = 0;
char reading[BUFSIZ];
int row = 2502;
int col = 12;
char fname[BUFSIZ] = "samplesEEG.csv";
if (packet->nPacketId + 1 > row || packet->nSourceId > col)
sprintf(reading, "");

//Rows in Excel file are mapped to the packet ID (packet->npacketId+1) 

//Columns are mapped to Application ID (info->id)
sprintf(reading, "%s", get_sensor_reading(packet->nPacketId + 1, info->id, fname));
len = strlen(reading);

if (payload)
for (i = 0; i < *payload; i++)

if (i < len)
real[i] = reading[i];
else if (len > 0)
real[i] = ' ';

if (info->encryption == Encryption_XOR)
real[i] = xor_encrypt('a' + i % 26, 16);
real[i] = 'a' + i % 26;

if (info->encryption == Encryption_TEA)
encryptBlock(real, payload, &key);
else if (info->encryption == Encryption_AES)
else if(info->encryption==Encryption_DES)

STEP 4: In the same Application.c file perform modifications at the start of the file as given below (hightlighted in red) : 

#include "main.h"
#include "Application.h"
#include "CoAP.h"

char* get_sensor_reading(int row, int col, char* filename);

char* get_sensor_reading(int row, int col, char* filename)
FILE* file;
file = fopen(filename, "r");

int i = 0, j = 0;
char line[4098];
const char* tok;
if (file)
//Match Row
while (fgets(line, 4098, file) && (i < row))
if (i == row)
fprintf(stderr, "\nRow: %d, Column: %d, Value: Error", row, col);
return "";

char* rest = line;
//Match Column
while ((tok = strtok_s(rest, ",", &rest)))

if (j == col)
//fprintf(stderr, "\nRow: %d, Column: %d, Value: %s", row, col, tok);
if (file)
return tok;


This function is used to initialize the parameter for all the application based on
the traffic type
_declspec(dllexport) int fn_NetSim_Application_Init(struct stru_NetSim_Network *NETWORK_Formal,
NetSim_EVENTDETAILS *pstruEventDetails_Formal,
char *pszAppPath_Formal,
char *pszWritePath_Formal,
int nVersion_Type,
void **fnPointer)
return fn_NetSim_Application_Init_F(NETWORK_Formal,

STEP 5: Right-click on the Application module in Visual Studio Solution Explorer, and select Rebuild to update NetSim Application binaries as per the modifications done. 

STEP 6: 

NetSim v14

Place the log file samplesEEG.csv in NetSim workspace bin_x64 path. The bin_x64 folder is located within your NetSim WBN workspace. To access it, in the Home Screen of NetSim, go to Your Work -> Workspaces -> Open Workspace Location. For Eg: C:\Users\<Your Username>\Documents\NetSim\Workspaces\WBN\bin_x64


A screenshot of a computer

Description automatically generated

NetSim v13

 Place the log file samplesEEG.csv in NetSim binary folder in <NetSim_Installation_Directory>/bin folder (Right-click on NetSim shortcut in desktop and select Open file location option to reach this path).

STEP 7: Import the attached network configuration into NetSim using the Import Experiment option available in the Open Simulation menu of NetSim Home Screen. Open the imported network configuration in NetSim.

(Parameters configured in the sample config file: Zigbee_ACK_Request Enabled in Datalink layer of sensors and Lowpan gateway, Application_End_Time is set to 10s in every application)


STEP 8: Run Simulation for 50 Seconds. Simulation time is set to 50 seconds as the log file contains entries sufficient for 50 seconds of simulation with an interval of 4 Milliseconds.

STEP 9: After the simulation, the packet capture log files associated with each sensor node can be accessed under the packet capture drop-down as shown below:

STEP 10: In the Wireshark logs the packet payload (Data) will contain the readings obtained from the log file during the simulation as shown below:

Abdomen Respiration Sensor:

ECG Sensor:

The above screenshot highlights the first reading from the ABDO_RES sensor and ECG sensor during the simulation, which can be related to the log file discussed above.

Writing a log of data received at the destination:

A log can be written at the receiver end with the payload from the packet. The following steps highlight the code modifications that are to be done for this purpose: 

1. Declare a file pointer at the beginning of the Application.c file as shown below:

#include "main.h"
#include "Application.h"
#include "CoAP.h"

static FILE* fpReceiverLog = NULL;

2. In the fn_NetSim_Application_Init() function, add code to create and add headings to the log file.
_declspec(dllexport) int fn_NetSim_Application_Init(struct stru_NetSim_Network* NETWORK_Formal,
    NetSim_EVENTDETAILS* pstruEventDetails_Formal,
    char* pszAppPath_Formal,
    char* pszWritePath_Formal,
    int nVersion_Type,
    void** fnPointer)

    char s[BUFSIZ];
    sprintf(s, "%s\\%s", pszIOLogPath, "Receiver_Log.csv");
    fpReceiverLog = fopen(s, "w");
    if (!fpReceiverLog)
        fnSystemError("Unable to open %s file", s);
        fprintf(fpReceiverLog, "%s,%s,%s,%s,%s,",
            "Time(MilliSeconds)", "Application ID", "Source ID", "Destination ID", "Packet Payload");

    return fn_NetSim_Application_Init_F();

3. In the fn_NetSim_Application_Run() function as part of the APPLICATION_IN_EVENT switch case, add the following lines to add packet details to the log file for each packet received by a destination node:

if (pstruPacket->nPacketType != PacketType_Control && pstruPacket->pstruAppData->nApplicationId &&
            pstruPacket->nControlDataType / 100 != PROTOCOL_APPLICATION)
            ptrAPPLICATION_INFO pstruappinfo;
            pstruappinfo = applicationInfo[pstruPacket->pstruAppData->nApplicationId - 1];
            pstruPacket->pstruAppData->dEndTime = pstruEventDetails->dEventTime;
            appmetrics_dest_add(pstruappinfo, pstruPacket, pstruEventDetails->nDeviceId);

            if (fpReceiverLog)

                fprintf(fpReceiverLog, "\n%lf,%d,%d,%d,%s,",
                    pstruEventDetails->dEventTime, pstruappinfo->id, pstruappinfo->sourceList[0], pstruappinfo->destList[0], pstruPacket->szPayload->packet);

            if (pstruappinfo->nAppType == TRAFFIC_PEER_TO_PEER && pstruPacket->pstruAppData->nAppEndFlag == 1)

4. In the fn_NetSim_Application_Finish() function, close the log file.

_declspec(dllexport) int fn_NetSim_Application_Finish()
    unsigned int loop;
    if (fpReceiverLog)

5. Rebuild Application Source codes and run Simulation. 

Post simulation you will be able to access the Receiver_Log.csv file from the NetSim results dashboard as shown below:

Related Articles:

How to read the payload from a file in the application layer?

How do I change the payload of a packet in NetSim?

How do I interface NetSim with Simulink? Can you give an example?   

Useful links

NetSim IoT module overview -

NetSim IoT module documentation -