Door / window sensor

A battery sensor with a reed switch that wakes from deep sleep only when the door opens.

Smart home Board: ESP32 DevKit V1 (WROOM-32) Level: ●○○ ⏱ 1–2 h

⚠ Caution

Batteries: watch polarity, use protected cells and a charger with protection, never charge damaged cells unattended.

Months on one battery, if you are careful

The reed switch closes a circuit with a magnet and wakes the ESP32 from deep sleep, so the chip does nothing until the door moves. The bare chip sleeps at a few microamps, but a typical dev board does not: its voltage regulator and power LED can eat a milliamp. For a real battery device use a bare module or remove the LED and the regulator.

Where the energy actually goes

The big cost is not the sleep, it is connecting to Wi-Fi after the wake-up, one to three seconds at high current. Shorten it by keeping a static IP and the channel and BSSID of the access point. If you have several sensors, ESP-NOW to a single always-on ESP32 gateway sends the message in tens of milliseconds and extends the life a lot.

  • Set the wake level to the opposite state after each event, so you catch both opening and closing.
  • The number of door events per day, not the calendar, determines the life. A front door and a wardrobe are very different.
  • Use a magnet with a gap of a few millimetres; reed switches are sensitive to misalignment.

Components

  • ESP32 DevKit V1 (30 pin, WROOM-32)
  • Reed switch + magnet (door sensor)
  • 18650 Li-ion cell + holder
  • TP4056 USB-C Li-ion charger with protection

Wiring

Module Module pin ESP32 pin Note
Reed switch one side GPIO33 (RTC) internal pull-up, other side to GND

Code

#include <WiFi.h>

const char* WIFI_SSID = "YOUR_WIFI";
const char* WIFI_PASS = "YOUR_PASSWORD";

bool connectWiFi(uint32_t timeoutMs = 20000) {
  WiFi.mode(WIFI_STA);
  WiFi.begin(WIFI_SSID, WIFI_PASS);
  uint32_t t0 = millis();
  while (WiFi.status() != WL_CONNECTED && millis() - t0 < timeoutMs) {
    delay(250);
    Serial.print(".");
  }
  Serial.println();
  if (WiFi.status() == WL_CONNECTED) {
    Serial.print("IP: ");
    Serial.println(WiFi.localIP());
    return true;
  }
  return false;
}
#include <WiFiClientSecure.h>
#include <HTTPClient.h>

const char* TG_TOKEN = "123456:ABC-YOUR-BOT-TOKEN";   // from @BotFather
const char* TG_CHAT  = "YOUR_CHAT_ID";

// Keep the text free of double quotes and line breaks (it is inserted into JSON).
bool telegramSend(const String& text) {
  if (WiFi.status() != WL_CONNECTED) return false;
  WiFiClientSecure client;
  client.setInsecure();   // simple; pin the certificate for production
  HTTPClient http;
  String url = String("https://api.telegram.org/bot") + TG_TOKEN + "/sendMessage";
  if (!http.begin(client, url)) return false;
  http.addHeader("Content-Type", "application/json");
  String body = String("{"chat_id":"") + TG_CHAT + "","text":"" + text + ""}";
  int code = http.POST(body);
  http.end();
  return code == 200;
}
#define WAKE_PIN GPIO_NUM_33
void setup() {
  Serial.begin(115200);
  pinMode(33, INPUT_PULLUP);
  esp_sleep_wakeup_cause_t cause = esp_sleep_get_wakeup_cause();
  if (cause == ESP_SLEEP_WAKEUP_EXT0 || cause == ESP_SLEEP_WAKEUP_UNDEFINED) {
    if (connectWiFi()) telegramSend(digitalRead(33) ? "Door opened" : "Door closed");
  }
  // wake on the opposite level so that both open and close are reported
  esp_sleep_enable_ext0_wakeup(WAKE_PIN, digitalRead(33) ? 0 : 1);
  esp_deep_sleep_start();
}
void loop() {}

Build tips

  • Use a 1 MΩ external pull-up to keep the idle current in the microamps.
  • Mount the magnet within 10 mm of the reed switch.

What you need

Buy the parts kit · Estimated total: $9.80

ESP32 DevKit V1 (30 pin, WROOM-32) × 1
$4.00
Buy at AliExpress Buy at Amazon.com
Reed switch + magnet (door sensor) × 1
$1.00
Buy at AliExpress Buy at Amazon.com
18650 Li-ion cell + holder × 1
$4.00
Buy at AliExpress Buy at Amazon.com
TP4056 USB-C Li-ion charger with protection × 1
$0.80
Buy at AliExpress Buy at Amazon.com
🇺🇦 Ukraine🌍 Other countries

Other countries: order these parts on AliExpress with the links below.

Some links are affiliate links: we may earn a commission at no extra cost to you.

Related ideas

⚠ Caution

Batteries: watch polarity, use protected cells and a charger with protection, never charge damaged cells unattended.

Beehive scale and hive monitor

A load-cell scale with a temperature probe tracks honey flow and hive health on solar or a Li-ion cell.

  • Board ESP32 DevKit V1 (WROOM-32)
  • Level ●●○ Intermediate
  • Build time 2–3 h
Pick parts / kit
⚠ Caution

Switch only low-voltage loads yourself. For mains loads use a certified isolated module in a box or call an electrician.

Gate and garage door status with remote pulse

Two reed switches report open and closed, a relay gives the opener a pulse, and a message tells you when the gate was left open.

  • Board ESP32-C3 SuperMini
  • Level ●○○ Beginner
  • Build time 1–2 h
Pick parts / kit
⛔ High risk

Mains 220–230 V can kill. Work with the power off, use certified isolated modules in an enclosure, fuse the line, and ask a qualified electrician if you are not sure.

Power outage notifier

A battery-backed ESP32-C3 that detects when mains disappears and sends a message with the time of the blackout.

  • Board ESP32-C3 SuperMini
  • Level ●●○ Intermediate
  • Build time 2–3 h
Pick parts / kit

← All module ideas →

Comments

No comments yet. Share your experience or ask a question.

Comments are moderated.