Wireless sensor networks are rapidly evolving technological platforms with tremendous applications in several domains. Since sensor nodes are battery powered and may be used in dangerous or inaccessible environments, it is difficult to replace or recharge their power supplies. Clustering is an effective approach to achieve energy efficiency in wireless sensor networks. In clustering-based routing protocols, cluster heads are selected among all sensor nodes within the network, and then clusters are formed by simply assigning each node to the nearest cluster head. The main drawback is that there is no control on the distribution of cluster heads over the network. In addition to the problem of generating unbalanced clusters, almost all routing protocols are designed for a certain application scope, and could not cover all applications. In this paper, we propose a swarm intelligence based fuzzy routing protocol (named SIF), in order to overcome the mentioned drawbacks. In SIF, fuzzy c-means clustering algorithm is utilized to cluster all sensor nodes into balanced clusters, and then appropriate cluster heads are selected via Mamdani fuzzy inference system. This strategy not only guarantees to generate balanced clusters over the network, but also has the ability to determine the precise number of clusters. In fuzzy-based routing protocols in literature, the fuzzy rule base table is defined manually, which is not optimal for all applications. Since tuning the fuzzy rules very affects on the performance of the fuzzy system, we utilize a hybrid swarm intelligence algorithm based on firefly algorithm and simulated annealing to optimize the fuzzy rule base table of SIF. The fitness function can be defined according to the application specifications. Unlike other routing protocols which have been designed for a certain application scope, the main objective of our methodology is to prolong the network lifetime based on the application specifications. In other words, SIF not only prolongs the network lifetime, but also is applicable to any kind of application. Obtained simulation results over 10 heterogeneous networks show that SIF outperforms the existing clustering-based protocols in terms of generating balanced clusters and prolonging the network lifetime.