A key goal in developing and establishing a wireless sensor network is to maximise network longevity. An effective topology control strategy for achieving this aim is clustering sensor nodes. In this research, we provide an improved particle swarm optimization algorithm-based protocol for extending network lifespan, an optimization technique created to choose target nodes. Additionally, approaches for encoding and decoding are utilised to prolong network lifespan by using less time. Relay nodes are utilised when the distance from the cluster head to the base station is greater in order to reduce the cluster heads' excessive power consumption and take into consideration both energy efficiency and transmission distance. The network's lifespan is increased by the suggested protocol's improved distributed sensors and well-balanced clustering architecture. The suggested protocol operates well, as shown by the simulation results, which also display the nodes' energy and longevity. Using Network Animator, the simulation's output depicts how well wireless sensor networks operate. Additionally, graphs showing the energy used by nodes, their average lifespan (First Dead Node, Quarter Dead Node, and Half Dead Node), the number of Alive nodes, and their total remaining energy have been collected.