Introduction
About a year ago, I started preparing for the move and putting together a prototype of a smart home. As a control system, I chose the most popular and actively developing solution - Home Assistant. As the smart home grew overgrown with sensors, the question arose about the security system, which, if something happened, would raise the alarm and inform me (neighbors) about uninvited guests. I don't see the need for a system from private security companies yet, so I decided to do everything myself.
How it works: an opening sensor is placed on the front door, which, using the Zigbee protocol, informs the smart home server that someone has entered the apartment. An alarm is triggered in "quiet mode" ( "triggered" event in embedded integration ; this does not appear in any way, but the countdown goes until the siren starts). If the lock is not removed within the time specified in the settings (by entering a code or NFC tag), the siren and light indication will start.
What is collected from:
ESP32 WROOM DevKit v1 (in theory, you can replace any ESP by changing the config for it)
RFID / NFC module PN532
Connecting wires (6 pieces)
3D printed case
Xiaomi Gateway 2 (which is locally controlled) I plan to use as a speaker and indicator light
Door opening sensor from Aqara
, LED-, , Home Assistant.
, 3D-. Xiaomi . Aliexpress 600 .
ESP
NFC- SPI. ( 1), - ( ). 8 .
, NFC- ESP-32: . , - , .
( ESP, PN532):
GPIO18 - SKC
GPIO19 - MSO
GPIO23 - MOSI
GPIO5 - SS
3V3 - VCC
GND - GND
ESPHome ESP-32. , :
:
esphome:
name: esp32
platform: ESP32
board: nodemcu-32s
wifi:
ssid: "My Wi-Fi"
password: "mypassword"
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Esp32 Fallback Hotspot"
password: "mypassword"
captive_portal:
# Enable logging
logger:
# Enable Home Assistant API
api:
password: "mypassword"
ota:
password: "mypassword"
web_server:
port: 80
spi:
clk_pin: GPIO18
miso_pin: GPIO19
mosi_pin: GPIO23
pn532_spi:
cs_pin: GPIO5
update_interval: 1s
esp32_ble_tracker:
switch:
- platform: gpio
name: "ESP LED"
pin:
number: GPIO2
mode: OUTPUT
binary_sensor:
- platform: pn532
uid: 79-EB-08-B4
name: "NFC Card"
spi pn532_spi, . switch ( , , ), binary_sensor Home Assistant ( uid true; uid ESP ESPHome). , RFID-, . NFC , .
ESP. , , RFID-. uid :
[17:42:35][D][pn532:149]: Found new tag '79-EB-08-B4'
ESP , Home Assistan
Home Assistant
Home Assistant Lovelace. - , configuration.yaml :
alarm_control_panel:
- platform: manual
code: !secret alarm_pin
code_arm_required: false
#
arming_time: 5
#
delay_time: 10
#
trigger_time: 600
, arming_time ( , ) delay_time ( , ) 5 10 . , Home Assistant.
Lovelace, ui-lovelace.yaml
- type: alarm-panel
name:
entity: alarm_control_panel.ha_alarm
states:
- arm_away
entity , alarm_control_panel. states , : " ( )".
NFC- , 5 :
(, )
( )
( , )
. , , . Xiaomi ESP-32. .
- id: '3-0001'
alias: ' '
trigger:
platform: state
entity_id: binary_sensor.158d000446f3fe_contact
to: 'on'
condition:
- condition: state
entity_id: alarm_control_panel.ha_alarm
state: armed_away
action:
- service: alarm_control_panel.alarm_trigger
entity_id: alarm_control_panel.ha_alarm
- repeat:
sequence:
- service: light.turn_on
data:
entity_id: light.gateway_light_44237c82f751
color_name: red
brightness: 255
- service: switch.turn_on
entity_id: switch.esp_led
- delay:
milliseconds: 500
- service: light.turn_off
entity_id: light.gateway_light_44237c82f751
- service: switch.turn_off
entity_id: switch.esp_led
- delay:
milliseconds: 500
until:
condition: or
conditions:
- condition: state
entity_id: alarm_control_panel.ha_alarm
state: armed_away
- condition: state
entity_id: alarm_control_panel.ha_alarm
state: disarmed
- ESP uid. , . , , ESP, 3 .
- id: '3-0002'
alias: ' '
trigger:
platform: state
entity_id: binary_sensor.nfc_card
to: 'on'
condition:
- condition: state
entity_id: alarm_control_panel.ha_alarm
state: disarmed
action:
- service: alarm_control_panel.alarm_arm_away
entity_id: alarm_control_panel.ha_alarm
- repeat:
sequence:
- service: light.turn_on
data:
entity_id: light.gateway_light_44237c82f751
color_name: orange
brightness: 255
- service: switch.turn_on
entity_id: switch.esp_led
- delay:
milliseconds: 500
- service: light.turn_off
entity_id: light.gateway_light_44237c82f751
- service: switch.turn_off
entity_id: switch.esp_led
- delay:
milliseconds: 500
until:
- condition: state
entity_id: alarm_control_panel.ha_alarm
state: armed_away
- service: switch.turn_on
entity_id: switch.esp_led
- service: light.turn_on
data:
entity_id: light.gateway_light_44237c82f751
color_name: red
brightness: 255
- delay:
seconds: 3
- service: light.turn_off
entity_id: light.gateway_light_44237c82f751
RFID-. . , , . ESP .
- id: '3-0003'
alias: ' '
trigger:
platform: state
entity_id: binary_sensor.nfc_card
to: 'on'
condition:
condition: or
conditions:
- condition: state
entity_id: alarm_control_panel.ha_alarm
state: armed_away
- condition: state
entity_id: alarm_control_panel.ha_alarm
state: pending
action:
- service: alarm_control_panel.alarm_disarm
data:
entity_id: alarm_control_panel.ha_alarm
code: !secret alarm_pin
- delay:
milliseconds: 100
- service: switch.turn_off
entity_id: switch.esp_led
- service: light.turn_on
data:
entity_id: light.gateway_light_44237c82f751
color_name: green
brightness: 255
- delay:
seconds: 3
- service: light.turn_off
entity_id: light.gateway_light_44237c82f751
, . .
- id: '3-0004'
alias: ' '
trigger:
- platform: state
entity_id: alarm_control_panel.ha_alarm
to: 'triggered'
action:
- service: xiaomi_aqara.play_ringtone
data:
gw_mac: 44237C82F751
ringtone_id: 0
ringtone_vol: 3
" " alarm_control_panel.ha_alarm ( triggered) .
- id: '3-0005'
alias: ' '
trigger:
platform: state
entity_id: binary_sensor.nfc_card
to: 'on'
condition:
- condition: state
entity_id: alarm_control_panel.ha_alarm
state: triggered
action:
- service: alarm_control_panel.alarm_disarm
data:
entity_id: alarm_control_panel.ha_alarm
code: !secret alarm_pin
- service: xiaomi_aqara.stop_ringtone
data:
gw_mac: 44237C82F751
- delay:
milliseconds: 500
- service: switch.turn_off
entity_id: switch.esp_led
- service: light.turn_on
data:
entity_id: light.gateway_light_44237c82f751
color_name: green
brightness: 255
- delay:
seconds: 3
- service: light.turn_off
entity_id: light.gateway_light_44237c82f751
, , - . . , . , ESP, . .
, - . , . , !
- , . .