Internet of Things has many applications requiring the use of wireless communications networks. It utilizes data collection from sensor nodes connected to Wireless Sensor Networks. As such the wireless sensor networks is considered an important key for data transmission between sensor node and the gateways which are connected to the internet. Of the main concerns is the lifetime of the network which is affected by the battery power of the sensor nodes. It is noteworthy that transmission energy that dominates overall energy consumption is proportional to the distance between the transmitter and receiver. Thus, there is a need to send data from source node to a destination node in the most efficient way when it comes to battery level. Although there are many algorithms that tried to perform energy efficient routing, we will propose an intelligent algorithm to further improve this routing problem. In this paper we proposed an AI algorithm to enhance the lifetime of wireless sensor networks. We show that our algorithm improves the lifetime of the network by up to 75%, depending on the traffic rate, over existing algorithms.