# 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
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
```
**Backup Cluster Head0**  
**Initiate clock x :=0;  
**If** x >= Heartbeat_TimeOut and no alive message received from the Primary CH then
28. Alerte: 'Failed Pimary CH'
29. proceed a roles exchange between the failed Primary CH and the Backup CH;
30. End If
31. End Backup Cluster Head0
```

**CH Roles Exchange0**

**Exchange (Primary CH; Backup CH);

**Broadcast the new Primary CH ID over the cluster;

**Send a failure alert to the BS with the failed CH ID;

**End CH Roles Exchange0**

**Sensor node0**

**If** receive data request from the Primary CH then

**Go to active state;

**Send environment data to the Primary CH;

**Go to passive state;

**End If**

**End Sensor node0**

### Fault-Tolerant Base Station

As our considered scenario is precision agriculture, it is imperative to ensure the correct operation of the BS in order to ensure the accurate transmission of information related to the agricultural field. In fact, any malfunction of the BS can lead to a breakdown of the network and the loss of the collected information. In order to avoid such problems, we used a physical replication approach and this was implemented by using two BSs: a primary BS and a backup BS. Both of these BSs are completely identical in terms of hardware as well as software resources, in addition to their close proximity in the agricultural field to ensure the same distance to the rest of the clusters. The main function of the primary BS is to execute all the assigned tasks of a Base Station in a WSN. First, it receives the final user's requests. Then, it sends data requests to the set of CHs. After that, it receives the collected information as well as the list of failed equipment (i.e., failed sensor nodes and failed CHs). Finally, it sends the collected data to the final user. On the other hand, the role of the backup BS is solely to monitor the primary BS and store an updated version of the collected data as well as the most recent request received from the final user. Figure 8 illustrates this concept. In the event of a failure or malfunction of the primary BS, the backup BS takes over the tasks seamlessly, and the WSN continues to function normally without any interruption as if no failure occurred. This is based on the assumption that the backup BS possesses all the necessary data to fulfil the tasks. In this context, it is essential to mention again that the activation of the primary BS and its backup is mutually exclusive and each one has its own ID (i.e., identification number). To start operating, an initialization step must be carried out between the primary BS and its backup by preparing themselves to work in rotation (when need be) and this can be achieved by exchanging the necessary information before starting the task. The primary information includes the primary BS ID and the set of CHs' TDMA schedule. After that, the primary BS takes on the tasks of collecting the data received from the CHs in addition to the specific data concerning sensor node and CH failures. The primary BS then reduces the amount of information by aggregating the data into a single packet, which is sent to the final user (see Figure 9). One of the additional tasks for the primary BS is to keep the backup station informed about all WSN updates and development through regular updates (i.e., checkpoints). Figure 8: Fault-tolerant Base Station. Again, the backup BS is ready to take over at any moment after the detection of a primary BS failure. The fault detection is provided when the live message is not received from the primary BS within the allocated _Heartbeat time_. In that event, the primary BS is considered as faulty and the role reversal is implemented immediately (see Figure 10). Hence, in the case of a fault detection, the Primary BS is replaced immediately by the Backup BS.