Train accidents continuously occur due to a variety of causes (carelessness by the conductor, broken wheels, disaster during train movements, etc.). To prevent train-related accidents in advance, it is considered the monitoring train systems with wireless sensor networks (WSNs). In this paper, we propose an efficient algorithm to dynamically configure WSNs-based trains for relaying sensing data via Wi-Fi. We believe that this algorithm can be exploited in WSNs design to monitor sensing data in long-range environments.