Main Content

Establish Friendship Between Friend Node and LPN in Bluetooth Mesh Network

This example shows how to simulate a Bluetooth® mesh network by using Bluetooth® Toolbox.

Using this example, you can:

  1. Create and configure a Bluetooth mesh network consisting of a source node, Friend node, a Low Power node (LPN), and a relay node.

  2. Configure and establish friendship between the LPN and Friend node.

  3. Add application traffic between the source node and LPN and relay the traffic by using the Friend node and relay node.

  4. Simulate Bluetooth mesh network and retrieve the statistics of mesh nodes.

Create a wireless network simulator.

networkSimulator = wirelessNetworkSimulator.init();

Create a default Bluetooth mesh profile configuration object, specifying the element address of the source node.

cfgMeshSource = bluetoothMeshProfileConfig(ElementAddress="0001");

Create a Bluetooth LE node, specifying the role as "broadcaster-observer". Specify the position of the source node. Assign the mesh profile configuration to the source node.

sourceNode = bluetoothLENode("broadcaster-observer",AdvertisingInterval=30e-3,ScanInterval=30e-3);
sourceNode.Position = [0 0 0];
sourceNode.MeshConfig = cfgMeshSource;

Create a default Bluetooth mesh profile configuration object, specifying the element address and enabling the relay feature of the Bluetooth LE node.

cfgMeshRelay = bluetoothMeshProfileConfig(ElementAddress="0002",Relay=true);

Create a Bluetooth LE node, specifying the role as "broadcaster-observer". Specify the position of the relay node. Assign the mesh profile configuration to the relay node.

relayNode = bluetoothLENode("broadcaster-observer",AdvertisingInterval=30e-3,ScanInterval=30e-3);
relayNode.Position = [25 0 0];
relayNode.MeshConfig = cfgMeshRelay;

Create a default Bluetooth mesh profile configuration object, specifying the element address and enabling the friend feature of the Bluetooth LE node.

cfgMeshFriend = bluetoothMeshProfileConfig(ElementAddress="0003",Friend=true);

Create a Bluetooth LE node, specifying the role as "broadcaster-observer". Specify the position of the friend node. Assign the mesh profile configuration to the friend node.

friendNode = bluetoothLENode("broadcaster-observer",AdvertisingInterval=30e-3,ScanInterval=30e-3);
friendNode.Position = [50 0 0];
friendNode.MeshConfig = cfgMeshFriend;

Create a default Bluetooth mesh profile configuration object, specifying the element address of the Bluetooth LE node.

cfgMeshLPN = bluetoothMeshProfileConfig(ElementAddress="0004",LowPower=true);

Create a Bluetooth LE node, specifying the role as "broadcaster-observer". Specify the position of the LPN node. Assign the mesh profile configuration to the LPN.

lowPowerNode = bluetoothLENode("broadcaster-observer",AdvertisingInterval=30e-3,ScanInterval=30e-3);
lowPowerNode.Position = [75 0 0];
lowPowerNode.MeshConfig = cfgMeshLPN;

Create a default Bluetooth mesh friendship configuration object. Specify the poll timeout to terminate a friendship, the receive window supported by Friend node, and the receive delay requested by LPN, in seconds.

cfgFriendship = bluetoothMeshFriendshipConfig;
cfgFriendship.PollTimeout = 6;          
cfgFriendship.ReceiveWindow = 180e-3; 
cfgFriendship.ReceiveDelay = 50e-3;

Configure the connection between the friend node and LPN by using the configureFriendship object function of the bluetoothMeshFriendshipConfig object.

configureFriendship(cfgFriendship,friendNode,lowPowerNode);

Create a networkTrafficOnOff object to generate an On-Off application traffic pattern. Specify the on time, data rate in kb/s, and packet size in bytes. Generate an application packet with a payload by enabling packet generation.

traffic = networkTrafficOnOff(OnTime=inf, ...
    DataRate=1, ...
    PacketSize=15, ...
    GeneratePacket=true);

Add application traffic between the source and destination node.

addTrafficSource(sourceNode,traffic, ...
    SourceAddress=cfgMeshSource.ElementAddress, ...
    DestinationAddress=cfgMeshLPN.ElementAddress,TTL=5);

Create a Bluetooth mesh network consisting of the source node, relay node, friend node and destination node.

nodes = {sourceNode relayNode friendNode lowPowerNode};

Add the mesh nodes to the wireless network simulator.

addNodes(networkSimulator,nodes)

Set the simulation time and run the simulation.

simulationTime = 5;                   % In seconds
run(networkSimulator,simulationTime);
Custom channel model is not added. Using free space path loss (fspl) model as the default channel model.

Retrieve application, link layer (LL) , and physical layer (PHY) statistics related to the source, relay, and destination nodes by using the statistics object function. For more information about the statistics, see Bluetooth LE Node Statistics.

sourceStats = statistics(sourceNode)
sourceStats = struct with fields:
         Name: "Node1"
           ID: 1
          App: [1x1 struct]
    Transport: [1x1 struct]
      Network: [1x1 struct]
           LL: [1x1 struct]
          PHY: [1x1 struct]

relayStats = statistics(relayNode)
relayStats = struct with fields:
         Name: "Node2"
           ID: 2
          App: [1x1 struct]
    Transport: [1x1 struct]
      Network: [1x1 struct]
           LL: [1x1 struct]
          PHY: [1x1 struct]

friendStats = statistics(friendNode)
friendStats = struct with fields:
         Name: "Node3"
           ID: 3
          App: [1x1 struct]
    Transport: [1x1 struct]
      Network: [1x1 struct]
           LL: [1x1 struct]
          PHY: [1x1 struct]

LPNStats = statistics(lowPowerNode)
LPNStats = struct with fields:
         Name: "Node4"
           ID: 4
          App: [1x1 struct]
    Transport: [1x1 struct]
      Network: [1x1 struct]
           LL: [1x1 struct]
          PHY: [1x1 struct]

Display the number of data messages transmitted by the Friend node.

fprintf("Number of data messages transmitted by the Friend node = %d\n",friendStats.Transport.TransmittedDataMessages);
Number of data messages transmitted by the Friend node = 3

Display the number of received data messages at the LPN.

fprintf("Number of data messages received by the LPN = %d\n",LPNStats.Transport.ReceivedDataMessages);
Number of data messages received by the LPN = 3

References

[1] Bluetooth® Technology Website. “Bluetooth Technology Website | The Official Website of Bluetooth Technology.” Accessed November 22, 2021. https://www.bluetooth.com/.

[2] Bluetooth Special Interest Group (SIG). "Bluetooth Core Specification." Version 5.3. https://www.bluetooth.com/.

See Also

Functions

Objects

Related Topics