SmartTrafficLight is a modular, scalable, and IoT-based system for managing smart traffic intersections with real-time monitoring, infraction detection, and environmental responsiveness.
This system simulates and manages a network of smart semaphores using:
- Raspberry Pi + GPIO for physical signals (LEDs, sensors)
- MQTT for communication
- Docker for modular microservices
- SQLite for storing infractions and registered semaphores
- Python as the main scripting language
⚠️ Important: Scripts that interact with GPIO (e.g. for LEDs and sensors) are not containerized. This is due to hardware access restrictions inside Docker: GPIO interfaces require direct access to/dev/gpiomemand other host-level resources, which are not straightforward to expose safely or portably within containers. As a result, all GPIO-based scripts are executed directly on the host system.
- Dynamic traffic light control (standard/emergency/pedestrian/vulnerable)
- Environmental response (e.g. ice risk via sensor simulation)
- Vehicle infraction detection (e.g. passing on red)
- Real-time display via LCD
- Telegram bot integration for remote queries and reports
- Dynamic semaphore creation via terminal interface
The SmartTrafficLight system is based on a distributed edge architecture where each node (traffic light, sensor, actuator) communicates using standard IoT protocols.
- Used for lightweight, asynchronous communication between modules.
- Managed via a central MQTT broker whose address is dynamically obtained from the
resource_catalog. - Topics follow a structured hierarchy:
SmartTrafficLight/<category>/<zone>/<event>
| Module | Event Published |
|---|---|
Button.py |
pedestrian button pressed |
PIR.py |
motion detected |
DHT22.py |
temperature and humidity |
infraction_sensor.py |
traffic violation |
led_manager.py |
command messages to traffic lights (mode, duration, etc.) |
| Module | Topic Subscribed |
|---|---|
led_manager.py |
listens to events from sensors, buttons, simulations |
Semaphore_X.py |
listens to commands and ice warnings |
ThingSpeak_Adaptor.py |
listens to sensor measurements |
violation_detection.py |
listens to violation data |
- Used for node registration and broker discovery.
- Each module performs:
GET /broker→ to retrieve the broker IP/portPUT /registerResource→ to register itself to the catalog
| Module | Purpose |
|---|---|
All edge modules (e.g., Button.py, DHT22.py, Semaphore_1.py) |
retrieve broker and register to catalog |
database_adaptor.py |
receives data via HTTP POST |
ThingSpeak_Adaptor.py |
pushes sensor data to ThingSpeak via HTTP POST |
Two adapter components play a specific role in HTTP-based communication:
database_adaptor.py: acts as a local HTTP server receivingPOSTrequests from other modules (e.g., violation detection) and writing data into a SQLite database.ThingSpeak_Adaptor.py: acts as an HTTP client that transforms internal MQTT messages into remotePOSTrequests toward the public ThingSpeak API.
These adapters serve as bridges between internal MQTT-based communication and external or persistent HTTP-based systems.
Each traffic light node:
- subscribes to MQTT commands
- processes logic autonomously
- updates its state independently
This ensures:
- ⚡ Low latency
- 🔁 High resilience to disconnections
- 🧩 Modular scalability
SmartTrafficLight/
├── database/ # SQLite DB file (database.db)
├── Semaphores/ # Semaphore_X.py + info.json
├── Sensors/ # DHT22, PIR, Button
├── violation_detection/ # Infraction sensor
├── services/ # Simulated services (e.g. ice risk)
├── LedManager/ # Emergency simulator
├── shared/ # Shared MQTT catalog info
├── start_system.sh # Main launcher with interactive menu
├── stop_system.sh # Stop all scripts and Docker
├── add_semaphore.py # Add new semaphore from terminal
├── docker-compose.yml # Services: resource_catalog, database_adaptor, telegram_bot
-
Raspberry Pi5 with GPIO
-
Python 3.8+
-
Docker + Docker Compose
-
Packages:
gpiozero,paho-mqtt,requests,sqlite3,cherrypy,lgpio,adafruit-circuitpython-dht,blinka
The installation is automatically done at start, but you can run the installation separately via:
./install_requirements.shRemember to compile the .env file in the "shared" folder before starting the system! After that, to launch everything:
./start_system.shThis will:
- Start Docker containers (resource_catalog, database_adaptor, telegram_bot)
- Start main sensors and semaphore scripts
- Show interactive menu for optional components
To stop everything:
./stop_system.shFrom the terminal:
python3 Semaphores/Add_semaphore.pyIt will:
- Ask for the zone (e.g. A)
- Ask which services to activate (lcd, emergency, etc.)
- Insert it into the database
- Create
Semaphore_X.pyandSemaphore_X_info.json - Prompt you to manually set GPIO pins and duty cycles
The system includes a Telegram bot for remote interaction:
- Query recent infractions by plate or zone
- Get real-time environment data from sensors (e.g. temperature, humidity)
- Export results as CSV
Once the system is started (./start_system.sh), the bot becomes accessible at:
https://t.me/Smart_Traffic_Lights_bot
📅 You can also scan the QR code below to open it directly in Telegram:
The resource catalog (running in Docker) is a key component of service discovery in this system. Every script (sensor, actuator, or service) registers itself with the catalog by periodically sending a PUT request with its metadata. This enables dynamic discovery of available services and brokers by other components, reducing hard-coded configuration.
The catalog runs at http://<host>:9090/ and stores all live services with their type, name, zone, IP, and topics.
-
File:
database/database.db -
Tables:
violations: stores infractions (plate, timestamp, station)semaphores: stores semaphores with zone and active services (as JSON)
- Use
logs/to debug any component. All scripts write to log files. - Use
script_pids.txtto stop manually any launched script. - Use
Ctrl+Cinside menu-launched scripts to return to the menu.
MIT License
- Web dashboard for live monitoring
- Database migrations (e.g. with Alembic)
- Better GUI for Raspberry Pi interface
- Integration with cameras and OCR
For questions, suggestions, or contributions, open an issue or reach out to the authors:
Enjoy your smart traffic light system! 🚗🚦🛡️
