In wireless sensor networks (WSNs), designing a stable, low-power routing protocol is a major challenge because successive changes in links or breakdowns destabilize the network topology. Therefore, choosing the right route in this type of network due to resource constraints and their operating environment is one of the most important challenges in these networks. Therefore, the main purpose of these networks is to collect appropriate routing information about the environment around the network sensors while observing the energy consumption of the sensors. One of the important approaches to reduce energy consumption in sensor networks is the use of the clustering technique, but in most clustering methods, only the criterion of the amount of energy of the cluster or the distance of members to the cluster has been considered. Therefore, in this paper, a method is presented using the firefly algorithm and using the four criteria of residual energy, noise rate, number of hops, and distance. The proposed method called EM-FIREFLY is introduced which selects the best cluster head with high attractiveness and based on the fitness function and transfers the data packets through these cluster head to the sink. The proposed method is evaluated with NS-2 simulator and compared with the algorithm-PSO and optimal clustering methods. The evaluation results show the efficiency of the EM-FIREFLY method in maximum relative load and network lifetime criteria compared to other methods discussed in this article.