The security of the network is a significant issue in any distributed system. For that intrusion detection system (IDS), have been proposed for securing the network from malicious activities. This research is proposed to design and develop an anomaly detection model for detecting attacks and unusual activities in IoT networks. The primary objective of this research is to design efficient IDS for IoT network. The intrusion detection plays an essential role in detecting different attacks on IoT and enhances the performance of the IoT. In this research, anomaly detection in IoT networks using glowworm swarm optimization (GSO) algorithm with principal component analysis (PCA) is proposed. However, the proposed model is metaheuristic algorithm-based anomaly detection model to identify attacks by using the NSL-KDD dataset. The GSO algorithm based on PCA is implemented to perform the anomaly detection. For feature extraction, the PCA is used, and for classification, the GSO algorithm is used. For performance analysis, various parameters like accuracy, precision, recall, detection rate and FAR are evaluated. For normal class the proposed model achieved 94.14% accuracy, for DoS 95.52%, for R2L 93.15%, for probe 93.50% and for U2R 88.62% accuracy. Overall the detection rate was 94.08% and FAR was 3.41%.