The Internet of Things (IoT) is an emerging phenomenon in the field of communication, in which smart objects communicate with each other and respond to user requests. The IoT provides an integrated framework providing interoperability across various platforms. One of the most essential and necessary components of IoT is wireless sensor networks. Sensor networks play a vital role in the lowest level of IoT. Sensors in sensor networks use batteries which are not replaceable, and hence, energy consumption becomes of great importance. For this reason, many algorithms have been recently proposed to reduce energy consumption. In this paper, a meta-heuristic method called whale optimization algorithm(WOA) is used to clustering and select the optimal cluster head in the network. Factors such as residual energy, shorter distance, and collision reduction have been considered to determine the optimal cluster head. To prove the optimal performance of the proposed method, it is simulated and compared with three other methods in the same conditions. It outperforms the other methods in terms of energy consumption and the number of dead nodes.