help@rskworld.in +91 93305 39277
RSK World
  • Home
  • Development
    • Web Development
    • Mobile Apps
    • Software
    • Games
    • Project
  • Technologies
    • Data Science
    • AI Development
    • Cloud Development
    • Blockchain
    • Cyber Security
    • Dev Tools
    • Testing Tools
  • Blog
  • About
  • Contact

Theme Settings

Color Scheme
Display Options
Font Size
100%
Back to Project
RSK World
iot-sensors
/
scripts
RSK World
iot-sensors
IoT Sensors Dataset - Time Series Analysis + Anomaly Detection + Sensor Fusion + Predictive Maintenance
scripts
  • __pycache__
  • analyze_sensors.py10 KB
  • anomaly_detection.py11.2 KB
  • api_server.py6.8 KB
  • ml_prediction.py10.9 KB
  • real_time_simulator.py6.8 KB
  • sensor_fusion.py11 KB
real_time_simulator.pyanomaly_detection.py
scripts/real_time_simulator.py
Raw Download
Find: Go to:
#!/usr/bin/env python3
"""
IoT Real-Time Data Simulator
Website: https://rskworld.in
Email: help@rskworld.in
Phone: +91 93305 39277
Author: RSK World

Simulates real-time IoT sensor data streaming:
- Live data generation
- WebSocket-like streaming simulation
- Real-time visualization
- Data export to CSV/JSON
"""

import pandas as pd
import numpy as np
import time
import json
from datetime import datetime, timedelta
import os
import warnings
warnings.filterwarnings('ignore')

class RealTimeSimulator:
    """
    Real-Time IoT Sensor Data Simulator
    Website: https://rskworld.in
    Email: help@rskworld.in
    Phone: +91 93305 39277
    Author: RSK World
    """
    
    def __init__(self, devices=['DEV001', 'DEV002', 'DEV003', 'DEV004', 'DEV005']):
        """
        Initialize simulator
        Website: https://rskworld.in
        Email: help@rskworld.in
        Phone: +91 93305 39277
        Author: RSK World
        """
        self.devices = devices
        self.data_buffer = []
        self.running = False
    
    def generate_sensor_reading(self, device_id, base_temp=22.0, base_humidity=60.0, 
                                base_pressure=1013.0):
        """
        Generate a single sensor reading
        Website: https://rskworld.in
        Email: help@rskworld.in
        Phone: +91 93305 39277
        Author: RSK World
        """
        # Add some realistic variation
        temp = base_temp + np.random.normal(0, 2) + np.sin(time.time() / 3600) * 3
        humidity = max(20, min(90, base_humidity + np.random.normal(0, 5)))
        pressure = base_pressure + np.random.normal(0, 2)
        motion = 1 if np.random.random() > 0.7 else 0
        
        # Detect anomalies
        anomaly = 1 if (temp > 32 or humidity < 25 or pressure < 1000) else 0
        
        return {
            'timestamp': datetime.now().isoformat(),
            'device_id': device_id,
            'temperature': round(temp, 2),
            'humidity': round(humidity, 2),
            'pressure': round(pressure, 2),
            'motion': motion,
            'anomaly': anomaly
        }
    
    def stream_data(self, duration=60, interval=1, output_file=None):
        """
        Stream sensor data in real-time
        Website: https://rskworld.in
        Email: help@rskworld.in
        Phone: +91 93305 39277
        Author: RSK World
        """
        print("\n" + "="*60)
        print("REAL-TIME SENSOR DATA STREAMING")
        print("="*60)
        print(f"Duration: {duration} seconds")
        print(f"Interval: {interval} seconds")
        print("Press Ctrl+C to stop\n")
        
        self.running = True
        start_time = time.time()
        count = 0
        
        try:
            while self.running and (time.time() - start_time) < duration:
                # Generate reading for each device
                for device in self.devices:
                    reading = self.generate_sensor_reading(device)
                    self.data_buffer.append(reading)
                    count += 1
                    
                    # Print to console
                    print(f"[{reading['timestamp']}] {device}: "
                          f"Temp={reading['temperature']}°C, "
                          f"Humidity={reading['humidity']}%, "
                          f"Pressure={reading['pressure']}hPa, "
                          f"Motion={reading['motion']}, "
                          f"Anomaly={reading['anomaly']}")
                
                time.sleep(interval)
        
        except KeyboardInterrupt:
            print("\n\nStreaming stopped by user")
        
        self.running = False
        print(f"\n✓ Generated {count} readings from {len(self.devices)} devices")
        
        # Save to file if specified
        if output_file:
            self.save_data(output_file)
    
    def save_data(self, filename):
        """
        Save buffered data to file
        Website: https://rskworld.in
        Email: help@rskworld.in
        Phone: +91 93305 39277
        Author: RSK World
        """
        if not self.data_buffer:
            print("No data to save")
            return
        
        df = pd.DataFrame(self.data_buffer)
        
        if filename.endswith('.csv'):
            df.to_csv(filename, index=False)
            print(f"✓ Saved {len(df)} records to {filename}")
        elif filename.endswith('.json'):
            df.to_json(filename, orient='records', date_format='iso', indent=2)
            print(f"✓ Saved {len(df)} records to {filename}")
        else:
            print(f"Unsupported file format: {filename}")
    
    def generate_batch(self, num_readings=100, output_file=None):
        """
        Generate a batch of sensor readings
        Website: https://rskworld.in
        Email: help@rskworld.in
        Phone: +91 93305 39277
        Author: RSK World
        """
        print(f"\nGenerating {num_readings} sensor readings...")
        
        base_time = datetime.now()
        for i in range(num_readings):
            device = np.random.choice(self.devices)
            timestamp = base_time + timedelta(minutes=i)
            
            reading = self.generate_sensor_reading(device)
            reading['timestamp'] = timestamp.isoformat()
            self.data_buffer.append(reading)
        
        print(f"✓ Generated {len(self.data_buffer)} readings")
        
        if output_file:
            self.save_data(output_file)
        
        return pd.DataFrame(self.data_buffer)


def main():
    """
    Main function
    Website: https://rskworld.in
    Email: help@rskworld.in
    Phone: +91 93305 39277
    Author: RSK World
    """
    import sys
    
    simulator = RealTimeSimulator()
    
    if len(sys.argv) > 1:
        mode = sys.argv[1]
        
        if mode == 'stream':
            duration = int(sys.argv[2]) if len(sys.argv) > 2 else 60
            interval = float(sys.argv[3]) if len(sys.argv) > 3 else 1.0
            simulator.stream_data(duration=duration, interval=interval)
        elif mode == 'batch':
            num = int(sys.argv[2]) if len(sys.argv) > 2 else 100
            output = sys.argv[3] if len(sys.argv) > 3 else 'real_time_data.csv'
            simulator.generate_batch(num_readings=num, output_file=output)
        else:
            print("Usage: python real_time_simulator.py [stream|batch] [args...]")
    else:
        # Default: generate batch
        simulator.generate_batch(num_readings=50, output_file='real_time_data.csv')
        print("\n" + "="*60)
        print("REAL-TIME SIMULATOR")
        print("="*60)
        print("Website: https://rskworld.in")
        print("Email: help@rskworld.in")
        print("Phone: +91 93305 39277")
        print("="*60)


if __name__ == "__main__":
    main()

211 lines•6.8 KB
python
scripts/anomaly_detection.py
Raw Download
Find: Go to:
#!/usr/bin/env python3
"""
IoT Sensor Anomaly Detection Script
Website: https://rskworld.in
Email: help@rskworld.in
Phone: +91 93305 39277
Author: RSK World

This script implements anomaly detection algorithms for IoT sensor data:
- Statistical outlier detection (Z-score method)
- Isolation Forest
- Local Outlier Factor (LOF)
- Visualization of detected anomalies
"""

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.ensemble import IsolationForest
from sklearn.neighbors import LocalOutlierFactor
from sklearn.preprocessing import StandardScaler
import warnings
warnings.filterwarnings('ignore')

# Set style
sns.set_style("whitegrid")
plt.rcParams['figure.figsize'] = (14, 8)

class AnomalyDetector:
    """
    Anomaly Detection Class for IoT Sensor Data
    Website: https://rskworld.in
    Email: help@rskworld.in
    Phone: +91 93305 39277
    Author: RSK World
    """
    
    def __init__(self, data_path):
        """
        Initialize the anomaly detector
        Website: https://rskworld.in
        Email: help@rskworld.in
        Phone: +91 93305 39277
        Author: RSK World
        """
        self.data_path = data_path
        self.df = None
        self.load_data()
    
    def load_data(self):
        """
        Load sensor data
        Website: https://rskworld.in
        Email: help@rskworld.in
        Phone: +91 93305 39277
        Author: RSK World
        """
        if self.data_path.endswith('.csv'):
            self.df = pd.read_csv(self.data_path)
        else:
            import json
            with open(self.data_path, 'r') as f:
                data = json.load(f)
            self.df = pd.DataFrame(data)
        
        self.df['timestamp'] = pd.to_datetime(self.df['timestamp'])
        self.df.set_index('timestamp', inplace=True)
        print(f"✓ Loaded {len(self.df)} records")
    
    def zscore_detection(self, threshold=3):
        """
        Detect anomalies using Z-score method
        Website: https://rskworld.in
        Email: help@rskworld.in
        Phone: +91 93305 39277
        Author: RSK World
        """
        print("\n" + "="*60)
        print("Z-SCORE ANOMALY DETECTION")
        print("="*60)
        
        features = ['temperature', 'humidity', 'pressure']
        anomalies = pd.Series([0] * len(self.df), index=self.df.index)
        
        for feature in features:
            z_scores = np.abs((self.df[feature] - self.df[feature].mean()) / self.df[feature].std())
            anomalies = anomalies | (z_scores > threshold).astype(int)
        
        self.df['zscore_anomaly'] = anomalies
        detected = anomalies.sum()
        print(f"Detected {detected} anomalies ({detected/len(self.df)*100:.2f}%)")
        
        return anomalies
    
    def isolation_forest_detection(self, contamination=0.1):
        """
        Detect anomalies using Isolation Forest
        Website: https://rskworld.in
        Email: help@rskworld.in
        Phone: +91 93305 39277
        Author: RSK World
        """
        print("\n" + "="*60)
        print("ISOLATION FOREST ANOMALY DETECTION")
        print("="*60)
        
        # Prepare features
        features = ['temperature', 'humidity', 'pressure']
        X = self.df[features].values
        
        # Standardize features
        scaler = StandardScaler()
        X_scaled = scaler.fit_transform(X)
        
        # Fit Isolation Forest
        iso_forest = IsolationForest(contamination=contamination, random_state=42)
        predictions = iso_forest.fit_predict(X_scaled)
        
        # Convert predictions: -1 = anomaly, 1 = normal
        anomalies = (predictions == -1).astype(int)
        self.df['isolation_forest_anomaly'] = anomalies
        
        detected = anomalies.sum()
        print(f"Detected {detected} anomalies ({detected/len(self.df)*100:.2f}%)")
        
        return anomalies
    
    def lof_detection(self, n_neighbors=20, contamination=0.1):
        """
        Detect anomalies using Local Outlier Factor
        Website: https://rskworld.in
        Email: help@rskworld.in
        Phone: +91 93305 39277
        Author: RSK World
        """
        print("\n" + "="*60)
        print("LOCAL OUTLIER FACTOR (LOF) ANOMALY DETECTION")
        print("="*60)
        
        # Prepare features
        features = ['temperature', 'humidity', 'pressure']
        X = self.df[features].values
        
        # Standardize features
        scaler = StandardScaler()
        X_scaled = scaler.fit_transform(X)
        
        # Fit LOF
        lof = LocalOutlierFactor(n_neighbors=n_neighbors, contamination=contamination)
        predictions = lof.fit_predict(X_scaled)
        
        # Convert predictions: -1 = anomaly, 1 = normal
        anomalies = (predictions == -1).astype(int)
        self.df['lof_anomaly'] = anomalies
        
        detected = anomalies.sum()
        print(f"Detected {detected} anomalies ({detected/len(self.df)*100:.2f}%)")
        
        return anomalies
    
    def compare_methods(self):
        """
        Compare different anomaly detection methods
        Website: https://rskworld.in
        Email: help@rskworld.in
        Phone: +91 93305 39277
        Author: RSK World
        """
        print("\n" + "="*60)
        print("METHOD COMPARISON")
        print("="*60)
        
        methods = ['zscore_anomaly', 'isolation_forest_anomaly', 'lof_anomaly']
        if 'anomaly' in self.df.columns:
            methods.append('anomaly')
        
        comparison = pd.DataFrame()
        for method in methods:
            if method in self.df.columns:
                comparison[method] = self.df[method]
        
        print("\nAnomaly Detection Summary:")
        print(comparison.sum())
        
        # Agreement between methods
        if len(comparison.columns) >= 2:
            agreement = (comparison.sum(axis=1) >= 2).sum()
            print(f"\nAgreement (2+ methods): {agreement} anomalies")
    
    def visualize_anomalies(self):
        """
        Visualize detected anomalies
        Website: https://rskworld.in
        Email: help@rskworld.in
        Phone: +91 93305 39277
        Author: RSK World
        """
        fig, axes = plt.subplots(3, 1, figsize=(16, 12))
        
        # Temperature with anomalies
        axes[0].plot(self.df.index, self.df['temperature'], 
                    color='blue', alpha=0.6, linewidth=1.5, label='Temperature')
        
        # Mark different types of anomalies
        if 'zscore_anomaly' in self.df.columns:
            zscore_anoms = self.df[self.df['zscore_anomaly'] == 1]
            if len(zscore_anoms) > 0:
                axes[0].scatter(zscore_anoms.index, zscore_anoms['temperature'],
                               color='red', s=100, marker='x', 
                               label='Z-Score Anomalies', zorder=5)
        
        if 'isolation_forest_anomaly' in self.df.columns:
            iso_anoms = self.df[self.df['isolation_forest_anomaly'] == 1]
            if len(iso_anoms) > 0:
                axes[0].scatter(iso_anoms.index, iso_anoms['temperature'],
                               color='orange', s=80, marker='o', 
                               label='Isolation Forest', zorder=5, alpha=0.7)
        
        axes[0].set_title('Temperature with Detected Anomalies', 
                         fontsize=14, fontweight='bold')
        axes[0].set_ylabel('Temperature (°C)')
        axes[0].legend()
        axes[0].grid(True, alpha=0.3)
        
        # Humidity with anomalies
        axes[1].plot(self.df.index, self.df['humidity'], 
                    color='green', alpha=0.6, linewidth=1.5, label='Humidity')
        
        if 'zscore_anomaly' in self.df.columns:
            zscore_anoms = self.df[self.df['zscore_anomaly'] == 1]
            if len(zscore_anoms) > 0:
                axes[1].scatter(zscore_anoms.index, zscore_anoms['humidity'],
                               color='red', s=100, marker='x', 
                               label='Z-Score Anomalies', zorder=5)
        
        axes[1].set_title('Humidity with Detected Anomalies', 
                         fontsize=14, fontweight='bold')
        axes[1].set_ylabel('Humidity (%)')
        axes[1].legend()
        axes[1].grid(True, alpha=0.3)
        
        # Pressure with anomalies
        axes[2].plot(self.df.index, self.df['pressure'], 
                    color='purple', alpha=0.6, linewidth=1.5, label='Pressure')
        
        if 'zscore_anomaly' in self.df.columns:
            zscore_anoms = self.df[self.df['zscore_anomaly'] == 1]
            if len(zscore_anoms) > 0:
                axes[2].scatter(zscore_anoms.index, zscore_anoms['pressure'],
                               color='red', s=100, marker='x', 
                               label='Z-Score Anomalies', zorder=5)
        
        axes[2].set_title('Pressure with Detected Anomalies', 
                         fontsize=14, fontweight='bold')
        axes[2].set_xlabel('Timestamp')
        axes[2].set_ylabel('Pressure (hPa)')
        axes[2].legend()
        axes[2].grid(True, alpha=0.3)
        
        plt.tight_layout()
        plt.savefig('anomaly_detection_results.png', dpi=300, bbox_inches='tight')
        print("\n✓ Saved visualization: anomaly_detection_results.png")
        plt.close()
    
    def generate_report(self):
        """
        Generate comprehensive anomaly detection report
        Website: https://rskworld.in
        Email: help@rskworld.in
        Phone: +91 93305 39277
        Author: RSK World
        """
        print("\n" + "="*60)
        print("ANOMALY DETECTION REPORT")
        print("="*60)
        print("Website: https://rskworld.in")
        print("Email: help@rskworld.in")
        print("Phone: +91 93305 39277")
        print("="*60)
        
        # Run all detection methods
        self.zscore_detection()
        self.isolation_forest_detection()
        self.lof_detection()
        
        # Compare methods
        self.compare_methods()
        
        # Visualize
        self.visualize_anomalies()
        
        print("\n" + "="*60)
        print("ANOMALY DETECTION COMPLETE!")
        print("="*60)


def main():
    """
    Main function
    Website: https://rskworld.in
    Email: help@rskworld.in
    Phone: +91 93305 39277
    Author: RSK World
    """
    import os
    
    # Determine data file path
    script_dir = os.path.dirname(os.path.abspath(__file__))
    data_dir = os.path.join(os.path.dirname(script_dir), 'data')
    
    csv_path = os.path.join(data_dir, 'iot_sensors.csv')
    json_path = os.path.join(data_dir, 'iot_sensors.json')
    
    # Use CSV if available, otherwise JSON
    if os.path.exists(csv_path):
        data_path = csv_path
    elif os.path.exists(json_path):
        data_path = json_path
    else:
        print("Error: No data file found in data/ directory")
        return
    
    # Initialize detector and run analysis
    detector = AnomalyDetector(data_path)
    detector.generate_report()


if __name__ == "__main__":
    main()

332 lines•11.2 KB
python

About RSK World

Founded by Molla Samser, with Designer & Tester Rima Khatun, RSK World is your one-stop destination for free programming resources, source code, and development tools.

Founder: Molla Samser
Designer & Tester: Rima Khatun

Development

  • Game Development
  • Web Development
  • Mobile Development
  • AI Development
  • Development Tools

Legal

  • Terms & Conditions
  • Privacy Policy
  • Disclaimer

Contact Info

Nutanhat, Mongolkote
Purba Burdwan, West Bengal
India, 713147

+91 93305 39277

hello@rskworld.in
support@rskworld.in

© 2026 RSK World. All rights reserved.

Content used for educational purposes only. View Disclaimer