基本上參考 Raspberry Pi Spy的
這篇文章 - Cheap PIR Sensors and the Raspberry Pi 。
紅外線動作感測器(PIR Sensor)可以視為是一個輸出 (out) 為 1 或 0的開關。
PIR Sensor 的
GND接到
Raspberry Pi 的
Pin 6(GND)、
POWER接到
Pin 2 (+5V)、
OUT則接到
Pin 26 (GPIO7)。
(Raspberry Pi腳位可參考這裡)
當紅外線動作感測器偵測到有物體(人體)移動時,
GPIO7的值可能就會由0轉變成為1,我們可以藉此狀態改變來達成其他應用,例如這次我們就利用紅外線偵測狀態改變,利用fswebcam來擷取一張照片。
1. 修改Raspberry Pi Spy所提供的pir_1.py如下,並記得存檔。
..
import time (新增下列兩行)
import os
import datetime
...
Previous_State=1 (新增下列五行)
dt = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
dt_filename = dt+".jpg"
fs_command = "fswebcam --background -d /dev/video0 "+dt_filename
print " ",fs_command
os.system(fs_command)
..
2. 修改執行 pir_1.py
> sudo python pir_1.py
PIR Module Test (CTRL-C to exit)
Waiting for PIR to settle ...
Ready
Motion detected!
fswebcam --background -d /dev/video0 20130513-000412.jpg
Ready
Motion detected!
fswebcam --background -d /dev/video0 20130513-000418.jpg
Ready
..
因為紅外線偵測到物體移動之後,才進行fswebcam來進行拍攝,所以時間差延遲可能會拍不到真正想要拍攝的移動物體。應該是不斷進行拍攝或錄影,然後在偵測物體移動發生的第 t 秒時,回頭去存下第 t-1 或 t-2 秒到第 t+1 或 t+2秒的影像,可能較能切合需要。
不過這只是拋磚引玉,一個結合紅外線動作感應器的小應用而已,更多有趣或有用的應用就有勞各位自己再去嘗試囉。
#