Project Objective:
Detect motion with an ESP32 and send a wireless alert to a Raspberry Pi.
The Raspberry Pi will light up an alarm LED and print a message to the terminal.
🧰 Hardware Requirements:
Component Quantity Description
ESP32 Board 1 Wi-Fi enabled microcontroller
PIR Motion Sensor 1 For motion detection
Raspberry Pi (any model with Wi- 1 Acts as server
Fi)
LED (red recommended) 1 Alarm indicator
Resistor (220Ω or 330Ω) 1 For LED
Breadboard + Jumper Wires — For wiring
Wi-Fi Network 1 Shared by both devices
🔌 Circuit Wiring:
ESP32 with PIR Sensor:
PIR Sensor Connect To
Pin
VCC 3.3V on ESP32
GND GND on ESP32
OUT GPIO13 (example)
Raspberry Pi with LED:
LED Pin Connect To
Long leg (+) GPIO17 through 220Ω resistor
Short leg (–) GND pin on Raspberry Pi
📝 ESP32 Code (Motion Sensor Client)
Upload this code to your ESP32 using Arduino IDE:
#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "YOUR_WIFI_NAME";
const char* password = "YOUR_WIFI_PASSWORD";
const char* serverURL = "http://YOUR_PI_IP:5000/motion"; // Replace with actual Pi IP
#define PIR_PIN 13
void setup() {
pinMode(PIR_PIN, INPUT);
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to WiFi.");
}
void loop() {
if (digitalRead(PIR_PIN) == HIGH) {
Serial.println("Motion detected!");
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(serverURL);
http.addHeader("Content-Type", "application/json");
http.POST("{\"motion\": true}");
http.end();
}
delay(5000); // Avoid multiple alerts
}
}
🔍 ESP32 Code Breakdown:
Connects to your Wi-Fi.
Monitors PIR sensor (GPIO13).
If motion is detected, sends an HTTP POST request to the Raspberry Pi server with JSON data:
{ "motion": true }
🧾 Raspberry Pi Python Code (Flask Server)
Install Flask first:
sudo apt update
pip3 install flask
Then create a Python script named alarm_server.py:
from flask import Flask, request
import RPi.GPIO as GPIO
import time
from datetime import datetime
app = Flask(__name__)
# Configure GPIO pin for alarm LED
LED_PIN = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(LED_PIN, GPIO.OUT)
GPIO.output(LED_PIN, GPIO.LOW)
@app.route('/motion', methods=['POST'])
def motion_detected():
data = request.get_json()
if data and data.get('motion'):
print("🚨 Motion detected at:", datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
GPIO.output(LED_PIN, GPIO.HIGH) # Turn LED ON
time.sleep(5) # Keep it ON for 5 seconds
GPIO.output(LED_PIN, GPIO.LOW) # Turn LED OFF
return {"status": "alarm triggered"}, 200
return {"status": "invalid request"}, 400
if __name__ == '__main__':
try:
app.run(host='0.0.0.0', port=5000)
finally:
GPIO.cleanup()
🔍 Raspberry Pi Code Breakdown:
Sets up a web server using Flask that listens for POST requests on /motion.
When motion is detected (from ESP32), it:
o Turns on the LED (connected to GPIO17).
o Prints a timestamped alert to the terminal.
o Waits 5 seconds, then turns off the LED.
🌐 Test the Communication:
1. Connect both devices to the same Wi-Fi.
2. Get the IP address of your Raspberry Pi:
hostname -I
3. Replace YOUR_PI_IP in the ESP32 code with the Raspberry Pi IP.
4. Upload and run the ESP32 code.
5. Run the server on the Pi:
python3 alarm_server.py
Now when motion is detected, you should:
See a terminal message on the Raspberry Pi.
The LED alarm should light up for 5 seconds.