Detailed explanation of Zigbee networking principle[Copy link]
Building a complete Zigbee mesh network includes two steps: network initialization and node joining the network. Among them, node joining the network includes two steps: joining the network by connecting to the coordinator and joining the network through an existing parent node. 1. Network Overview Building a complete Zigbee mesh network includes two steps: network initialization and node joining the network. Among them, node joining the network includes two steps: joining the network by connecting to the coordinator and joining the network through an existing parent node. 2. Network Initialization Preparation The establishment of a Zigbee network is initiated by the network coordinator. Any Zigbee node must meet the following two requirements to form a network: (1) The node is an FFD node and has the ability of a Zigbee coordinator; (2) The node has not yet been connected to other networks. When the node has been connected to other networks, this node can only be a child node of the network, because there is only one network coordinator in a Zigbee network. FFD: Full Function Device RFD: Reduced Function Device 3. Network initialization process 3.1 Determine the network coordinator: First determine whether the node is an FFD node, and then determine whether the FFD node is in other networks or whether there is already a coordinator in the network. Through active scanning, send a beacon request command (Beaconrequest command), and then set a scanning period (T_scan_duration). If no beacon is detected during the scanning period, it is considered that the FFD has no coordinator in its POS. At this time, you can establish your own Zigbee network, and as the coordinator of this network, continuously generate beacons and broadcast them. Note: There is only one coordinator in a network. 3.2 Perform channel scanning process. Including energy scanning and active scanning: First, perform energy detection on the specified channel or the default channel to avoid possible interference. Sort the channels in an ascending manner according to the measured energy values, discard those channels whose energy values exceed the allowable energy level, select channels with allowable energy levels and mark them as available channels. Then, active scanning is performed to search for network information within the communication radius of the node. This information is broadcast in the network in the form of beacon frames. The node obtains these beacon frames through active channel scanning, and then finds the best and relatively quiet channel based on this information. According to the recorded results, a channel is selected. The channel should have the least zigbee network, preferably no zigbee device. During active scanning, the MAC layer will discard all frames except beacons received by the PHY layer data service. 3.3 Set the network ID. After finding a suitable channel, the coordinator will select a network identifier (PAN ID, value = 0x3FFF) for the network. This ID must be unique in the channel used, and cannot conflict with other zigbee networks, and cannot be the broadcast address 0xFFFF (this address is a reserved address and cannot be used). The PAN ID can be obtained by listening to the IDs of other networks and then selecting an ID that will not conflict, or by manually specifying the scanned channel to determine the PAN ID that does not conflict with other networks. There are two address modes in the zigbee network: extended address (64 bits) and short address (16 bits). The extended address is assigned by the IEEE organization for unique device identification; the short address is used for device identification in the local network. In a network, the short address of each device must be unique. When a node joins the network, it is assigned by its parent node and communicates by using the short address. For the coordinator, the short address is usually set to 0x0000. After completing the above steps, the zigbee mesh network is successfully initialized, and then waits for other nodes to join. When a node joins the network, it will select the parent node with the strongest signal within the range (including the coordinator) to join the network. If successful, it will get a network short address and send and receive data through this address. The network topology relationship and address will be saved in their respective flash. 4. The node joins the network through the coordinator. After the node coordinator is determined, the node first needs to establish a connection with the coordinator to join the network. In order to establish a connection, the FFD node needs to make a request to the coordinator. After receiving the node's connection request, the coordinator decides whether to allow it to connect according to the situation, and then responds to the node requesting the connection. Only after the node establishes a connection with the coordinator can data be sent and received. The specific process of a node joining a network can be divided into the following steps: 4.1 Finding a network coordinator. First, it will actively scan and search for coordinators in the surrounding networks. If a beacon is detected within the scanning period, the relevant information of the coordinator will be obtained, and then a connection request will be sent to the coordinator. After selecting a suitable network, the upper layer will request the MAC layer to set the PIB attributes such as phyCurrentChannel and macPANID of the physical layer PHY and MAC layer accordingly. If it is not detected, the node will re-initiate scanning after a period of time. 4.2 Send an association request command (Associaterequest command). The node sends the association request command to the coordinator. After receiving it, the coordinator immediately replies with an acknowledgment frame (ACK) and sends a connection indication primitive to its upper layer, indicating that the node's connection request has been received. However, this does not mean that the connection has been established, but only that the coordinator has received the node's connection request. When the upper layer of the coordinator's MAC layer receives the connection indication primitive, it will decide whether to agree to the node's joining request based on its own resource conditions (storage space and energy), and then send a response to the node's MAC layer. 4.3 Waiting for the coordinator to process. When the node receives the ACK of the coordinator's joining association request command, the node MAC will wait for a period of time to accept the coordinator's connection response. Within the predetermined time, if a connection response is received, it will notify its upper layer of this response. When the coordinator sends a response to the node's MAC layer, it will set a waiting response time (T_ResponseWaitTime) to wait for the coordinator to process its joining request command. If the coordinator's resources are sufficient, the coordinator will assign a 16-bit short address to the node and generate a connection response command containing the new address and connection success status. Then, this node will successfully establish a connection with the coordinator and can start communication. If the coordinator's resources are insufficient, the node to be joined will resend the request information and directly join the network successfully. 4.4 Sending a data request command. If the coordinator agrees to the node joining within the response time, an association response command (Associaterresponse command) will be generated and stored. When the response time is over, the node sends a data request command to the coordinator. The coordinator immediately replies with an ACK after receiving it, and then sends the stored association response command to the node. If the coordinator has not decided whether to agree to the node to join after the response time is up, the node will try to extract the association response command from the coordinator's beacon frame. If successful, it can successfully join the network. Otherwise, it will resend the request information until it succeeds. 4.5 Reply. After receiving the association response command, the node immediately replies with an acknowledgment frame (ACK) to the coordinator to confirm that it has received the connection response command. At this time, the node will save the coordinator's short address and extended address, and the node's MLME will send a connection confirmation primitive to the upper layer to notify the successful association joining information. 5.Nodes join the network through existing nodes. When the FFD nodes close to the coordinator successfully associate with the coordinator, other nodes within the network range join the network with these FFD nodes as parent nodes. There are two ways to join the network. One is through the association method, that is, the node to be joined initiates the joining of the network; the other is the direct method, that is, the node to be joined specifically joins that node as the child node of the node. Among them, the association method is the main way for new nodes to join the network in the Zigbee network. For a node, only those that have not joined the network can join the network. Among these nodes, some have joined the network before, but have lost contact with their parent nodes (such as orphan nodes), while some are new nodes. When it is an orphan node, the information of the original parent node is stored in its adjacent table, so it can directly send a request message to the original parent node to join the network. If the parent node is able to agree to its joining, it will directly tell its previously assigned network address, and it will successfully join the network; if the number of child nodes in the network of its original parent node has reached the maximum value, that is, the network address has been fully allocated, the parent node will not be able to approve its joining, and it can only re-search and join the network as a new node. For a new node, it will first actively or passively scan the surrounding networks it can find on one or more pre-set channels to find a parent node that is able to approve its joining the network, and store the information of the parent node that can be found in its own adjacent table. The information of the parent node stored in the adjacent table includes the version of the Zigbee protocol, the specification of the protocol stack, the PAN ID and the information that can be joined. Select the one with the smallest depth from all the parent nodes in the adjacent table and send a request message to it. If there are more than two parent nodes with the same minimum depth, then randomly select one to send a request. If there is no information about the appropriate parent node in the adjacent table, it means that the network access has failed and the process is terminated. If the request is approved, the parent node will also assign a 16-bit network address. At this time, the network access is successful and the child node can start communicating. If the request fails, the neighbor table is searched again and the request message is sent again until the network is joined.