A full-stack Railway Level Crossing Safety System that integrates ESP32 hardware sensors, a Flask backend, MongoDB, and a modern web dashboard to monitor trains, control gates, and log safety events in real time.
This system automates railway gate operations using ultrasonic sensors and an ESP32, while providing a secure web dashboard for railway and government officers.
- Prevent accidents at railway level crossings
- Automate gate control based on train movement
- Detect obstacles during gate operation
- Maintain complete activity and sensor history
- Allow authorized manual gate override in emergencies
ESP32 + Sensors ↓ Flask REST API ↓ MongoDB Database ↓ Web Dashboard (HTML, CSS, JS)
- Secure login/signup using JWT
- Password hashing with Werkzeug
- Roles:
- Government Officer
- Railway Officer
- Auto-generated Badge ID
- Detects:
- Train arrival
- Obstacles on track
- Train departure
- Gate behavior:
- Slow close for safety
- Half-close if obstacle detected
- Resume closing after obstacle clears
- Slow open after train departure
- Real-time sensor monitoring
- Gate override controls (manual)
- User profile & role display
- System health indicators
- Sensor logs from ESP32
- Manual gate override logs
- Timestamped and stored permanently in MongoDB
- Python
- Flask
- Flask-CORS
- PyJWT
- MongoDB (PyMongo)
- HTML5
- CSS3 (Glassmorphism UI)
- JavaScript (Vanilla JS)
- Responsive Design
- ESP32
- Ultrasonic Sensors (HC-SR04)
- Servo Motor
- ESP32Servo Library
Database: railway_system
users– user accounts & rolessensors– latest live sensor valueshistory– permanent event logs
| Method | Endpoint | Description |
|---|---|---|
| POST | /signup |
Create new user |
| POST | /login |
Login & get JWT |
| GET | /status | Server & MongoDB status |
| POST | /sensor/update | ESP32 sensor data |
| GET | /sensor/live | Live sensor data (JWT protected) |
| POST | /gate/log | Manual gate action logging |
| GET | /history | Last 50 system logs (JWT protected) |
pip install flask flask-cors pymongo pyjwt werkzeug
python app.py
Ensure MongoDB is running locally:
mongod
2️⃣ Frontend Setup
Open index.html in browser
Ensure backend runs at:
http://127.0.0.1:5000
3️⃣ ESP32 Setup
Install ESP32Servo library
Connect:
Servo → Pin 13
Ultrasonic Sensors → Pins as defined in code
Upload the ESP32 .ino code
Open Serial Monitor (115200 baud)
🧪 Testing Flow
Signup & Login from dashboard
ESP32 sends sensor data
Gate auto-closes on train arrival
Obstacle triggers half-close
Gate opens after train departure
All actions logged in History tab
🔐 Security Highlights
JWT-based API protection
Password hashing
Role-based UI access
Session auto-logout on tab close
🚀 Future Enhancements
Cloud deployment (AWS / Railway / Render)
WebSockets for real-time updates
SMS/Email alerts
AI-based train detection (camera)
Multi-crossing support