How leonimeloo/esp32-smart-traffic-light-project works
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: main.py FastAPI vision API · cam_freenove.ino Freenove S3-EYE firmware · best.pt YOLO weights · cam_aithinker.ino AI-Thinker camera firmware
Components 13
Camera devices
- AI-Thinker camera firmware · Captures JPEG road frames.
esp32/camera-1/cam_aithinker.ino - Freenove S3-EYE firmware · Hardware-specific camera capture and HTTP support.
esp32/camera-2/cam_freenove.ino - Camera HTTP server
esp32/camera-2/app_httpd.cpp - Board camera config
esp32/camera-2/board_config.h
Vision service
- FastAPI vision API · Accepts images and returns structured detections.
api/app/main.py - API runtime package · Dockerized FastAPI, YOLO, and OpenCV runtime.
api/Dockerfile
Model lifecycle
- YOLO weights · Versioned runtime detection weights.
ai/models/best.pt - Standalone inference
ai/inference.py - Model training notebook · Trains from the externally managed Roboflow dataset.
ai/notebooks/Model-Training.ipynb - Roboflow dataset · Dataset is not stored in this repository.
ai/dataset/README.md
Intersection control
- Traffic controller firmware · Selects phases from demand and waiting time, then drives GPIO.
esp32/main/main_esp.ino - Traffic lights
Other components
- Road scenes
Connections 13
- 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