PLC桥接MCP服务器

工业自动化桥接服务,通过模型上下文协议(MCP)连接Beckhoff TwinCAT PLC和AI助手,提供实时数据读取、交互式Web仪表板和AI驱动的仪表板生成功能。

  • by hugo-palafox
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 hugo-palafox/PLC_MCP

使用说明

PLC-Bridge MCP Server

Industrial automation bridge connecting Beckhoff TwinCAT PLCs to AI assistants via the Model Context Protocol (MCP).

Features

  • Live PLC Integration: Read real-time data from Beckhoff TwinCAT PLCs via ADS/AMS
  • Interactive Web Dashboard: Modern web interface with conversational AI for querying PLC data
  • AI-Powered Dashboard Generator: Uses DeepSeek LLM to intelligently create Grafana dashboards
  • Local AI Chat: Interact with PLC data using natural language via DeepSeek (offline)
  • Autonomous Dashboards: Generate Grafana dashboards programmatically using grafanalib
  • Optimized Performance: Batch reads and symbol caching for large projects
  • Mock Mode: Test without hardware using simulated data

Quick Start

1. Install Dependencies

pip install -r requirements.txt

2. Run the Web Dashboard (Recommended)

.venv\Scripts\python.exe web_dashboard_server.py

Then open http://localhost:5000 in your browser.

Features:

  • Ask questions: "Show me all metrics", "What is the OEE?"
  • Live dashboard updates every 5 seconds
  • Modern dark theme with glassmorphism effects

3. Alternative: Run the AI Chat Host

.venv\Scripts\python.exe ollama_host.py

Default: LIVE mode. Configure the PLC address for your own lab environment; public examples should not contain production or customer network details.

To use MOCK mode instead, edit ollama_host.py and change:

server_env["PLC_MODE"] = "LIVE"  # Change to "MOCK"

Project Structure

Core/
├── web_dashboard_server.py  # Web dashboard server
├── static/                   # Web dashboard frontend
│   ├── index.html           # Main page
│   ├── styles.css           # Styling
│   └── app.js               # JavaScript logic
├── server.py                # MCP server with PLC tools
├── ollama_host.py           # Local AI chat interface
├── ai_dashboard_generator.py # AI-powered dashboard generator
├── dashboard_generator.py   # Template-based dashboard creation
├── drivers/
│   ├── beckhoff.py         # TwinCAT ADS driver
│   └── mock.py             # Simulated PLC driver
├── plc_settings/
│   └── Testing_Machine.json # Machine configuration
├── tests/
│   └── test_plc_connection.py  # Connection verification
└── dashboards/              # Generated dashboard JSONs

Available Interfaces

1. Web Dashboard

Modern web interface with conversational AI:

  • URL: http://localhost:5000
  • Features: Live metrics, chat interface, auto-refresh
  • Best for: Monitoring and visualization

2. AI Chat Host

Terminal-based chat interface:

  • Command: .venv\Scripts\python.exe ollama_host.py
  • Features: Natural language queries, tool calling
  • Best for: Advanced queries and automation

3. MCP Tools

When using the AI host, you can:

  • List machines: "Show me the machines"
  • List tags: "What tags are available?"
  • Read values: "Read all tag values" or "What is the OEE?"
  • Generate dashboards: "Create a dashboard for Testing_Machine"

Configuration

Edit plc_settings/Testing_Machine.json to configure:

  • Machine name and type
  • IP address
  • Tag definitions with types and descriptions

Documentation

Requirements

  • Python 3.12+
  • Ollama with DeepSeek model (for local AI)
  • TwinCAT ADS (for live PLC connection)
  • Grafanalib (for dashboard generation)
  • Flask, Flask-SocketIO (for web dashboard)

Web Dashboard Usage

  1. Start the server:

    .venv\Scripts\python.exe web_dashboard_server.py
    
  2. Open browser: Navigate to http://localhost:5000

  3. Ask questions:

    • "Show me all metrics" - Displays all PLC tags
    • "What is the OEE?" - Shows OEE metric
    • "Show production data" - Displays production metrics
  4. Watch live updates: Dashboard auto-refreshes every 5 seconds

Technology Stack

  • Backend: Flask + Flask-SocketIO for real-time communication
  • Frontend: HTML5 + CSS3 + Vanilla JavaScript
  • PLC Communication: pyads for TwinCAT ADS protocol
  • AI: DeepSeek via Ollama for intelligent analysis
  • Dashboard Generation: Grafanalib for programmatic dashboard creation