# srinath-13/nodemcu-ml-waste-classifier architecture

> Architecture diagram of the GitHub repository srinath-13/nodemcu-ml-waste-classifier, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/srinath-13/nodemcu-ml-waste-classifier
- Repository: https://github.com/srinath-13/nodemcu-ml-waste-classifier

## Overview

##### Project type and purpose
- This is a full‐stack, embedded + web + machine‐learning system that automates waste classification and sorting.
- Hardware modules (ESP32-CAM, ESP8266+ultrasonic sensor, servo motor) capture sensor data and actuate mechanical parts.
- A PHP server acts as an intermediary HTTP endpoint for image upload and result forwarding.
- A Flask server hosts a TensorFlow/ResNet50 model (`final_model.h5`) to classify images into recyclable vs. non-recyclable categories.

##### File structure
Top level:
- “ESP 8266” folder: Arduino sketch for ultrasonic sensor & servo logic.
- “ESP32 Cam” folder: Arduino sketch (`CaptureAndClassify.ino`) to capture images and POST to PHP.
- “Flask (Backend)/TARP”: Python code (`main.py`, `test.py`, `tempCodeRunnerFile.py`) and model file (`final_model.h5`).
- “PHP Server/ESP32CAM”: PHP frontend (index.php, upload_img.php, loader scripts) plus static assets & captured_images folder.
- Root scripts (`main.py`, `test.py`, etc.) appear duplicate—focus on Flask/TARP for ML.

##### README for workflow insights
- Ultrasonic sensor triggers image capture when object within 5–15 cm.
- ESP32-CAM captures JPEG, streams bytes to PHP server endpoints.
- PHP server forwards the image to Flask ML server via HTTP.
- The Flask model classifies into six waste classes and returns a binary recyclable/non-recyclable signal.
- PHP relays classification back to ESP32, which signals the ESP8266 to rotate a servo motor to sort waste.

##### Main components & interactions
a. Hardware Layer (Left side)
- Ultrasonic Sensor + ESP8266 (NodeMCU)
- ESP32-CAM module
- Servo Motor
b. Edge Communication Layer (Middle)
- PHP Server (HTTP endpoint)
– Endpoints: upload_img.php, CountImageFile.php, loadPhotos.php
- Flask ML Server (HTTP API)
– Endpoint: `/predict` in `main.py` loads `final_model.h5`
1. Ultrasonic sensor → ESP8266 (distance trigger)
2. ESP8266 → ESP32-CAM (trigger capture)
3. ESP32-CAM → PHP server (HTTP POST image bytes)
4. PHP server → Flask server (HTTP POST forwarding)
5. Flask server → PHP server (classification JSON response)
6. PHP server → ESP32-CAM (classification signal)
7. ESP32-CAM → ESP8266 (signal via Wi-Fi or digital line)
8. ESP8266 → Servo motor (PWM rotation command)
d. Data Stores
- `captured_images/` directory on PHP server (temporary storage)
- `final_model.h5` in Flask server

##### Architectural patterns & design principles
- Separation of concerns:
– Embedded code handles hardware I/O, network I/O.
– PHP acts as a thin middleware layer (web UI + HTTP relay).
– Flask hosts the ML microservice.
- RESTful service: Flask exposes a prediction API.
- Modular: Arduino sketches isolated per microcontroller.
- Stateless HTTP calls for image classify requests.

## Diagram (Mermaid)

```mermaid
graph TB
    %% Hardware Layer
    subgraph "Hardware Layer"
        US["Ultrasonic Sensor"]:::hardware
        ESP8266["ESP8266 (NodeMCU)\nultrasonic_nodemcu.ino"]:::hardware
        ESP32CAM["ESP32-CAM Module\nCaptureAndClassify.ino"]:::hardware
        Servo["Servo Motor"]:::hardware
    end

    %% Edge Communication Layer
    subgraph "Edge Communication Layer"
        Upload["upload_img.php"]:::middleware
        Index["index.php"]:::middleware
        Count["CountImageFile.php"]:::middleware
        Load["loadPhotos.php"]:::middleware
        CI["captured_images/"]:::middleware
        CSS["css/"]:::middleware
        JS["js/"]:::middleware
    end

    %% ML Service Layer
    subgraph "ML Service Layer"
        MainPy["main.py (/predict)"]:::ml
        Model["final_model.h5"]:::ml
    end

    %% Data Flows
    US -->|"distance trigger"| ESP8266
    ESP8266 -->|"trigger capture"| ESP32CAM
    ESP32CAM -->|"HTTP POST image bytes"| Upload
    Upload -->|"store JPEG"| CI
    Upload -->|"forward image"| MainPy
    MainPy -->|"loads model"| Model
    MainPy -->|"JSON classification"| Upload
    Upload -->|"classification signal"| ESP32CAM
    ESP32CAM -->|"signal"| ESP8266
    ESP8266 -->|"PWM servo command"| Servo

    %% Click Events
    click ESP8266 "https://github.com/srinath-13/nodemcu-ml-waste-classifier/blob/main/ESP 8266/ultrasonic_nodemcu.ino"
    click ESP32CAM "https://github.com/srinath-13/nodemcu-ml-waste-classifier/blob/main/ESP32 Cam/CaptureAndClassify.ino"
    click Index "https://github.com/srinath-13/nodemcu-ml-waste-classifier/blob/main/PHP Server/ESP32CAM/index.php"
    click Upload "https://github.com/srinath-13/nodemcu-ml-waste-classifier/blob/main/PHP Server/ESP32CAM/upload_img.php"
    click Count "https://github.com/srinath-13/nodemcu-ml-waste-classifier/blob/main/PHP Server/ESP32CAM/CountImageFile.php"
    click Load "https://github.com/srinath-13/nodemcu-ml-waste-classifier/blob/main/PHP Server/ESP32CAM/loadPhotos.php"
    click CI "https://github.com/srinath-13/nodemcu-ml-waste-classifier/tree/main/PHP Server/ESP32CAM/captured_images/"
    click CSS "https://github.com/srinath-13/nodemcu-ml-waste-classifier/tree/main/PHP Server/ESP32CAM/css/"
    click JS "https://github.com/srinath-13/nodemcu-ml-waste-classifier/tree/main/PHP Server/ESP32CAM/js/"
    click MainPy "https://github.com/srinath-13/nodemcu-ml-waste-classifier/blob/main/Flask (Backend)/TARP/main.py"
    click Model "https://github.com/srinath-13/nodemcu-ml-waste-classifier/blob/main/Flask (Backend)/TARP/final_model.h5"

    %% Styles
    classDef hardware fill:#a8e6cf,stroke:#2e7d32,stroke-width:2px;
    classDef middleware fill:#dcedc1,stroke:#388e3c,stroke-width:2px;
    classDef ml fill:#ffd3b6,stroke:#ef6c00,stroke-width:2px;
```
