With the rapid development of the Internet of Things (IoT), the number of mobile terminal devices is increasing. Massive data are generated by mobile terminal devices, resulting in high delay and high energy consumption. In most cases, however, a low delay means high energy consumption. To balance energy consumption and delay, we adopt a tradeoff strategy that can realize optimal energy consumption with a delay threshold in this paper. First, we introduce the role of the delay threshold in reducing delay. Then, we describe the delay and energy consumption of the mobile terminal layer, fog node layer and cloud server layer with queue theory. Nonlinear programming is used to solve the energy optimization problem by calculating the optimal workload of each layer. We design a cloud-fog cooperation scheduling algorithm to reduce energy consumption. A task offloading algorithm is also designed to complete tasks when their nodes leave. The experimental results show that the energy consumption is reduced by approximately 22%, while the delay is 12.5% less than the first come first served (FCFS) approach. INDEX TERMS Energy consumption optimization, fog computing, tasks scheduling.