Given the importance of using the railway networks for the reliable and safe transportation of goods and passengers, which plays an important role in the economy of rail transport of the country, it is necessary to use wireless sensor network techniques to raise the quality and quantity and also maintain the application of these facilities for better control and optimal use of relevant technologies in this regard. Regarding the development of wireless sensor networks for collecting and processing environmental information, we intended to employ them in order to maintain railways, and also prevent defects that might occur in mobile machines and parts, as well as to avoid probable events and reduce human errors. Considering the limited capacity of batteries for sensor nodes in a wireless sensor network, energy efficiency is an important issue; thus mobile sinks are presented as a possible solution. In this study, the proposed routing uses Ring Routing and an optimization gravitational search algorithm for the primary selection of the ring causes an increase in a sensor network's lifespan. In this study, by applying the proposed method in the railway transportation system, it is improved to reduce the existing errors by creating a system with a higher safety factor and high quality and efficiency, together with maximum efficiency and good speed.