# EquipServer_ubuntuserver_python **Repository Path**: Equip_Workbench/equip-server_ubuntuserver_python ## Basic Information - **Project Name**: EquipServer_ubuntuserver_python - **Description**: Raspberry Pi4b(2G) + Ubuntu Server 20.04.5 LTS - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2025-09-23 - **Last Updated**: 2025-09-26 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Raspberry Pi PLC to MQTT Service 这是一个在树莓派上运行的PLC数据采集并通过MQTT协议发布的服务程序。 ## 功能特性 1. **自动启动**: 系统启动后自动运行服务 2. **手动启动**: 支持手动启动服务程序 3. **日志功能**: 完整的日志记录和轮转机制 4. **数据缓存**: 本地缓存PLC数据,支持历史数据查询 5. **PLC数据采集**: - 支持Modbus TCP/RTU协议 - 支持OMRON FINS协议 - 通过JSON配置文件定义采集点 6. **MQTT数据发布**: 定时将数据发布到MQTT代理 ## 系统要求 - 树莓派4B (2G内存) - Ubuntu Server 22.04 LTS - Python 3.8+ ## 项目结构 ``` raspberry_plc_mqtt/ ├── config/ │ ├── plc_config.json # PLC配置文件 │ ├── mqtt_config.json # MQTT配置文件 │ └── logging_config.json # 日志配置文件 ├── src/ │ ├── main.py # 主程序入口 │ ├── plc_client/ # PLC客户端模块 │ ├── cache/ # 数据缓存模块 │ ├── mqtt_client/ # MQTT客户端模块 │ └── utils/ # 工具模块 ├── systemd/ │ └── plc_mqtt_service.service # systemd服务配置 ├── requirements.txt # Python依赖 └── README.md # 项目说明文档 ``` ## 安装和配置 ### 1. 安装依赖 ```bash sudo apt update sudo apt install python3-pip pip3 install -r requirements.txt ``` ### 2. 配置文件 #### PLC配置 (config/plc_config.json) 配置PLC连接参数和数据点: ```json { "connection": { "protocol": "modbus", // 协议类型: modbus 或 fins "server_ip": "192.168.1.10", // PLC IP地址 "server_port": 502, // 端口号 "slave_id": 1, // Modbus从机ID "poll_interval": 1000 // 采集间隔(毫秒) }, "list": [ // 数据点定义 { "type": "uint16_t", // 数据类型 "addr": [40001], // PLC地址 "name_cn": "产量", // 中文名称 "name_en": "throughput", // 英文名称 "len": "1", // 数据长度 "reg": "03", // 寄存器类型 "comment": "产量统计" // 描述 } ] } ``` #### MQTT配置 (config/mqtt_config.json) 配置MQTT代理连接参数: ```json { "broker": { "host": "192.168.1.100", // MQTT代理地址 "port": 1883, // 端口号 "client_id": "plc_client", // 客户端ID "username": "user", // 用户名 "password": "password" // 密码 }, "publishing": { "topic_prefix": "plc/data/", // 主题前缀 "publish_interval": 5000 // 发布间隔(毫秒) } } ``` ### 3. 设置自动启动 将服务配置文件复制到systemd目录: ```bash sudo cp systemd/plc_mqtt_service.service /etc/systemd/system/ sudo systemctl daemon-reload sudo systemctl enable plc_mqtt_service.service ``` 启动服务: ```bash sudo systemctl start plc_mqtt_service.service ``` 查看服务状态: ```bash sudo systemctl status plc_mqtt_service.service ``` ## 手动运行 ```bash python3 src/main.py ``` ## 开发环境配置 (Windows 7 + VS Code) 1. 安装VS Code 2. 安装Python 3.8+ 3. 安装Remote-SSH扩展 4. 配置SSH连接到树莓派 5. 在VS Code中打开远程文件夹进行开发 ## 日志查看 ```bash # 查看实时日志 tail -f logs/app.log # systemd日志 sudo journalctl -u plc_mqtt_service.service -f ``` # 第一版: ## 项目概述 项目名称: - Raspberry Pi PLC to MQTT Service 功能: - 系统启动后自动启动服务程序,支持手动启动 - 完整的日志记录功能 - 数据缓存功能,支持历史数据查询 - PLC数据采集功能,支持Modbus和OMRON FINS协议 - MQTT数据发布功能 ## 项目结构 ```latex raspberry_plc_mqtt/ ├── config/ │ ├── plc_config.json # PLC配置文件 │ ├── mqtt_config.json # MQTT配置文件 │ └── logging_config.json # 日志配置文件 ├── src/ │ ├── main.py # 主程序入口 │ ├── plc_client/ # PLC客户端模块 │ ├── cache/ # 数据缓存模块 │ ├── mqtt_client/ # MQTT客户端模块 │ └── utils/ # 工具模块 ├── systemd/ │ └── plc_mqtt_service.service # systemd服务配置 ├── requirements.txt # Python依赖 └── README.md # 项目说明文档 ``` ## 技术实现 ### PLC客户端模块 - 支持Modbus和OMRON FINS两种协议 - 通过配置文件定义数据点(中文名、英文名、地址、数据类型等) - 基于抽象基类设计,易于扩展其他协议 ### 数据缓存模块 - 实时缓存最新数据 - 历史数据存储和查询 - 线程安全访问 ### MQTT发布模块 - 定时发布数据到MQTT代理 - 支持配置主题前缀、发布间隔等参数 ### 日志模块 - 支持控制台和文件双重输出 - 日志轮转机制,避免占用过多磁盘空间 ## 开发环境配置 ### 在Windows 7 + VS Code上开发 - 安装VS Code - 安装Remote-SSH扩展 - 配置SSH连接到树莓派 - 在VS Code中打开远程文件夹进行开发调试 ### 在树莓派上部署 - 安装Ubuntu Server 22.04 LTS - 安装Python依赖:pip3 install -r requirements.txt - 配置config目录下的JSON文件 - 设置systemd服务实现自动启动 ## 编译、调试、运行 ### 手动运行 ```shell python3 src/main.py ``` ### 设置自动启动 ```shell sudo cp systemd/plc_mqtt_service.service /etc/systemd/system/ sudo systemctl daemon-reload sudo systemctl enable plc_mqtt_service.service sudo systemctl start plc_mqtt_service.service ``` ## 配置文件说明 ### PLC配置 (plc_config.json) 定义PLC连接参数和需要采集的数据点信息,包括数据类型、地址、中英文名称等。 ### MQTT配置 (mqtt_config.json) 定义MQTT代理地址、端口、认证信息以及数据发布参数。 ### 日志配置 (logging_config.json) 定义日志输出格式、级别和轮转策略。 这个项目具有良好的模块化设计,易于维护和扩展。同时支持多种工业通信协议,满足您的PLC数据采集需求,并通过MQTT协议将数据传输到远程服务器。 # 第二版: ## 编译、调试、运行 - vscode 编写、修改代码 打开终端输入指令,将本地代码复制到树莓派的ubuntu server中: ```shell scp -r d:\DataServer\EquipServer_UbuntuServer\* server@192.168.45.41:/home/server/EquipServer/ ``` 输入远端密码:123 - raspberry pi4b 进入代码目录 ```shell cd /home/server/EquipServer ``` 运行代码: ```shell python3 src/main.py ``` 的