# leonimeloo/esp32-smart-traffic-light-project architecture

> Architecture diagram of the GitHub repository leonimeloo/esp32-smart-traffic-light-project, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/leonimeloo/esp32-smart-traffic-light-project
- Repository: https://github.com/leonimeloo/esp32-smart-traffic-light-project

## Overview

#### Purpose and Entry Points
An academic adaptive-intersection prototype: two ESP32 camera devices capture road images, a FastAPI/YOLO service detects vehicles, and a controller ESP32 selects and drives traffic-light phases. Primary executable entry points are `api/app/main.py`, `esp32/main/main_esp.ino`, `esp32/camera-1/cam_aithinker.ino`, and `esp32/camera-2/cam_freenove.ino`.

#### 1. Camera Acquisition Nodes
Separate firmware supports hardware-specific camera stacks while preserving the same system role: acquire JPEG frames for a road and send them to the vision service. AI-Thinker implementation is in `esp32/camera-1/cam_aithinker.ino`; the Freenove S3-EYE implementation spans `esp32/camera-2/cam_freenove.ino`, `esp32/camera-2/app_httpd.cpp`, and `esp32/camera-2/board_config.h`.

#### 2. Vision API Boundary
The Python API is the compute boundary between constrained embedded cameras and inference. It accepts camera image requests, runs YOLO detection, and returns structured results such as vehicle classes, confidence, boxes, and image metadata. The application entry point is `api/app/main.py`; deployable packaging is defined by `api/Dockerfile`, with environment inputs indicated by `api/.env.example`.

#### 3. Model Artifact and Inference Lifecycle
Training and experimentation are separated from runtime serving. Versioned YOLO weights are committed at `ai/models/best.pt` and `ai/models/training_v1.pt`; standalone inference is represented by `ai/inference.py`. Training derives from a Roboflow-managed dataset not stored in the repository, documented under `ai/dataset/README.md`, with the notebook in `ai/notebooks/Model-Training.ipynb`.

#### 4. Traffic Decision Controller
The main ESP32 consumes current traffic information and owns intersection state, timing, priority selection, and GPIO-level light control. Its governing firmware is `esp32/main/main_esp.ino`. Priority incorporates vehicle demand and accumulated waiting time rather than a fixed cycle; green duration may continue while a queue remains active.

#### 5. Device-to-Service and Device-to-Controller Flow
The primary path is: road scene → camera-specific ESP32 firmware → HTTP JPEG upload to FastAPI → YOLO inference → JSON detection result → main controller → physical traffic lights. Camera/API communication uses HTTP/REST. Controller/device coordination may use Wi-Fi and ESP-NOW, requiring matching MAC peers and Wi-Fi channel configuration in `esp32/main/main_esp.ino`.

#### 6. Deployment and Runtime Boundaries
The backend runs on Python with FastAPI, YOLO, and OpenCV dependencies declared in `api/requirements.txt`; it can run under Uvicorn locally or in Docker using `api/Dockerfile`. The intended hosted runtime is Google Cloud Run, while cameras must target a LAN-reachable host or cloud endpoint rather than `localhost`. ESP32 firmware is C++/Arduino or PlatformIO-oriented and depends on board-specific pins/configuration, notably `esp32/camera-2/camera_pins.h`.

#### 7. Key Operational Constraints
The system is prototype-oriented: model accuracy, queue estimation, phase thresholds, network URLs, credentials, peer MAC addresses, and GPIO assignments are deployment-specific. The most important integration contracts are image upload/request formatting at `api/app/main.py`, JSON detection response consumption by `esp32/main/main_esp.ino`, and hardware-specific camera initialization in the two camera firmware branches.

Main entry points: [`api/app/main.py`](https://github.com/leonimeloo/esp32-smart-traffic-light-project/blob/main/api/app/main.py) (FastAPI vision API), [`esp32/camera-2/cam_freenove.ino`](https://github.com/leonimeloo/esp32-smart-traffic-light-project/blob/main/esp32/camera-2/cam_freenove.ino) (Freenove S3-EYE firmware), [`ai/models/best.pt`](https://github.com/leonimeloo/esp32-smart-traffic-light-project/blob/main/ai/models/best.pt) (YOLO weights), [`esp32/camera-1/cam_aithinker.ino`](https://github.com/leonimeloo/esp32-smart-traffic-light-project/blob/main/esp32/camera-1/cam_aithinker.ino) (AI-Thinker camera firmware)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_cameras["Camera devices"]
  node_cam_ai["AI-Thinker camera firmware<br/>ESP32 firmware<br/>[cam_aithinker.ino]"]
  node_cam_freenove["Freenove S3-EYE firmware<br/>ESP32 firmware<br/>[cam_freenove.ino]"]
  node_freenove_http["Camera HTTP server<br/>firmware component<br/>[app_httpd.cpp]"]
  node_freenove_board["Board camera config<br/>board configuration<br/>[board_config.h]"]
end

subgraph group_vision["Vision service"]
  node_api["FastAPI vision API<br/>Python service<br/>[main.py]"]
  node_api_runtime["API runtime package<br/>container deployment"]
end

subgraph group_ml["Model lifecycle"]
  node_weights["YOLO weights<br/>model artifact<br/>[best.pt]"]
  node_inference["Standalone inference<br/>Python inference<br/>[inference.py]"]
  node_training["Model training notebook<br/>training workflow"]
  node_dataset["Roboflow dataset<br/>external dataset<br/>[README.md]"]
end

subgraph group_control["Intersection control"]
  node_controller{{"Traffic controller firmware<br/>ESP32 firmware<br/>[main_esp.ino]"}}
  node_lights(("Traffic lights<br/>physical actuator"))
end

node_road(("Road scenes<br/>physical input"))

node_road -->|"captured as JPEG"| node_cam_ai
node_road -->|"captured as JPEG"| node_cam_freenove
node_freenove_board -->|"camera setup"| node_cam_freenove
node_freenove_http -->|"HTTP capture support"| node_cam_freenove
node_cam_ai -->|"HTTP JPEG upload"| node_api
node_cam_freenove -->|"HTTP JPEG upload"| node_api
node_api_runtime -->|"deploys"| node_api
node_weights -->|"loaded for detection"| node_api
node_weights -->|"loaded for inference"| node_inference
node_dataset -.->|"training data"| node_training
node_training -->|"produces model versions"| node_weights
node_api -->|"JSON detection results"| node_controller
node_controller -->|"GPIO phase control"| node_lights

click node_cam_ai "https://github.com/leonimeloo/esp32-smart-traffic-light-project/blob/main/esp32/camera-1/cam_aithinker.ino"
click node_cam_freenove "https://github.com/leonimeloo/esp32-smart-traffic-light-project/blob/main/esp32/camera-2/cam_freenove.ino"
click node_freenove_http "https://github.com/leonimeloo/esp32-smart-traffic-light-project/blob/main/esp32/camera-2/app_httpd.cpp"
click node_freenove_board "https://github.com/leonimeloo/esp32-smart-traffic-light-project/blob/main/esp32/camera-2/board_config.h"
click node_api "https://github.com/leonimeloo/esp32-smart-traffic-light-project/blob/main/api/app/main.py"
click node_api_runtime "https://github.com/leonimeloo/esp32-smart-traffic-light-project/blob/main/api/Dockerfile"
click node_weights "https://github.com/leonimeloo/esp32-smart-traffic-light-project/blob/main/ai/models/best.pt"
click node_inference "https://github.com/leonimeloo/esp32-smart-traffic-light-project/blob/main/ai/inference.py"
click node_training "https://github.com/leonimeloo/esp32-smart-traffic-light-project/blob/main/ai/notebooks/Model-Training.ipynb"
click node_dataset "https://github.com/leonimeloo/esp32-smart-traffic-light-project/blob/main/ai/dataset/README.md"
click node_controller "https://github.com/leonimeloo/esp32-smart-traffic-light-project/blob/main/esp32/main/main_esp.ino"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_cam_ai,node_cam_freenove,node_freenove_http,node_freenove_board toneBlue
class node_api,node_api_runtime toneAmber
class node_weights,node_inference,node_training,node_dataset toneMint
class node_controller,node_lights toneRose
class node_road toneNeutral
```

## Components

### Camera devices

- [AI-Thinker camera firmware](https://github.com/leonimeloo/esp32-smart-traffic-light-project/blob/main/esp32/camera-1/cam_aithinker.ino): ESP32 firmware · Captures JPEG road frames. · `esp32/camera-1/cam_aithinker.ino`
- [Freenove S3-EYE firmware](https://github.com/leonimeloo/esp32-smart-traffic-light-project/blob/main/esp32/camera-2/cam_freenove.ino): ESP32 firmware · Hardware-specific camera capture and HTTP support. · `esp32/camera-2/cam_freenove.ino`
- [Camera HTTP server](https://github.com/leonimeloo/esp32-smart-traffic-light-project/blob/main/esp32/camera-2/app_httpd.cpp): firmware component · `esp32/camera-2/app_httpd.cpp`
- [Board camera config](https://github.com/leonimeloo/esp32-smart-traffic-light-project/blob/main/esp32/camera-2/board_config.h): board configuration · `esp32/camera-2/board_config.h`

### Vision service

- [FastAPI vision API](https://github.com/leonimeloo/esp32-smart-traffic-light-project/blob/main/api/app/main.py): Python service · Accepts images and returns structured detections. · `api/app/main.py`
- [API runtime package](https://github.com/leonimeloo/esp32-smart-traffic-light-project/blob/main/api/Dockerfile): container deployment · Dockerized FastAPI, YOLO, and OpenCV runtime. · `api/Dockerfile`

### Model lifecycle

- [YOLO weights](https://github.com/leonimeloo/esp32-smart-traffic-light-project/blob/main/ai/models/best.pt): model artifact · Versioned runtime detection weights. · `ai/models/best.pt`
- [Standalone inference](https://github.com/leonimeloo/esp32-smart-traffic-light-project/blob/main/ai/inference.py): Python inference · `ai/inference.py`
- [Model training notebook](https://github.com/leonimeloo/esp32-smart-traffic-light-project/blob/main/ai/notebooks/Model-Training.ipynb): training workflow · Trains from the externally managed Roboflow dataset. · `ai/notebooks/Model-Training.ipynb`
- [Roboflow dataset](https://github.com/leonimeloo/esp32-smart-traffic-light-project/blob/main/ai/dataset/README.md): external dataset · Dataset is not stored in this repository. · `ai/dataset/README.md`

### Intersection control

- [Traffic controller firmware](https://github.com/leonimeloo/esp32-smart-traffic-light-project/blob/main/esp32/main/main_esp.ino): ESP32 firmware · Selects phases from demand and waiting time, then drives GPIO. · `esp32/main/main_esp.ino`
- **Traffic lights**: physical actuator

### Other components

- **Road scenes**: physical input

## Connections

- Road scenes → AI-Thinker camera firmware: captured as JPEG
- Road scenes → Freenove S3-EYE firmware: captured as JPEG
- Board camera config → Freenove S3-EYE firmware: camera setup
- Camera HTTP server → Freenove S3-EYE firmware: HTTP capture support
- AI-Thinker camera firmware → FastAPI vision API: HTTP JPEG upload
- Freenove S3-EYE firmware → FastAPI vision API: HTTP JPEG upload
- API runtime package → FastAPI vision API: deploys
- YOLO weights → FastAPI vision API: loaded for detection
- YOLO weights → Standalone inference: loaded for inference
- Roboflow dataset → Model training notebook: training data
- Model training notebook → YOLO weights: produces model versions
- FastAPI vision API → Traffic controller firmware: JSON detection results · Wi-Fi or ESP-NOW coordination uses configured peers.
- Traffic controller firmware → Traffic lights: GPIO phase control
