With the extensive application of Internet of Things and wireless sensor networks (WSNs) in various real-time fields, they have become increasingly vulnerable to several types of attacks that could have a serious impact in their functionalities. Recently, intrusion detection systems have become one of the crucial security components. We propose in this work an approach based on machine and deep learning for DoS and DDoS attack detection. We have evaluated, analyzed, and compared the efficiency of three learning models separately, including deep neural network (DNN), random forest, and decision tree, using the standard metrics of evaluation such as accuracy, precision, F1-score, and recall. In our contribution, an approach ensemble learning was introduced in order to enhance the accuracy rate of classification. Our study was carried out using two well-known benchmark and real-time datasets, CICIoT-2023 and WSN-DS, intended for wireless sensor networks and IoT. These datasets containing various types of attacks. CICIoT2023 dataset contains 33 attacks divided into 7 classes, while WSN-DS dataset contains four types of DoS attacks, including Blackhole, Grayhole, Flooding, and TDMA. The experiment result demonstrate the effectiveness of our approach in attacks detection with high accuracy achieved close to perfect. Our approach result demonstrates that using the ensemble learning technique works better than each ML architecture independently.