Get a phone alert when post arrives

A tilt switch on the mailbox flap tells the ESP32 when the post comes. You get a notification, and the app counts deliveries and shows the time of the last one.
  • Level:Advanced
  • Duration: 55 min
  • Ages 13+
  • Platform: ESP32

Tools for this lesson

Helpful for this build. Open a tool to see tips and more projects that use it.

Get a phone alert when post arrives – EEMENTOR STEM™ lesson

No more walking to an empty mailbox. In this lesson a tilt switch sits on the inside of the mailbox flap. When the postman pushes letters through, the flap tilts, and the ESP32 sends a notification to your phone. The Blynk app shows how many deliveries arrived today and when the last one came, and a reset button clears the count once you've emptied the box.

What you'll learn

  • How a tilt switch detects movement of a flap
  • How to count an event once, even when the flap swings several times
  • How to reset a count from the app
  • What to think about when a device is far from the router

What you need

  • ESP32 board, expansion board and USB cable (ESP32 IoT Starter Kit)
  • Tilt switch module (45-in-1 Sensor Kit)
  • A free Blynk account and the Blynk IoT app on a phone
  • A 2.4 GHz Wi-Fi network that reaches your mailbox, and a USB power bank or power supply
  • Tape
  • Breadboard and jumper wires (ESP32 IoT Starter Kit)

Make it real: try it first on a cardboard box with a flap: tape the tilt switch to the inside of the flap so that opening the flap tilts it. On a real mailbox, tape it to the inside of the flap with the wires running along the hinge.

Wiring

Breadboard wiring diagram: Get a phone alert when post arrives
Breadboard view. Check each connection against the wiring table below.
From To
Tilt switch module S ESP32 GPIO 27
Tilt switch module + and − 3V3 and GND

Sensor modules from different makers can label and order their pins differently. Always follow the labels printed on your module.

Before you mount it: check in the Serial Monitor at the mailbox that the ESP32 connects to your Wi-Fi from there. Keep the electronics dry, for example in a plastic food box.

Set up Blynk

In the Blynk console, create a new template called Mailbox (hardware ESP32, connection type Wi-Fi) and add these datastreams. Lesson 1 of this path shows each step in detail.

Virtual pin Name Data type Range Phone widget
V0 Deliveries today Integer 0 – 99 Value Display
V1 Last delivery String n/a Value Display
V2 Emptied Integer 0 – 1 Button (push mode)
V3 Flap Integer 0 – 1 LED

Then open the template's Events & Notifications tab, add an event with the code mail_arrived (name: Post has arrived) and turn on push notifications for it. The event code must match the sketch exactly.

Save the template, create a new device from it and copy its three BLYNK_… lines. In the phone app, open the device, tap the wrench icon and add two Value Displays (V0 and V1), a Button in push mode (V2) and an LED (V3). Link each widget to its datastream.

Example screen for: Get a phone alert when post arrives
Example screen with sample values. Yours may look different.

The sketch

Replace the three BLYNK_… lines at the top with the ones from your device's Device info page, and add your Wi-Fi name and password (2.4 GHz only). The BLYNK_… lines must stay above #include <BlynkSimpleEsp32.h>.

#define BLYNK_TEMPLATE_ID "YOUR_TEMPLATE_ID"
#define BLYNK_TEMPLATE_NAME "Mailbox"
#define BLYNK_AUTH_TOKEN "YOUR_AUTH_TOKEN"
#define BLYNK_PRINT Serial          // show connection messages in the Serial Monitor

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <time.h>

char ssid[] = "YOUR_WIFI_NAME";
char pass[] = "YOUR_WIFI_PASSWORD";
const char* TIME_ZONE = "EET-2EEST,M3.5.0/3,M10.5.0/4";  // Estonia; change for your country

const int FLAP_PIN = 27;
const unsigned long QUIET_TIME = 60000;   // swings within 1 minute count as one delivery

BlynkTimer timer;
int flapRest;
int deliveries = 0;
int today = -1;
unsigned long lastDelivery = 0;
bool flapWasOpen = false;

String timeNow() {
  struct tm t;
  if (!getLocalTime(&t, 10)) return "time not set yet";
  char text[20];
  strftime(text, sizeof(text), "%d.%m %H:%M", &t);
  return String(text);
}

BLYNK_WRITE(V2) {                          // "Emptied" button
  if (param.asInt() == 1) {
    deliveries = 0;
    Blynk.virtualWrite(V0, deliveries);
  }
}

BLYNK_CONNECTED() {
  Blynk.virtualWrite(V0, deliveries);
}

void checkFlap() {
  bool open = digitalRead(FLAP_PIN) != flapRest;
  if (open != flapWasOpen) Blynk.virtualWrite(V3, open);
  flapWasOpen = open;
  if (!open) return;

  if (lastDelivery != 0 && millis() - lastDelivery < QUIET_TIME) return;   // same delivery
  lastDelivery = millis();

  struct tm t;
  if (getLocalTime(&t, 10) && t.tm_yday != today) {
    today = t.tm_yday;                     // a new day: start counting again
    deliveries = 0;
  }
  deliveries++;
  Blynk.virtualWrite(V0, deliveries);
  Blynk.virtualWrite(V1, timeNow());
  Blynk.logEvent("mail_arrived", "Post arrived at " + timeNow());
}

void setup() {
  Serial.begin(115200);
  pinMode(FLAP_PIN, INPUT_PULLUP);
  delay(500);
  flapRest = digitalRead(FLAP_PIN);        // the flap must be closed at start-up
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  configTzTime(TIME_ZONE, "pool.ntp.org");
  timer.setInterval(100L, checkFlap);      // 10 times per second
}

void loop() {
  Blynk.run();
  timer.run();
}

How it works

The tilt switch contains a small metal ball that rolls and closes a contact when the switch is tilted. The sketch stores the reading with the flap closed at start-up and treats any change as "open", so it doesn't matter which way you mount it.

When the postman pushes letters through, the flap swings open and shut several times. Without care, that would send five notifications. The sketch counts only the first movement and then ignores the flap for a quiet time of one minute, so one delivery is one message. The LED widget still follows every movement, so you can test the switch from the app. The count starts again at midnight, and the "Emptied" button sets it back to zero whenever you collect the post.

Try this next

  • Send a second notification if the post hasn't been collected by evening.
  • Count deliveries per week instead of per day.
  • Save power with deep sleep and wake the ESP32 with the tilt switch on a wake-up pin.

Troubleshooting

  • The device shows "Offline" in the app: check the Serial Monitor at 115200 baud. "Invalid auth token" means the BLYNK_AUTH_TOKEN line is wrong; if it never connects, check the Wi-Fi name and password, and that your network is 2.4 GHz.
  • Several notifications for one delivery: raise QUIET_TIME to 120000 (2 minutes).
  • No notification at all: check the event code is exactly mail_arrived, and watch the Flap LED in the app while you open the flap.
  • It reports post with the flap shut: the flap was open at start-up, or the switch is mounted almost upright. Mount it so the flap tilts it clearly.

Build it yourself

Everything used in this lesson comes in one kit, with parts tested by our engineers.

ESP32 IoT Starter Kit | Arduino IDE Compatible | Free Tutorials $57.00

Shop the kit