Thursday, 20 August 2015

Filled Under:

DIY Home Monitoring System using Raspberry Pi and Webcam

Post By - Tanmay | 8/20/2015 11:49:00 am
Traditional wireless CCTV cameras are cheap but anyone with a wireless receiver can view your signal. On the other hand, IP cameras are secure but they can be quite expensive and usually the video quality is poor — unless you go for a really expensive model.

Necessary hardware:
  1. Raspberry Pi Model B Revision 2.0 (512MB)
  2. Logitech HD Webcam C270 or a similarly compatible usb webcam (list of compatible webcams here).
  3. A usb hub with an external power supply
  4. (optional): a usb extension cable
Step #1: Setup your raspberry pi
Your pi needs to boot a linux operating system in order to run motion. The most popular choice is Raspbian, a debian-based OS that is optimized for pi’s hardware.
To prepare your SD card and install Raspbian I recommend following Adafruit’s excellent tutorials here.
Since you are not going to have your pi connected to a monitor or have a keyboard and mouse, I also recommend enabling Secure Shell (SSH) in your pi so that you can remote control your Raspberry Pi over your local network.
Finally, it is a good thing to force a static IP address so that you can easily find the webcam server even if pi restarts.
To do this, first type from the command prompt:
ifconfig
This reveals your router information. If you have an ethernet connection check out the eth0 bit. If you have a wireless connection check out the wlan0bit. Make a note of the following info:
inet addr – 192.168.1.5 (pi’s IP Address)
Bcast – 192.168.1.255 (broadcast IP range)
Mask – 255.255.255.0 (subnet mask)
then run:
route -n
and note the following:
Gateway Address – 192.168.1.1
then run the following command to edit the network configuration:
sudo nano /etc/network/interfaces
and change the following entry from:
iface wlan0 inet dhcp
to:
iface wlan0 inet static
address 192.168.1.5
netmask 255.255.255.0
gateway 192.168.1.1
network 192.168.1.0
broadcast 192.168.1.255
Press CTRL and together to save and exit nano editor.
If you reboot your pi now you should have a static address.

Step #3: Setup motion
First you need to use rpi-update to add to your raspbian image the initially-missing UVC support:
sudo apt-get install rpi-update
sudo rpi-update
Next you need to upgrade your packages:
sudo apt-get update
sudo apt-get upgrade 
Then you can install motion:
sudo apt-get install motion
Now if you run
lsusb
you should see your camera listed as a usb device, like so:
Bus 001 Device 006: ID 046d:0825 Logitech, Inc. Webcam C270
(If not then perhaps your webcam is not compatible with pi)
Next we proceed to configure motion:
sudo nano /etc/motion/motion.conf
This is a configuration file where you get to define parameters such as the port which motion will run on, or actions that will be triggered when movement is detected.
Here’s a list of the parameters you most likely would want to configure:
  • daemon: set to ON to start motion as a daemon service when pi boots,
  • webcam_localhost: set to OFF so that you can access motion from other computers,
  • stream_port: the port for the video stream (default 8081),
  • control_localhost: set to OFF to be able to update parameters remotely via the web config interface,
  • control_port: the port that you will access the web config interface (default 8080),
  • framerate: number of frames per second to be captured by the webcam. Warning: setting above 5 fps will hammer your pi’s performance!
  • post_capture: specify the number of frames to be captured after motion has been detected.
You also need to edit the following file if you want to run motion as a daemon service:
sudo nano /etc/default/motion
and set start_motion_daemon to YES:
start_motion_daemon=yes
Then start motion by typing:
sudo service motion start
Wait for about 30 seconds for motion to start and then open the video stream from VLC or a similar program that can show video streams. If you use VLC player go to File>Open Network and enter the IP address of your pi followed by the stream_port, for example: 192.168.1.5:8083
Via - The Medium

0 comments:

Post a Comment