Wireless door / window sensor (MQTT)

A reed switch wakes the ESP32 from deep sleep on every open/close and publishes the state to MQTT for Home Assistant.

Level: Intermediate Board: ESP32 DevKit V1 (WROOM-32) Smart home

⚠ Safety

Use a protected Li-ion cell with a TP4056 board; never charge unprotected cells.

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

Ukraine: we buy and sell these parts ourselves, order them on this site.

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

Wiring

ModuleModule pinESP32 pinNote
Reed switchone sideGPIO33 (RTC)internal pull-up, other side to GND

Code

door-window-sensor.ino
#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() {}

Flash a ready firmware from the browser Paste your error into the AI Error Doctor

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.

← ← All projects

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

Comments

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

Comments are moderated.