The document proposes an approach to optimize energy consumption in sensor networks. It allocates tasks to sensor nodes using a particle swarm optimization algorithm that considers energy for data communication between nodes. Simulation results show the proposed approach reduces energy consumption and increases network lifetime compared to existing approaches that only allocate tasks to cluster gateways. The key aspects of the proposed approach are using a cost function that includes communication energy in the task allocation algorithm and having nodes send combined data from neighboring nodes to reduce the number of messages.