# heuscartist/smart-agriculture-dashboard architecture

> Architecture diagram of the GitHub repository heuscartist/smart-agriculture-dashboard, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-24). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/heuscartist/smart-agriculture-dashboard
- Repository: https://github.com/heuscartist/smart-agriculture-dashboard

## Overview

##### Project Type and Purpose
- This is a full-stack IoT data engineering pipeline for smart agriculture:
- Hardware layer: ESP32 sensor node (temperature, humidity, soil moisture) and ESP32-CAM.
- Ingestion & API layer: Flask-based API for sensor data (real or simulated).
- Time-series storage: InfluxDB.
- Processing layer: Python scripts (influx_writer.py, fetch_image.py) and PyTorch model for plant-health classification.
- Visualization: Streamlit dashboard (app.py).

##### Main Components
a. Hardware
- ESP32 Sensor Node (sensors.ino)
- ESP32-CAM Node (camera.ino, handlers.cpp, WifiCam.hpp)
b. Network / Transport
- Wi-Fi network
- HTTP endpoints (Flask API for sensor data; ESP32-CAM HTTP server for images)
c. Ingestion & API
- sensor_simulator.py (Flask server to simulate or relay ESP32 sensor data)
- influx_writer.py (polls Flask API every 5s → writes JSON data to InfluxDB)
d. Storage
- InfluxDB (bucket “esp32_data”) for time-series sensor readings
- Local filesystem (data/latest.jpg) for the latest plant image
- model/plant_classifier.pth (PyTorch ResNet18 binary)
e. Processing
- fetch_image.py (downloads current image from ESP32-CAM → saves to data/)
- PyTorch inference (load ResNet18 model → classify data/latest.jpg)
f. Visualization
- app.py (Streamlit): queries InfluxDB for sensor metrics; loads latest.jpg; applies model; renders charts, image, health prediction

##### Relationships & Data Flow
1. ESP32 Sensor Node → HTTP POST/GET → Flask API (sensor_simulator or real)
2. influx_writer.py → HTTP GET → Flask API → InfluxDB write
3. ESP32-CAM → HTTP GET → fetch_image.py → data/latest.jpg
4. app.py:
- Reads sensor time-series from InfluxDB
- Reads data/latest.jpg
- Runs PyTorch model inference
- Renders Streamlit UI → browser
5. User interacts via browser → Streamlit app

##### Architectural Patterns & Principles
- Separation of concerns: hardware, ingestion, storage, processing, presentation
- Modular pipeline: independent scripts/services linked by HTTP & DB
- Microservice-style endpoints (Flask)
- Event-driven polling vs. push model
- Use of specialized stores: time-series DB for metrics; file system for images; model artifact store

##### Technologies & Libraries
- Embedded C++: ESP32, ESP32-CAM firmware
- Python:
- flask (sensor API)
- influxdb-client (DB writes & queries)
- requests (HTTP fetch)
- torch, torchvision, PIL (model inference)
- streamlit (dashboard)
- pandas, numpy (data manipulation)
- InfluxDB (time-series storage)
- PyTorch (ResNet18 binary classification)

##### Final Checklist
- [ ] All components labeled (firmware, scripts, DB, model, UI)
- [ ] Callouts for key design principles (modularity, separation, polling)

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Hardware Layer
    subgraph "Hardware Layer"
        direction TB
        ESP32Sensor["ESP32 Sensor Node\n(sensors.ino)"]:::hw
        ESP32Cam["ESP32-CAM Node\n(camera.ino, handlers.cpp, WifiCam.hpp)"]:::hw
    end

    %% Ingestion/API Layer
    subgraph "Ingestion/API Layer"
        direction TB
        SensorAPI["sensor_simulation.py\n(Flask API)"]:::ing
        InfluxWriter["influx_writer.py\n(polls every 5s)"]:::ing
        FetchImage["fetch_image.py\n(downloads image)"]:::ing
    end

    %% Storage Layer
    subgraph "Storage Layer"
        direction TB
        InfluxDB["InfluxDB\n(bucket: esp32_data)"]:::storage
        ImageFS["Local FS\n(data/latest.jpg)"]:::storage
        ModelArt["Plant Model\n(plant_classifier.pth)"]:::storage
        ImageSamples["Image Samples\n(data/healthy/, data/unhealthy/)"]:::storage
    end

    %% Processing Layer
    subgraph "Processing Layer"
        direction TB
        PyTorch["PyTorch Inference\n(ResNet18)"]:::proc
    end

    %% Visualization/UI Layer
    subgraph "Visualization/UI Layer"
        direction TB
        Streamlit["app.py\n(Streamlit Dashboard)"]:::ui
    end

    %% User
    User((User)):::external

    %% Data Flows
    ESP32Sensor -->|"HTTP POST/GET"| SensorAPI
    InfluxWriter -->|"HTTP GET every 5s"| SensorAPI
    InfluxWriter -->|"DB Write"| InfluxDB
    FetchImage -->|"HTTP GET"| ESP32Cam
    FetchImage -->|"saves image"| ImageFS
    Streamlit -->|"DB Query"| InfluxDB
    ImageFS -->|"load latest.jpg"| PyTorch
    ModelArt -->|"load model"| PyTorch
    PyTorch -->|"prediction"| Streamlit
    User -->|"interact via browser"| Streamlit

    %% Click Events
    click ESP32Sensor "https://github.com/heuscartist/smart-agriculture-dashboard/blob/main/esp_firmware/sensors/sensors.ino"
    click ESP32Cam "https://github.com/heuscartist/smart-agriculture-dashboard/tree/main/esp_firmware/camera"
    click SensorAPI "https://github.com/heuscartist/smart-agriculture-dashboard/blob/main/sensor_simulation.py"
    click InfluxWriter "https://github.com/heuscartist/smart-agriculture-dashboard/blob/main/influx_writer.py"
    click FetchImage "https://github.com/heuscartist/smart-agriculture-dashboard/blob/main/fetch_image.py"
    click Streamlit "https://github.com/heuscartist/smart-agriculture-dashboard/blob/main/app.py"
    click ImageFS "https://github.com/heuscartist/smart-agriculture-dashboard/tree/main/data/"
    click ImageSamples "https://github.com/heuscartist/smart-agriculture-dashboard/tree/main/data/healthy/"

    %% Styles
    classDef hw fill:#D0EFFF,stroke:#0F569F,color:#000
    classDef ing fill:#DFFFD0,stroke:#4F900F,color:#000
    classDef storage fill:#FFEFD0,stroke:#A06E00,color:#000
    classDef proc fill:#F0D0FF,stroke:#7F349F,color:#000
    classDef ui fill:#D0FFF5,stroke:#0F977F,color:#000
    classDef external fill:#FFFFFF,stroke:#000000,color:#000,stroke-dasharray: 2 2
```
