To implement 6TiSCH (IPv6 over the TSCH mode of IEEE 802.15.4e) sensors communication in ns3, we need to configure the Time-Slotted Channel Hopping (TSCH) mechanism and integrating it with IPv6 to set-up a simulation in which that sensor nodes use the 6TiSCH protocol for communication. If you face difficulties even after looking out the implementation steps then feel free to contact us.
The following steps will guide on how to implement 6TiSCH sensors communication in ns3.
Step-by-step guide to implement in ns3
Step 1: Set Up the ns3 Environment
- Install ns-3: Make sure that ns3 is installed on the system
sudo apt-get update
sudo apt-get install ns3
- Install the Required Modules: Make sure that the necessary modules installed. For 6TiSCH, we need to have the lr-wpan and internet
Step 2: Create a New ns3 Project
- Create a directory for the new project within the ns3 workspace:
cd ns-3
mkdir scratch/6tisch-sensor-communication
Step 3: Implement 6TiSCH Communication
- Create a New Simulation Script: Create a new script in the scratch directory to implement the simulation scenario.
// 6tisch-sensor-communication.cc
#include “ns3/core-module.h”
#include “ns3/network-module.h”
#include “ns3/internet-module.h”
#include “ns3/ipv6-address.h”
#include “ns3/ipv6-static-routing-helper.h”
#include “ns3/lr-wpan-module.h”
#include “ns3/ipv6.h”
#include “ns3/ipv6-routing-table-entry.h”
using namespace ns3;
NS_LOG_COMPONENT_DEFINE (“6TiSCHSensorCommunication”);
void ReceivePacket (Ptr<Socket> socket) {
Ptr<Packet> packet;
Address from;
while ((packet = socket->RecvFrom (from))) {
if (InetSocketAddress::IsMatchingType (from)) {
NS_LOG_UNCOND (“Received one packet!”);
}
}
}
int main (int argc, char *argv[]) {
CommandLine cmd;
cmd.Parse (argc, argv);
NodeContainer nodes;
nodes.Create (3);
LrWpanHelper lrWpanHelper;
NetDeviceContainer lrwpanDevices = lrWpanHelper.Install (nodes);
lrWpanHelper.AssociateToPan (lrwpanDevices, 0);
MobilityHelper mobility;
mobility.SetMobilityModel (“ns3::ConstantPositionMobilityModel”);
mobility.Install (nodes);
InternetStackHelper internetv6;
internetv6.Install (nodes);
Ipv6AddressHelper ipv6;
ipv6.SetBase (Ipv6Address (“2001:1::”), Ipv6Prefix (64));
Ipv6InterfaceContainer ipv6Interfaces = ipv6.Assign (lrwpanDevices);
// Set up routing
Ipv6StaticRoutingHelper ipv6RoutingHelper;
Ptr<Ipv6> ipv6Ptr = nodes.Get (0)->GetObject<Ipv6> ();
Ptr<Ipv6StaticRouting> staticRouting = ipv6RoutingHelper.GetStaticRouting (ipv6Ptr);
staticRouting->AddNetworkRouteTo (Ipv6Address (“2001:2::”), Ipv6Prefix (64), 1);
// Set up applications
uint16_t port = 9;
Address sinkAddress (Inet6SocketAddress (ipv6Interfaces.GetAddress (1, 1), port));
PacketSinkHelper packetSinkHelper (“ns3::UdpSocketFactory”, sinkAddress);
ApplicationContainer sinkApps = packetSinkHelper.Install (nodes.Get (1));
sinkApps.Start (Seconds (0.0));
sinkApps.Stop (Seconds (10.0));
Ptr<Socket> source = Socket::CreateSocket (nodes.Get (0), TypeId::LookupByName (“ns3::UdpSocketFactory”));
Inet6SocketAddress remote = Inet6SocketAddress (ipv6Interfaces.GetAddress (1, 1), port);
source->SetAllowBroadcast (true);
source->Connect (remote);
Simulator::Schedule (Seconds (1.0), &ReceivePacket, source);
AsciiTraceHelper ascii;
lrWpanHelper.EnableAsciiAll(ascii.CreateFileStream (“6tisch-sensor-communication.tr”));
Simulator::Stop (Seconds (10.0));
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
Compile the Script: Compile the script using the waf build system.
./waf build
Run the Simulation: Run the simulation script and observe the results.
./waf –run scratch/6tisch-sensor-communication
Step 4: Configure 6TiSCH Communication
- Create Nodes and Set Up Mobility: Create sensor nodes and set their positions.
NodeContainer nodes;
nodes.Create (3);
MobilityHelper mobility;
mobility.SetMobilityModel (“ns3::ConstantPositionMobilityModel”);
mobility.Install (nodes);
Install 6TiSCH and Network Devices: Set up the 6TiSCH network devices and associate them to a PAN (Personal Area Network).
LrWpanHelper lrWpanHelper;
NetDeviceContainer lrwpanDevices = lrWpanHelper.Install (nodes);
lrWpanHelper.AssociateToPan (lrwpanDevices, 0);
Configure IPv6 Addressing: Assign IPv6 addresses to the nodes.
InternetStackHelper internetv6;
internetv6.Install (nodes);
Ipv6AddressHelper ipv6;
ipv6.SetBase (Ipv6Address (“2001:1::”), Ipv6Prefix (64));
Ipv6InterfaceContainer ipv6Interfaces = ipv6.Assign (lrwpanDevices);
Set Up Routing: Configure static routing to establish paths between nodes.
Ipv6StaticRoutingHelper ipv6RoutingHelper;
Ptr<Ipv6> ipv6Ptr = nodes.Get (0)->GetObject<Ipv6> ();
Ptr<Ipv6StaticRouting> staticRouting = ipv6RoutingHelper.GetStaticRouting (ipv6Ptr);
staticRouting->AddNetworkRouteTo (Ipv6Address (“2001:2::”), Ipv6Prefix (64), 1);
Install Applications: Set up applications to generate and receive traffic.
uint16_t port = 9;
Address sinkAddress (Inet6SocketAddress (ipv6Interfaces.GetAddress (1, 1), port));
PacketSinkHelper packetSinkHelper (“ns3::UdpSocketFactory”, sinkAddress);
ApplicationContainer sinkApps = packetSinkHelper.Install (nodes.Get (1));
sinkApps.Start (Seconds (0.0));
sinkApps.Stop (Seconds (10.0));
Ptr<Socket> source = Socket::CreateSocket (nodes.Get (0), TypeId::LookupByName (“ns3::UdpSocketFactory”));
Inet6SocketAddress remote = Inet6SocketAddress (ipv6Interfaces.GetAddress (1, 1), port);
source->SetAllowBroadcast (true);
source->Connect (remote);
Step 5: Enable Tracing and Run the Simulation
- Enable Tracing: Enable tracing to collect data for analysis.
AsciiTraceHelper ascii;
lrWpanHelper.EnableAsciiAll (ascii.CreateFileStream (“6tisch-sensor-communication.tr”));
Run the Simulation: Set the simulation stop time and run it.
Simulator::Stop (Seconds (10.0));
Simulator::Run ();
Simulator::Destroy ();
On the conclusion the 6TiSCH sensor communication has been implemented in ns3 by configuring the Time-slotted Channel Hopping mechanism and integration with IPv6 for the simulation process.