# An Enhanced Mechanism for Fault Tolerance in Agricultural Wireless Sensor Networks
## 5 Fault-Tolerant Scheme for WSN in Precision Agriculture
### Fault-Tolerant Clusters
#### 5.1.2 Fault-Tolerant CH  
Two CHs must be placed (see Figure 4) for our model. The first one has the role of the primary CH, while the other plays the role of the secondary CH. Figure 5 illustrates this concept. First, an initialization phase has to be performed between the two CHs by communicating the primary CH ID and sensor nodes' TDMA schedule. This step can be considered as a task assignment in order to start working (initialization). After that, the primary CH takes the role of receiving, aggregating, and transmitting environmental data from the sensor nodes to the BS. In addition, it is responsible for detecting any crashed or failed sensor(s) and providing a report on them to the BS. Furthermore, it keeps the backup CH updated with any data it has received. In this context, it is essential to mention that the primary CH and its backup work in mutual exclusion and each one has its own ID (i.e.,  
Figure 4: Fault-tolerant WSN model for precision agriculture. Figure 5: Fault-tolerant Cluster Head. identity). It is also possible to use more than one backup CH; it can be two or more and as the number increases, the efficiency also increases for precision agriculture (see Figure 6). On the other hand, the backup CH is constantly on standby to take over the tasks in the case of a malfunction or breakdown of the primary CH (see Figure 7). Moreover, it is responsible for monitoring the primary CH at all times, and if no _Heartbeat message_ is received, the backup CH will directly assume the tasks and role reversal will take place. **Role reversal:** In the case of primary CH failure, the backup CH would broadcast its identity over the cluster and subsequently, it would send a notification carrying the identity of the failed CH to the BS with the aim of promptly repairing it. After that, the new primary CH would start working normally while the previous primary CH would assume the role of the backup CH. Figure 6: Fault-tolerant primary CH process. Figure 7: Fault-tolerant backup CH process. All the steps described above are summarized in Algorithm 3. Instructions 1-24 pertain to the primary CH, while instructions 25-31 are related to the backup CH. The role reversal instructions are cited in lines 32-36. Finally, the sensor node process is presented in instructions 37-43. ```
Input: Wireless sensor network Output: Fault-tolerant cluster communication protocol
```
1. Primary Cluster Head 0
2. initiate clock x =0;
3. For loop = 1 to \(n\) nodes
4. If\(x=time\)\(slot_{loop}\)then
5. Send Data Request to the \(node_{loop}\)
6. If the Primary CH Receives data from \(Node_{loop}\)then
7. save \((NodeData_{i})\) in Sensor_data_file;
8. Else
9. If x >= \(Node\_TimeOut\) and no data received from the sensor node then
10. Threads \(node_{loop}\) in Failed_Sensornodes_File;
11. End If
12. End If
14. End For
15. Aggregate collected data;
16. If data_request received from the BS then
17. Send Aggregated Data to the BS;
18. Send the Failed_Nodes_File to the BS;
19. End If
20. If x = Heartbeat_Time then
21. Send 'I am alive' message to the Backup CH;
22. Send a checkpoint with Aggregated Data and Failed_Sensornodes_File to the Backup CH;
23. End If
24. End Primary Cluster Head process0
```
**Algorithm 3** Fault-tolerant protocol in the cluster

**Backup Cluster Head0**
```
Input: Wireless sensor network Output: Fault-tolerant cluster communication protocol
```
1. Primary Cluster Head 0
2. initiate clock x =0;
3. For loop = 1 to \(n\) nodes
4. If\(x=time\)\(slot_{loop}\)then
5. Send Data Request to the \(node_{loop}\)
6. If the Primary CH Receives data from \(Node_{loop}\)then
7. save \((NodeData_{i})\) in Sensor_data_file;
8. Else
9. If x >= \(Node\_TimeOut\) and no data received from the sensor node then
10. Threads \(node_{loop}\) in Failed_Sensornodes_File;
11. End If
12. End If
13. End If
14. End For
15. Aggregate collected data;
16. If data_request received from the BS then
17. Send Aggregated Data to the BS;
18. Send the Failed_Nodes_File to the BS;
19. End If
20. If x = Heartbeat_Time then
21.