Free tech learning for everyone, from basics to pro

IoT ProjectsIoT SecurityIntermediate

Project: a home security door/window sensor with alerts

In short: This project uses a magnetic reed switch and an ESP32 to report when a door or window opens. The board sleeps almost all the time, waking on the switch or on an hourly timer to send a heartbeat. The security lesson is that a sensor which goes quiet is as important as one that reports an opening.

What you will build and what you need

You will build a contact sensor that sends open and closed events to your MQTT broker, plus an hourly heartbeat with the battery voltage. A home automation system such as Home Assistant or Node-RED turns events into phone notifications. This is a learning project; it is not a replacement for a certified, professionally monitored alarm.

PartNotes
ESP32 board with low sleep currentBoards without a power LED and USB chip always on are best for batteries
Reed switch and magnetSurface-mount door contacts are ready-made and tidy
100 kilohm resistorExternal pull-up so the input is stable in deep sleep
Battery packA lithium cell with a suitable regulator, or 3 x AA with a low-dropout regulator
Two resistors for a voltage dividerTo measure battery voltage on an ADC pin
Small enclosureKeeps out dust and curious fingers

Design

Door sensor event pathReed switchmagnet movesESP32 wakesfrom deepsleepPublishMQTT, QoS 1Automationrule checksmodeAlertphonenotification
Figure 1. Opening the door wakes the sleeping board, which reports the event; your automation decides whether to alert.

Fit the reed switch to the frame and the magnet to the door. Wire the switch between an RTC-capable GPIO (for example GPIO 33) and ground, with the pull-up resistor to 3.3 V. With the door closed, the magnet closes the switch and the pin reads low. When the door opens, the switch opens and the pin goes high, which wakes the ESP32 through wake_on_ext0. A timer wake every hour sends a heartbeat so the system knows the sensor is alive.

Build steps

  1. Prototype on a breadboard and confirm the pin value changes as you move the magnet.
  2. Reuse the Wi-Fi, TLS and secrets.py approach from the temperature monitor.
  3. Upload the code below, then test open, close and heartbeat messages with mosquitto_sub.
  4. Create an automation: notify when open arrives while the house is in "away" mode, and notify when no heartbeat arrives for 90 minutes.
  5. Measure sleep current with a multimeter and estimate battery life before final installation.
import machine, esp32, time
from machine import Pin, ADC
from net import publish          # your Wi-Fi + MQTT/TLS helper from the earlier project

door = Pin(33, Pin.IN)            # external pull-up fitted
batt = ADC(Pin(35)); batt.atten(ADC.ATTN_11DB)
BASE = "home/hall/door01/"

def battery_volts():
    return round(batt.read_uv() * 2 / 1_000_000, 2)   # divider halves the voltage

if machine.wake_reason() == machine.EXT0_WAKE:
    publish(BASE + "state", "open", retain=True)
    while door.value() == 1:      # wait for the door to close again
        time.sleep(1)
    publish(BASE + "state", "closed", retain=True)
else:
    state = "open" if door.value() else "closed"
    publish(BASE + "heartbeat", str(battery_volts()))
    publish(BASE + "state", state, retain=True)

esp32.wake_on_ext0(pin=door, level=esp32.WAKEUP_ANY_HIGH)
machine.deepsleep(3_600_000)      # wake again in one hour for a heartbeat

Connecting to Wi-Fi from deep sleep takes a few seconds, so expect a short delay before the alert. A retained state message lets a dashboard show the current position immediately.

Threats and security checklist

Threats to a DIY door sensorJammingradio blockedFlat batterysilent failureTamperingmagnet or coverSpoofingfake "closed"Lost Wi-Fino alert sentBroker downsingle point
Figure 2. Most failures are silent, so the design must detect missing reports, not just open doors.
  • Supervise the sensor: alert on a missing heartbeat and on low battery, not only on "open".
  • Encrypt and authenticate with TLS and per-device MQTT credentials so nobody on the network can publish a fake "closed".
  • Restrict topics: the sensor can write only to home/hall/door01/#.
  • Tamper resistance: hide wiring, and consider a second switch on the enclosure lid.
  • Avoid single points of failure: keep the broker and router on a small UPS.
  • Protect privacy: door events reveal when you are home; keep them local rather than in a public cloud if you can.

Practice

Why does the design send an hourly heartbeat?
So the automation can detect a silent failure, such as a flat battery, lost Wi-Fi or jamming. No heartbeat for longer than expected raises an alert.
Why is an external pull-up resistor used instead of relying only on the internal one?
It keeps the input at a defined level during deep sleep, when internal pull-ups on many pins are not maintained, preventing false wake-ups.
How do you stop someone on your network from publishing a fake 'closed' message?
Require per-device authentication and TLS on the broker and use ACLs so only that sensor's credentials can publish to its topic.

Key takeaways

  • A reed switch plus an ESP32 in deep sleep makes a long-life contact sensor.
  • Wake on the switch for events and on a timer for heartbeats.
  • Alert on missing heartbeats and low battery, not just on openings.
  • Use TLS, unique credentials and topic ACLs to stop spoofed messages.
  • A DIY sensor is a learning tool, not a certified alarm system.
Tracks your learning on this device only. See your progress on the learning paths.

About the author

Mohamed Fayas

Founder of FM TecHub. CCNA certified, with a BTEC HND in Network Engineering, and BEng (Hons) in Computer Networking and Cloud Security. He writes practical, plain-English guides on networking, security and cloud.

More about the author

Original content written by Mohamed Fayas for FM TecHub. © 2026 FM TecHub. You're welcome to share a link to this page; please don't copy the text elsewhere without permission.

Stay up to date by email

New notes, tools and security news from FM TecHub, explained simply. Free.

We only use your email to send these updates. See our privacy policy.