In recent decades, Intelligent home systems are popular because they improve comfort and quality of life. A growing number of homes are becoming "smarter" by incorporating Internet of Things (IoT) technology to improve comfort, energy efficiency, and safety. Increases in resourceconstrained IoT devices heighten security threats and vulnerabilities connected with them. Using SDN and virtualization, the IoT's size and adaptability can be managed at a lower cost than ever before. Using these intelligent security solutions, we can achieve real-time detection and automation for attack detection and prevention using artificial intelligence. Consequently, a large variety of solutions utilizing machine learning and deep learning have been developed to mitigate attacks on the IoT. Thus, the goal of this work is to use machine learning and deep learning to defend smart homes with SDNbased. We have designed smart home environments using Software-Defined Networking and Mininet that provide Instant Virtual networks for IoT in smart homes. Two datasets were used in this work: the first SDN dataset, which we acquired from smart homes by launching real attacks and creating normal traffic, and the second IoTID20 dataset, which is publicly available online. On both datasets, conducted ML and DL experiments. The best accuracy on SDN Dataset was 99.9% using Xgboost classifier, and on IoTID20 was 98.9% LSTM in binary classification, and ANN 85.7% on multiclass.