The rapid expansion of artificial intelligence (AI) integrated with the Internet of Things (IoT) has fueled the development of various smart devices, particularly for smart city applications. However, the heterogeneity of these devices necessitates a robust communication network capable of maintaining a consistent traffic flow. This paper employs Machine Learning (ML) models to classify continuously received network parameters from diverse IoT devices, identifying necessary adjustments to enhance network performance. Key network traffic parameters, such as packet data, are transmitted through gateways via specialized tools. Six different ML techniques with default parameters were used: Decision Tree (DT), Random Forest (RF), Support Vector Machines (SVMs), K-Nearest Neighbors (KNN), Naive Bayes (NB), and Stochastic Gradient Descent Classifiers (SGDC), to classify the traffic of the environment (IoT / non IoT). The models' performance was evaluated in a real-time smart laboratory environment comprising 38 IoT devices from various vendors with the following metrics: Accuracy, F1-score, Recall and Precision. The RF model achieved the highest Accuracy of 95.6%. Also the Binary Particle Swarm Optimizer (BPSO) was used across the RF. The results demonstrated that the BPSO-RF with hyperparameter optimization enhanced the Accuracy from 95.6% to 99.4%.