基于FastAPI+React+树莓派的产线数据系统连接故障求助
系统规格
- 处理器:13代i5
- 内存:16GB
- 固态硬盘:NVMe 500GB
- 操作系统:Windows Home
- 树莓派:Pi4 8GB(共16台)
系统架构与功能说明
Windows主机部署FastAPI后端与React前端,React通过FastAPI完成CRUD操作,用于存储工厂生产线的各班次日产量数据。
每条产线的树莓派负责两项核心工作:
- 对接设备,通过FastAPI接口向服务器上报产量数据
- 运行pywebview脚本加载React页面,在显示屏展示产量、目标产量、差额等信息
树莓派展示脚本代码
import webview import requests import threading import time # Define the URL to load on success success_url = 'http://172.17.17.110:3000/line/12' # Define the path to the error HTML file error_html_content = """ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Connection Error</title> <style> body { display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; background-color: #f8f8f8; font-family: Arial, sans-serif; } .error-message { font-size: 48px; color: #ff0000; text-align: center; } </style> </head> <body> <div class="error-message">Connection Error</div> </body> </html> """ # Define the health check API URL health_check_url = 'http://172.17.17.110:8000/healthz' # Time interval (in seconds) between health checks check_interval = 5 request_timeout = 5 def load_error_html(window): window.evaluate_js(f'document.documentElement.innerHTML = `{error_html_content}`') def health_check(window): first_success = False while True: print("FIRST SUCCESS", first_success) try: response = requests.get(health_check_url, timeout=request_timeout) print("RESPONSE", response) if response.status_code == 200: if not first_success: # Load the success URL in the webview window window.load_url(success_url) first_success = True else: if first_success: # If the health check fails, load the error HTML load_error_html(window) first_success = False except requests.RequestException: print("REQUEST EXCEPTION") if first_success: load_error_html(window) first_success = False except requests.Timeout: print("TIMEOUT EXCEPTION") if first_success: # If there is an exception, load the error HTML load_error_html(window) fisrt_success = False # Wait for the specified interval before checking again time.sleep(check_interval) def create_window(): # Create an initial window with the error HTML window = webview.create_window(title='IILMS', html=error_html_content, width=1920, height=1080, fullscreen=True, resizable=False, text_select=False) # Start the health check in a background thread threading.Thread(target=health_check, args=(window,), daemon=True).start() # Start the webview webview.start() if __name__ == '__main__': create_window()
开机自启动配置
- Windows主机:
- FastAPI:通过CMD执行
uvicorn main:app --host 0.0.0.0 --port 8000,开机自动启动 - React:通过CMD执行
npm start --host 0.0.0.0,开机自动启动
- FastAPI:通过CMD执行
- 树莓派:
- 上述pywebview展示脚本开机自动运行
- 后台运行另一脚本,监听GPIO引脚并向FastAPI上报数据
网络拓扑
16台树莓派通过网线接入交换机,交换机连接WiFi路由器,Windows服务器通过网线直接连接路由器;曾尝试将交换机直接连接服务器,问题未解决。
问题描述
- 有线连接方案下:当同时连接的树莓派超过7台时,树莓派的pywebview脚本会显示连接错误页面
- 原WiFi方案问题:树莓派通过2.4GHz WiFi中继器连接主路由器时,会出现自动断开且无法重连的情况,但手机、笔记本等设备可正常连接中继器
故障排查方向
1. Windows系统连接数限制排查
Windows Home版本默认存在TCP连接数限制,当并发连接数超过阈值时会拒绝新连接:
- 查看当前端口连接数:打开CMD执行
netstat -ano | findstr ":8000" | findstr "ESTABLISHED",统计连接数;同时检查:3000端口的连接数 - 修改系统连接数限制:通过注册表编辑器修改
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters下的MaxUserPort(建议设为65534)和TcpTimedWaitDelay(建议设为30),修改后重启系统
2. 服务并发处理能力排查
- FastAPI默认单进程单线程,并发请求过多会导致响应失败:修改启动命令为多进程模式
uvicorn main:app --host 0.0.0.0 --port 8000 --workers 4 - React开发服务器(
npm start)性能有限,生产环境建议构建静态文件后用Nginx等web服务器托管,替换当前启动方式
3. 网络设备瓶颈排查
- 确认交换机、路由器LAN口是否为千兆,百兆设备会因带宽饱和导致连接失败
- 检查路由器是否开启连接数限制,部分家用路由器默认限制并发连接数在10以内
- 逐步增加树莓派连接数量,观察故障触发节点,同时用
ping 172.17.17.110测试连通性,查看是否有丢包
4. 树莓派脚本问题修复
- 修正脚本拼写错误:
fisrt_success = False应为first_success = False,避免超时异常后状态未正确重置 - 增加日志详细度:在
health_check函数中记录错误类型(如ConnectionRefusedError、TimeoutError)和请求耗时,便于定位具体失败原因 - 调整超时时间:将
request_timeout从5秒延长至10秒,避免网络延迟导致误判
5. IP地址冲突排查
检查所有树莓派和服务器的IP地址,确保无重复IP,IP冲突会导致设备无法正常连接服务器
内容的提问来源于stack exchange,提问作者akdgamer
相关产品推荐
相关产品推荐

