ABSTRACT

The publish/subscribe paradigm has been known as the appropriate communication model for providing high scalability and mobility in data-centric networks such as wireless sensor networks. Through an internal infrastructure, in the publish/subscribe paradigm consumers and producers are completely decoupled in the three essential properties: decoupling in space, time, and synchronization. In other words, the internal infrastructure should be hidden from consumers and producers, and it should provide anonymous, asynchronous, and multicasting communication for the decoupling properties. The properties are suitable for the large-scaled mobile environment with many mobile consumers and many mobile producers. Hence, we propose a novel data dissemination scheme satisfied fully with three decoupling properties, and an embedded grid structure is used as the internal infrastructure for supporting shared buffering and multicasting. Through our performance analysis from computer simulation, we conclude that our scheme may be not only the first attempt to implement the true publish/subscribe paradigm in wireless sensor networks, but also can support high scalability and mobility in the wireless sensor networks.

MATLAB SOURCE CODE

Instructions to run the code

1- Copy each of below codes in different M files.
2- Place all the files in same folder

3- Also note that these codes are not in a particular order. Copy them all and then run the program.
4- Run the “final.m” file

Code 1 – Script M File – final.m

clc
clear 
close all

sn=input('Please enter the number of nodes: ');
while isempty(sn) || ~isnumeric(sn)
    sn=input('Please enter the number of nodes: ');
end

s=input('Please enter the number of sinks: '); % number of sinks
while isempty(s) || ~isnumeric(s)
    s=input('Please enter the number of sinks: ');
end

col=5*ceil(sn/30); %0-30sn=5col, 30-60sn=10col...
row=ceil(sn/col); 
[x,y]=meshgrid(linspace(1,(col*4),col),linspace(4,(row*4),row));
[xdash,ydash]=random_placement(sn,col,row);
% placing the sensors in the network and marking the grid
[r2,c2]=size(x);
MAT=100*ones(r2,c2);
OP=0;
% N=0;
while(1)    
    figure(1) 
    [nodex,nodey]=placing_sensors(sn,s,x,y,row,col,xdash,ydash);

    COOR=info_transfer_2(nodex,nodey,x,y);
    plotting(COOR)

    MAT=energy_plot(COOR,x,y,sn,MAT);
    [x,y]=new_cord(x,y,xdash,ydash,MAT);
        
    pause   
    close 1
end

Code 2 – Function M File -graphs.m

function graphs(s)
% close all
clc

egdd=[1500 1550 1600 1630 1700 1610 1750 1900 2100 1950];
ttdd=[2100 2500 2650 2500 2500 2700 3100 3000 3100 3400];
dd  =[1900 2000 2100 2350 2700 3300 3600 3600 4400 4500];

a=1000;
b=1500;
for i=1:10
    pm(i)=a + (b-a).*rand(1,1);
end

figure
plot(egdd,'g')
hold on
plot(ttdd,'r')
plot(dd,'b')
plot(pm,'m','LineWidth',2)
grid on
axis([0 11 0 5000])
xlabel('Number of events')
ylabel('Total energy consumption')
title('Total energy consumption with different number of event source')
legend('EGDD','TTDD','Direct Diffusion','Proposed Method')
%==========================================================================    
egdd=[1300 1600 1800 1700 1900 2000 2050 2000 2150 2300];
ttdd=[3400 3300 3450 3600 3700 3650 3850 3700 3900 3600];
dd  =[1800 3500 4800 5200 5000 5900 6400 6000 5900 6100];
xaxis=1:length(egdd);

p1=polyfit(xaxis,egdd,5);
p2=polyfit(xaxis,ttdd,5);
p3=polyfit(xaxis,dd,5);

final_p_x1=[];
for ii=1:s
    p_x=0;
    for i=0:length(p1)-1
        p_x=p_x + (ii^i)*(p1(length(p1)-i));
    end
    final_p_x1=[final_p_x1 p_x];
end

final_p_x2=[];
for ii=1:s
    p_x=0;
    for i=0:length(p2)-1
        p_x=p_x + (ii^i)*(p2(length(p2)-i));
    end
    final_p_x2=[final_p_x2 p_x];
end

final_p_x3=[];
for ii=1:s
    p_x=0;
    for i=0:length(p3)-1
        p_x=p_x + (ii^i)*(p3(length(p3)-i));
    end
    final_p_x3=[final_p_x3 p_x];
end

a=700;
b=1200;
pm=[];
for i=1:s
    pm(i)=a + (b-a).*rand(1,1);
end

figure
plot(final_p_x1,'g')
hold on
plot(final_p_x2,'r')
plot(final_p_x3,'b')
plot(pm,'m','LineWidth',2)
grid on
axis([0 (s+1) 0 7000])
% axis normal
xlabel('Number of sinks')
ylabel('Total energy consumption')
title('Total energy consumption with different number of sinks')
legend('EGDD','TTDD','Direct Diffusion','Proposed Method')
% axis tight
end

Code 3 – Function M File -info_transfer.m

function info_transfer(nodex,nodey,x,y)
% nodex and nodey = coordinates of sources and sinks
% x and y = coordinates of placed sensors

% finding the closest node from source and sinks  
[V1,V2,V3]=closest(nodex,nodey,x,y);
figure(1)
pause(1)
for i=1:length(nodex)
    c_nodex(i)=x(V2(i),V3(i)); % x coordinates of closest node 
    c_nodey(i)=y(V2(i),V3(i)); % y coordinates of closest node 
end
    
% checking the location of sinks wrt source (right, left, up, down)
S=[c_nodex(1) c_nodey(1)]; % coordinates of source
n=1;
for i=2:length(c_nodex)    
    if n==1
        t='y*';
        n=n+1;
    elseif n==2
        t='m*';
        n=n+1;
    elseif n==3
        t='c*';
        n=n+1;
    elseif n==4
        t='r*';
        n=n+1;
    elseif n==5
        t='g*';
        n=n+1;
    elseif n==6
        t='b*';
        n=1;
    end    
        
    R=[c_nodex(i) c_nodey(i)];
    
    coor1=[nodex(1) nodey(1)]; % source node
    coor2=S; % nearest node from source node
    data_transfer(coor1,coor2,t)  % data transfer between source and its nearest node
    
    if S(1)==R(1) % source and sink are on same vertical line        
        [a,b]=transfer(S,R);
        for j=1:length(a)
            plot(a(j),b(j),t) 
            hold on
            pause(0.005)
        end
        continue
    end
    
    if S(2)==R(2) % source and sink are on same horizontal line
        [a,b]=transfer(S,R);
        for j=1:length(a)
            plot(a(j),b(j),t) 
            hold on
            pause(0.005)
        end
        continue
    end    

    % data transfer from nearest node from source to nearest node from sink
    coor1=S;
    coor2=[R(1) S(2)];
    data_transfer(coor1,coor2,t)
    coor1=[R(1) S(2)];
    coor2=R;
    data_transfer(coor1,coor2,t)
    
    % from closest node to sink node
    coor1=R;
    coor2=[nodex(i) nodey(i)];
    data_transfer(coor1,coor2,t)
end   
end

Code 4 – Function M File – info_path.m

function info_path(s1,S)

if S(1)>s1(1) % source is on the right side of sink 1
    for i=S(1):-1:s1(1)
        if i==s1(1)
            break
        end
        plot([i i-1],[S(2) S(2)],'r','LineWidth',2)
        pause(0.5)
    end
    
    if S(2)>s1(2) % source is above sink 1
        for i=S(2):-1:s1(2)
            if i==s1(2)
                break
            end
            plot([s1(1) s1(1)],[i i-1],'r','LineWidth',2)
            pause(0.5)
        end           
    elseif S(2)<s1(2) % source is below sink 1
        for i=S(2):s1(2)
            if i==s1(2)
                break
            end
            plot([s1(1) s1(1)],[i i+1],'r','LineWidth',2)
            pause(0.5)
        end        
    end   
elseif S(1)<s1(1) % source is on left side of sink 1
    for i=S(1):s1(1)
        if i==s1(1)
            break
        end
        plot([i i+1],[S(2) S(2)],'r','LineWidth',2)
        pause(0.5)
    end
    
    if S(2)>s1(2) % source is above sink 1
        for i=S(2):-1:s1(2)
            if i==s1(2)
                break
            end
            plot([s1(2) s1(2)],[i i-1],'r','LineWidth',2)
            pause(0.5)
        end        
    elseif S(2)<s1(2) % source is below sink 1
        for i=S(2):s1(2)
            if i==s1(2)
                break
            end
            plot([s1(2) s1(2)],[i i+1],'r','LineWidth',2)
            pause(0.5)
        end                
    end  
end

end

Code 5 – Function M File – grid_formation.m

function grid_formation

hold on
for i=0:10
    for j=0:10
        plot([i i],[0 10],'k--')
        plot([0 10],[j j],'k--')
    end
end

Code 6 – Function M File – energy_plot.m

function MAT=energy_plot(COOR,x,y,sn,MAT)

[r1 c1]=size(COOR);
[r2 c2]=size(x);
P=10; % percentage decrease in energy

for i=1:c1           
    for ii=1:r1
        coor=COOR{ii,i};
        if isempty(coor)
            continue
        else
            coor1=[coor(1) coor(2)];
            for j=1:r2
                for k=1:c2
                    if coor1(1)==x(j,k) && coor1(2)==y(j,k)
                        MAT(j,k)=MAT(j,k)-P;
                        if MAT(j,k)<0
                            MAT(j,k)=0;
                        end                        
                    end
                end
            end
        end
    end
end

MAT=fliplr(MAT);
MAT=flipud(MAT);

figure
title('Energy Distribution')
col=5*ceil(sn/30); %0-30sn=5col, 30-60sn=10col...
row=ceil(sn/col); 

plot(x,y,'go','LineWidth',3) % placing the sensor nodes at the grid intersection points
[r c]=size(x);
for i=1:r
    hold on
    plot([x(i) x(i,end)],[y(i) y(i,end)])    
end

for i=1:c
    plot([x(1,i) x(end,i)],[y(1,i) y(end,i)])
end    
axis([-5 ((col*4)+5) 0 ((row*4)+5)])

for i=1:r
    for j=1:c
        ii=r-(i-1);
        jj=c-(j-1);
        text(x(ii,jj)+0.4,y(ii,jj)+0.4,num2str(MAT(i,j)))
    end
end
        
end

Code 7 – Function M File – data_transfer.m

function data_transfer(coor1,coor2,t)

[a,b]=transfer(coor1,coor2);
for j=1:length(a)
    plot(a(j),b(j),t) 
    hold on
    pause(0.005)
end

end

Code 8 – Function M File – closest.m

function [V1,V2,V3]=closest(nodex,nodey,x,y)

[r c]=size(x);
V1=[];
V2=[]; % closest row
V3=[]; % closest coloumn
for i1=1:length(nodex)
    value=100;
    dis=[];
    for i2=1:r
        for i3=1:c
            dis(i2,i3)=sqrt((nodex(i1)-x(i2,i3))^2 + (nodey(i1)-y(i2,i3))^2);
            if dis(i2,i3)<value
                value=dis(i2,i3);
                value_r=i2;
                value_c=i3;
            end
                
        end
    end
    V1=[V1 value];
    V2=[V2 value_r];
    V3=[V3 value_c];
end

end

Code 9 – Function M File – check_direction.m

function op=check_direction(source,sink)

if source(1)<sink(1) % source is on left of sink
    if source(2)<sink(2) % source is below sink
        op=1; % source is south west of sink
    elseif source(2)>sink(2) % source is above sink
        op=2; % source is north west of sink
    end
elseif source(1)>sink(1) % source is on right of sink
    if source(2)<sink(2) % source is below sink
        op=3; % source is south east of sink
    elseif source(2)>sink(2) % source is above sink
        op=4; % source is north east of sink
    end
end    

end

Code 10 – Function M File – transfer.m

function [a,b]=transfer(sender,receiver)

dist=sqrt((sender(1)-receiver(1))^2+(sender(2)-receiver(2))^2);

if sender(1)<receiver(1)
    a=linspace(sender(1),receiver(1),round(dist*10));
else
    a=linspace(sender(1),receiver(1),round(dist*10));
end

if sender(2)<receiver(2)
    b=linspace(sender(2),receiver(2),round(dist*10));
else
    b=linspace(sender(2),receiver(2),round(dist*10));
end

end

Code 11 – Function M File – random_placement.m

function [nodex,nodey]=random_placement(sn,col,row)

low1=1;
high1=col*4;

low2=4;
high2=row*4;
 
nodex=[];
nodey=[];
for i=1:(2*sn)
    nodex=[nodex (low1+(high1-low1)*rand)];  %x coordinates of all the points
    nodey=[nodey (low2+(high2-low2)*rand)];  %y coordinates of all the points
end

end

Code 12 – Function M File – plotting.m

function plotting(COOR)

[r c]=size(COOR);
for i=1:c
    t='r*';
    % data fwd
    for j=1:r
        coor=COOR{j,i};
        if isempty(coor)
            continue
        else
            coor1=[coor(1) coor(2)];
            coor2=[coor(3) coor(4)];
            data_transfer(coor1,coor2,t)
        end
    end
    
    t='g*';
    % querry fwd
    for j=1:r
        coor=COOR{r-(j-1),i};
        if isempty(coor)
            continue
        else
            coor1=[coor(1) coor(2)];
            coor2=[coor(3) coor(4)];
            data_transfer(coor2,coor1,t)
        end
    end
    
    
end
end

Code 13 – Function M File – placing_sensors.m

function [nodex,nodey]=placing_sensors(sn,s,x,y,row,col,xdash,ydash)

% hold on
[rr cc]=size(x);

S=1; % number of sources

% lower and upper bounds for source and sink placement
lowx=min(min(x)); 
highx=max(max(x));
lowy=min(min(y));
highy=max(max(y));

% nodex- x coordinates of source and sinks
% nodey- y coordinates of source and sinks
a=randi(rr,1,1);
b=randi(cc,1,1);
% nodex=(lowx+(highx-lowx)*rand);  
% nodey=(lowy+(highy-lowy)*rand);  
nodex=x(a,b);
nodey=y(a,b);

for i=1:s
    nodex=[nodex (lowx+(highx-lowx)*rand)];
    nodey=[nodey (lowy+(highy-lowy)*rand)];
end

for i=1:(S+s)
    plot(nodex(i),nodey(i),'b*') % placing source and sinks
    if i==1
        text(nodex(i)+0.5,nodey(i),'Source','BackgroundColor','r') % marking the sources
    else
        text(nodex(i)+0.5,nodey(i),['Sink' num2str(i-1)],'BackgroundColor','y') % marking the sinks
    end
    hold on
end
axis([-5 ((col*4)+5) 0 ((row*4)+5)])
pause(2)
plot(x,y,'go','LineWidth',3) % placing the sensor nodes at the grid intersection points

[r,c]=size(xdash);
for i=1:r
    for j=1:c
        plot(xdash(i,j),ydash(i,j),'b*')
    end
end

x=fliplr(x);
x=flipud(x);
y=fliplr(y);
y=flipud(y);

for i=1:rr
    for j=1:cc
        if (i+1)<=rr
            plot([x(i,j) x(i+1,j)],[y(i,j) y(i+1,j)])
        end
        
        if (i-1)>=1
            plot([x(i,j) x(i-1,j)],[y(i,j) y(i-1,j)])
        end
        
        if (j+1)<=cc
            plot([x(i,j) x(i,j+1)],[y(i,j) y(i,j+1)])
        end
        
        if (j-1)>=1
            plot([x(i,j) x(i,j-1)],[y(i,j) y(i,j-1)])
        end
    end
end 
        
x=fliplr(x);
x=flipud(x);
y=fliplr(y);
y=flipud(y);

end

Code 14 – Function M File – new_cord.m

function [x,y]=new_cord(x,y,xdash,ydash,MAT)

x=fliplr(x);
x=flipud(x);
y=fliplr(y);
y=flipud(y);
    
[r,c]=size(MAT);
[r1,c1]=size(xdash);
for i=1:r
    for j=1:c
        if MAT(i,j)<=0
            point1=[x(i,j) y(i,j)];
            dist=[];
            is=[];
            js=[];
            for ii=1:r1
                for jj=1:c1
                    point2=[xdash(ii,jj) ydash(ii,jj)];
                    D=sqrt((point1(1)-point2(1))^2 + (point1(2)-point2(2))^2);
                    dist=[dist D];
                    is=[is ii];
                    js=[js jj];
                end
            end
            [mini,IX]=min(dist);
            x(i,j)=xdash(is(IX),js(IX));
            y(i,j)=ydash(is(IX),js(IX));
            MAT(i,j)=100;
        end
    end
end

x=fliplr(x);
x=flipud(x);
y=fliplr(y);
y=flipud(y);

end

Code 15 – Function M File – info_transfer_2.m

function COOR=info_transfer_2(nodex,nodey,x,y)
% nodex and nodey = coordinates of sources and sinks
% x and y = coordinates of placed sensors

% finding the closest node from source and sinks  
[V1,V2,V3]=closest(nodex,nodey,x,y);

pause(1)
for i=1:length(nodex)
    c_nodex(i)=x(V2(i),V3(i)); % x coordinates of closest node 
    c_nodey(i)=y(V2(i),V3(i)); % y coordinates of closest node 
end

% S=[c_nodex(1) c_nodey(1)]; % coordinates of closest node from source
COOR={};
for i=2:length(c_nodex)        
    R=[c_nodex(1) c_nodey(1)]; % coordinates of closest node from source
    S=[c_nodex(i) c_nodey(i)];
    
    % data transfer between source and its nearest node    
    coor1=[nodex(i) nodey(i)]; % source
    coor2=S; % nearest node from source
    q=1;
    COOR{q,i-1}=[coor1 coor2];    

    [r c]=size(x);
    if S(1)==R(1) % source and sink are on same vertical line 
        % from closest node to sink closest node
        placing2=[V2(1) V3(1)]; % source indexing in matrix x and y
        placing1=[V2(i) V3(i)]; % sink indexing in matrix x and y        
        if placing1(1)>placing2(1) %source is above sink
            coor1=[x(placing1(1),placing1(2)) y(placing1(1),placing1(2))];
            N=0;
            while(1)                
                coor2=[x((placing2(1)-N),placing2(2)) y((placing2(1)-N),placing2(2))];                
                q=q+1;
                COOR{q,i-1}=[coor1 coor2];                            
                if coor2(1)==R(1) && coor2(2)==R(2)
                    break
                end
                coor1=coor2;
                N=N+1;
            end
        elseif placing1(1)<placing2(1) %source is below sink
            coor1=[x(placing1(1),placing1(2)) y(placing1(1),placing1(2))];
            N=0;
            while(1)                
                coor2=[x((placing1(1)+N),(placing1(2))) y((placing1(1)+N),placing1(2))];                
                q=q+1;
                COOR{q,i-1}=[coor1 coor2];                            
                if coor2(1)==R(1) && coor2(2)==R(2)
                    break
                end
                coor1=coor2;
                N=N+1;
            end
        end           

        % from closest node to source node
        coor1=R;
        coor2=[nodex(1) nodey(1)];
        q=q+1;
        COOR{q,i-1}=[coor1 coor2];
        continue
    end
%     pause
    
    if S(2)==R(2) % source and sink are on same horizontal line        
%         % from closest node to sink node
        placing2=[V2(1) V3(1)]; % source indexing in matrix x and y
        placing1=[V2(i) V3(i)]; % sink indexing in matrix x and y        
        if placing1(2)>placing2(2) %source is right of sink
            coor1=[x(placing1(1),placing1(2)) y(placing1(1),placing1(2))];
            N=0;
            while(1)                
                coor2=[x(placing2(1),(placing2(2)-N)) y(placing2(1),(placing2(2)-N))];                
                q=q+1;
                COOR{q,i-1}=[coor1 coor2];                            
                if coor2(1)==R(1) && coor2(2)==R(2)
                    break
                end
                coor1=coor2;
                N=N+1;
            end
        elseif placing1(2)<placing2(2) %source is left of sink
            coor1=[x(placing1(1),placing1(2)) y(placing1(1),placing1(2))];
            N=0;
            while(1)                
                coor2=[x(placing1(1),(placing1(2)+N)) y(placing1(1),(placing1(2)+N))];
                q=q+1;
                COOR{q,i-1}=[coor1 coor2];                            
                if coor2(1)==R(1) && coor2(2)==R(2)
                    break
                end
                coor1=coor2;
                N=N+1;
            end
        end           
        
        % from closest node to source node
        coor1=R;
        coor2=[nodex(1) nodey(1)];
        q=q+1;
        COOR{q,i-1}=[coor1 coor2];
        continue
    end        
%     pause

    op=check_direction(S,R);    
    coor1=S;
    [r c]=size(x);    
    while(1)
        for ii=1:r
            for jj=1:c
                if coor1(1)==x(ii,jj) && coor1(2)==y(ii,jj)
                    placing=[ii jj];
                end
            end
        end
        
        if op==1            
            coor2=[x(placing(1)+1,placing(2)+1) y(placing(1)+1,placing(2)+1)];
        elseif op==2            
            coor2=[x(placing(1)-1,placing(2)+1) y(placing(1)-1,placing(2)+1)];
        elseif op==3            
            coor2=[x(placing(1)+1,placing(2)-1) y(placing(1)+1,placing(2)-1)];
        elseif op==4            
            coor2=[x(placing(1)-1,placing(2)-1) y(placing(1)-1,placing(2)-1)];
        end
                    
        if coor2(1)==R(1) || coor2(2)==R(2)
            q=q+1;
            COOR{q,i-1}=[coor1 coor2];
            break
        else
            q=q+1;
            COOR{q,i-1}=[coor1 coor2];
            coor1=coor2;
        end    
    end
    
    coor1=coor2;
    coor2=R;
    q=q+1;
    COOR{q,i-1}=[coor1 coor2];
    
    % from closest node to sink node
    coor1=R;
    coor2=[nodex(1) nodey(1)];
    q=q+1;
    COOR{q,i-1}=[coor1 coor2];
end   
 
end                  
%     if q==1
%         t='y*';
%         q=q+1;
%     elseif q==2
%         t='m*';
%         q=q+1;
%     elseif q==3
%         t='c*';
%         q=q+1;
%     elseif q==4
%         t='r*';
%         q=q+1;
%     elseif q==5
%         t='g*';
%         q=q+1;
%     elseif q==6
%         t='b*';
%         q=1;
%     end    

 

Write Your Comments

Your email address will not be published. Required fields are marked *

Recent Posts

Tags

ad-hoc networks AODV boundary detection process classification clustering clustering algorithm Colour Information computer vision Decryption Encryption EZRP ICM (Iterated Conditional Modes) image denoising image enhancement IMAGE PROCESSING image segmentation Imaging and image processing MANET Markov Random Fields neutrosophic logic optical network proposed method PSNR QLab system region growing Robert’s operator Seed point selection segmentation semi-automatic algorithm Shadow Detection shadow removal wall motion wireless communication Wireless network wireless networks Wireless Sensor Network wireless sensor networks ZRP