In this paper, we propose a lightweight FEC (forward error correction) coding algorithm combined with a fault tolerant routing scheme in wireless sensor networks (WSN). The coding-decoding algorithm is based on XOR operation and requires little computation and storage space, which are critical for WSN. There are some existing channel coding algorithms, but they are not suitable in WSN, due to their high computing, storage, delay cost and inflexibility. We adopt a cross-layer design wherein higher network layers use information about packet loss to adjust the coding level according to the dynamics of the network. Simulation shows that our coding algorithm with self-adaptive routing scheme performs better than existing schemes