Next Garage Io T System for ESP32
An ESP32 display project involving OLED display, Servo motor.
Parts and sourceBefore you start
Check the exact OLED display, Servo motor configuration against the upstream wiring or parts list.
Upstream README
NextGarage IoT System ๐
Smart parking management system with automated gate control, real-time monitoring, and cloud connectivity using ESP32 microcontroller.
๐ This project was developed as part of the 2025/26 Internet of Things course at the
Universitร degli Studi di Salerno (University of Salerno, Italy).
๐ Table of Contents
- Project Overview
- Technologies & Dependencies
- Main Features
- Hardware Components
- Project Architecture
- System States & Logic
- MQTT Integration
- Configuration & Thresholds
- Setup & Installation
- Node-RED Dashboard
- Team Members
๐ Project Overview
NextGarage is an intelligent single-space parking management system that combines:
- Automated barrier control with IR sensors for entry/exit detection
- Real-time parking spot monitoring using ultrasonic distance sensors
- Environmental safety with gas leak detection (MQ2 sensor)
- Automatic lighting control based on ambient brightness (TSL2561 sensor)
- Remote monitoring and control via MQTT protocol and Node-RED dashboard
- OLED display for local status visualization with custom graphics
The system is built on ESP32 microcontroller running MicroPython, providing a complete IoT solution for smart parking management with cloud connectivity.
๐ Technologies & Dependencies
- Microcontroller: ESP32 (dual-core, WiFi/Bluetooth)
- Programming Language: MicroPython 1.20+
- Communication Protocol: MQTT (HiveMQ public broker)
- Display Driver: SSD1306 (I2C OLED 128x64)
- Sensors:
- HC-SR04 Ultrasonic Distance Sensor
- IR Obstacle Detection Sensors
- MQ2 Gas Sensor (Analog ADC)
- TSL2561 Ambient Light Sensor (I2C)
- Dashboard: Node-RED for remote monitoring and control
- Libraries:
umqtt.simple- MQTT clientssd1306- OLED display driverframebuf- Graphics rendering
โ Main Features
๐ง Automated Gate Control
- Entry detection via IR sensor with traffic light signals (Red โ Green)
- Exit detection with priority opening (automatic bypass)
- Manual control via physical button or MQTT commands
- Safety system prevents closing if vehicle detected
- Smooth servo movement with configurable speed
๐ ฟ๏ธ Intelligent Parking Management
- Ultrasonic distance monitoring with advanced filtering algorithms
- Trimmed mean filter (discards outliers)
- Low-pass filter for signal smoothing
- Hysteresis for stable state transitions
- Parking assistance with progressive buzzer feedback
- Visual indicators (Red/Green LEDs) for spot occupancy
- Automatic state confirmation with configurable delay timers
๐ก๏ธ Safety & Environmental Monitoring
- Gas leak detection with configurable threshold and hysteresis
- Visual/audio alarm on gas detection (LED + buzzer)
- Automatic lighting control based on ambient light levels
- AUTO mode: lights on when lux < threshold
- Manual ON/OFF override via MQTT
๐ Real-Time Monitoring & Control
- OLED display with custom bitmap graphics:
- Main status screen (gate, parking, gas, light)
- Parking assistance screen with progress bar
- Gas alarm screen with warning icon
- Connection status icons (WiFi, MQTT, System Reset)
- MQTT telemetry published every 2 seconds:
- Gate state, parking occupancy, distance
- Gas level (raw ADC values), alarm status
- Ambient light (lux)
- Remote configuration via MQTT:
- Adjustable sensor thresholds
- Light mode selection
- Gate control commands
๐ง Hardware Components
| Component | Pin | Description |
|---|---|---|
| OLED Display | SDA: 21, SCL: 22 | 128x64 I2C display (SSD1306) |
| TSL2561 Light Sensor | I2C (0x39) | Ambient light measurement |
| IR Entrance Sensor | GPIO 14 | Vehicle entry detection |
| IR Exit Sensor | GPIO 26 | Vehicle exit detection |
| Ultrasonic Sensor | TRIG: 5, ECHO: 18 | Distance measurement (parking spot) |
| MQ2 Gas Sensor | GPIO 34 (ADC) | Gas leak detection |
| Servo Motor | GPIO 15 (PWM) | Barrier gate control |
| Traffic Light | R: 27, Y: 4, G: 2 | Entry signaling |
| Parking LEDs | Red: 16, Green: 33 | Spot occupancy indicator |
| Buzzer | GPIO 17 (PWM) | Parking assist + gas alarm |
| Parking Light | GPIO 25 | Automatic/manual lighting |
| Alarm LED | GPIO 13 | Gas alarm indicator |
| Gate Button | GPIO 12 | Manual gate control |
| Master Button | GPIO 32 | System reset (5s hold) |
๐งฑ Project Architecture
NextGarage-IoT-System/
โโโ boot.py # System boot sequence
โโโ main.py # Main entry point with error handling
โโโ config.py # Configuration and thresholds
โโโ parking.py # Core system logic and state machine
โโโ mqtt_handler.py # MQTT communication manager
โโโ ssd1306.py # OLED display driver
โโโ tsl2561.py # Light sensor driver
โโโ sensors/
โ โโโ ir_sensor.py # IR obstacle detection
โ โโโ ultrasonic.py # Distance measurement
โ โโโ mq2.py # Gas sensor with calibration
โ โโโ brightness_sensor.py # TSL2561 wrapper
โโโ actuators/
โ โโโ servo_gate.py # Barrier control state machine
โ โโโ buzzer.py # Audio feedback (parking/alarm)
โ โโโ parking_leds.py # Spot status LEDs
โ โโโ parking_light.py # Automatic lighting
โ โโโ traffic_light.py # Entry traffic signals
โโโ net/
โ โโโ mqtt_client.py # MQTT client wrapper
โ โโโ wifi_manager.py # WiFi connection handler
โโโ input/
โ โโโ button.py # Button debouncing and press detection
โโโ display/
โโโ oled_display.py # Display UI with custom graphics
๐ System States & Logic
Gate State Machine
The servo gate operates through a finite state machine with 6 states:
-
IDLE (0) - Gate closed, red light on
- Monitors entry IR sensor
- Blocks entry if parking is full
- Allows exit with priority
-
GREEN (1) - Ready for entry
- Green light on, waiting for button press
- Returns to IDLE if vehicle leaves
- Still allows exit override
-
OPENING (2) - Gate opening
- Yellow light blinking
- Smooth servo movement (90ยฐ in steps)
- Transitions to WAIT_CLEAR or MANUAL_OPEN
-
WAIT_CLEAR (3) - Gate open, auto mode
- Waits for vehicle to clear sensors (1s delay)
- Automatically closes when safe
-
CLOSING (4) - Gate closing
- Yellow light blinking
- Safety check: reopens if obstacle detected
- Returns to IDLE when closed
-
MANUAL_OPEN (5) - Gate open, manual mode
- Stays open until remote close command
- Requires explicit MQTT command to close
Parking State Logic
- Distance filtering: 7-sample burst โ discard min/max โ low-pass filter (70% new, 30% old)
- Occupancy detection: Distance โค 3cm for 3 seconds โ OCCUPIED
- Vacancy detection: Distance > 9cm for 2 seconds โ FREE
- Parking assist zones:
- < 3cm: STOP (continuous tone)
- 3-5cm: Slow down (medium frequency)
- 5-7cm: Approach (low frequency)
- > 7cm: No assist
๐ก MQTT Integration
Connection Details
- Broker:
broker.hivemq.com(public HiveMQ) - Port: 1883
- Client ID:
nextgarage-esp32
Published Topics (Telemetry)
parking/state/gate โ "CHIUSA" / "APERTA" / "APERTURA" / "CHIUSURA"
parking/state/spot โ "LIBERO" / "OCCUPATO"
parking/ultrasonic/distance โ Distance in cm (0.0-8.0, capped for dashboard)
parking/sensors/gas โ Raw ADC value (0-4095)
parking/alarms/gas โ "OK" / "ALLARME!"
parking/env/light โ Ambient light in lux
Subscribed Topics (Commands)
parking/cmd/open_gate โ Open gate remotely
parking/cmd/close_gate โ Close gate (if safe)
parking/cmd/parking_light_mode โ "AUTO" / "ON" / "OFF"
parking/cmd/reset_config โ Reset all thresholds to defaults
Configuration Topics (Dynamic Thresholds)
parking/cfg/mq2_threshold โ Gas alarm threshold (raw ADC)
parking/cfg/mq2_hyst โ Gas alarm hysteresis
parking/cfg/lux_threshold โ Auto-light activation threshold
โ๏ธ Configuration & Thresholds
All configurable parameters are in config.py and can be updated dynamically via MQTT:
Distance Thresholds (Ultrasonic)
ULTRASONIC_MIN_DISTANCE = 3 # cm - parking spot occupied
ULTRASONIC_MAX_DISTANCE = 7 # cm - detection range
ULTRASONIC_OCCUPIED_CONFIRM = 3000 # ms - confirmation delay
ULTRASONIC_FREE_CONFIRM = 2000 # ms - vacancy confirmation
Gas Sensor (MQ2)
MQ2_THRESHOLD = 1500 # Raw ADC value for alarm
MQ2_HYSTERESIS = 200 # Prevent alarm flapping
Ambient Light (TSL2561)
LUX_THRESHOLD = 50 # Lux - below this = lights ON (AUTO mode)
Servo & Timing
SERVO_OPEN_ANGLE = 90 # Logical open position
SERVO_CLOSE_ANGLE = 0 # Logical closed position
SERVO_STEP = 2 # Degrees per movement
SERVO_INTERVAL = 30 # ms between steps
๐ Setup & Installation
Prerequisites
- ESP32 development board (30-pin or compatible)
- MicroPython firmware v1.20 or higher installed on ESP32
- Thonny IDE or similar for file upload
- Node-RED (optional, for dashboard)
1๏ธโฃ Flash MicroPython
Download and flash MicroPython firmware to ESP32:
esptool.py --port /dev/ttyUSB0 erase_flash
esptool.py --port /dev/ttyUSB0 write_flash -z 0x1000 esp32-xxxxxx.bin
2๏ธโฃ Configure WiFi & MQTT
Edit config.py to set your credentials:
WIFI_SSID = "YourNetworkName"
WIFI_PASSWORD = "YourPassword"
# MQTT settings (or use default HiveMQ public broker)
MQTT_BROKER = "broker.hivemq.com"
MQTT_PORT = 1883
3๏ธโฃ Upload Files to ESP32
Using Thonny or ampy, upload all files maintaining the folder structure:
# Upload all Python files to ESP32
ampy --port /dev/ttyUSB0 put boot.py
ampy --port /dev/ttyUSB0 put main.py
ampy --port /dev/ttyUSB0 put config.py
# ... (continue for all files)
4๏ธโฃ Hardware Assembly
- Connect all sensors and actuators according to pin mapping in Hardware Components
- Ensure I2C devices (OLED, TSL2561) share SDA/SCL bus
- Use external 5V power for servo motor (not from ESP32)
- Connect ground (GND) common to all components
5๏ธโฃ Power On & Test
# Connect to ESP32 serial console
screen /dev/ttyUSB0 115200
# System will:
# 1. Show NextGarage logo on OLED
# 2. Connect to WiFi (with icon animation)
# 3. Connect to MQTT broker (cloud icon)
# 4. Start main monitoring loop
๐๏ธ System Controls
- Short press gate button: Open gate (when green light)
- Hold master button 5 seconds: System reset
- MQTT commands: See MQTT Integration
๐ Node-RED Dashboard
Setup Node-RED Flow
- Import the flow from
node-red-flow.json(if provided) - Configure MQTT broker node to match ESP32 settings
- Deploy the flow
Dashboard Features
- Real-time gauges: Distance, gas level, ambient light
- Status indicators: Gate position, parking occupancy
- Control buttons: Open/close gate, toggle lights
- Configuration sliders: Adjust sensor thresholds live
- Alarm notifications: Visual alerts for gas detection
Access Dashboard
http://localhost:1880/ui
๐ฏ Design Principles
- Real-time responsiveness: Non-blocking main loop with time-based updates
- Robust sensor filtering: Multi-stage filtering prevents false readings
- Safety-first logic: Gate never closes on obstacles, exit has priority
- Modular architecture: Clear separation between sensors, actuators, and logic
- Remote configurability: All critical parameters adjustable via MQTT
- Graceful error handling: System recovers from sensor/network failures
- Visual feedback: OLED provides clear status with custom graphics
๐ Language & Naming Conventions
The project is developed for the Internet of Things course at the University of Salerno (Universitร degli Studi di Salerno, Italy).
-
Business terminology and UI labels are in Italian (e.g. Sbarra, Posto, Parcheggio), following the course language and local context.
-
Source code identifiers (class names, method names, variables) are in English, following standard programming conventions (SmartParking, ServoGate, MQTTHandler, etc.).
This approach maintains international code readability while keeping the interface aligned with the Italian academic environment.
๐ License
This project is developed for educational purposes as part of the IoT course at University of Salerno.
๐บ Team Members
- โฝ Christian Salvatore Bove
- โฝ Andrea Botta
- โฝ Cosimo Rivellini
๐ธ Project Images
OLED Display Screens
- Main status screen with gate/parking/gas/light info
- Parking assistance with distance bar
- Gas alarm with warning icon
- WiFi/MQTT connection animations
Node-RED Dashboard
๐ฎ Future Enhancements
- Multi-spot support: Scale to multiple parking spaces
- License plate recognition: Camera integration with OCR
- Payment system: Integration with payment gateways
- Mobile app: Native iOS/Android control application
- Database logging: Historical data storage and analytics
- Energy monitoring: Track power consumption
- Weather integration: Automatic light/heat adjustments
Happy Parking! ๐โจ