Skip to content

Mayan10/final-solar

Repository files navigation

Solar Power Prediction & Digital Twin System

An ML-powered system that predicts solar power generation and helps you manage home appliances intelligently through a digital twin interface.

Built with XGBoost, FastAPI, and a real-time web dashboard.


What It Does

  • Predicts solar power output based on weather and time conditions
  • Simulates a digital twin of your home appliances
  • Automatically manages appliance usage based on available solar power
  • Provides a live dashboard to monitor, control, and optimize energy consumption
  • Exposes a RESTful API for easy integration with other systems

System Architecture

┌─────────────────┐    ┌─────────────────┐    ┌─────────────────┐
│   Weather Data  │    │   Power Data    │    │   ML Model      │
│   (CSV Files)   │───▶│   (Excel Files) │───▶│   (XGBoost)     │
└─────────────────┘    └─────────────────┘    └─────────────────┘
                                │
                                ▼
┌─────────────────┐    ┌─────────────────┐    ┌─────────────────┐
│   FastAPI       │◀───│   Data          │───▶│   Predictions   │
│   Backend       │    │   Processor     │    │   & Analytics   │
└─────────────────┘    └─────────────────┘    └─────────────────┘
         │
         ▼
┌─────────────────┐    ┌─────────────────┐
│   Web Frontend  │◀───│   Digital Twin  │
│   (HTML/JS)     │    │   Controller    │
└─────────────────┘    └─────────────────┘

Getting Started

1. Clone the repository

git clone https://github.com/Mayan10/final-solar.git
cd final-solar

2. Install dependencies

pip install -r requirements.txt

3. Run the system

python run.py

The dashboard will be available at http://localhost:8000.


Project Structure

final-solar/
├── api.py                 # FastAPI backend
├── data_processor.py      # Data loading and preprocessing
├── model_trainer.py       # XGBoost model training
├── run.py                 # Main entry point
├── requirements.txt       # Python dependencies
├── static/
│   └── index.html         # Web frontend
├── data/                  # Raw data files
└── models/                # Trained models

API Reference

Power Prediction

Method Endpoint Description
POST /predict/power Predict power for specific conditions
POST /predict/daily Predict power for an entire day
POST /train-model Train or retrain the model

Digital Twin

Method Endpoint Description
GET /digital-twin/status Get current system status
GET /digital-twin/appliances List all appliances
POST /digital-twin/control Control a specific appliance
POST /digital-twin/optimize Optimize energy usage

System

Method Endpoint Description
GET / Web dashboard
GET /health Health check
GET /docs API documentation (Swagger UI)

Usage Examples

Single power prediction

import requests

response = requests.post("http://localhost:8000/predict/power", json={
    "hour": 12,
    "temperature": 25.5,
    "global_irradiance": 800,
    "direct_irradiance": 600,
    "diffuse_irradiance": 200,
    "month": 6
})

print(f"Predicted Power: {response.json()['power_kw']} kW")

Control an appliance

# Turn on air conditioner
response = requests.post("http://localhost:8000/digital-twin/control", json={
    "appliance_id": "ac_1",
    "action": "on"
})

# Optimize for 5 kW of available solar
response = requests.post("http://localhost:8000/digital-twin/optimize", json=5.0)

Full-day prediction

weather_forecast = [
    {"hour": h, "temperature": 25, "global_irradiance": 800,
     "direct_irradiance": 600, "diffuse_irradiance": 200, "month": 6}
    for h in range(24)
]

response = requests.post("http://localhost:8000/predict/daily", json={
    "prediction_date": "2024-06-15",
    "weather_forecast": weather_forecast
})

print(f"Total Energy: {response.json()['total_power_kwh']} kWh")

Digital Twin — Appliances

Appliance Power (kW) Priority Controllable
Air Conditioner 1 2.0 High Yes
Air Conditioner 2 2.0 Medium Yes
Water Heater 1.5 Medium Yes
Washing Machine 1.0 Medium Yes
TV 0.2 Low Yes
LED Lights 0.1 Low Yes
Refrigerator 0.2 High No

Model Details

Input features:

  • Time: Hour and month (cyclically encoded)
  • Weather: Temperature, humidity, global/direct/diffuse irradiance
  • Derived: Irradiance ratios, rolling averages, lag features

XGBoost parameters:

n_estimators=100
max_depth=6
learning_rate=0.1
subsample=0.8
colsample_bytree=0.8

Typical performance:

  • R² Score: 0.85 – 0.95
  • RMSE: 50 – 150 W
  • MAE: 30 – 100 W

Energy Optimization Logic

  1. High-priority appliances (e.g., refrigerator, AC) are allocated power first
  2. Available solar power is distributed across remaining loads
  3. Non-essential appliances are switched off when supply is low
  4. All changes are tracked in real time through the dashboard

Troubleshooting

Model not trained

  • Call POST /train-model to trigger training
  • Make sure data files are present in the data/ folder

API not responding

  • Confirm the server is running on port 8000
  • Check for firewall or port conflicts

Prediction errors

  • Verify your input matches the expected schema
  • Ensure the model has been loaded successfully

Data Sources

  • Solar irradiance data from PVGIS (European Commission)

Author

Mayan Sharma GitHub: @Mayan10


License

This project is licensed under the MIT License. See the LICENSE file for details.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors