0%

基于树莓的派动态监控

当有人移动时,捕获下照片(可用于监控)

硬件

  • 树莓派
  • camera
  • 人体接近传感器

接线方式

可以在树莓派终端查看各个引脚的作用

1
pinout

pir-motion-sensor-with-raspberry-pi

开关的作用
按下后退出程序。

烧录文件

创建一个.py文件,并拷贝以下代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#Project 13 - Burglar Detector With Photo Capture
#latest code updates available at: https://github.com/RuiSantosdotme/RaspberryPiProject
#project updates at: https://nostarch.com/RaspberryPiProject

#import the necessary packages
from gpiozero import Button, MotionSensor
from picamera import PiCamera
from time import sleep
from signal import pause

#create objects that refer to a button,
#a motion sensor and the PiCamera
button = Button(2)
pir = MotionSensor(4)
camera = PiCamera()

#start the camera
camera.start_preview()

#image image names
i = 0

#stop the camera when the pushbutton is pressed
def stop_camera():
camera.stop_preview()
#exit the program
exit()

#take photo when motion is detected
def take_photo():
global i
i = i + 1
# 自己选择拍照后要安装的位置
camera.capture('/home/pi/Desktop/image_%s.jpg' % i)
print('A photo has been taken')
sleep(10)

#assign a function that runs when the button is pressed
button.when_pressed = stop_camera
#assign a function that runs when motion is detected
pir.when_motion = take_photo

pause()

参考文章