A real-time IoT factory monitoring system with virtual machine simulation, ESP32 sensor integration, and predictive maintenance capabilities.
- Real-time Monitoring: WebSocket-based live data streaming
- Virtual Machine Simulation: Simulates 5 factory machines with health degradation
- Physical Sensor Integration: ESP32 support with DHT22 (temperature/humidity) and ADXL335 (vibration) sensors
- Predictive Maintenance: Health tracking and failure prediction
- RESTful API: Machine control endpoints (start/stop/maintenance)
- Interactive Dashboard: Real-time React-based web interface
- System Architecture
- Prerequisites
- Installation
- Configuration
- Running the Application
- ESP32 Setup
- API Endpoints
- Project Structure
- Testing
- Development
- Troubleshooting
βββββββββββββββββββββββ
β React Dashboard β β Real-time UI updates via WebSocket
ββββββββββββ¬βββββββββββ
β
ββββββββΌβββββββββββ
β FastAPI Server β β Python backend with WebSocket support
ββββββββ¬βββββββββββ
β
ββββββΌβββββ
βSimulatorβ β Virtual machine simulation engine
ββββββ¬βββββ
β
ββββββββΌβββββββββ
β ESP32 Nodes β β Physical sensors (DHT22 + ADXL335)
βββββββββββββββββ
- Python: 3.9 or higher
- Node.js: Not required (React is loaded via CDN)
- PlatformIO: For ESP32 programming (optional)
- Git: For version control
- ESP32 Development Board
- DHT22 Temperature/Humidity Sensor
- ADXL335 Accelerometer
- Jumper wires and breadboard
git clone <repository-url>
cd smart-factory-simulator# Create virtual environment
python -m venv venv
# Activate virtual environment
# On Linux/Mac:
source venv/bin/activate
# On Windows:
venv\Scripts\activatepip install -r requirements.txtCreate a .env file in the project root:
# Server Configuration
HOST=0.0.0.0
PORT=8000
DEBUG=True
# CORS Settings
CORS_ORIGINS=http://localhost:8000,http://127.0.0.1:8000
# Redis Configuration (if needed)
REDIS_URL=redis://localhost:6379/0Edit config/settings.py to customize:
- Server host and port
- CORS origins
- Redis connection
- Application metadata
Edit app/simulator.py to adjust:
- Number of machines (
num_machines=5) - Health degradation rates
- Temperature/vibration ranges
- Update intervals
# Make sure virtual environment is activated
source venv/bin/activate # or venv\Scripts\activate on Windows
# Run the application
python -m app.main
# Or using uvicorn directly
uvicorn app.main:app --host 0.0.0.0 --port 8000 --reloadOpen your browser and navigate to:
http://localhost:8000
The dashboard will automatically connect to the WebSocket and start receiving real-time updates.
DHT22 Pin β ESP32 Pin
VCC β 3.3V
DATA β GPIO4
GND β GND
ADXL335 Pin β ESP32 Pin
VCC β 3.3V
X_OUT β GPIO34
Y_OUT β GPIO35
Z_OUT β GPIO32
GND β GND
-
Install PlatformIO
pip install platformio
-
Navigate to ESP32 project
cd esp32_factory_sensor -
Update Configuration Edit
src/main.cppand update:const char* ssid = "YOUR_WIFI_SSID"; const char* password = "YOUR_WIFI_PASSWORD"; const char* websocket_server = "YOUR_SERVER_IP"; // e.g., "192.168.1.100"
-
Build and Upload
# Build the project pio run # Upload to ESP32 (connect via USB) pio run --target upload # Monitor serial output pio device monitor
To run multiple sensors:
- Change
machine_idinsrc/main.cppfor each ESP32 - Flash different boards with unique IDs
- All will connect to the same WebSocket server
GET /api/start/{machine_id}Response:
{
"status": "success"
}GET /api/stop/{machine_id}Response:
{
"status": "success"
}GET /api/maintenance/{machine_id}Response:
{
"status": "success"
}ws://localhost:8000/ws
{
"timestamp": "2024-11-15T10:30:00.000000",
"machines": [
{
"id": 0,
"type": "CNC",
"status": "running",
"health": 95.5,
"temperature": 32.1,
"vibration": 0.23,
"operating_hours": 120.5,
"last_maintenance": "2024-11-15T08:00:00.000000"
}
]
}smart-factory-simulator/
βββ app/
β βββ __init__.py
β βββ main.py # FastAPI application entry point
β βββ simulator.py # Virtual machine simulation logic
βββ config/
β βββ __init__.py
β βββ settings.py # Configuration management
β βββ logging.py # Logging configuration
βββ esp32_factory_sensor/
β βββ platformio.ini # PlatformIO configuration
β βββ src/
β βββ main.cpp # ESP32 firmware
βββ static/
β βββ index.html # Frontend HTML
β βββ js/
β βββ app.jsx # React dashboard component
βββ tests/
β βββ test_api.py # API endpoint tests
β βββ test_simulator.py # Simulator unit tests
βββ .env # Environment variables (create this)
βββ requirements.txt # Python dependencies
βββ README.md # This file
# Run all tests
pytest
# Run with coverage
pytest --cov=app tests/
# Run specific test file
pytest tests/test_simulator.py
# Run with verbose output
pytest -v# Using wscat (install: npm install -g wscat)
wscat -c ws://localhost:8000/ws- Update
VirtualMachineclass inapp/simulator.py - Add new sensor properties and update logic
- Modify frontend in
static/js/app.jsxto display new metrics
Edit static/js/app.jsx:
// Add new metrics, charts, or controls
// Uses React, TailwindCSS for stylingEdit app/main.py:
@app.get("/api/your-endpoint")
def your_endpoint():
# Your logic here
return {"status": "success"}# Enable debug logging
export DEBUG=True
python -m app.mainProblem: Dashboard shows "Connecting to factory..." Solution:
- Ensure backend is running:
http://localhost:8000 - Check browser console for errors
- Verify firewall settings
Problem: ESP32 can't connect to WiFi/WebSocket Solution:
- Verify WiFi credentials in
main.cpp - Check server IP address (use your machine's local IP, not localhost)
- Ensure both devices are on the same network
- Check serial monitor for error messages:
pio device monitor
Problem: ModuleNotFoundError
Solution:
# Reinstall dependencies
pip install -r requirements.txt
# Or install specific package
pip install fastapi uvicorn websocketsProblem: Address already in use
Solution:
# Find process using port 8000
lsof -i :8000 # Linux/Mac
netstat -ano | findstr :8000 # Windows
# Kill the process or change port in .env
PORT=8001- Check the logs in the terminal
- Review browser console (F12) for frontend errors
- Verify all dependencies are installed
- Ensure Python 3.9+ is being used
- Machine Health: 0-100% scale
- Temperature: Celsius (Β°C)
- Vibration: G-force magnitude
- Operating Hours: Cumulative runtime
- Status: idle, running, error
# Health decreases based on:
- Operating time
- Random wear and tear
- Temperature deviation from optimal
- Vibration levels- Historical data storage (database integration)
- Predictive analytics with machine learning
- Alert notifications (email/SMS)
- Multi-factory support
- User authentication
- Data export (CSV/Excel)
- Mobile app support
- Advanced charting with historical trends
This project is licensed under the MIT License - see the LICENSE file for details.
- Yama Narendra Reddy - Initial Development
- FastAPI for the excellent web framework
- ESP32 community for hardware support
- React for the frontend library
Last Updated: November 15, 2024
Version: 1.0.0