# UAV-Assisted Dynamic Clustering of Wireless Sensor Networks for Crop Health Monitoring
## 6 Developed System Algorithm  
The overall working of the developed system is explained in the state diagram (Figure 8) where the homological sequences of steps are shown. The variables used in the algorithm are listed in Table 4. As shown in Figure 8, blue color represents the top layer, Yellow is the middle one and green represents the last layer.. The UAV needs the following inputs to start the mission and data collection procedure: the path of the UAV consists of waypoints \(\{(x1,y1),\ (x2,\ y2),\ (x3,\ y3),\ldots\}\), \(D_{1}\) height, the UAV normally flies at this height and conducts localization and navigation (e.g., 200), \(D_{2h}\) is maximum possible height for data collection (e.g., 170 m), \(D_{2L}\) is the lowest possible height for the data collection (e.g., 20 m), \(D_{2}\) is the data collection height, and the UAV only obtains this height to establish a backbone high data rate link with the CH to collect data, after that UAV goes back to its normal height \(D_{1}\). Figure 8: State diagram of the developed system. Initially \(D_{2}\) should mainly be set to an average height (e.g., \(170+20/2=95\) m), \(Th\) the threshold value \(0~{}\leq Th\leq 1\), \(Th_{L}\) denotes minimum threshold, \(Th_{H}\) stands for maximum threshold and \(g\) is the GPS accuracy (e.g., \(5\) m). The UAV is equipped with a virtual phase array antenna which can operate at two different modes Low/High, where Low means with the least specification that can locate fewer targets and High means maximum specifications [12]. The UAV sends beacon messages to activate the ground sensors. The beacon is composed of three basic kinds of information: \(T_{ys}\) is the type of nodes must be activated, \(D_{2}\) distance at which UAV will collect data and \(Th\) is threshold. The process steps are as below:  
1. The nodes not mentioned in this beacon message continue sleeping, while others will become activated. 2. The activated nodes will broadcast a message to all their neighbors to let them know how much data and energy they have. 3. All activated nodes will calculate the cost of transmission \(TE_{Tx}\) as per Equation (12) and their probability to be a cluster head P\({}_{i}\) as per Equation (10). All the nodes having energy greater than \(TE_{Tx}\) and get a higher probability than the threshold will declare themselves candidate cluster heads (CCHs) and remaining ones will be cluster members (CM). 4. CCHs will proceed further and start sending a narrowband signal to UAV having P\({}_{i}\) and their IDs. 5. When the UAV gets a reply from ground sensor nodes, it will use its on board virtual antenna and estimate the number of replying sensor nodes \(q\). If the UAV finds \(0~{}<~{}q~{}<~{}m\) then it will go into localization mode, to estimate the location of these CCHs, otherwise it will start the shunting process. 6. Shunting is a process to pull or push some sensor nodes from the process to keep their number within some reasonable range \((1~{}~{}to~{}m-1)\), \(m\) is the total number of virtual antenna elements. The shunting process is shown by a red diamond in the state diagram (see Figure 6 For shunting, UAVs have three options that it will be used in steps: 1. The first option: the UAV is equipped with an adjustable virtual antenna that can operate in two different modes: fast mode with minimum localization ability and high specification mode. If the UAV finds many CCH nodes contesting for cluster head (CH), it will switch its mode to high performance mode. 2. If the UAV finds that the number of replying CCHs is even greater than the high performance antenna's capacity then it will try to reduce the number of CCHs by increasing the data collection height \(D_{2}\). The \(D_{2}\) can also be used in reverse order, if no sensor node is contesting for CH, it means none has the ability to send aggregated data to the UAV at this distance and in this case, \(D_{2}\) can be decreased up to a minimum level (safe flight). 3.