Ns3 Projects for B.E/B.Tech M.E/M.Tech PhD Scholars.  Phone-Number:9790238391   E-mail: ns3simulation@gmail.com

How to Implement Industrial Internet of Things in ns3

To implement the industrial internet of things (IIoT) in ns3 consists to simulate the interaction among numerous industrial gadgets, sensors, and actuators and control system. Here are detailed procedures on how to set up a general IIoT network in ns3 environment.

Step-by-Step Implementation

  1. Install ns-3

To make certain ns3 is installed on your system and can we download it from official website.

  1. Define the Network Topology

The numerous topologies of nodes that includes:

  • Sensor nodes (e.g., temperature, humidity, vibration sensors)
  • Actuator nodes (e.g., motors, valves)
  • Control nodes (e.g., PLCs, SCADA systems)
  • Gateway nodes (for connectivity between different parts of the network)
  1. Create Network Nodes

Create network nodes using NodeContainer.

NodeContainer sensorNodes, actuatorNodes, controlNodes, gatewayNodes;

sensorNodes.Create(10);  // Create 10 sensor nodes

actuatorNodes.Create(5); // Create 5 actuator nodes

controlNodes.Create(2);  // Create 2 control nodes

gatewayNodes.Create(1);  // Create 1 gateway node

 

  1. Set Up Network Devices

Install network devices on the nodes using appropriate network interfaces, such as WiFi or Ethernet for industrial communication.

WifiHelper wifi;

wifi.SetStandard(WIFI_PHY_STANDARD_80211n_5GHZ);

WifiMacHelper mac;

mac.SetType(“ns3::AdhocWifiMac”);

YansWifiPhyHelper phy = YansWifiPhyHelper::Default();

YansWifiChannelHelper channel = YansWifiChannelHelper::Default();

phy.SetChannel(channel.Create());

NetDeviceContainer sensorDevices = wifi.Install(phy, mac, sensorNodes);

NetDeviceContainer actuatorDevices = wifi.Install(phy, mac, actuatorNodes);

NetDeviceContainer controlDevices = wifi.Install(phy, mac, controlNodes);

NetDeviceContainer gatewayDevices = wifi.Install(phy, mac, gatewayNodes);

  1. Configure Mobility Model

Set up the mobility model for the nodes to simulate fixed positions typical in industrial environments.

MobilityHelper mobility;

mobility.SetMobilityModel(“ns3::ConstantPositionMobilityModel”);

mobility.Install(sensorNodes);

mobility.Install(actuatorNodes);

mobility.Install(controlNodes);

mobility.Install(gatewayNodes);

  1. Set Up Routing Protocols

Configure routing protocols for the network. A suitable routing protocol for IIoT networks can be AODV, OLSR, or RPL.

AodvHelper aodv;

InternetStackHelper internet;

internet.SetRoutingHelper(aodv);

internet.Install(sensorNodes);

internet.Install(actuatorNodes);

internet.Install(controlNodes);

internet.Install(gatewayNodes);

  1. Assign IP Addresses

Assign IP addresses to the network devices.

Ipv4AddressHelper address;

address.SetBase(“10.1.1.0”, “255.255.255.0”);

Ipv4InterfaceContainer sensorInterfaces = address.Assign(sensorDevices);

Ipv4InterfaceContainer actuatorInterfaces = address.Assign(actuatorDevices);

Ipv4InterfaceContainer controlInterfaces = address.Assign(controlDevices);

Ipv4InterfaceContainer gatewayInterfaces = address.Assign(gatewayDevices);

 

  1. Implement IIoT Applications

Create applications that simulate IIoT functionalities. Below is an example of a simple application that simulates sensor data collection and actuator control.

Sensor Application

class SensorApplication : public Application {

public:

    void StartApplication() override {

        sendSocket = Socket::CreateSocket(GetNode(), UdpSocketFactory::GetTypeId());

        InetSocketAddress remote = InetSocketAddress(gatewayAddress, gatewayPort);

        sendSocket->Connect(remote);

        // Schedule the first data send

        Simulator::Schedule(Seconds(1.0), &SensorApplication::SendSensorData, this);

    }

    void SetGatewayAddress(Ipv4Address address, uint16_t port) {

        gatewayAddress = address;

        gatewayPort = port;

    }

    void SendSensorData() {

        std::ostringstream msg;

        msg << “Sensor data from node ” << GetNode()->GetId();

        Ptr<Packet> packet = Create<Packet>((uint8_t*) msg.str().c_str(), msg.str().size());

        sendSocket->Send(packet);

        // Schedule the next data send

        Simulator::Schedule(Seconds(10.0), &SensorApplication::SendSensorData, this);

    }

private:

    Ptr<Socket> sendSocket;

    Ipv4Address gatewayAddress;

    uint16_t gatewayPort;

};

Actuator Application

class ActuatorApplication : public Application {

public:

    void StartApplication() override {

        recvSocket = Socket::CreateSocket(GetNode(), UdpSocketFactory::GetTypeId());

        recvSocket->Bind(InetSocketAddress(Ipv4Address::GetAny(), localPort));

        recvSocket->SetRecvCallback(MakeCallback(&ActuatorApplication::HandleRead, this));

    }

    void SetLocalPort(uint16_t port) {

        localPort = port;

    }

    void HandleRead(Ptr<Socket> socket) {

        Ptr<Packet> packet;

        Address from;

        while ((packet = socket->RecvFrom(from))) {

            std::cout << “Actuator node ” << GetNode()->GetId() << ” received control message.” << std::endl;

            // Actuate based on the received message

        }

    }

private:

    Ptr<Socket> recvSocket;

    uint16_t localPort;

};

Control Application

class ControlApplication : public Application {

public:

    void StartApplication() override {

        recvSocket = Socket::CreateSocket(GetNode(), UdpSocketFactory::GetTypeId());

        recvSocket->Bind(InetSocketAddress(Ipv4Address::GetAny(), localPort));

        recvSocket->SetRecvCallback(MakeCallback(&ControlApplication::HandleRead, this));

        sendSocket = Socket::CreateSocket(GetNode(), UdpSocketFactory::GetTypeId());

    }

    void SetLocalPort(uint16_t port) {

        localPort = port;

    }

    void HandleRead(Ptr<Socket> socket) {

        Ptr<Packet> packet;

        Address from;

        while ((packet = socket->RecvFrom(from))) {

            std::cout << “Control node ” << GetNode()->GetId() << ” received data.” << std::endl;

            // Process sensor data and send control messages if needed

            SendControlMessage();

        }

    }

    void SendControlMessage() {

        std::ostringstream msg;

        msg << “Control message from node ” << GetNode()->GetId();

        Ptr<Packet> packet = Create<Packet>((uint8_t*) msg.str().c_str(), msg.str().size());

        for (uint32_t i = 0; i < actuatorInterfaces.GetN(); ++i) {

            sendSocket->SendTo(packet, 0, InetSocketAddress(actuatorInterfaces.GetAddress(i), actuatorPort));

        }

    }

    void SetActuatorInterfaces(Ipv4InterfaceContainer interfaces, uint16_t port) {

        actuatorInterfaces = interfaces;

        actuatorPort = port;

    }

private:

    Ptr<Socket> recvSocket;

    Ptr<Socket> sendSocket;

    Ipv4InterfaceContainer actuatorInterfaces;

    uint16_t localPort;

    uint16_t actuatorPort;

};

  1. Install Applications

Install the applications on the nodes.

ApplicationContainer sensorApps, actuatorApps, controlApps;

// Install sensor applications

for (uint32_t i = 0; i < sensorNodes.GetN(); ++i) {

    Ptr<SensorApplication> app = CreateObject<SensorApplication>();

    app->SetGatewayAddress(gatewayNodes.Get(0)->GetObject<Ipv4>()->GetAddress(1, 0).GetLocal(), 9);

    sensorNodes.Get(i)->AddApplication(app);

    app->SetStartTime(Seconds(1.0));

    app->SetStopTime(Seconds(20.0));

    sensorApps.Add(app);

}

// Install actuator applications

for (uint32_t i = 0; i < actuatorNodes.GetN(); ++i) {

    Ptr<ActuatorApplication> app = CreateObject<ActuatorApplication>();

    app->SetLocalPort(9);

    actuatorNodes.Get(i)->AddApplication(app);

    app->SetStartTime(Seconds(1.0));

    app->SetStopTime(Seconds(20.0));

    actuatorApps.Add(app);

}

// Install control applications

for (uint32_t i = 0; i < controlNodes.GetN(); ++i) {

    Ptr<ControlApplication> app = CreateObject<ControlApplication>();

    app->SetLocalPort(9);

    app->SetActuatorInterfaces(actuatorInterfaces, 9);

    controlNodes.Get(i)->AddApplication(app);

    app->SetStartTime(Seconds(1.0));

    app->SetStopTime(Seconds(20.0));

    controlApps.Add(app);

}

 

  1. Run the Simulation

Configure the simulation runtime and execute it.

Simulator::Stop(Seconds(20.0));

Simulator::Run();

Simulator::Destroy();

Example of a Simple IIoT Network Script

Below is the sample script to complete the IIoT network simulation in ns-3 environment:

#include “ns3/core-module.h”

#include “ns3/network-module.h”

#include “ns3/internet-module.h”

#include “ns3/wifi-module.h”

#include “ns3/mobility-module.h”

#include “ns3/applications-module.h”

#include “ns3/aodv-module.h”

using namespace ns3;

class SensorApplication : public Application {

public:

    void StartApplication() override {

        sendSocket = Socket::CreateSocket(GetNode(), UdpSocketFactory::GetTypeId());

        InetSocketAddress remote = InetSocketAddress(gatewayAddress, gatewayPort);

        sendSocket->Connect(remote);

        // Schedule the first data send

        Simulator::Schedule(Seconds(1.0), &SensorApplication::SendSensorData, this);

    }

    void SetGatewayAddress(Ipv4Address address, uint16_t port) {

        gatewayAddress = address;

        gatewayPort = port;

    }

    void SendSensorData() {

        std::ostringstream msg;

        msg << “Sensor data from node ” << GetNode()->GetId();

        Ptr<Packet> packet = Create<Packet>((uint8_t*) msg.str().c_str(), msg.str().size());

        sendSocket->Send(packet);

        // Schedule the next data send

        Simulator::Schedule(Seconds(10.0), &SensorApplication::SendSensorData, this);

    }

private:

    Ptr<Socket> sendSocket;

    Ipv4Address gatewayAddress;

    uint16_t gatewayPort;

};

 

class ActuatorApplication : public Application {

public:

    void StartApplication() override {

        recvSocket = Socket::CreateSocket(GetNode(), UdpSocketFactory::GetTypeId());

        recvSocket->Bind(InetSocketAddress(Ipv4Address::GetAny(), localPort));

        recvSocket->SetRecvCallback(MakeCallback(&ActuatorApplication::HandleRead, this));

    }

    void SetLocalPort(uint16_t port) {

        localPort = port;

    }

    void HandleRead(Ptr<Socket> socket) {

        Ptr<Packet> packet;

        Address from;

        while ((packet = socket->RecvFrom(from))) {

            std::cout << “Actuator node ” << GetNode()->GetId() << ” received control message.” << std::endl;

            // Actuate based on the received message

        }

    }

private:

    Ptr<Socket> recvSocket;

    uint16_t localPort;

};

class ControlApplication : public Application {

public:

    void StartApplication() override {

        recvSocket = Socket::CreateSocket(GetNode(), UdpSocketFactory::GetTypeId());

        recvSocket->Bind(InetSocketAddress(Ipv4Address::GetAny(), localPort));

        recvSocket->SetRecvCallback(MakeCallback(&ControlApplication::HandleRead, this));

        sendSocket = Socket::CreateSocket(GetNode(), UdpSocketFactory::GetTypeId());

    }

    void SetLocalPort(uint16_t port) {

        localPort = port;

    }

    void HandleRead(Ptr<Socket> socket) {

        Ptr<Packet> packet;

        Address from;

        while ((packet = socket->RecvFrom(from))) {

            std::cout << “Control node ” << GetNode()->GetId() << ” received data.” << std::endl;

            // Process sensor data and send control messages if needed

            SendControlMessage();

        }

    }

    void SendControlMessage() {

        std::ostringstream msg;

        msg << “Control message from node ” << GetNode()->GetId();

        Ptr<Packet> packet = Create<Packet>((uint8_t*) msg.str().c_str(), msg.str().size());

        for (uint32_t i = 0; i < actuatorInterfaces.GetN(); ++i) {

            sendSocket->SendTo(packet, 0, InetSocketAddress(actuatorInterfaces.GetAddress(i), actuatorPort));

        }

    }

    void SetActuatorInterfaces(Ipv4InterfaceContainer interfaces, uint16_t port) {

        actuatorInterfaces = interfaces;

        actuatorPort = port;

    }

private:

    Ptr<Socket> recvSocket;

    Ptr<Socket> sendSocket;

    Ipv4InterfaceContainer actuatorInterfaces;

    uint16_t localPort;

    uint16_t actuatorPort;

};

int main(int argc, char *argv[]) {

    NodeContainer sensorNodes, actuatorNodes, controlNodes, gatewayNodes;

    sensorNodes.Create(10);

    actuatorNodes.Create(5);

    controlNodes.Create(2);

    gatewayNodes.Create(1);

    // WiFi setup

    WifiHelper wifi;

    wifi.SetStandard(WIFI_PHY_STANDARD_80211n_5GHZ);

    WifiMacHelper mac;

    mac.SetType(“ns3::AdhocWifiMac”);

    YansWifiPhyHelper phy = YansWifiPhyHelper::Default();

    YansWifiChannelHelper channel = YansWifiChannelHelper::Default();

    phy.SetChannel(channel.Create());

    NetDeviceContainer sensorDevices = wifi.Install(phy, mac, sensorNodes);

    NetDeviceContainer actuatorDevices = wifi.Install(phy, mac, actuatorNodes);

    NetDeviceContainer controlDevices = wifi.Install(phy, mac, controlNodes);

    NetDeviceContainer gatewayDevices = wifi.Install(phy, mac, gatewayNodes);

    // Mobility setup

    MobilityHelper mobility;

    mobility.SetMobilityModel(“ns3::ConstantPositionMobilityModel”);

    mobility.Install(sensorNodes);

    mobility.Install(actuatorNodes);

    mobility.Install(controlNodes);

    mobility.Install(gatewayNodes);

    // Internet stack and routing

    AodvHelper aodv;

    InternetStackHelper internet;

    internet.SetRoutingHelper(aodv);

    internet.Install(sensorNodes);

    internet.Install(actuatorNodes);

    internet.Install(controlNodes);

    internet.Install(gatewayNodes);

    Ipv4AddressHelper address;

    address.SetBase(“10.1.1.0”, “255.255.255.0”);

    Ipv4InterfaceContainer sensorInterfaces = address.Assign(sensorDevices);

    Ipv4InterfaceContainer actuatorInterfaces = address.Assign(actuatorDevices);

    Ipv4InterfaceContainer controlInterfaces = address.Assign(controlDevices);

    Ipv4InterfaceContainer gatewayInterfaces = address.Assign(gatewayDevices);

    // Install applications

    ApplicationContainer sensorApps, actuatorApps, controlApps;

 

    // Install sensor applications

    for (uint32_t i = 0; i < sensorNodes.GetN(); ++i) {

        Ptr<SensorApplication> app = CreateObject<SensorApplication>();

        app->SetGatewayAddress(gatewayNodes.Get(0)->GetObject<Ipv4>()->GetAddress(1, 0).GetLocal(), 9);

        sensorNodes.Get(i)->AddApplication(app);

        app->SetStartTime(Seconds(1.0));

        app->SetStopTime(Seconds(20.0));

        sensorApps.Add(app);

    }

    // Install actuator applications

    for (uint32_t i = 0; i < actuatorNodes.GetN(); ++i) {

        Ptr<ActuatorApplication> app = CreateObject<ActuatorApplication>();

        app->SetLocalPort(9);

        actuatorNodes.Get(i)->AddApplication(app);

        app->SetStartTime(Seconds(1.0));

        app->SetStopTime(Seconds(20.0));

        actuatorApps.Add(app);

    }

    // Install control applications

    for (uint32_t i = 0; i < controlNodes.GetN(); ++i) {

        Ptr<ControlApplication> app = CreateObject<ControlApplication>();

        app->SetLocalPort(9);

        app->SetActuatorInterfaces(actuatorInterfaces, 9);

        controlNodes.Get(i)->AddApplication(app);

        app->SetStartTime(Seconds(1.0));

        app->SetStopTime(Seconds(20.0));

        controlApps.Add(app);

    }

    Simulator::Stop(Seconds(20.0));

    Simulator::Run();

    Simulator::Destroy();

    return 0;

}

In the conclusion, we deliberated the industrial internet of things procedures, process, to analysis the performance in ns3 environment and also provide the full support for any other information about industrial Internet of Things environment. Related to your project you can get programming support on all areas of IIOT from our developers.