Files
rtu_v5/.planning/research/STACK.md
2026-03-12 06:04:19 +08:00

158 lines
7.9 KiB
Markdown

# Stack Research
**Domain:** Web-based RTU Interface for Raspberry Pi Embedded Sensor Monitoring
**Researched:** 2026-03-12
**Confidence:** HIGH
## Recommended Stack
### Core Technologies
| Technology | Version | Purpose | Why Recommended |
|------------|---------|---------|-----------------|
| **Flask** | 3.1.0 | Lightweight Python web framework | The de facto standard for Raspberry Pi web projects. Extensive ecosystem, minimal overhead, perfect for Pi Zero 2 W. Multiple verified projects use Flask for sensor dashboards. |
| **Flask-SocketIO** | 5.6.0 | Real-time bidirectional communication | Proven combination with Flask for live sensor data. Latest version (5.6.0) released Dec 2025, compatible with Python 3.8+. |
| **Chart.js** | 4.4.x | JavaScript charting library | Industry standard for dashboard visualizations. Lightweight (~60KB), canvas-based rendering performs well on embedded displays. Extensive documentation and examples for Raspberry Pi projects. |
| **Bootstrap** | 5.3.x | CSS framework for responsive UI | Works exceptionally well for touchscreen interfaces. Built-in support for 1024x600 resolution layouts. Grid system simplifies the menu-driven RTU interface. |
| **Python** | 3.11+ | Backend runtime | Raspberry Pi OS (Bookworm) ships with Python 3.11. Excellent sensor library ecosystem (GPIO, I2C, serial). |
### Supporting Libraries
| Library | Version | Purpose | When to Use |
|---------|---------|---------|-------------|
| **python-socketio** | 5.12.0+ | Socket.IO client/server | Required by Flask-SocketIO, handles WebSocket transport |
| **psutil** | 6.1.0+ | System monitoring | Reading CPU temp, memory for health checks |
| **pyserial** | 3.5+ | Serial communication | Reading sensor data from ADC/serial ports |
| **smbus2** | 0.5+ | I2C communication | Water level sensors, other I2C devices |
| **gunicorn** | 23.0+ | WSGI server | Production deployment, replaces Flask dev server |
| **eventlet** | 0.40+ | Async worker | Recommended for Flask-SocketIO production (better than gevent) |
| **paramiko** | 3.5+ | SSH/SFTP | SCP/SFTP file transmission to server |
### Real-Time Communication
| Technology | Purpose | Why Use |
|------------|---------|---------|
| **Socket.IO** (via Flask-SocketIO) | Server-push for sensor updates | Bidirectional, auto-reconnection, fallback to polling. Proven in multiple Pi monitoring projects. |
### Development Tools
| Tool | Purpose | Notes |
|------|---------|-------|
| **systemd** | Service management | Auto-start Flask app on boot, restart on failure |
| **Chromium Kiosk Mode** | Local display | `chromium-browser --kiosk --incognito http://localhost:8080` |
| **nginx** | Reverse proxy | Route 8080 (kiosk) and 9090 (remote) ports |
## Installation
```bash
# Core Python packages
pip install flask==3.1.0
pip install flask-socketio==5.6.0
pip install python-socketio==5.12.0
# Sensor and system access
pip install psutil==6.1.0
pip install pyserial==3.5
pip install smbus2==0.5
# Production server
pip install gunicorn==23.0
pip install eventlet==0.40
# File transmission
pip install paramiko==3.5
# Frontend (via CDN or download)
# - Bootstrap 5.3.x CSS/JS
# - Chart.js 4.4.x
# - Socket.IO client 4.x
```
## Alternatives Considered
| Recommended | Alternative | When to Use Alternative |
|-------------|-------------|-------------------------|
| Flask | FastAPI | Only if you need async/await for high-concurrency (50+ concurrent users). Flask is simpler for embedded. |
| Flask | Quart | If you prefer async Flask syntax. Flask-SocketIO still works but adds complexity. |
| Socket.IO | Server-Sent Events (SSE) | SSE is simpler (no JS library) but only one-way. Use only if you never need client→server real-time messages. |
| Socket.IO | Polling | Only for absolute minimal overhead. Not recommended—Socket.IO overhead is negligible, benefits are substantial. |
| Chart.js | Lightweight Charts | Only if you need financial-style candlestick charts. Chart.js is better for standard line/bar charts. |
| Bootstrap 5 | Pure CSS | If you need absolute minimum footprint. Bootstrap adds ~200KB but saves大量 development time for touchscreen layouts. |
## What NOT to Use
| Avoid | Why | Use Instead |
|-------|-----|-------------|
| **Django** | Too heavy (~30MB+), complex setup, overkill for embedded RTU | Flask (lightweight, sufficient for this use case) |
| **FastAPI** | While fast, adds Pydantic complexity without benefit. Async not needed for single-user dashboard | Flask (simpler, same performance for this use case) |
| **React/Vue/Angular** | Framework overhead, build step required, memory intensive on Pi Zero | Vanilla JS + Bootstrap (direct, no build, ~500KB total) |
| **Node.js** | Adds runtime complexity, unnecessary for Python-centric sensor stack | Python/Flask (native sensor access, simpler deployment) |
| **gunicorn (threading mode)** | Doesn't work well with Flask-SocketIO | eventlet worker (required for WebSocket support) |
| **InfluxDB + Grafana** | Full database + visualization stack, too heavy for Pi Zero | Flask + Chart.js (direct data, minimal overhead) |
## Stack Patterns by Variant
**If running on Pi Zero 2 W (primary target):**
- Use Flask development server (`python app.py`) for simplicity
- Single worker, Socket.IO handles concurrency
- Minimal CSS, avoid heavy JavaScript frameworks
- Reason: Pi Zero 2 W has limited RAM (~512MB), every MB counts
**If running on Pi 3B (alternative):**
- Can use gunicorn + eventlet with 2-4 workers
- More aggressive caching allowed
- Consider nginx for static files
- Reason: Pi 3B has more RAM (~1GB), can handle more overhead
**If remote HD interface needed (port 9090):**
- Serve same Flask app on different port
- Use CSS media queries to show full HD layout on larger screens
- No separate codebase needed
- Reason: Single app, responsive design handles both
## Version Compatibility
| Package | Compatible With | Notes |
|---------|----------------|-------|
| Flask 3.1.0 | Python 3.10+ | Requires Python 3.10 minimum |
| Flask-SocketIO 5.6.0 | Flask >=2.1.0, python-socketio >=5.12.0 | Use latest python-socketio for best performance |
| Socket.IO client 4.x | Flask-SocketIO 5.x | Client/server version must match protocol |
| Bootstrap 5.3.x | All modern browsers | No IE11 support (not needed for kiosk) |
| Chart.js 4.4.x | All modern browsers | Requires canvas support |
| eventlet 0.40+ | Python 3.10+ | Monkey-patching required before other imports |
| psutil 6.1.0 | Python 3.8+ | Works on all Pi models |
## Why This Stack
### Performance on Pi Zero 2 W
- Flask overhead: ~10-15MB RAM (vs Django ~80MB)
- Chart.js canvas rendering is GPU-accelerated on Pi
- Socket.IO connection: ~1-2MB per client
- Total app footprint: ~50-80MB RAM
### Developer Experience
- Flask: Simple, Pythonic, extensive documentation
- Bootstrap: Ready-made touchscreen-friendly components
- Chart.js: Extensive examples for real-time data
- Single codebase serves both kiosk (1024x600) and remote (HD)
### Ecosystem Evidence
Multiple verified GitHub projects use this exact stack:
- `g1forfun/Pi-Monitor` — Flask + Socket.IO + Chart.js (Pi 5 dashboard)
- `Mohammed-Shehsin/sensehat-iot-dashboard` — Flask + real-time sensors
- `idleCyrex/raspberry-pi-weather-station` — Flask + environmental monitoring
- Multiple Raspberry Pi community tutorials (Cytron, official forums)
## Sources
- **Flask-SocketIO PyPI** — Verified latest version 5.6.0 (Dec 2025), Python 3.8+ compatibility
- **piwheels.org** — Verified Flask-SocketIO 5.6.0 available for Raspberry Pi OS
- **GitHub g1forfun/Pi-Monitor** — Active project (last push Feb 2026), Flask + Socket.IO + Chart.js pattern
- **GitHub sensehat-iot-dashboard** — Real-time sensor example with Flask
- **Cytron.io tutorial** — "Create a Real-Time Raspberry Pi Dashboard" (Dec 2025), Flask + Chart.js + psutil
- **Multiple Raspberry Pi forum discussions** — Confirmed Flask + Socket.IO is the community standard for sensor dashboards
---
*Stack research for: Raspberry Pi RTU Web Interface*
*Researched: 2026-03-12*