Published Book on Amazon
|All of IOT Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 1|
|All of IOT Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 2|
출판된 한글판 도서
|최신 라즈베리파이(Raspberry Pi)로 시작하는 사물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (상)|
|최신 라즈베리파이(Raspberry Pi)로 시작하는 사물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (하)|
Original Book Contents
126.96.36.199 Framework to Access Camera via Web
<Motion> program is a program that monitors video signals coming from a camera. It has the ability to detect if there is a significant change in the image, that is, motion. This program not only has a function to view the contents on the web in real time by transmitting a video taken by a camera to the Internet, but also has a function of storing the motion as an image or video when a motion is detected on the screen.
Here, we will use the following framework to broadcast motion detection video to the Web.
Here, <Motion> program records video using "/dev/video0" device through V4L2 API and transmit the captured video to the Web over the network. Then a media player program that can process streaming data from a computer on the Internet receives and plays the corresponding video data.
To do this, <Motion> program must be installed on Raspberry Pi, and a media player program to handle streaming video data must be installed on the computer on Web.
For more information for <Motion> program, please refer to the followings:
188.8.131.52 Capturing Motion Detection in Raspberry Pi System
First, <Motion> program needs to be installed in Raspberry Pi. Install it as follows.
sudo apt-get install motion
When the program installation is complete, you need to make some settings for <Motion> program. Important settings for this program are stored in "/etc/motion/motion.conf" file. The followings are important items excerpted from the contents of the file. Use a text editor to modify what you need. Super user privilege is needed to modify this file.
# Start in daemon (background) mode and release terminal (default: off)
daemon off à Change to "on"
# Videodevice to be used for capturing (default /dev/video0)
# for FreeBSD default is /dev/bktr0
# Threshold for number of changed pixels in an image that
# triggers motion detection (default: 1500)
# Use ffmpeg to encode mpeg movies in realtime (default: off)
# Codec to used by ffmpeg for the video compression.
# Timelapse mpegs are always made in mpeg1 format independent from this option.
# Supported formats are: mpeg1 (ffmpeg-0.4.8 only), mpeg4 (default), and msmpeg4.
# mpeg1 - gives you files with extension .mpg
# mpeg4 or msmpeg4 - gives you files with extension .avi
# msmpeg4 is recommended for use with Windows Media Player because
# it requires no installation of codec on the Windows client.
# swf - gives you a flash film with extension .swf
# flv - gives you a flash video with extension .flv
# ffv1 - FF video codec 1 for Lossless Encoding ( experimental )
# mov - QuickTime ( testing )
# The mini-http server listens to this port for requests (default: 0 = disabled)
webcam_port 8081 à Change if necessary
# Restrict webcam connections to localhost only (default: on)
webcam_localhost on à Change to "off"
# Target base directory for pictures and films
# Recommended to use absolute path. (Default: current working directory)
■ Setting for "daemon".
A daemon is a program that is executed in advance and running so that it can process a certain service request immediately upon receipt. It is usually configured to run automatically when a system boots. Change the setting "daemon off" to "daemon on" as shown above.
■ Setting for "videodevice".
Setting of "videodevice /dev/video0" means that <Motion> program records a video using "/dev/video0" device through V4L2 driver.
■ Setting for "threshold".
It is a setting for sensitivity of movement. It is about how much the scene should be changed to detect movement. Decreasing numbers makes it sensitive, and large numbers makes it dull.
■ Setting for "ffmpeg_cap_new".
This is to specify whether to save video file with image when motion is detected.
■ Setting for "ffmpeg_video_codec".
When motion is detected and video file is saved, it specifies in what format to save it.
■ Setting for "webcam_localhost".
It specifies whether to allow webcam to be connected from local host computer only. Change it to "webcam_localhost off" state to allow access from another computer on network.
■ Setting for "webcam_port".
To connect to the webcam, you need to specify the port here, which is set to "8081" by default. If you want to use a different port, change this.
■ Setting for "target_dir".
It specifies where images and video files are stored when motion is detected.
Next is the configuration in "/etc/default/motion" file. Use a text editor program to modify what you need. Super user privilege is necessary to modify this file.
# set to 'yes' to enable the motion daemon
start_motion_daemon=no àChange to "yes"
■ Setting for "start_motion_daemon".
This is a setting for whether <Motion> program should be run automatically when system boots. If you set it here, you do not have to activate <Motion> program manually every time Raspberry Pi system works. For reference, we have studied what an init script is in [20.3.3 Creating Init Script]. This program has been developed so that if you set this daemon here to start, the init script automatically starts the daemon.
After the setting is completed, now let's run <Motion> program manually. The following command starts <Motion> program.
sudo service motion start
The following command also has the same effect.
sudo /etc/init.d/motion start
This is the command to start daemon manually without "motion" daemon running. The next time system boots, <Motion> program will work automatically, even if you do not do this, because the daemon will starts automatically due to the previous configuration.
You can view the camera screen using any media player on another computer on the network after while the program is running. Here, we will use <VLC> program installed earlier. Select menu Media àOpen Network Stream to specify the connection information for a Raspberry Pi system in which <Motion> program is running. This time, the default port is "8081", so if you input "http://<URL>: 8081>/" and press [Play] button, you can view the camera screen in real time.
Next, we will see if <Motion> program detects motion and does necessary tasks. First, make an appropriate motion in front of the camera, and then check the contents of "/temp/motion" folder where the motion is saved as an image or video.
pi@raspberrypi ~ $ ls /tmp/motion –l
-rw-r--r-- 1 motion motion 47533 May 17 20:47 07-20150517204646.swf
-rw-r--r-- 1 motion motion 9271 May 17 20:46 07-20150517204647-00.jpg
-rw-r--r-- 1 motion motion 9286 May 17 20:46 07-20150517204647-01.jpg
-rw-r--r-- 1 motion motion 7679 May 17 20:46 07-20150517204648-00.jpg
-rw-r--r-- 1 motion motion 7425 May 17 20:46 07-20150517204648-01.jpg
-rw-r--r-- 1 motion motion 8458 May 17 20:46 07-20150517204649-00.jpg
Then, you can see that there are several pictures of "jpg" format and videos "swf" format. It captures the movement and stores the contents when there is any movement.