A comprehensive Raspberry Pi monitoring system with real-time system metrics and air quality monitoring using the PMS7003 sensor.
- Real-time CPU monitoring - Usage percentage and temperature tracking
- Memory usage statistics with detailed breakdowns
- Disk usage visualization with progress bars for all mounted drives
- Network information display showing all interfaces and IP addresses
- System information including hostname, platform, architecture, and uptime
- Historical data with 24-hour graphs for all system metrics
- Temperature history with both real-time (10 min) and database (24h) views
- Real-time air quality measurements using PMS7003 sensor
- Air Quality Index (AQI) calculation and display with color-coded levels
- Particle concentration monitoring - PM1.0, PM2.5, and PM10 measurements
- "Lowest Air Quality (24h)" card showing worst readings with timestamps
- Historical air quality data with multiple time ranges (1h, 6h, 24h)
- Air quality forecasting with hourly predictions and daily summaries
- Today/Tomorrow forecast cards with timezone-aware date handling
- Persistent data storage in SQLite database with automatic cleanup
- Automatic sensor reconnection on connection failures
- π± Mobile-first responsive design optimized for all screen sizes
- π¨ Clean modular architecture with organized JavaScript modules
- π Interactive charts with touch gestures and time range selection
- π Proper timezone handling (UTC to Pacific Time conversion)
- π― Touch-friendly controls with enhanced mobile UX
- β‘ Real-time updates with configurable refresh intervals
- π¨ AQI color coding for instant air quality assessment
- Raspberry Pi Zero 2 W (or any Raspberry Pi model)
- Python 3.7 or higher
- Git
- PMS7003 air quality sensor (optional, for air quality monitoring)
Connect the PMS7003 sensor to your Raspberry Pi:
- VCC β 5V (Pin 2 or 4)
- GND β Ground (Pin 6, 9, 14, 20, 25, 30, 34, or 39)
- TX β GPIO15/RXD (Pin 10)
- RX β GPIO14/TXD (Pin 8) (optional, for sending commands)
- Clone the repository on your Raspberry Pi:
git clone https://github.com/andrewrweber/pi_air.git
cd pi_air- Run the setup script:
chmod +x scripts/setup_pi.sh
./scripts/setup_pi.shThis script will:
- Create a Python virtual environment
- Install all dependencies
- Set up the SQLite database
- Create systemd services with automatic user/path detection
- Configure services to start on boot
If you prefer to set up manually:
- Create and activate virtual environment:
python3 -m venv venv
source venv/bin/activate- Install dependencies:
pip install -r requirements.txt- Initialize the database:
python -c "from src.database import init_database; init_database()"- Install systemd services (automatically detects user and paths):
sudo chmod +x scripts/install_service.sh
sudo ./scripts/install_service.shThe installation script automatically detects:
- Your current username (works with any user, not just 'pi')
- Project directory location
- User's primary group
- Virtual environment path
This ensures the services work regardless of your username or installation directory.
Start the services:
sudo systemctl start pimonitor.service
sudo systemctl start air-quality-monitor.serviceCheck service status:
sudo systemctl status pimonitor.service
sudo systemctl status air-quality-monitor.serviceView logs:
sudo journalctl -u pimonitor.service -f
sudo journalctl -u air-quality-monitor.service -f- Find your Pi's IP address:
hostname -I- Open a browser and navigate to:
http://YOUR_PI_IP_ADDRESS:5000
GET /- Web dashboard interfaceGET /api/system- Complete system information (JSON)GET /api/stats- Real-time stats (CPU, memory, temperature)
GET /api/air-quality-latest- Latest air quality sensor readingGET /api/air-quality-worst-24h- Worst air quality reading from last 24hGET /api/air-quality-history?range={1h|6h|24h}- Historical air quality dataGET /api/air-quality-forecast?hours={12|24|48|72}- Hourly air quality forecastGET /api/air-quality-forecast-summary?days={1|2|3}- Daily forecast summaries
GET /api/temperature-history- CPU temperature history dataGET /api/system-history- Historical system metrics (24h)
The Flask app supports the following environment variables:
FLASK_DEBUG- Set to 'true' for debug mode (development only)FLASK_HOST- IP address to bind to (default: 127.0.0.1)FLASK_PORT- Port number (default: 5000)
Example:
FLASK_HOST=0.0.0.0 FLASK_PORT=8080 python src/app.pyThe SQLite database is stored at data/monitoring.db. The database is automatically created on first run and includes tables for:
- System metrics (CPU, memory, disk usage, temperature)
- Air quality readings (PM values, AQI)
- Make changes on your development machine
- Commit and push to GitHub:
git add .
git commit -m "Your commit message"
git push origin main- Pull changes on your Pi:
cd /path/to/your/pi_air # Or wherever you installed the project
git pull origin main- Restart services if needed:
sudo systemctl restart pimonitor.service
sudo systemctl restart air-quality-monitor.serviceThe project includes comprehensive test coverage for both backend and frontend components.
# Install test dependencies
pip install -r requirements.txt
# Run all backend tests
pytest
# Run specific test categories
pytest -m unit # Unit tests only
pytest -m api # API tests only
pytest -m database # Database tests only
pytest -m "not hardware" # Skip hardware-specific tests
# Run with coverage
pytest --cov=src --cov-report=htmlOpen the frontend test runner in your browser:
# Start local server (optional)
python -m http.server 8000
# Open in browser
open http://localhost:8000/tests/frontend/test_runner.html- Unit Tests (
@pytest.mark.unit) - Individual component testing - Integration Tests (
@pytest.mark.integration) - Component interaction testing - API Tests (
@pytest.mark.api) - Flask endpoint testing - Database Tests (
@pytest.mark.database) - Database operation testing - Frontend Tests - JavaScript module testing
- Hardware Tests (
@pytest.mark.hardware) - Raspberry Pi specific tests
Tests are well-documented with comprehensive coverage of all major functionality.
-
Can't access web interface from other devices
- Ensure the service is running with
FLASK_HOST=0.0.0.0 - Check firewall settings:
sudo ufw allow 5000
- Ensure the service is running with
-
Temperature shows N/A
- The
vcgencmdcommand is Raspberry Pi specific - Ensure you're running on actual Raspberry Pi hardware
- The
-
Air quality data not updating
- Check sensor connection: TX β GPIO15/RXD (Pin 10)
- Verify serial port permissions:
sudo usermod -a -G dialout $USER - Check service logs:
sudo journalctl -u air-quality-monitor.service -f
-
High CPU usage
- Normal for Pi Zero 2 W during data collection
- Consider increasing update intervals in the web interface
View database statistics:
sqlite3 data/monitoring.db "SELECT COUNT(*) FROM air_quality_readings;"The database includes automatic cleanup to prevent unlimited growth.
- config.js - Centralized configuration and constants
- utils.js - Shared utility functions with timezone management
- charts.js - Chart management with ChartManager class
- hardware.js - Hardware monitoring functionality
- air-quality.js - Air quality monitoring and display
- forecast.js - Air quality forecasting with timezone-aware date handling
- app.js - Main application controller
- app.py - Flask web application and API routes
- air_quality_monitor.py - Air quality monitoring service
- pms7003.py - PMS7003 sensor driver
- database.py - Database operations and models
- logging_config.py - Logging configuration
- services/forecast_service.py - Air quality forecast integration
pi_air/
βββ src/ # Backend Python modules
β βββ app.py # Flask web application
β βββ air_quality_monitor.py # Air quality monitoring service
β βββ pms7003.py # PMS7003 sensor driver
β βββ database.py # Database operations
β βββ logging_config.py # Logging configuration
β βββ services/
β β βββ forecast_service.py # Air quality forecast integration
β βββ utils/
β βββ timestamp_utils.py # Timezone and timestamp utilities
βββ static/
β βββ js/ # Frontend JavaScript modules
β β βββ config.js # Configuration and constants
β β βββ utils.js # Utility functions with timezone management
β β βββ charts.js # Chart management
β β βββ hardware.js # Hardware monitoring
β β βββ air-quality.js # Air quality functionality
β β βββ forecast.js # Air quality forecasting
β β βββ app.js # Main application controller
β βββ style.css # Dashboard styling
βββ templates/
β βββ index.html # Web dashboard template
βββ tests/ # Comprehensive test suite
β βββ conftest.py # Test configuration and fixtures
β βββ test_database.py # Database tests
β βββ test_api_routes.py # API endpoint tests
β βββ test_forecast_service.py # Forecast service tests
β βββ test_hardware_monitoring.py # Hardware tests
β βββ test_frontend_forecast.html # Frontend forecast tests
β βββ frontend/ # Frontend JavaScript tests
βββ scripts/
β βββ setup_pi.sh # Automated setup script
β βββ install_service.sh # Service installation
βββ systemd/ # Service templates
βββ data/ # Database storage
βββ requirements.txt # Python dependencies
βββ pytest.ini # Test configuration
βββ CLAUDE.md # Project documentation for Claude Code
βββ README.md # This file
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Run tests (
pytestand frontend tests) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Create a Pull Request
- 80%+ test coverage with comprehensive backend and frontend tests
- Mobile-first responsive design tested across devices
- API validation with proper error handling
- Database integrity with automatic cleanup and migrations
- Performance optimization for Raspberry Pi Zero 2 W
MIT License - see LICENSE file for details
- Built with Flask and psutil
- Charts powered by Chart.js
- Designed for Raspberry Pi Zero 2 W
- Air quality calculations based on EPA AQI standards
- Mobile UX optimized for touch interfaces
- Comprehensive testing with pytest framework