Fork me on GitHub

树莓派使用USB摄像头自制家庭监控

目录

  • 背景
  • 第一部分 部署步骤
  • 参考文献及资料

背景

树莓派摄像头和USB摄像头

树莓派有配套的摄像头模块(Raspberry Pi camera board),如下图。

另外树莓派也支持USB摄像头。关于树莓派支持的USB摄像头有个清单参考(需要梯子)。大家购买前最好确认一下是否在兼容清单中。

camera_pi

第一部分 部署步骤

第一步:检查USB摄像头和树莓派的兼容性

将USB摄像头和树莓派连接,查看USB接口连接情况。

1
2
3
4
5
root@raspberrypi:/# lsusb
Bus 001 Device 004: ID 046d:0825 Logitech, Inc. Webcam C270
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. SMSC9512/9514 Fast Ethernet Adapter
Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

发现004口上面连接并识别了摄像头(我的是Logitech 270摄像头)。

如果没有识别出来,需要查看USB兼容清单是否有该型号。另外由于树莓派供电功率较小,也有可能是USB供电功率不足,需要有外置电源的USB摄像头。

另外还可以查看设备驱动情况:

1
2
root@raspberrypi:/# ls /dev/vid*
/dev/video0

发现video0设备,说明识别了USB摄像头(罗技的c270i)

第二步:MOTION软件实现

对于USB摄像头,有多种软件包可以实现拍照和摄像等功能,这里使用motion

安装motion
1
root@raspberrypi:/# sudo apt-get install motion
编辑配置配置文件
1
root@raspberrypi:~# vi /etc/motion/motion.conf
调整相关参数
1
2
3
4
5
6
7
8
9
10
11
12
# The mini-http server listens to this port for requests (default: 0 = disabled)
stream_port 8082
# web界面访问端口
# TCP/IP port for the http server to listen on (default: 0 = disabled)
webcontrol_port 8080
#控制端口
# Restrict control connections to localhost only (default: on)
webcontrol_localhost off
# Target base directory for pictures and films
# Recommended to use absolute path. (Default: current working directory)
target_dir /var/lib/motion
#照片及视频存放路径

其他参数调整如下:

ffmpeg_output_movies=off

stream_localhost=off

webcontrol_localhost=off

locate_motion_mode=peview

locate_motion_style=redbox

text_changes=on

如果需要对网页进行加密,可以调整下面的配置实现:

1
2
3
4
5
6
7
8
9
# Set the authentication method (default: 0)
# 0 = disabled
# 1 = Basic authentication
# 2 = MD5 digest (the safer authentication)
stream_auth_method 1

# Authentication for the stream. Syntax username:password
# Default: not defined (Disabled)
stream_authentication admin:admin1234
开启motion进程

修改motion文件,设置为守护进程运行(即参数配置为:yes):

1
2
3
# vi /etc/default/motion
# set to 'yes' to enable the motion daemon
start_motion_daemon=yes

启进程:

1
2
3
4
5
6
7
root@raspberrypi:/etc/init.d# motion start
[0] [NTC] [ALL] conf_load: Processing thread 0 - config file /etc/motion/motion.conf
[0] [ALR] [ALL] conf_cmdparse: Unknown config option "sdl_threadnr"
[0] [NTC] [ALL] motion_startup: Motion 3.2.12+git20140228 Started
[0] [NTC] [ALL] motion_startup: Logging to syslog
[0] [NTC] [ALL] motion_startup: Using log type (ALL) log level (NTC)
[0] [NTC] [ALL] become_daemon: Motion going to daemon mode
查看监控画面

地址栏中输入地址和端口号(IP:8082),上面配置的web界面访问端口为8082:

查看监控数据存放目录

另外目录/var/lib/motion中存放历史数据。

第三步:内网穿透(外网访问web监控界面)

实现上面的步骤,你只能在家里本地局域网访问监控界面,意义不大。

由于目前中国宽带服务公司都不会给家庭网络外网地址。所以需要内网穿透,实现外网访问家庭内网。

具体可以使用frp软件实现内网穿透。具体做法参考的博客中另一篇介绍frp的分享文章。

参考文献及资料

1、Building a Motion Activated Security Camera with the Raspberry Pi Zero,链接: https://www.bouvet.no/bouvet-deler/utbrudd/building-a-motion-activated-security-camera-with-the-raspberry-pi-zero

2、How to make a DIY home alarm system with a raspberry pi and a webcam,链接:https://medium.com/@Cvrsor/how-to-make-a-diy-home-alarm-system-with-a-raspberry-pi-and-a-webcam-2d5a2d61da3d

本文标题:树莓派使用USB摄像头自制家庭监控

文章作者:rong xiang

发布时间:2018年03月18日 - 19:03

最后更新:2022年10月25日 - 23:10

原始链接:https://zjrongxiang.github.io/posts/4fc1ecec/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

0%