라즈베리 파이 Home Assistant로 안전한 홈 모니터링: 모션, 알람, 비디오 캡처
라즈베리 파이에 Home Assistant(HA)로 신뢰할 수 있는 홈 보안 시스템을 구축하는 것은 핵심 구성요소인 모션 센서, 알람 패널, 카메라 기반 비디오 캡처를 이해하면 간단합니다. 이 가이드는 HC-SR501 PIR 센서 배선부터 HA 설정, mjpg-streamer 실행, 사진 첨부 즉각 이메일 알림 발송까지 전체 설정을 안내합니다.
전제 조건
- Home Assistant 실행 중인 라즈베리 파이 (권장 0.101.3 이상)
- HC-SR501 PIR 모션 센서
- USB 또는 CSI 카메라 (mjpg-streamer와 호환되는 모든 카메라)
- 알림용 인터넷 연결 이메일 계정 (예: Gmail)
- YAML과 Docker 기본 익숙함 (HA를 컨테이너로 실행 시)
1. HC-SR501을 파이에 배선
센서에 세 핀이 있습니다: VCC(+), GND(–), OUT. 다음과 같이 연결하세요:
- VCC → 핀 5 (3.3V)
- GND → 핀 6 (그라운드)
- OUT → 핀 7 (GPIO 4)
이 연결로 HA가 rpi_gpio 플랫폼을 통해 센서의 이진 상태를 읽을 수 있습니다.
2. mjpg-Streamer 설치
이 가이드를 사용해 mjpg-streamer와 선택적 FFMpeg 레코더를 설정하세요. 저장소의 스크립트가 스트림을 시작하고 필요 시 녹화를 시작/중지합니다.
3. Home Assistant 설정
/usr/share/hassio/homeassistant/configuration.yaml (또는 HA 설정 폴더)를 여세요. 다음 섹션을 추가하세요:
# PIR 바이너리 센서
binary_sensor:
- platform: rpi_gpio
ports:
7: Sensor HC-SR501
invert_logic: false
# 수동 알람 패널
alarm_control_panel:
- platform: manual
name: Home Alarm
pending_time: 60
delay_time: 40
code: 1234
# mjpg-streamer 제어용 커맨드라인 스위치
switch:
- platform: command_line
switches:
start_stop_motion_rec_timelapse:
friendly_name: 'Record motion timelapse'
command_on: "curl https://localhost/start_mjpgstrm.php && curl https://localhost/rec-motion-mjpg.php"
command_off: "curl https://localhost/stop_mjpgstrm.php && curl https://localhost/rec-motion-mjpg-stop.php"
# 커스텀 스크립트용 쉘 명령
shell_command:
take_snapshot_webcam: '/config/scripts/takeSnapshotWebcam.sh'
start_mgpg_streamer: 'curl https://localhost/start_mjpgstrm.php'
stop_mgpg_streamer: 'curl https://localhost/stop_mjpgstrm.php'
start_motion_rec: 'curl https://localhost/rec-motion-mjpg.php'
stop_motion_rec: 'curl https://localhost/rec-motion-mjpg-stop.php'
# 이메일 알림 (Gmail 예시)
notify:
- name: ha_sendmail
platform: smtp
server: smtp.gmail.com
port: 587
timeout: 15
sender:
encryption: starttls
username:
password:
recipient:
-
sender_name: My Home Assistant
편집 후 Configuration → Server Controls → Check Config로 설정 검증 후 자동화와 스크립트 리로드하세요.
4. 자동화 로직
automation.yaml에 다음 자동화를 생성해 센서, 알람, 카메라를 연동하세요.
# 1. 무장 상태에서 모션 감지 시 알람 트리거
- alias: 'Trigger alarm while armed away'
trigger:
- platform: state
entity_id: binary_sensor.sensor_hc_sr501
to: 'on'
condition:
- condition: state
entity_id: alarm_control_panel.home_alarm
state: armed_away
action:
- service: shell_command.start_mgpg_streamer
- service: shell_command.start_motion_rec
- service: alarm_control_panel.alarm_trigger
target:
entity_id: alarm_control_panel.home_alarm
# 2. 알람 해제 시 녹화 중지
- alias: 'Stop recording when alarm disarmed'
trigger:
- platform: state
entity_id: alarm_control_panel.home_alarm
to: 'disarmed'
action:
- service: shell_command.stop_mgpg_streamer
- service: shell_command.stop_motion_rec
- service: persistent_notification.create
data:
message: 'The alarm is disarmed at {{ states('sensor.date_time') }}'
# 3. 알람 트리거 시 사진 이메일 전송
- alias: 'Email alert on alarm trigger'
trigger:
- platform: state
entity_id: alarm_control_panel.home_alarm
to: 'triggered'
action:
- service: persistent_notification.create
data:
message: 'Alarm triggered by motion sensor.'
- delay: { seconds: 4 }
- service: script.webcam_snapshot
- service: notify.ha_sendmail
data:
title: 'Intruder alert'
message: '{{now().strftime("%H:%M %Y-%m-%d")}}: Alarm triggered by motion sensor.'
data:
images:
- /config/camera/snapshot.jpg
script.webcam_snapshot은 takeSnapshotWebcam.sh를 호출해 단일 프레임을 캡처하고 /config/camera/snapshot.jpg로 저장해 이메일 첨부용으로 사용합니다.
5. Docker 볼륨 주의사항
HA가 Docker에서 실행 중이면 컨테이너의 /config 폴더가 호스트의 /usr/share/hassio/homeassistant에 매핑됩니다. docker inspect homeassistant | less로 볼륨 바인딩을 확인하세요.
6. 시스템 테스트
- HA UI에서 알람을
armed_away로 무장하세요.
- PIR 센서 트리거 (예: 센서 앞을 지나가기).
- mjpg-streamer 시작, 모션 레코더가 짧은 클립 캡처, 스냅샷 포함 이메일 수신 확인.
- 알람 해제하고 녹화 중지 및 해제 알림 나타나는지 확인.
결론
이 단계를 통해 모션 감지, 알람 제어, 즉각 사진 알림을 모두 라즈베리 파이의 Home Assistant가 오케스트레이션하는 독립형 저비용 홈 보안 솔루션을 갖게 됩니다.
스크립트와 알림 설정을 본인 환경에 맞게 자유롭게 조정하세요. 즐거운 자동화!