# h3xb1n/iot-based-smart-energy-meter architecture

> Architecture diagram of the GitHub repository h3xb1n/iot-based-smart-energy-meter, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/h3xb1n/iot-based-smart-energy-meter
- Repository: https://github.com/h3xb1n/iot-based-smart-energy-meter

## Overview

##### Project type and purpose
- This is a full-stack Django web application (“IoT Based Smart Energy Meter”) that:
- Receives live power-usage data from an external IoT device (Arduino Uno + current sensors + ESP8266)
- Stores and manages that data in a relational database
- Presents charts, day-/month-wise views, and downloadable Excel reports via a web UI

##### File structure
- sem_project/ – Django project settings, URL routing, WSGI entrypoint
- api/ – Django REST Framework app handling JSON endpoints for IoT data ingestion and possibly chart data
- pages/ – Django app rendering HTML pages (home, headers, charts), resources for Excel export
- users/ – Authentication/registration: models, forms, views, URLs
- templates/ – HTML templates plus embedded JavaScript/CSS (jQuery UI) for client-side charting
- db.sqlite3 – development database
- Pipfile & Pipfile.lock – Python dependency management

##### Key insights from README
- Data flow: Arduino Uno → computes power usage → ESP8266 sends HTTP requests → Django API → DB
- Web UI polls or fetches latest DB records to plot charts in real time
- Excel export via a Django view

##### Main components
A. IoT Device Layer
- Arduino Uno + Current Sensor hardware
- ESP8266 Wi-Fi module
B. Web Server / Application Layer (Django Monolith)
- WSGI server (Gunicorn/UWSGI, implicit)
- Django core (settings, URL router)
- Sub-apps:
– api app (views → serializers → models)
– pages app (views/templates → chart rendering & Excel generation)
– users app (authentication, forms, user profile)
C. Database Layer
- SQLite (or equivalent RDBMS in production)
D. Client Layer
- Browser (HTML/CSS/JavaScript – jQuery UI, chart library)

##### Relationships & interactions
1. IoT Device → HTTP POST → api/data-ingest endpoint → Django api.views
2. api.views → validate via serializers → write to DB
3. Browser → HTTP GET → pages/home endpoint → Django pages.views → render templates
4. Browser JavaScript → AJAX GET → api/chart-data endpoint → JSON from api.serializers
5. Browser → click “Download Excel” → pages/resources view → generate and stream .xlsx from DB
6. Users app mediates login/signup → grants access to pages

##### Architectural patterns & design principles
- Monolithic MVC (Django)
- RESTful API for IoT ingestion and data retrieval
- Template-driven server-side rendering plus client-side AJAX chart updates
- Clear separation of concerns via distinct Django apps

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% IoT Hardware Layer
    subgraph "IoT Hardware" 
        style Hardware fill:#d3d3d3,stroke:#333,stroke-width:1px
        Hardware1["Arduino Uno + Sensors"]:::hardware
        Hardware2["ESP8266 Wi-Fi Module"]:::hardware
    end

    Hardware1 -->|"senses power usage"| Hardware2

    %% Deployment Layer
    subgraph "Edge to Server"
        WSGI["WSGI Server"]:::server
        WSGI -->|HTTP POST /api/data-ingest| APIViews
    end

    Hardware2 -->|HTTP POST /api/data-ingest| WSGI

    %% Django Application
    subgraph "Django Monolith" 
        style DjangoApp fill:#e8f4fc,stroke:#0288d1,stroke-width:2px

        subgraph "Core (sem_project)" 
            Settings["settings.py"]:::code
            UrlsCore["urls.py"]:::code
            WsgiPy["wsgi.py"]:::code
            Manage["manage.py"]:::code
        end

        subgraph "api App" 
            APIUrls["urls.py"]:::code
            APIViews["views.py"]:::server
            APISerial["serializers.py"]:::code
            APIModels["models.py"]:::code
            APIAdmin["admin.py"]:::code
        end

        subgraph "pages App" 
            PagesUrls["urls.py"]:::code
            PagesViews["views.py"]:::server
            PagesResources["resources.py"]:::code
            PagesModels["models.py"]:::code
            PagesAdmin["admin.py"]:::code
        end

        subgraph "users App" 
            UsersUrls["urls.py"]:::code
            UsersViews["views.py"]:::server
            UsersForms["forms.py"]:::code
            UsersModels["models.py"]:::code
            UsersAdmin["admin.py"]:::code
        end

        subgraph "Templates & Assets"
            BaseHTML["base.html"]:::code
            HomeHTML["home.html"]:::code
            H1HTML["h1.html"]:::code
            SignupHTML["signup.html"]:::code
            LoginHTML["registration/login.html"]:::code
            JQCSS["jquery-ui.css"]:::code
            JQJS["jquery-ui.js"]:::code
        end

        Settings --> UrlsCore
        UrlsCore --> APIUrls
        UrlsCore --> PagesUrls
        UrlsCore --> UsersUrls
        WsgiPy --> Manage
    end

    %% Database Layer
    DB["SQLite DB"]:::database

    %% Client Layer
    Browser["Web Browser"]:::client

    %% Interactions
    Browser -->|GET /| PagesViews
    Browser -.->|AJAX GET /api/chart-data| APISerial
    Browser -->|GET /pages/download-excel| PagesResources
    Browser -->|POST /login, /signup| UsersViews
    APIViews -->|validate & save| APIModels
    PagesViews -->|read/write| PagesModels
    UsersViews -->|authenticate| UsersModels
    APIModels --> DB
    PagesModels --> DB
    UsersModels --> DB

    %% Click Events
    click Settings "https://github.com/h3xb1n/iot-based-smart-energy-meter/blob/master/sem_project/settings.py"
    click UrlsCore "https://github.com/h3xb1n/iot-based-smart-energy-meter/blob/master/sem_project/urls.py"
    click WsgiPy "https://github.com/h3xb1n/iot-based-smart-energy-meter/blob/master/sem_project/wsgi.py"
    click Manage "https://github.com/h3xb1n/iot-based-smart-energy-meter/blob/master/manage.py"
    click APIModels "https://github.com/h3xb1n/iot-based-smart-energy-meter/blob/master/api/models.py"
    click APISerial "https://github.com/h3xb1n/iot-based-smart-energy-meter/blob/master/api/serializers.py"
    click APIViews "https://github.com/h3xb1n/iot-based-smart-energy-meter/blob/master/api/views.py"
    click APIUrls "https://github.com/h3xb1n/iot-based-smart-energy-meter/blob/master/api/urls.py"
    click APIAdmin "https://github.com/h3xb1n/iot-based-smart-energy-meter/blob/master/api/admin.py"
    click PagesViews "https://github.com/h3xb1n/iot-based-smart-energy-meter/blob/master/pages/views.py"
    click PagesUrls "https://github.com/h3xb1n/iot-based-smart-energy-meter/blob/master/pages/urls.py"
    click PagesResources "https://github.com/h3xb1n/iot-based-smart-energy-meter/blob/master/pages/resources.py"
    click PagesModels "https://github.com/h3xb1n/iot-based-smart-energy-meter/blob/master/pages/models.py"
    click PagesAdmin "https://github.com/h3xb1n/iot-based-smart-energy-meter/blob/master/pages/admin.py"
    click UsersModels "https://github.com/h3xb1n/iot-based-smart-energy-meter/blob/master/users/models.py"
    click UsersForms "https://github.com/h3xb1n/iot-based-smart-energy-meter/blob/master/users/forms.py"
    click UsersViews "https://github.com/h3xb1n/iot-based-smart-energy-meter/blob/master/users/views.py"
    click UsersUrls "https://github.com/h3xb1n/iot-based-smart-energy-meter/blob/master/users/urls.py"
    click UsersAdmin "https://github.com/h3xb1n/iot-based-smart-energy-meter/blob/master/users/admin.py"
    click BaseHTML "https://github.com/h3xb1n/iot-based-smart-energy-meter/blob/master/templates/base.html"
    click HomeHTML "https://github.com/h3xb1n/iot-based-smart-energy-meter/blob/master/templates/home.html"
    click H1HTML "https://github.com/h3xb1n/iot-based-smart-energy-meter/blob/master/templates/h1.html"
    click SignupHTML "https://github.com/h3xb1n/iot-based-smart-energy-meter/blob/master/templates/signup.html"
    click LoginHTML "https://github.com/h3xb1n/iot-based-smart-energy-meter/blob/master/templates/registration/login.html"
    click JQCSS "https://github.com/h3xb1n/iot-based-smart-energy-meter/blob/master/templates/files/jquery-ui.css"
    click JQJS "https://github.com/h3xb1n/iot-based-smart-energy-meter/blob/master/templates/files/jquery-ui.js"

    %% Styles
    classDef hardware fill:#d3d3d3,stroke:#333;
    classDef server fill:#bbdefb,stroke:#0d47a1;
    classDef database fill:#b2ebf2,stroke:#006064,stroke-width:2px,shape:cylinder;
    classDef client fill:#c8e6c9,stroke:#1b5e20;
    classDef code fill:#f5f5f5,stroke:#9e9e9e;
```
