When using wireless sensor networks (WSNs) for data transmission, some critical respects should be considered. These respects are limited computational power, storage capability and energy consumption. To save the energy in WSNs and prolong the network lifetime, we design for the signal control input, routing selection and capacity allocation by the optimization model based on compressed sensing (CS) framework. The reasonable optimization model is decomposed into three subsections for three layers in WSNs: congestion control in transport layer, scheduling in link layer and routing algorithm in network layer, respectively. These three functions interact and are regulated by congestion ratio so as to achieve a global optimality. Congestion control can be robust and stable by CS theory that a relatively small number of the projections for a sparse signal contain most of its salient information. Routing selection is abided by fair resource allocation principle. The resources can be allocated more and more to the channel in the case of not causing more severe congestion, which can avoid conservatively reducing resources allocation for eliminating congestion. Simulation results show the stability of our algorithm, the accurate ratio of CS, the throughput, as well as the necessity of considering congestion in WSNs.