MCP Arduino继电器控制器
使用本地LLM(LLMA 3.2)通过模型上下文协议(MCP)控制Arduino继电器的Python应用程序。
🏗️ 建筑
┌─────────────────┐ HTTP/MCP ┌─────────────────┐ UDP ┌─────────────┐
│ Client Llama │ ←─────────────────→ │ MCP Server │ ←──────────────→ │ Arduino │
│ (Llama 3.2) │ Port 8000 │ (FastMCP) │ Port 8888 │ (8 relays) │
└─────────────────┘ └─────────────────┘ └─────────────┘📋 先决条件
- Python 3.10+
- 带CUDA的NVIDIA GPU(可选,但推荐)
- 为UDP配置8个继电器的Arduino
- 最低4-8GB RAM
🚀 安装
1.克隆项目
git clone
cd mcp-arduino-relay2.创建虚拟环境
python -m venv venv
source venv/bin/activate # Linux/Mac
# ou
venv\Scripts\activate # Windows3.安装依赖项
pip install -r requirements.txt4.配置
编辑 config.py 要配置:
# Arduino
ArduinoConfig.ip = "192.168.12.222" # IP de votre Arduino
ArduinoConfig.port = 8888 # Port UDP de l'Arduino
# Logging
LoggingConfig.log_level = "INFO" # DEBUG, INFO, WARNING, ERROR
LoggingConfig.log_file = "./logs/arduino_mcp.log"📝 使用
显示配置
python main.py configArduino连接测试仪
python main.py test-arduino --arduino-ip 192.168.12.222 --arduino-port 8888可用的测试命令:
STATUS,0,0-所有继电器的状态SET,1,0-打开继电器1RESET,1,0-关闭继电器1SET,2,5000-打开继电器2 5秒
启动MCP服务器
在终端中:
python main.py server服务器启动于 http://0.0.0.0:8000
启动交互式客户端
在另一终端中:
python main.py client可用选项:
# Modèle personnalisé
python main.py client --model meta-llama/Llama-3.2-1B-Instruct
# Device spécifique
python main.py client --device cuda
# Niveau de log
python main.py client --log-level DEBUG💬 使用示例
启动客户端后,您可以使用自然语言进行交互:
👤 You: Turn on relay 3
🤖 Assistant: Success: Relay 3 turned ON
👤 You: Turn on relay 5 for 10 seconds
🤖 Assistant: Success: Relay 5 turned ON (will auto-reset after 10000ms)
👤 You: Turn off relay 3
🤖 Assistant: Success: Relay 3 turned OFF
👤 You: What's the status of relay 1?
🤖 Assistant: Relay status: Relay 1 is OFF🛠️ 项目结构
mcp-arduino-relay/
├── main.py # Point d'entrée principal
├── server.py # Serveur MCP (FastMCP)
├── client.py # Client MCP avec Llama
├── arduino_relays.py # Communication UDP avec Arduino
├── config.py # Configuration centralisée
├── requirements.txt # Dépendances Python
├── README.md # Cette documentation
└── logs/ # Fichiers de logs (créé automatiquement)🔧 MCP工具可用
command_relays
控制特定继电器。
设置 :
command:“设置”或“重置”relay_index:继电器编号(1-8)auto_reset_delay:以毫秒为单位的自熄延迟(0-10000)
示例:
{
"tool": "command_relays",
"arguments": {
"command": "SET",
"relay_index": 3,
"auto_reset_delay": 5000
}
}get_relay_status
获取一个或多个继电器的状态。
设置 :
relay_index:继电器编号(1-8)或所有继电器编号为0
示例:
{
"tool": "get_relay_status",
"arguments": {
"relay_index": 0
}
}🐛 故障排除
模型不加载
# Vérifier CUDA
python -c "import torch; print(torch.cuda.is_available())"
# Utiliser CPU si pas de GPU
python main.py client --device cpuArduino连接错误
- 检查Arduino是否可访问:
ping 192.168.12.222- 连接测试仪:
python main.py test-arduino --arduino-ip 192.168.12.222- 检查日志:
tail -f logs/arduino_mcp.log客户找不到工具
- 检查服务器是否已启动
- 检查日志中的连接URL
- 手动测试:
curl http://localhost:8000/mcp
📊 日志
日志存储在 ./logs/arduino_mcp.log 自动旋转(最大10 MB,5个文件)。
日志级别:
DEBUG:每次操作的详细信息INFO:重要操作WARNING:警告ERROR:错误
🔒 安全
⚠️ 重要 :此应用程序控制物理继电器。确保:
- 首先使用非临界负载进行测试
- 在Arduino上实现安全机制
- 限制对MCP服务器的网络访问
- 在执行之前验证所有订单
📚 资源
🤝 贡献
欢迎捐款!不要犹豫:
- 分叉项目
- 创建分支(
git checkout -b feature/amelioration) - 提交您的更改(
git commit -am 'Ajout fonctionnalité') - 推到
