Over the past few decades, the Internet of Things (IoT) has become increasingly significant due to its capacity to enable low-cost device and sensor communication. Implementation has opened up many new opportunities in terms of efficiency, productivity, convenience, and security. However, it has also brought about new privacy and data security challenges, interoperability, and network reliability. The research issue is that IoT devices are frequently open to attacks. Certain machine learning (ML) algorithms still struggle to handle imbalanced data and have weak generalization skills when compared to ensemble learning. The research aims to develop security for IoT networks based on enhanced ensemble learning by using Grid Search and Random Search techniques. The method used is the ensemble learning approach, which consists of Random Forest (RF), Adaptive Boosting (AdaBoost), Gradient Boosting Machine (GBM), and Extreme Gradient Boosting (XGBoost). This study uses the UNSW-NB15 IoT dataset. The study's findings demonstrate that XGBoost performs better than other methods at identifying IoT network attacks. By employing Grid Search and Random Search optimization, XGBoost achieves an accuracy rate of 98.56% in binary model measurements and 97.47% on multi-class data. The findings underscore the efficacy of XGBoost in bolstering security within IoT networks.