To implement the wireless sensor network (WSN) in OMNeT++ has needs to encompass to generate the emulation with the sensor nodes then describe the network topology and then execute the communication protocols and compile the mimic.
The given below are the detailed procedures on how to implement the Wireless sensor network in OMNet++:
Step-by-Step Implementation:
Step 1: Install OMNeT++ and INET Framework
Step 2: Set Up Your Project
Step 3: Define Wireless Sensor Network Models Using NED
package wsn;
import inet.node.inet.StandardHost;
import inet.node.inet.Router;
import inet.networklayer.configurator.ipv4.Ipv4NetworkConfigurator;
import inet.physicallayer.ieee80211.packetlevel.Ieee80211ScalarRadioMedium;
import inet.mobility.single.RandomWaypointMobility;
network WirelessSensorNetwork
{
parameters:
int numSensors = default(20);
types:
channel radioChannel extends Ieee80211ScalarRadioMedium {}
submodules:
configurator: Ipv4NetworkConfigurator {
@display(“p=100,100”);
}
sink: Router {
@display(“p=200,100”);
}
sensor[numSensors]: StandardHost {
@display(“p=300+100*i,200”);
mobility.typename = “RandomWaypointMobility”;
}
radioMedium: radioChannel {
@display(“p=400,100”);
}
connections allowunconnected:
for i=0..numSensors-1 {
sensor[i].wlan[0] <–> radioMedium <–> sink.wlan[0];
}
}
Step 4: Implement Wireless Sensor Communication Logic in C++
#include <omnetpp.h>
#include “inet/applications/base/ApplicationBase.h”
#include “inet/applications/udpapp/UdpBasicApp.h”
#include “inet/networklayer/common/L3AddressResolver.h”
#include “inet/networklayer/contract/ipv4/Ipv4Address.h”
#include “inet/networklayer/contract/IL3AddressType.h”
using namespace omnetpp;
using namespace inet;
class SensorApp : public ApplicationBase
{
protected:
virtual void initialize(int stage) override;
virtual void handleMessageWhenUp(cMessage *msg) override;
void sendSensorData();
void handleSensorData(cPacket *pkt);
};
Define_Module(SensorApp);
void SensorApp::initialize(int stage)
{
ApplicationBase::initialize(stage);
if (stage == INITSTAGE_LOCAL) {
// Initialization code
if (par(“sendPackets”).boolValue()) {
scheduleAt(simTime() + par(“startDelay”), new cMessage(“sendSensorData”));
}
}
}
void SensorApp::handleMessageWhenUp(cMessage *msg)
{
if (msg->isSelfMessage()) {
if (strcmp(msg->getName(), “sendSensorData”) == 0) {
sendSensorData();
scheduleAt(simTime() + par(“sendInterval”), msg);
}
} else {
cPacket *pkt = check_and_cast<cPacket *>(msg);
handleSensorData(pkt);
}
}
void SensorApp::sendSensorData()
{
// Create and send a sensor data packet
EV << “Sending sensor data packet” << endl;
cPacket *pkt = new cPacket(“SensorData”);
pkt->setByteLength(par(“packetSize”));
send(pkt, “lowerLayerOut”);
}
void SensorApp::handleSensorData(cPacket *pkt)
{
// Handle received sensor data packet
EV << “Received sensor data packet: ” << pkt->getName() << endl;
delete pkt;
}
network WirelessSensorNetwork
{
parameters:
int numSensors = default(20);
types:
channel radioChannel extends Ieee80211ScalarRadioMedium {}
submodules:
configurator: Ipv4NetworkConfigurator {
@display(“p=100,100”);
}
sink: Router {
@display(“p=200,100”);
}
sensor[numSensors]: StandardHost {
@display(“p=300+100*i,200”);
mobility.typename = “RandomWaypointMobility”;
@children:
udpApp: SensorApp {
localPort = 12345;
destPort = 54321;
startTime = uniform(0, 1s);
packetSize = 512B;
sendInterval = exponential(1s);
}
}
radioMedium: radioChannel {
@display(“p=400,100”);
}
connections allowunconnected:
for i=0..numSensors-1 {
sensor[i].wlan[0] <–> radioMedium <–> sink.wlan[0];
}
}
Step 5: Configure Simulation Parameters
network = WirelessSensorNetwork
sim-time-limit = 100s
**.sendPackets = true
**.startDelay = 1s
**.sendInterval = 2s
**.packetSize = 512B
**.sensor[*].mobility.typename = “inet.mobility.single.RandomWaypointMobility”
Step 6: Build and Run the Simulation
Step 7: Analyse Results
In this entire script, we completely provide the procedures, sample snippets to compile the OMNet++ for wireless sensor network. We will give simulation results on how the wireless sensor network is executed with practical discussion.