Wireless sensor networks are employed to monitor physical areas in different places. Fault tolerance and energy efficiency are main challenges of the networks. WSNs are designed to transmit sensed data to the base station when a part of the network is faulty. In this paper, an algorithm is presented to withstand the challenges. In the suggested algorithm, HEED clustering algorithm and sleep/wake-up method are used for the members of cluster to improve energy consumption. To detect and recover the faults of cluster heads and cluster member nodes, a node is selected as backup cluster head. Also, weighted median is employed to detect and recover the faults of cluster nodes. To recover an fault, the faulty node is isolated and replaced by a neighboring node going from sleep mode to wake-up mode. On comparing the proposed algorithm with HEED and DFD algorithms, the results of simulation reveal that energy consumption is improved and the rate of received correct data, detection accuracy and survival nodes are increase and false alarm rate are decrees.