Skip to content

mahdirajaee/SmartBolt_IOT_Platform

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

416 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SmartBolt IoT Platform

A production-grade, microservices-based IoT platform for real-time industrial pipeline monitoring with smart bolt sensors and automated valve control.

Python 3.11 License: MIT MQTT InfluxDB


Table of Contents


Overview

SmartBolt IoT Platform is designed for monitoring industrial pipelines using smart bolt sensors. The system provides:

  • Real-time Monitoring: Temperature and pressure readings from smart bolt sensors
  • Automated Control: 12 priority-ordered rules for automatic valve management
  • Anomaly Detection: 6 detection methods with predictive analytics
  • Multi-tenant Access: Role-based access control with sector isolation
  • Multi-interface: Web Dashboard and Telegram Bot for monitoring and control

Key Features

Feature Description
Microservices Architecture 10 independent, loosely-coupled services
Event-Driven Design MQTT-based real-time message routing
Time-Series Analytics InfluxDB v3 Cloud for sensor data storage
Predictive Maintenance ML-based anomaly detection and forecasting
Automated Response Rule-based valve control with emergency shutdown
Service Discovery Centralized catalog with health monitoring

Architecture

                                    ┌─────────────────────────────────────┐
                                    │       External Services             │
                                    │  InfluxDB Cloud │ Supabase │ Telegram│
                                    └─────────────────────────────────────┘
                                                     ▲
                                                     │
┌─────────────────────────────────────────────────────────────────────────────────┐
│                              PRESENTATION LAYER                                  │
│  ┌───────────────────────────────┐    ┌───────────────────────────────┐        │
│  │     Web Dashboard (5000)      │    │    Telegram Bot (8087)        │        │
│  │   Flask + Dash + Plotly       │    │  python-telegram-bot          │        │
│  │   8 Pages │ Real-time UI      │    │  14 Commands │ Alerts         │        │
│  └───────────────────────────────┘    └───────────────────────────────┘        │
└─────────────────────────────────────────────────────────────────────────────────┘
                                                     │
┌─────────────────────────────────────────────────────────────────────────────────┐
│                             INTELLIGENCE LAYER                                   │
│  ┌───────────────────────────────┐    ┌───────────────────────────────┐        │
│  │   Analytics Service (8083)    │    │   Control Center (8085)       │        │
│  │   scikit-learn │ pandas       │    │   12 Rules │ Decision Engine  │        │
│  │   Anomaly Detection │ Forecast│    │   Valve Automation            │        │
│  └───────────────────────────────┘    └───────────────────────────────┘        │
└─────────────────────────────────────────────────────────────────────────────────┘
                                                     │
┌─────────────────────────────────────────────────────────────────────────────────┐
│                               DATA LAYER                                         │
│  ┌───────────────────────────────┐    ┌───────────────────────────────┐        │
│  │  TimeSeries DB Connector      │    │   Resource Catalog (8081)     │        │
│  │  (8082) │ InfluxDB v3         │    │   Service Registry │ Config   │        │
│  │  100-point Buffer │ Batch     │    │   Health Monitoring (30s)     │        │
│  └───────────────────────────────┘    └───────────────────────────────┘        │
│                                                                                  │
│  ┌───────────────────────────────┐                                              │
│  │  Account Manager (8084)       │                                              │
│  │  JWT Auth │ bcrypt │ RBAC     │                                              │
│  │  Supabase PostgreSQL          │                                              │
│  └───────────────────────────────┘                                              │
└─────────────────────────────────────────────────────────────────────────────────┘
                                                     │
┌─────────────────────────────────────────────────────────────────────────────────┐
│                             MESSAGE LAYER                                        │
│  ┌───────────────────────────────────────────────────────────────────────────┐  │
│  │                    Message Broker │ Content Router                        │  │
│  │                   Fan-out Pattern │ Topic Filtering                       │  │
│  └───────────────────────────────────────────────────────────────────────────┘  │
│                                       │                                          │
│  ┌───────────────────────────────────────────────────────────────────────────┐  │
│  │                  MQTT Broker (Mosquitto) │ Port 1883                      │  │
│  │                      QoS 2 │ Exactly-Once Delivery                        │  │
│  └───────────────────────────────────────────────────────────────────────────┘  │
└─────────────────────────────────────────────────────────────────────────────────┘
                                                     │
┌─────────────────────────────────────────────────────────────────────────────────┐
│                               EDGE LAYER                                         │
│  ┌───────────────────────────────┐    ┌───────────────────────────────┐        │
│  │  Raspberry Pi North (8086)    │    │  Raspberry Pi South (8088)    │        │
│  │  Sector: sector-north         │    │  Sector: sector-south         │        │
│  │  Pipelines: N1, N2, N3        │    │  Pipelines: S1, S2, S3        │        │
│  │  Sensor Simulation │ Valve    │    │  Sensor Simulation │ Valve    │        │
│  └───────────────────────────────┘    └───────────────────────────────┘        │
└─────────────────────────────────────────────────────────────────────────────────┘

Data Flow

  1. Sensor Data: Raspberry Pi simulators generate temperature/pressure readings every 5 seconds
  2. Message Routing: Data published to MQTT, routed by Message Broker to subscribers
  3. Storage: TimeSeries DB Connector buffers and writes to InfluxDB Cloud
  4. Analytics: Analytics Service processes data for anomaly detection and forecasting
  5. Control: Control Center evaluates rules and issues valve commands
  6. Notification: Alerts sent via Telegram Bot and displayed on Web Dashboard

Microservices

Service Port Description Framework
Resource Catalog 8081 Service registry, discovery, configuration hub CherryPy
TimeSeries DB Connector 8082 MQTT→InfluxDB bridge with buffering CherryPy
Analytics Service 8083 Anomaly detection, risk assessment, forecasting CherryPy + scikit-learn
Account Manager 8084 JWT authentication, RBAC, user management CherryPy + Supabase
Control Center 8085 Rule engine, valve automation, emergency shutdown CherryPy
Raspberry Pi North 8086 Sensor simulator for sector-north (N1, N2, N3) CherryPy
Telegram Bot 8087 Alert notifications, 14 commands, user auth python-telegram-bot
Raspberry Pi South 8088 Sensor simulator for sector-south (S1, S2, S3) CherryPy
Web Dashboard 5000 8-page monitoring UI with real-time visualization Flask + Dash
Message Broker - MQTT content router with fan-out pattern paho-mqtt

Service Startup Order

Phase 1: Foundation    → Resource Catalog (8081)
Phase 2: Message       → MQTT Broker, Message Broker
Phase 3: Data          → TimeSeries DB (8082), Account Manager (8084)
Phase 4: Edge          → Raspberry Pi North (8086), South (8088)
Phase 5: Intelligence  → Analytics (8083), Control Center (8085)
Phase 6: Interfaces    → Web Dashboard (5000), Telegram Bot (8087)

Technology Stack

Backend

Technology Version Purpose
Python 3.11 Primary language
CherryPy 18.8.0 REST API framework
paho-mqtt 2.2.1 MQTT client library
Flask <2.3.0 Web Dashboard backend
Dash 2.14.0 Interactive UI components

Data & Storage

Technology Version Purpose
InfluxDB v3 Cloud Time-series database
Supabase PostgreSQL Relational database
influxdb3-python 0.16.0 InfluxDB client

Analytics & ML

Technology Version Purpose
pandas 2.0.3 Data processing
scikit-learn 1.3.0 Anomaly detection
numpy 1.24.4 Numerical computing
Plotly 5.18.0 Data visualization

Security

Technology Version Purpose
PyJWT 2.8.0 JWT token handling
bcrypt 4.1.2 Password hashing
python-dotenv 1.0.0 Environment management

Infrastructure

Technology Purpose
Eclipse Mosquitto MQTT broker
tmux Multi-service orchestration
Make Build automation

Project Structure

SmartBolt_IOT_Platform/
├── MS_catalog/                      # Resource Catalog Service
│   ├── main.py                      # REST API endpoints (648 lines)
│   ├── device_manager.py            # Device CRUD operations
│   ├── service_registry.py          # Service registration
│   ├── config_manager.py            # Configuration sync
│   ├── catalog.json                 # Data persistence
│   └── requirements.txt
│
├── MS_AccountManager/               # Authentication Service
│   ├── main.py                      # Auth endpoints (468 lines)
│   ├── db_manager.py                # Database operations
│   ├── auth_utils.py                # JWT/bcrypt utilities
│   ├── models.py                    # User/Session models
│   └── requirements.txt
│
├── MS_TimeSeriesDBConnector/        # Time-Series Storage
│   ├── main.py                      # MQTT subscriber & API
│   ├── storage.py                   # InfluxDB operations (42K)
│   ├── api.py                       # REST query endpoints
│   └── requirements.txt
│
├── MS_Analytics/                    # Analytics Engine
│   ├── main.py                      # Analysis orchestration
│   ├── alert.py                     # Alert generation
│   ├── prediction.py                # ML forecasting
│   └── requirements.txt
│
├── MS_ControlCenter/                # Control System
│   ├── main.py                      # Control loop
│   ├── control_logic.py             # Decision engine
│   ├── rule_engine.py               # 12 control rules
│   ├── mqtt_handler.py              # Command publishing
│   └── requirements.txt
│
├── MS_RaspberryPiConnector/         # Edge Simulators
│   ├── main.py                      # HTTP server
│   ├── api.py                       # REST endpoints
│   ├── sensor_simulator.py          # Physics simulation
│   ├── valve_control.py             # Valve actuation
│   ├── pipeline_data.json           # Pipeline config
│   └── requirements.txt
│
├── MS_TelegramBot/                  # Telegram Integration
│   ├── main.py                      # Service setup
│   ├── telegram_bot.py              # 14 bot commands
│   ├── catalog_client.py            # Service discovery
│   └── requirements.txt
│
├── MS_WebAashboard/                 # Web Interface
│   ├── app.py                       # Flask/Dash main app
│   ├── components/
│   │   ├── auth.py                  # Authentication UI
│   │   ├── layouts.py               # Common layouts
│   │   └── service_client.py        # API client
│   ├── pages/
│   │   ├── landing.py               # Public landing page
│   │   ├── overview.py              # System dashboard
│   │   ├── pipelines.py             # Pipeline monitoring
│   │   ├── alerts.py                # Alert history
│   │   ├── analytics.py             # Analytics dashboard
│   │   ├── control.py               # Control panel
│   │   ├── users.py                 # User management
│   │   └── pipeline_management.py   # Pipeline CRUD
│   └── requirements.txt
│
├── MessageBroker/                   # MQTT Router
│   ├── main.py                      # Content router
│   ├── client.py                    # MQTT client
│   └── requirements.txt
│
├── util/                            # Shared utilities
├── Makefile                         # Service orchestration
├── requirements.txt                 # Global dependencies
├── devices.json                     # Device registry
├── port_manager.py                  # Port management utility
└── SYSTEM_ARCHITECTURE.md           # Detailed documentation

Getting Started

Prerequisites

  • Python 3.11+
  • Eclipse Mosquitto MQTT Broker
  • tmux (for multi-service management)
  • InfluxDB v3 Cloud account
  • Supabase account

Installation

  1. Clone the repository

    git clone https://github.com/your-org/SmartBolt_IOT_Platform.git
    cd SmartBolt_IOT_Platform
  2. Create and activate virtual environment

    python -m venv venv
    source venv/bin/activate  # Linux/macOS
    # or
    .\venv\Scripts\activate   # Windows
  3. Install dependencies

    make setup
    # or manually:
    pip install -r requirements.txt
  4. Start MQTT broker

    mosquitto -c /usr/local/etc/mosquitto/mosquitto.conf
  5. Start all services

    make run-all

Quick Start (Individual Services)

# Start services individually
make run-catalog      # Resource Catalog (8081)
make run-broker       # Message Broker
make run-timeseries   # TimeSeries DB (8082)
make run-analytics    # Analytics (8083)
make run-account      # Account Manager (8084)
make run-control      # Control Center (8085)
make run-raspberry-north  # Pi North (8086)
make run-raspberry-south  # Pi South (8088)
make run-telegram     # Telegram Bot (8087)
make run-dashboard    # Web Dashboard (5000)

Configuration

Global Thresholds

# Temperature (Celsius)
TEMP_MIN_NORMAL = 20.0
TEMP_MAX_NORMAL = 40.0
TEMP_ALERT = 45.0
TEMP_CRITICAL = 60.0

# Pressure (PSI)
PRESSURE_MIN_NORMAL = 90.0
PRESSURE_MAX_NORMAL = 110.0
PRESSURE_ALERT = 120.0
PRESSURE_CRITICAL = 150.0

Control Rules (Priority Order)

Priority Rule Condition Action
100 Critical Risk risk_score >= 90 EMERGENCY_SHUTDOWN
95 High Pressure Relief pressure > 115 PSI OPEN_VALVE
90 High Temperature temperature > 48°C CLOSE_VALVE
88 Leak Detected leak_probability > 0.8 CLOSE_VALVE
85 Sensor Failure sensor_status == "failed" CLOSE_VALVE
80 Prediction Breach predicted_temp > 45°C ALERT_OPERATOR
75 Multiple Anomalies anomaly_count >= 3 ALERT_OPERATOR
70 Combined High temp > 42°C AND pressure > 105 CLOSE_VALVE
60 Rapid Change temp_delta > 10°C/min ALERT_OPERATOR
50 Low Health Score health_score < 30 CLOSE_VALVE
40 Moderate Risk 50 <= risk_score < 70 ALERT_OPERATOR
30 System Recovery all_normal AND health > 80 OPEN_VALVE

API Reference

Resource Catalog (8081)

Method Endpoint Description
GET /catalog Full catalog snapshot
GET /services List registered services
GET /services/health All services health status
POST /services Register new service
GET /devices/pipelines List all pipelines
GET /devices/bolts List all bolts
GET /devices/valves List all valves
GET /config/thresholds Alert thresholds
GET /config/rules Control rules

Account Manager (8084)

Method Endpoint Description
POST /login Authenticate user
POST /logout Invalidate session
GET /validate Validate JWT token
POST /register Register new user (admin)
GET /users List all users (admin)
PUT /users/{id} Update user
DELETE /users/{id} Delete user (admin)

TimeSeries DB Connector (8082)

Method Endpoint Description
GET /all_sensor_data All sensor data
GET /sensor_data Specific sensor data
GET /valve_states Valve state history
GET /devices Registered devices

Query Parameters:

  • hours: Last N hours of data
  • start / end: ISO datetime range
  • device_id: Filter by device
  • sensor_type: Filter by type (temperature, pressure)

Control Center (8085)

Method Endpoint Description
GET /status Control center status
GET /rules List control rules
POST /manual Manual valve command
POST /rules Add new rule
DELETE /rules/{id} Remove rule

Analytics (8083)

Method Endpoint Description
GET /health Service health
GET /forecast Latest forecast data
POST /run_analysis Trigger analysis

MQTT Topics

Topic Hierarchy

sectors/
├── sector-north/pipelines/{N1,N2,N3}/
│   ├── measurements          # Sensor data from Pi
│   ├── commands/valves       # Valve commands to Pi
│   └── alerts/{type}         # Alert notifications
└── sector-south/pipelines/{S1,S2,S3}/
    └── ...

sensors/{pipeline_id}         # Routed sensor data
temperature/{pipeline_id}     # Temperature readings
pressure/{pipeline_id}        # Pressure readings
valves/{pipeline_id}          # Valve status
alerts/anomalies/{pipeline_id} # Anomaly alerts
commands/valves/{pipeline_id} # Control commands

Message Formats

Sensor Measurement:

{
  "timestamp": "2024-12-16T20:05:30.123Z",
  "sector_id": "sector-north",
  "pipeline_id": "N1",
  "bolts": [{
    "bolt_id": "bolt_n1",
    "temperature": 25.3,
    "pressure": 1013.25,
    "health_status": "good"
  }],
  "valve_state": "open",
  "anomaly_detected": false
}

Valve Command:

{
  "pipeline_id": "N1",
  "action": "close",
  "reason": "high_temperature_hazard",
  "timestamp": "2024-12-16T20:05:45Z",
  "source": "control_center",
  "priority": 90
}

Database Schema

Supabase PostgreSQL

-- Users table
CREATE TABLE iot_users (
    id SERIAL PRIMARY KEY,
    username VARCHAR(255) UNIQUE NOT NULL,
    email VARCHAR(255) UNIQUE NOT NULL,
    password_hash VARCHAR(255) NOT NULL,
    role VARCHAR(50) NOT NULL,  -- 'admin', 'operator', 'viewer'
    sector_id VARCHAR(100),
    is_active BOOLEAN DEFAULT true,
    telegram_chat_id BIGINT,
    created_at TIMESTAMP DEFAULT now()
);

-- Sessions table
CREATE TABLE iot_sessions (
    id SERIAL PRIMARY KEY,
    user_id INT REFERENCES iot_users(id),
    token VARCHAR(500) UNIQUE NOT NULL,
    expires_at TIMESTAMP NOT NULL,
    is_active BOOLEAN DEFAULT true
);

-- User-Pipeline assignments (many-to-many)
CREATE TABLE iot_user_pipelines (
    id SERIAL PRIMARY KEY,
    user_id INT REFERENCES iot_users(id),
    pipeline_id VARCHAR(100),
    sector_id VARCHAR(100)
);

InfluxDB v3 Cloud

Buckets: smartboltbucket-north, smartboltbucket-south

Measurements:

Measurement Tags Fields
temperature pipeline_id, bolt_id, sector_id value (float)
pressure pipeline_id, bolt_id, sector_id value (float)
valve_status pipeline_id, valve_id, sector_id state (string)
anomalies pipeline_id, bolt_id, severity description, temperature, pressure

Security

Authentication

  • JWT Tokens: HS256 algorithm, 24-hour expiry
  • Password Hashing: bcrypt with 12 rounds
  • Session Management: Database-backed with hourly cleanup

Authorization (RBAC)

Role Permissions
Admin Full access: view, control, configure, manage users/pipelines
Operator View, manual control, emergency shutdown
Viewer Read-only access to dashboards and data

Data Isolation

  • Sector-based filtering: Users only see assigned sector data
  • API queries automatically filtered by user's sector assignments

Security Recommendations

  • Change default admin password immediately
  • Enable HTTPS in production
  • Configure MQTT authentication
  • Restrict CORS origins
  • Implement API rate limiting
  • Use environment variables for all secrets

Makefile Commands

make setup          # Create venv and install dependencies
make run-all        # Start all services in tmux
make stop-all       # Stop all services
make run-catalog    # Start Resource Catalog
make run-broker     # Start Message Broker
make run-timeseries # Start TimeSeries DB
make run-analytics  # Start Analytics
make run-account    # Start Account Manager
make run-control    # Start Control Center
make run-raspberry-north  # Start Pi North simulator
make run-raspberry-south  # Start Pi South simulator
make run-telegram   # Start Telegram Bot
make run-dashboard  # Start Web Dashboard
make test           # Run tests
make lint           # Run linter
make clean          # Clean temporary files

Pipeline Topology

Sector North (sector-north)
├── Pipeline N1
│   ├── Bolt: bolt_n1 (temperature + pressure sensors)
│   └── Valve: valve_n1 (solenoid, normally closed)
├── Pipeline N2
│   ├── Bolt: bolt_n2
│   └── Valve: valve_n2
└── Pipeline N3
    ├── Bolt: bolt_n3
    └── Valve: valve_n3

Sector South (sector-south)
├── Pipeline S1
│   ├── Bolt: bolt_s1
│   └── Valve: valve_s1
├── Pipeline S2
│   ├── Bolt: bolt_s2
│   └── Valve: valve_s2
└── Pipeline S3
    ├── Bolt: bolt_s3
    └── Valve: valve_s3

Web Dashboard Pages

Page Route Access Description
Landing / Public Platform introduction
Login /login Public Authentication
Overview /overview All roles System dashboard with metrics
Pipelines /pipelines All roles Pipeline monitoring
Alerts /alerts All roles Alert history and details
Analytics /analytics All roles Charts and predictions
Control /control Operator+ Manual valve control
Users /users Admin User management
Pipeline Management /pipeline-management Admin Pipeline CRUD

Telegram Bot Commands

Command Description
/start Initialize bot interaction
/help Display available commands
/login Authenticate with credentials
/logout End session
/temperature Get current temperature readings
/pressure Get current pressure readings
/actuator Control valves (open/close)
/status System status overview

Monitoring & Health Checks

Service Health Endpoint

All services expose /health endpoint returning:

{
  "status": "healthy",
  "service": "resource_catalog",
  "timestamp": "2024-12-16T20:00:00Z",
  "uptime": 3600
}

Catalog Health Monitoring

  • Interval: Every 30 seconds
  • Timeout: 5 seconds per service
  • States: unknown, healthy, unhealthy, unreachable

Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit changes (git commit -m 'Add amazing feature')
  4. Push to branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Development Guidelines

  • Follow PEP 8 style guide
  • Write unit tests for new features
  • Update documentation for API changes
  • Use meaningful commit messages

License

This project is licensed under the MIT License - see the LICENSE file for details.


Acknowledgments

  • Eclipse Mosquitto for MQTT broker
  • InfluxData for time-series database
  • Supabase for PostgreSQL hosting
  • Plotly/Dash for visualization components

SmartBolt IoT Platform
Industrial Pipeline Monitoring System

About

SmartBolt_IOT_Platform is a modular IoT system designed for real-time monitoring and control of industrial pipelines using Smart IoT Bolts. The platform integrates sensor data acquisition, real-time analytics, actuator control, and web-based monitoring to enhance predictive maintenance and operational efficiency.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors