Smart Door Lock (with Zigbee)

This circuit is live in Circuit Canvas. Open it to explore every part and wire, or make your own copy to change values, swap parts and build your own version. Free, right in your browser.

I made my door lock smart!

I added a limit switch inside the lock casing, and soldered two wires from it and to an ESP32-H2 device. With some simple code, I made my lock into a Zigbee sensor for Home Assistant.

See the full tutorial here: https://www.build-electronic-circuits.com/smart-door-lock-esp32-h2/

Smart Door Lock (with Zigbee) Parts List

BAT1
3.6V (AA Lithium)
C1
100µF
SW1
Microswitch
U1
ESP32-H2-DevKitM-1

How to Build the Smart Door Lock (with Zigbee)

Code

sketch.ino
#ifndef ZIGBEE_MODE_ED
#error "Select Tools -> Zigbee mode: Zigbee ED (end device)"
#endif

#include "Zigbee.h"
#include <Preferences.h>

#define SENSOR_PIN     10   // Must be GPIO 7-14 to wake the ESP32-H2 from deep sleep
#define ENDPOINT       1    // Same endpoint as the example you paired with
#define CHECKIN_HOURS  6    // Wake up anyway every few hours so Home Assistant knows we're alive

ZigbeeContactSwitch zbContact = ZigbeeContactSwitch(ENDPOINT);
Preferences preferences;    // Stores the "enrolled" flag in flash

bool readIsOpen() {
  return digitalRead(SENSOR_PIN) == HIGH;
}

void sendState(bool isOpen) {
  if (isOpen) {
    zbContact.setOpen();
    Serial.println("Sent: OPEN");
  } else {
    zbContact.setClosed();
    Serial.println("Sent: CLOSED");
  }
}

void goToSleep() {
  // Wake up when the pin changes: if it's HIGH now, wake on LOW, and the other way around
  bool isOpen = readIsOpen();
  esp_sleep_enable_ext1_wakeup(1ULL << SENSOR_PIN,
                               isOpen ? ESP_EXT1_WAKEUP_ANY_LOW : ESP_EXT1_WAKEUP_ANY_HIGH);

  // Also wake up on a timer, as a "still alive" check-in
  esp_sleep_enable_timer_wakeup((uint64_t)CHECKIN_HOURS * 3600ULL * 1000000ULL);

  Serial.println("Going to sleep");
  Serial.flush();
  esp_deep_sleep_start();
}

void setup() {
  Serial.begin(115200);
  pinMode(SENSOR_PIN, INPUT_PULLUP);  // Built-in pull-up resistor

  // Has this board been enrolled before? (Same flag as the example uses)
  preferences.begin("Zigbee", false);
  bool enrolled = preferences.getBool("ENROLLED");
  preferences.end();

  // Set up the Zigbee device
  zbContact.setManufacturerAndModel("DIY", "ESP32H2_Door_Sensor");
  zbContact.setPowerSource(ZB_POWER_SOURCE_BATTERY);
  Zigbee.addEndpoint(&zbContact);

  // Settings for a sleepy end device (from Espressif's sleepy sensor example)
  esp_zb_cfg_t zigbeeConfig = ZIGBEE_DEFAULT_ED_CONFIG();
  zigbeeConfig.nwk_cfg.zed_cfg.keep_alive = 10000;
  Zigbee.setTimeout(10000);

  if (!Zigbee.begin(&zigbeeConfig, false)) {
    Serial.println("Zigbee failed to start");
    goToSleep();
  }

  // Wait for the network, but give up after 10 seconds to save the battery
  unsigned long start = millis();
  while (!Zigbee.connected()) {
    if (millis() - start > 10000) {
      Serial.println("No network, sleeping");
      goToSleep();
    }
    delay(100);
  }

  // Restore the enrollment from flash, or ask for a new one the first time
  if (enrolled) {
    zbContact.restoreIASZoneEnroll();
  } else {
    zbContact.requestIASZoneEnroll();
  }

  start = millis();
  while (!zbContact.enrolled()) {
    if (millis() - start > 10000) {
      Serial.println("Not enrolled, sleeping");
      goToSleep();
    }
    delay(100);
  }

  // Save the flag the first time, so we can restore it after every wake-up
  if (!enrolled) {
    preferences.begin("Zigbee", false);
    preferences.putBool("ENROLLED", true);
    preferences.end();
  }

  // Send the state, then send again if it changed while we were busy
  bool sentState = readIsOpen();
  sendState(sentState);
  delay(1000);  // Give the radio time to deliver the message

  if (readIsOpen() != sentState) {
    sendState(readIsOpen());
    delay(1000);
  }

  goToSleep();
}

void loop() {
  // Never reached - the board sleeps at the end of setup()
}

Make it your own

Every circuit here is a live project in Circuit Canvas, not just a picture. Copy this one and change a value, swap a part or move it to a stripboard: the schematic and the breadboard layout are linked, so a part added to one appears in the other.

Or start from a blank canvas and draw your own circuit, with thousands of ready-made parts.