This project is a real-time motion detection system using an ESP8266 microcontroller, MQTT protocol, and a React + Vite frontend for displaying motion events in real-time. It involves sending motion detection data from an ESP8266 device, publishing the data to an MQTT broker, and displaying these events in a web-based dashboard.
- Overview
- Features
- Architecture and System Design
- Prerequisites
- Setup Instructions
- Working
- Future Enhancements
- License
The system consists of three major parts:
- ESP8266 Microcontroller: Detects motion using a PIR sensor and sends the motion data to an MQTT broker.
- Backend Server: A Node.js server using MQTT to capture and store motion events in a MongoDB database.
- React + Vite Frontend: A web interface that subscribes to the MQTT broker and displays motion events in real-time.
- Real-time motion detection using MQTT protocol.
- PIR sensor connected to ESP8266 to detect motion events.
- Web-based dashboard to view live motion logs.
- Backend powered by Node.js and MongoDB to store motion logs.
- Highly responsive interface with real-time updates using MQTT and WebSockets.
The system architecture is designed to facilitate real-time communication between the motion detection hardware and the user interface. Below is an overview of the architecture:
-
ESP8266 Microcontroller:
- Motion Detection: The ESP8266 is connected to a PIR motion sensor. It continuously monitors for motion.
- MQTT Publish: When motion is detected, it publishes a message to the MQTT broker on the topic
motion-detection/events
. - MQTT Subscribe: Optionally, it can subscribe to topics if you wish to send commands from the backend or frontend to the ESP8266.
-
MQTT Broker:
- Acts as a message broker that routes messages between publishers (ESP8266) and subscribers (Backend and Frontend).
- Facilitates real-time communication using the publish/subscribe pattern.
-
Backend Server (Node.js):
- MQTT Subscribe: Subscribes to the
motion-detection/events
topic to receive motion events. - Data Processing: Upon receiving a motion event, it processes and stores the event in the MongoDB database.
- API Endpoints: Provides RESTful API endpoints (e.g.,
/api/motion-events
) for fetching stored motion events. - Optional MQTT Publish: Can publish messages back to the MQTT broker if needed (e.g., to control devices).
- MQTT Subscribe: Subscribes to the
-
MongoDB Database:
- Stores motion event logs with timestamps and motion status.
- Enables persistent storage and retrieval of historical data.
-
React + Vite Frontend:
- MQTT Subscribe: Subscribes directly to the
motion-detection/events
topic to receive real-time updates. - Real-Time UI Update: Updates the user interface immediately when a new motion event is received.
- HTTP GET Requests: Can fetch historical motion events from the backend via API endpoints for display or analysis.
- MQTT Subscribe: Subscribes directly to the
- Motion Event Detection:
- The PIR sensor detects motion and the ESP8266 publishes a message to the MQTT broker.
- Real-Time Updates:
- Both the backend server and the frontend application are subscribed to the MQTT broker.
- They receive the motion event in real-time.
- Data Storage:
- The backend processes the motion event and stores it in the MongoDB database.
- User Interface Update:
- The frontend updates the motion log display immediately upon receiving the event.
- Historical Data Access:
- The frontend can request historical data from the backend via HTTP GET requests to display past motion events.
- Efficiency: MQTT is a lightweight messaging protocol ideal for small sensors and mobile devices.
- Real-Time Communication: Enables instant data transfer with minimal overhead.
- Scalability: Supports multiple subscribers and publishers, allowing for easy system expansion.
- Decoupled Architecture: Publishers and subscribers are independent, enhancing system flexibility.
- ESP8266: Wi-Fi enabled microcontroller used for IoT applications.
- PIR Sensor: Passive Infrared Sensor for detecting motion.
- MQTT Protocol: Messaging protocol for IoT devices.
- Node.js: Server-side JavaScript runtime for the backend server.
- MongoDB: NoSQL database for storing motion events.
- React + Vite: Frontend framework and tooling for building the user interface.
Before starting the setup, make sure you have the following installed:
- Node.js and npm
- MongoDB database
- Arduino IDE (for ESP8266 programming)
- MQTT Broker (e.g., HiveMQ, Mosquitto)
- React + Vite for frontend development
-
Hardware Requirements:
- ESP8266 microcontroller
- PIR sensor
- Jumper wires
- Breadboard
-
Pseudocode for ESP8266:
- Initialize the Wi-Fi connection.
- Connect to the MQTT broker using the provided credentials.
- Continuously check for motion using the PIR sensor:
- If motion is detected:
- Publish a message to the
motion-detection/events
topic indicating motion detected.
- Publish a message to the
- Else, publish a message indicating no motion.
- If motion is detected:
- Repeat the loop with a small delay to avoid spamming the network.
-
Connections:
- Connect the PIR sensor's VCC, GND, and OUT to the ESP8266's 3.3V, GND, and D2 pin, respectively.
-
MQTT Broker:
- Use a public MQTT broker like
broker.hivemq.com
or set up your own broker.
- Use a public MQTT broker like
-
ESP8266:
- Detects motion using a PIR sensor and publishes data to an MQTT broker (
motion-detection/events
topic).
- Detects motion using a PIR sensor and publishes data to an MQTT broker (
-
Backend:
- Subscribes to the MQTT topic, listens for incoming messages, and stores the motion events in a MongoDB database.
-
Frontend:
- Subscribes to the MQTT broker and listens for real-time motion events, displaying them immediately in the web UI.
- Implement user authentication to secure access to motion events.
- Visualize motion detection data over time using charts and graphs.
- Add support for multiple sensors and more complex event handling.
This project is licensed under the MIT License.