Source code for multi hop LEACH protocol in wireless sensor networks

70 views (last 30 days)
This question was flagged by Walter Roberson
Source code for multi hop LEACH protocol in wireless sensor networks
  1 Comment
Walter Roberson
Walter Roberson on 28 Nov 2021
The paper https://iopscience.iop.org/article/10.1088/1757-899X/1010/1/012014/pdf indicates that they ran tl-leach simulations in MATLAB. You could write to the authors and ask if they will make their code available.

Sign in to comment.

Accepted Answer

Vishal Bauskar
Vishal Bauskar on 28 May 2017
i had done work on this concept. i can provide u the code do contact me vishalbauskar@live.com
  14 Comments
Walter Roberson
Walter Roberson on 30 Aug 2022
noor raad
I noticed that there are at least two pay-to-plaigerize sites that are offering code for this purpose. You could buy code from one of them.

Sign in to comment.

More Answers (1)

Eng.Miasser
Eng.Miasser on 17 Jan 2023 at 10:32
clc;
clear;
close all ;
warning off all;
tic;
%% create sensor nodes , Set Parameters and create Energy model
%% initial parameters
n = 100 ; %Number of Nodes in the field
[Area,Model]=setParameters(n); % set Parameter sensors and network
%configuration Sensors
CreateRandomSen(Model,Area); %create a random scenario
load locations %load sensor location
Sensors= ConfigureSensors(Model , n , X , Y );
ploter(Sensors, Model); %Plot sensors
% parameter initialization
countCHs=0 ; %counter for CHs
flag_first_dead=0; %flag_first_dead
deadNum =0 ; %Number of dead nodes
initEnergy=0; %Initial Energy
for i=1:n
initEnergy = Sensors(i).E+initEnergy;
end
SRP =zeros(1,Model , rmax); %number of sent routing packets
RRP = zeros(1 , Model , rmax); %number of receive routing packets
SDP = zeros(1, Model , rmax); %number of sent data packets
RDP = zeros(1,Model ,rmax); %number of receive data packets
Sum_DEAD =zeros(1,Model.rmax);
CLUSTERHS = zeros(1, Model.rmax);
AllSensorEnergy = zeros(1 , Model.rmax);
%start simulation
globe srp rrp sdp rdp
srp=0 ; %counter number of sent routing packets
rrp=0; %counter number of receive routing packets
sdp=0; %counter number of sent data packets
rdp=0; % counter number of receive data packets
%sink broadcast start message to all nodes
Sender= n+1 ; %sink
Receiver= 1:n ; %All nodes
Sensors = SendReceivePackets(Sensors , Model , Sender , 'Hello' , Receiver);
%All sensor send location information to sink
Sensors=disToSink(Sensors , Model);
%Sender = 1:n ; %All nodes
%Receiver = n+1; %Sink
%Sensors = SendReceivePackets(Sensors , Model , Sender , 'Hello' ,Receiver);
%Save metrics
SRP(1) = srp;
RRP(1) = rrp;
SDP(1) = sdp;
RDP(1) = rdp;
%Main loop program
for r=1:1:Model.rmax
% Initialization
%This section operate for each epoch
member=[]; %Member of each cluster in per period
coutCHs=0; %Number of CH in per period
%counter for bit transmitted to bases station and cluster Heads
srp=0 ; %counter number of sent routing packets
rrp=0; %counter number of receive routing packets
sdp=0; %counter number of sent data packets to sink
rdp=0; % counter number of receive data packets by sink
%initialization per round
SRP(r+1) = srp;
RRP(r+1) = rrp;
SDP(r+1) = sdp;
RDP(r+1) = rdp;
pause(0.001) %pause simulation
hold off; %clear figure
Sensors=resetSensors(Sensors, Model);
% allow to sensor to become cluster-head LEACH algorithm
AroundClear =10 ;
if(mod(r, AroundClear) == 0)
for i=1:1:n
Sensors(i).G=0;
end
end
%plot Sensors
deadNum=ploter(Sensors, Model);
% save r'th period when the first node dies
if(deadNum >= 1)
if(flag_first_dead == 0)
first_dead = r ;
flag_first_dead =1 ;
end
end
% cluster head election
% Selection Candidate Cluster Head Based on LEACH set-up Phase
[TotalCH , Sensors] = SelectCH(Sensors , Model , r);
% Broadcasting CHs to All Sensors that are in Radio Rage CH
for i=1:length(TotalCH)
Sender=TotalCH(i).id ;
SenderRR= Model.RR ;
Receiver = findReceiver(Sensors , Model , Sender , SenderRR);
Sensors = SendReceivePackets(Sensors , Model , Sender , 'Hello' , Receiver);
end
% Sensors join to nearest CH
Sensors = JoinToNearestCH(Sensors , Model , TotalCH);
% End of cluster head election phase
% plot network status in end of set-up phase
for i=1:n
if(Sensors(i).type == 'N' && Sensors(i).dis2ch<Sensors(i).dis2sink && Sensors(i).E>0)
XL = [Sensors(i).xd , Sensors(Sensors(i).MCH).xd];
YL = [Sensors(i).yd , Sensors(Sensors(i).MCH).yd];
hold on
line(XL , YL)
end
end
% Steady-State phase
NumPacket = Model.NumPacket ;
for i = 1 :1 : 1 % NumPacket
%Plotter
deadNum = ploter(Sensors , Model);
% All sensors send data packet to CH
for j = 1 :length(TotalCH)
Receiver = TotalCH(j).id ;
Sender = findSender(Sensors , Model , Receiver);
Sensors = SendReceivePackets(Sensors , Model , Sender , 'Data' , Receiver);
end
end
% Send data packet from CH to sink after Data aggregation
for i = 1: length(TotalCH)
Receiver= n+1 ; %sink
Sender = TotalCH(i).id ; %CH
Sensors = SendReceivePackets(Sensors , Model , Sender , 'Data' , Receiver);
end
% send data packet directly from other nodes (that are not in each cluster ) to sink
for i = 1 : n
if(Sensors(i).MCH == Sensors(n+1).id)
Receiver = n+1 ; % sink
Sender = Sensors(i).id ; % other nodes
Sensors = SendReceivePackets(Sensors , Model , Sender , 'Data' , Receiver);
end
end
% Statistics
Sum_DEAD(r+1) = deadNum ;
SRP(r+1) = srp;
RRP(r+1) = rrp;
SDP(r+1) = sdp;
RDP(r+1) = rdp;
CLUSTERHS(r+1) = countCHs ;
alive = 0 ;
SensorEnergy = 0;
for i=1:n
if Sensors(i).E>0
alive= alive + 1 ;
SensorEnergy = SensorEnergy + Sensors(i).E ;
end
end
aliveSensors(r) = alive; % OK
SumEnergyAllSensor(r+1) = SensorEnergy ;
AvgEnergyAllSensor(r+1) = SensorEnergy / alive ;
ConsumEnergy(r+1) = ( initEnergy-SumEnergyAllSensor(r+1))/n ;
En=0 ;
for i =1:n
if Sensors(i).E>0
En = En+ Sensors(i).E-AvgEnergyAllSensor(r+1))^2 ;
end
end
Enheraf(r+1) = En/ alive ;
title(sprintf( 'Round=%d , Dead nodes = %d' , r+1 , deadNum));
%dead
if(n==deadNum)
lastPeriod = r ;
break;
end
end % for r=0:1:rmax
% end % for r=0:1:rmax
disp('End of simulation');
toc;
disp('create report ')
filename= sprintf('leach%d.mat' , n);
% save report
save(filename);

Categories

Find more on WSNs in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!