This Repo contains the firmware code for RoomSpy in ./esp.ino, a room occupancy monitoring system using the HC-SR501 Motion Sensor and ESP32 microcontroller. The system detects motion, determines room occupancy status, and communicates with a backend API over WiFi.
- ESP32 microcontroller
- HC-SR501 Motion Sensor
- LEDs for status indication:
- Built-in LED (WiFi connection indicator)
- External LED for error indication
- Power supply and wires for connections
- Motion Detection: The HC-SR501 sensor detects motion and triggers an interrupt.
- Room Occupancy Logic:
- Tracks motion events in a rolling time window (default: 10 entries).
- Considers the room "occupied" if at least one motion event occurs within the last minute.
- WiFi Communication:
- Connects to a specified WiFi network.
- Sends room occupancy status updates to a backend API in JSON format.
- Status Indication:
- Built-in LED blinks during WiFi connection and stays solid when connected.
- Error LED blinks if a failure occurs while sending data or reconnecting WiFi.
- Connect the HC-SR501 sensor to the ESP32:
- VCC: 5V
- GND: GND
- OUT: GPIO 34
- Connect an external error LED to GPIO 12.
- Power the ESP32.
- Update the WiFi credentials in the code:
const char* ssid = "[WIFI SSID]"; // Your WiFi SSID const char* password = "[WIFI PASSWORD]"; // Your WiFi password
- Replace the backend API URL:
const char* serverUrl = "https://[BACKEND API URL]";
- Customize the room name if needed:
const char* RoomName = "Damietta";
- Open the code in the Arduino IDE or your preferred development environment.
- Select the correct board (ESP32) and port.
- Compile and upload the code to the ESP32.
- The system automatically connects to WiFi on boot.
- When motion is detected, the system:
- Updates the rolling motion timestamps.
- Checks and updates the room's occupancy status.
- Sends the status to the backend API if there's a change or periodically (every minute).
- The built-in LED indicates WiFi connection status.
- The error LED blinks if communication with the backend API fails.
void IRAM_ATTR handleMotion() {
motionDetected = true;
}
void updateRoomStatus() {
bool occupied = motionCount >= 1; // Detects occupancy based on motion within the window
if (occupied != roomOccupied) {
roomOccupied = occupied;
sendStatusData(roomOccupied);
}
}
void sendStatusData(bool occupied) {
String payload = "{\"RoomName\": \"" + String(RoomName) + "\", \"occupied\": " + String(occupied ? "true" : "false") + ", \"timestamp\": " + String(millis()) + "}";
// Sends the data using HTTP POST
}
- Rolling Window Size: Adjust
motionWindowSize
to modify the number of motion events tracked. - Occupancy Threshold: Change the logic in
updateRoomStatus
to define custom thresholds for occupancy.
- No WiFi Connection:
- Verify the SSID and password.
- Ensure the WiFi network is active.
- No API Updates:
- Check the
serverUrl
value. - Ensure the backend API is reachable.
- Check the
- Unexpected Behavior:
- Check the serial monitor for debugging messages.
- Verify the motion sensor's wiring and orientation.
This project is open-source. Feel free to modify and redistribute as needed.
Enjoy monitoring your room with RoomSpy! π