Applicable VersionsNetSim StandardNetSim Pro


Applicable Releasesv11.1v12v13


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_v13_0.netsim_exp for v13, Configuration.netsim and WBAN_v12_2.netsim_exp for v12 and BAN_v11_1 for NetSim v11.1). The sensors are part of the BAN which connects to a server on the cloud. 



  • 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.


 


Customization


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, "");
else

//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] = ' ';
}
else
 {

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

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


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)
break;
i++;
}
else
{
fprintf(stderr, "\nRow: %d, Column: %d, Value: Error", row, col);
_getch();
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)
fclose(file);
return tok;
}
j++;
}

}


/**
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,
pstruEventDetails_Formal,
pszAppPath_Formal,
pszWritePath_Formal,
nVersion_Type,
fnPointer);
}


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: 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.


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 - https://tetcos.com/iot-wsn.html

NetSim IoT module documentation - https://tetcos.com/downloads/v13.1/IOT-WSN.pdf