Skip to content

div-dev123/Energy_Trading

Repository files navigation

⚡ Decentralized Energy Trading Platform with AI/ML Forecasting

A blockchain-based peer-to-peer energy trading platform built on Ethereum that enables users to buy and sell energy tokens representing kilowatt-hours (kWh) of renewable energy, enhanced with AI/ML predictive analytics for smart energy management.

🌟 Features

  • Token-Based Trading: Energy represented as KWh tokens (1 token = 0.01 kWh)
  • Peer-to-Peer Marketplace: Users can create buy/sell orders for energy trading
  • Secure Transactions: Direct token transfers between users
  • Persistent Wallets: User wallet addresses stored for consistent transactions
  • Real-Time Dashboard: Live monitoring of energy trades and balances
  • Database Persistence: User balances maintained across blockchain redeployments
  • Smart Meter Simulation: Realistic energy consumption data generation
  • AI/ML Energy Forecasting: Predictive models for energy demand forecasting
  • Responsive UI: Modern web interface for seamless trading experience

🏗️ Architecture

Core Components

  1. Smart Contracts (Solidity):

    • KWhToken.sol: ERC-20 compliant energy token
    • Market.sol: Trading marketplace for energy orders
    • MeterRegistry.sol: Smart meter verification system
  2. Backend Services (Python):

    • Flask API for user authentication and trading operations
    • Blockchain integration layer for smart contract interactions
    • PostgreSQL database for user data and order management
  3. Frontend (HTML/CSS/JavaScript):

    • Interactive dashboard for energy trading
    • Real-time order book and transaction history
    • User-friendly interface for creating and managing trades
  4. Infrastructure:

    • Hardhat for smart contract development and testing
    • Local Ethereum node for development
    • MQTT for IoT device communication (simulated meters)
    • Machine Learning models for energy consumption prediction

🚀 Getting Started

Prerequisites

  • Node.js (v16 or higher)
  • Python (3.8 or higher)
  • PostgreSQL database
  • Hardhat (for blockchain development)

Installation

  1. Clone the repository:

    git clone <repository-url>
    cd energy-trading-platform
  2. Install blockchain dependencies:

    cd blockchain
    npm install
  3. Start local blockchain:

    npx hardhat node
  4. Deploy smart contracts:

    npx hardhat run scripts/deploy_contracts.js --network localhost
  5. Install Python dependencies:

    pip install -r requirements.txt
  6. Set up database:

    psql -f create_tables.sql
  7. Start the application:

    python api.py
  8. Access the platform: Open http://localhost:5001 in your browser

📊 Token Economics

  • Initial Allocation: New users receive 10,000 KWh tokens upon signup
  • Trading Mechanism:
    • Buyers must hold sufficient tokens to purchase energy
    • Sellers earn tokens when their energy is purchased
    • 1 KWh token = 0.01 kWh of energy
  • Price: Fixed at $0.16 USD per KWh token ($16 per token)

🔧 Key Operations

User Registration

  • Sign up to receive initial 10,000 KWh tokens
  • Persistent wallet address assigned for all transactions

Energy Trading

  1. Sell Energy:

    • Create sell orders specifying energy amount
    • Tokens transferred to seller upon successful trade
  2. Buy Energy:

    • Browse available sell orders
    • Purchase energy with tokens from your balance

Smart Meter Simulation

  • Realistic energy consumption data generation
  • MQTT-based communication protocol
  • Simulated meter readings for testing and demonstration
  • Continuous data streaming for real-time monitoring

AI/ML Energy Forecasting

  • Predictive models for energy demand forecasting
  • 24-hour consumption forecasting using machine learning
  • Personalized energy usage patterns based on historical data
  • Random forest regression models for accurate predictions

Balance Management

  • Token balances automatically synchronized with blockchain
  • Balances persisted in database across sessions
  • Automatic restoration after blockchain redeployment

🛠️ Development

Smart Contract Development

# Compile contracts
npx hardhat compile

# Run tests
npx hardhat test

# Deploy to local network
npx hardhat run scripts/deploy_contracts.js --network localhost

Backend Development

# Start API server
python api.py

# Run blockchain integration tests
python test_blockchain.py

📁 Project Structure

energy-trading-platform/
├── blockchain/              # Smart contracts and blockchain tools
│   ├── contracts/           # Solidity smart contracts
│   ├── scripts/             # Deployment scripts
│   └── test/               # Contract tests
├── api.py                  # Main Flask API server
├── blockchain_integration.py # Blockchain interaction layer
├── app.js                  # Frontend application logic
├── create_tables.sql       # Database schema
└── index3.html             # Main frontend interface

🤝 Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a pull request

📄 License

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

🙏 Acknowledgments

  • Built with Hardhat for Ethereum development
  • Uses Web3.py for blockchain integration
  • Powered by Flask for backend services
  • PostgreSQL for data persistence

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors