The Internet of Things enables objects to interact via data exchange to reach specific objectives. IoT applications rely heavily on data aggregation, e.g., systems for control and computation on the Internet. Due to the increasing number of IoT devices connected via wireless networks with limited resources, highly efficient data aggregation is essential to handle complicated queries from IoT applications. This paper proposes a novel method using the firefly optimization algorithm to decrease energy consumption, improve network lifetime, reduce delay, and increase the packet delivery rate. The base station acts as the root of the aggregation tree, while the intermediate nodes act as aggregators of their children's nodes. According to simulation results, we observed that the energy consumption, network lifetime, packet delivery rate, and delay had been improved up to 20%, 10%, 10%, and 5%, respectively, compared to previous works.