Project Overview
This project creates a smart mailbox notifier that alerts you when mail is delivered. A PIR sensor detects motion inside the mailbox when the mail carrier opens the door or inserts mail. An ESP32 sends a notification to your phone via Wi-Fi using the Blynk platform.
Difficulty: Intermediate
Estimated time: 2-3 hours
Estimated cost: $20-30
How It Works
A PIR sensor placed inside the mailbox detects motion when the door is opened or when mail is inserted. The ESP32 connects to your Wi-Fi and sends a notification to the Blynk app on your phone. You can also add an LED indicator to show notification status and a reset button to clear the alert after checking mail.
Materials Needed
- ESP32 development board (1)
- HC-SR501 PIR sensor (1) or AM312 (for lower power)
- LED (any color, for status indication)
- Resistor (220Ω for LED)
- Push button (for resetting alert)
- Battery pack (3xAA or 18650 lithium battery) or USB power
- TP4056 charging module (if using lithium battery)
- Jumper wires
- Waterproof enclosure (for outdoor installation)
- Micro-USB cable (for programming)
Circuit Diagram
Connection Table
| Component | Pin | ESP32 Pin |
|---|---|---|
| PIR Sensor | VCC | 3.3V or VIN (5V) |
| PIR Sensor | GND | GND |
| PIR Sensor | OUT | GPIO 4 |
| LED | Anode | GPIO 5 (through 220Ω resistor) |
| LED | Cathode | GND |
| Push Button | One pin | GPIO 0 (with 10k pull-up) |
| Push Button | Other pin | GND |
Blynk Setup
- Download the Blynk IoT app from your app store.
- Create a new account or log in.
- Create a new template for your mailbox notifier.
- Add a Notification widget to your dashboard.
- Note your BLYNK_TEMPLATE_ID and BLYNK_TEMPLATE_NAME from the template settings.
- Generate an auth token for your device.
Arduino Code
// Smart Mailbox Notifier with ESP32 and Blynk
// Blynk IoT platform required
#define BLYNK_TEMPLATE_ID "YourTemplateID"
#define BLYNK_TEMPLATE_NAME "Mailbox Notifier"
#define BLYNK_AUTH_TOKEN "YourAuthToken"
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
// Wi-Fi credentials
char ssid[] = "YourWiFiSSID";
char pass[] = "YourWiFiPassword";
// Pin definitions
const int pirPin = 4;
const int ledPin = 5;
const int resetButtonPin = 0;
// Variables
bool mailDelivered = false;
unsigned long lastMotionTime = 0;
const unsigned long debounceDelay = 2000; // 2 seconds debounce
// Blynk virtual pin for resetting from app
BLYNK_WRITE(V0) {
if (param.asInt() == 1) {
resetAlert();
}
}
void resetAlert() {
mailDelivered = false;
digitalWrite(ledPin, LOW);
Serial.println("Alert reset");
Blynk.virtualWrite(V1, 0); // Update app status
}
void sendNotification() {
if (!mailDelivered) {
mailDelivered = true;
digitalWrite(ledPin, HIGH);
Serial.println("Mail delivered! Sending notification...");
// Send Blynk notification
Blynk.logEvent("mail_delivered", "Mail has been delivered to your mailbox!");
// Update app status
Blynk.virtualWrite(V1, 1); // 1 = mail waiting
}
}
void setup() {
Serial.begin(115200);
// Initialize pins
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(resetButtonPin, INPUT_PULLUP);
digitalWrite(ledPin, LOW);
// Connect to Blynk
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
Serial.println("Smart Mailbox Notifier Ready");
Serial.println("Waiting 60 seconds for PIR warm-up...");
delay(60000);
}
void loop() {
Blynk.run();
// Check reset button
if (digitalRead(resetButtonPin) == LOW) {
delay(50); // Debounce
if (digitalRead(resetButtonPin) == LOW) {
resetAlert();
while (digitalRead(resetButtonPin) == LOW) {
delay(10);
}
}
}
// Check PIR sensor
bool motionDetected = digitalRead(pirPin) == HIGH;
if (motionDetected) {
unsigned long now = millis();
if (now - lastMotionTime > debounceDelay) {
lastMotionTime = now;
Serial.println("Motion detected in mailbox");
sendNotification();
}
}
delay(100);
}
Alternative: Using ESP-NOW for Long Battery Life
If your mailbox is far from Wi-Fi range or you need longer battery life, use ESP-NOW protocol to communicate between two ESP32 boards:
// Sender code (mailbox unit) - ESP-NOW
#include <esp_now.h>
#include <WiFi.h>
uint8_t receiverAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; // Replace with receiver MAC
const int pirPin = 4;
bool lastState = false;
void sendMotionAlert() {
esp_now_send(receiverAddress, (uint8_t *)"MOTION", 6);
Serial.println("Motion alert sent");
}
void setup() {
Serial.begin(115200);
pinMode(pirPin, INPUT);
WiFi.mode(WIFI_STA);
if (esp_now_init() != ESP_OK) {
Serial.println("ESP-NOW init failed");
return;
}
esp_now_register_send_cb([](uint8_t* mac, esp_now_send_status_t status) {
Serial.print("Send status: ");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Success" : "Fail");
});
esp_now_peer_info_t peerInfo;
memcpy(peerInfo.peer_addr, receiverAddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
esp_now_add_peer(&peerInfo);
Serial.println("Sender ready");
delay(60000); // PIR warm-up
}
void loop() {
bool motion = digitalRead(pirPin) == HIGH;
if (motion && !lastState) {
sendMotionAlert();
}
lastState = motion;
delay(100);
}
Installation Steps
- Prepare the mailbox: Clean the interior of the mailbox where the sensor will be placed. Ensure the sensor will have a clear view of the door opening area.
- Assemble the circuit: Connect components on a breadboard and test with USB power.
- Configure Blynk: Set up your Blynk template and note the auth token.
- Update code: Enter your Wi-Fi credentials and Blynk auth token.
- Upload code: Connect ESP32 to computer and upload the code.
- Test indoors: Verify notifications are received when motion is detected.
- Place in enclosure: Put the ESP32 and battery in a waterproof enclosure. Drill a small hole for the PIR sensor window.
- Mount in mailbox: Attach the sensor and enclosure inside the mailbox, ensuring the sensor faces the door area.
- Final test: Open the mailbox door and verify notification is received.
Power Options
Option 1: USB Power
If your mailbox has a nearby power outlet, use a USB adapter and long cable. Ensure the cable is rated for outdoor use and protected from weather.
Option 2: Battery Power with Deep Sleep
For battery operation, modify the code to use ESP32 deep sleep mode:
// Deep sleep code snippet
esp_sleep_enable_ext0_wakeup(GPIO_NUM_4, 1); // Wake on PIR HIGH
esp_deep_sleep_start();
With deep sleep, battery life can extend to 6-12 months on 3xAA batteries.
Troubleshooting
- No Wi-Fi connection: Check credentials and ensure ESP32 is within range of your router. Consider adding an external antenna if mailbox is far.
- No notifications: Verify Blynk auth token and template ID. Check that notification widget is configured correctly.
- False triggers: Adjust PIR sensitivity. Ensure sensor is not facing direct sunlight or heat sources. Consider using a pet-immune lens if animals access the mailbox area.
- Battery drains quickly: Implement deep sleep mode. Use AM312 sensor (35µA) instead of HC-SR501 (50µA).
Project Extensions
- Mailbox open indicator: Add a magnetic reed switch to detect if mailbox door is left open.
- Temperature sensor: Add a DS18B20 to monitor temperature inside the mailbox.
- Camera integration: Add an ESP32-CAM to capture images when mail is delivered.
- LED notification inside home: Add an RGB LED that changes color when mail arrives.
- Google Assistant integration: Use IFTTT or Blynk to announce mail arrival on Google Home devices.
Conclusion
This smart mailbox notifier eliminates the need to check the mailbox unnecessarily. With notifications sent directly to your phone, you’ll know exactly when mail arrives.
