Installation Guide for motion detection with webcam


13 posts
by tupf » Mon Sep 24, 2012 3:52 pm
Hi, here an installation guide for motion detection with webcam and wifi. I put it in here for now. Let me know if there are better places .
Tupf.

Needed HW
  • Lenovo W500 (or other HW to programm your SD card)
  • Raspberry Pi
  • HDMI cable, ethernet cable, usb mouse and keyboard
  • power supply with mini usb (I use Vivanco PAH3)
  • Webcam Microsoft VX 800
  • SD card SAMSUNG MB-SS8GA, or other
  • wifi usb EW-7811UN

Install Driver on W500 for the built in SD drive

I read and heard a couple of times that I cannot use built in SD drives for SD card setup. It works for me though. But I had to follow the steps below.

Ref.: http://forums.lenovo.com/t5/W-Series-ThinkPad-Laptops/W500-card-reader-problems-with-a-wrinkle/td-p/202550

  • Start Windows XP and log on with a user ID authorized as an administrator
  • Click Start and then click Control Panel
  • Click Performance and Maintenance
  • Click System
  • Click the Hardware tab
  • Click Device Manager
  • Double-click the IDE ATA/ATAPI Controller section
  • Double-click Ricoh SD Bus Host Adapter
  • Click the Driver tab
  • Click the Update Driver button
  • Select No, not this time and click Next
  • Select Install from a list or specific location and click Next
  • Select Don't search. I will choose the driver to install and click Next
  • Select SDA Standard Compliant SD Host Controller and click Next
  • Follow the instructions on the screen to complete installation
  • Restart the computer

Format SD card with FAT32. Use explorer for this (right click on SD card - format ...)

Retrieve image

Ref.: http://www.raspberrypi.org/downloads

such as 2012-08-16-wheezy-raspbian.zip, this one works fine.

Setup SD card

Ref.: http://elinux.org/RPi_Easy_SD_Card_Setup

see Section "Copying the image to an SD Card on Windows if first option isn't successful"


First time boot
  • connect to tv, mouse, keyboard and power supply
  • boot prompts for boot settings: change password + ssh server. You want ssh server enabled


Editor (emacs)

Ref.: http://romkey.com/2012/06/03/gnu-emacs-on-rasperry-pi/

  • sudo apt-get update
  • sudo apt-get install emacs

Timezone
  • sudo cp /usr/share/zoneinfo/Europe/Berlin /etc/localtime

Install vnc

Ref.: http://www.raspberrypi.org/phpBB3/viewtopic.php?t=7395

  • sudo apt-get install tightvncserver
  • vncserver and enter password, and enter view only password
  • cd /etc/init.d
  • sudo emacs tightvncserver
  • copy this into the file

#! /bin/sh
# /etc/init.d/tightvncserver
#

# Carry out specific functions when asked to by the system
case "$1" in
start)
su pi -c '/usr/bin/vncserver'
echo "Starting VNC server "
;;
stop)
pkill vncserver
echo "VNC Server has been stopped (didn't double check though)"
;;
*)
echo "Usage: /etc/init.d/blah {start|stop}"
exit 1
;;
esac

exit 0


  • Make script executable: sudo chmod +x tightvncserver
  • Make it run during boottime: sudo update-rc.d tightvncserver defaults
  • Kill previous vnc server: sudo pkill Xtightvnc and check if vnc is still runninh with exeuting ps aux | grep vnc.
  • Start vnc again: sudo /etc/init.d/tightvncserver start
  • Open vnc client on other computer ...

Webcam

Ref.: http://www.chrisge.org/blog/2012-04-10/weather.chrisge.org_die_software_i/iii_motion

  • sudo apt-get install motion
  • sudo apt-get install libv4l-0
  • sudo apt-get install uvccapture
  • dmesg | tail, you should see you camera attached in the output message, if it is connected to your HW
  • edit /etc/default/motion and set "start_motion_daemon" to yes
  • edit /etc/motion/motion.conf with the following settings (these are just examples, which work for me. You will need to do optimizations yourself)

set "daemon on"
set "minimum_frame_time 5", this can be modified, depending how often you want to take picture
set "pre_capture 2"
set "post_capture 2"
set "output_normal on"
set "quality 100"
set "ffmpeg_cap_new on"
set "ffmpeg_timelapse 30"
set "ffmpeg_variable_bitrate 2"
set "get_dir /media/webcam/motion"
set "webcam_port 8080"
set "control_port 8081", this matters!!!
set "webcam_localhost off"
set "width 320", this matters!!!
set "height 240", this matters!!!


note that webcam_port, control_port, width, height do matter. Wrong settings can cause the camera not to work.

  • give /media dir access rights: sudo chmod 777 /media
  • start motion with sudo /etc/init.d/motion start
  • check if all is ok with tail -f /var/log/syslog
  • if you want to stop motion: sudo /etc/init.d/motion stop
  • check you browser if picture is displayed: http://xx.xx.xx.xx:8080/
  • controls can be also set here: http://xx.xx.xx.xx:8081/

Wireless

Ref.: http://www.raspberrypi.org/phpBB3/viewtopic.php?f=31&t=7471
Ref.: http://www.ctrl-alt-del.cc/2012/05/raspberry-pi-meets-edimax-ew-7811un-wireless-ada.html
Ref.: http://dl.dropbox.com/u/80256631/install-rtl8188cus.txt

  • cd /boot
  • get script: sudo wget http://dl.dropbox.com/u/80256631/ install-rtl8188cus-latest.sh -O /boot/install-rtl8188cus-latest.sh
  • plug in EW-7811UN
  • run script
  • enter SSID
  • enter password

done!!!!!
Posts: 14
Joined: Mon Sep 24, 2012 1:55 pm
by tupf » Sat Oct 06, 2012 7:54 am
I need to include something here, because I figured that the SD becomes full pretty fast. So I recommend to add another partition, right after Editor (Emacs). Since I used a 8GB SD card, there is plenty space:

Add another partition

Ref.: http://elinux.org/RPi_Easy_SD_Card_Setup

  • The wheezy version I used has already parted installed, otherwhise: sudo su, and apt-get install parted
  • parted /dev/mmcblk0 (this starts parted, another command line appears)
  • unit chs
  • print (you will see a number of partitions printed, I have two.)
  • mkpart primary a,b,c x,y,z (while a, b, c, x, y, z are cylinders, heads and sectors). So at the Start you need to go one sector up from the End of you last printed partition. For x,y,z you choose whatever size you want to have for you partition. Check the size of Cylinder from the print output.
  • print (you partition should be added)
  • quit (this leaves the parted command prompt.)
  • mkfs.ext4 /dev/mmcblk0p3 (this depends on how many partitions you have)
  • e2label /dev/mmcblk0p3 data
  • emacs /etc/fstab (or other editor)
  • add at the end of the file fstab: /def/mmclblk0p3 /data ext4 defaults 1 2
  • mkdir /data
  • mount /data
  • cd /data
  • mkdir webcam
  • cd webcam
  • mkdir motion

Now one modification in Webcam

Instead of setting up this in the motion.conf file

set "get_dir /media/webcam/motion"

you should setup this

set "get_dir /data/webcam/motion"

This is better!
Posts: 14
Joined: Mon Sep 24, 2012 1:55 pm
by pluggy » Sat Oct 06, 2012 9:58 am
Why not stretch the existing partition to use all of the SD card rather than putting another one on and then changing a lot of stuff so it gets used ?
Don't judge Linux by the Pi.......
User avatar
Posts: 2083
Joined: Thu May 31, 2012 3:52 pm
Location: Barnoldswick, Lancashire,UK
by shuckle » Sat Oct 06, 2012 11:18 am
Do you get good quality picture?
Posts: 365
Joined: Sun Aug 26, 2012 11:49 am
by tupf » Sun Oct 07, 2012 7:47 am
sure I could stretch the partition. Probably there are many ways. I just showed one way which works for me and I am happy with it.

I added a cronjob to delete old files in /data/webcam/motion if the the size becomes too large. I am testing this out now, and will update after I see it works.

The quality of pictures is alright. You cannot expect too much for a 10Euro webcam with a fixed aperture. I did not test out the maximum pixel size yet, But I will do.

It does capture a person when he walks by, but I had to change the minimum frame time in motion.conf from set "minimum_frame_time 5" to set "minimum_frame_time 3". I am still optimizing around.

I had to spent effort in keeping the camera in the shadow by attaching a shield around it. Direct sunlight causes overexposed pictures. Also sudden cloud covers causes motion to capture pictures. But I was able to minimize it, and it works ok now. I will also put some light absorbing paint around the camera, and see if this helps. Probably a better camera with automatic aperture would improve all this, but I dont want to spent money for it.
Posts: 14
Joined: Mon Sep 24, 2012 1:55 pm
by tupf » Wed Oct 10, 2012 6:55 pm
One more thing:

I had the problem that jpg and swf files from motion added up about 100M of space every day, so I want to delete old files automatically.

I solved this by adding the entries below in the crontab file and it works fine:

  • sudo su
  • cd /etc
  • emacs crontab
  • append the entries below
0 12 * * * root find /data/webcam/motion/* -mtime +2 -exec rm {} \;
0 23 * * * root ifdown wlan0
30 5 * * * root ifup wlan0


The first item deletes all files older than 2 days from the /data/webcam/motion/. The last two crontab items switch off wlan at 11 pm and turn it on at 5.30 am.
Posts: 14
Joined: Mon Sep 24, 2012 1:55 pm
by 5up Mushroom » Sun Oct 21, 2012 2:26 pm
For the file storage, you could use NFS (assuming you have a linux box elsewhere on the same network).

On the other linux box:
1. sudo apt-get install nfs-kernel-server
2. sudo {your editor of choice} /etc/exports - this file tells your nfs server which computers are allowed to connect to which folder on your nfs server
3. To allow any computer on your network to connect to a folder on your nfs server (assuming your router assigned IPs in the 192.168.1.1-254 range... you might have to change that to 192.168.0.0 depending on the DHCP settings on your router.)

/the/folder/ 192.168.1.0/255.255.255.0(rw,fsid=0,insecure,no_subtree_check,async)

On your PI:
4. Make a new directory to link to your share folder: mkdir ~/nfs
5. sudo apt-get install nfs-client
6. sudo {your editor of choice} /etc/fstab
7. To mount the nfs share on raspberry startup toss this line in fstab:

{your nfs servers ip}:/the/folder /home/pi/nfs nfs soft,intr,rsize=8192,wsize=8192

8. Now just reboot and you should be golden. sudo shutdown -r now

You should now be able to navigate to /home/pi/nfs and see everything in the folder on your other computer. You can just create a bash script to move older recordings to the folder and toss in a new cron job to kick of the script.
Posts: 1
Joined: Sun Oct 21, 2012 1:12 pm
by properphatboy » Sat Dec 01, 2012 10:52 am
Awesome guide tupf.

I'm just struggling on 2 things. The first is that I can't find the "get_dir /data/webcam/motion" anywhere in the file.

The second is that I'm not familiar with VNC. Having set it up as shown above, how do I then access it from my windows laptop?

Thanks,

Dee.
Dee
Posts: 14
Joined: Wed Nov 21, 2012 6:42 pm
Location: Bristol
by tupf » Sat Dec 08, 2012 5:24 pm
Thanks Dee.

about the vnc viewer. Just google for vncviewer you will find many download sites. You will need to install on your windows laptop.

about the motion.conf, looks like it is a typo. It should be target_dir ... and not get_dir. Thanks for pointing it out.

Tupf.
Posts: 14
Joined: Mon Sep 24, 2012 1:55 pm
by skyemoor » Wed Oct 23, 2013 8:12 pm
I was trying to figure out how to use Motion in combination with raspivid, but this obviates the need for the latter altogether, saving quite a bit of processing power.

Good thinking, thanks!
Posts: 11
Joined: Wed Oct 16, 2013 1:21 pm
by caporeira » Sun Jan 26, 2014 10:58 pm
I can't install motion on my Rasbian. Anyone have idea why ?

Code: Select all
pi@raspberrypi ~ $ sudo apt-get install motion
Reading package lists... Done
Building dependency tree
Reading state information... Done
motion is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
1 not fully installed or removed.
After this operation, 0 B of additional disk space will be used.
Do you want to continue [Y/n]? Y
Setting up watchdog (5.12-1) ...
/run/udev or .udevdb or .udev presence implies active udev.  Aborting MAKEDEV in                              vocation.
insserv: warning: script 'mathkernel' missing LSB tags and overrides
insserv: There is a loop between service watchdog and mathkernel if stopped
insserv:  loop involving service mathkernel at depth 2
insserv:  loop involving service watchdog at depth 1
insserv: Stopping mathkernel depends on watchdog and therefore on system facilit                              y `$all' which can not be true!
insserv: exiting now without changing boot order!
update-rc.d: error: insserv rejected the script header
dpkg: error processing watchdog (--configure):
 subprocess installed post-installation script returned error exit status 1
Errors were encountered while processing:
 watchdog
E: Sub-process /usr/bin/dpkg returned an error code (1)


And when I want expand sd card:
Code: Select all
pi@raspberrypi ~ $ sudo raspi-config

Command (m for help):
Disk /dev/mmcblk0: 16.1 GB, 16147021824 bytes
4 heads, 16 sectors/track, 492768 cylinders, total 31537152 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000981cb

        Device Boot      Start         End      Blocks   Id  System
/dev/mmcblk0p1            8192      122879       57344    c  W95 FAT32 (LBA)
/dev/mmcblk0p2          122880    31537151    15707136   83  Linux

Command (m for help): Partition number (1-4):
Command (m for help): Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): Partition number (1-4, default 2): First sector (2048-31537151, default 2048): Last sector, +sectors or +size{K,M,G} (122880-31537151, default 31537151): Using default value 31537151

Command (m for help):
Disk /dev/mmcblk0: 16.1 GB, 16147021824 bytes
4 heads, 16 sectors/track, 492768 cylinders, total 31537152 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000981cb

        Device Boot      Start         End      Blocks   Id  System
/dev/mmcblk0p1            8192      122879       57344    c  W95 FAT32 (LBA)
/dev/mmcblk0p2          122880    31537151    15707136   83  Linux

Command (m for help): The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
update-rc.d: using dependency based boot sequencing
update-rc.d: warning: default start runlevel arguments (2 3 4 5) do not match resize2fs_once Default-Start values (2 3 4 5 S)
update-rc.d: warning: default stop runlevel arguments (0 1 6) do not match resize2fs_once Default-Stop values (none)
insserv: warning: script 'mathkernel' missing LSB tags and overrides
insserv: There is a loop between service watchdog and mathkernel if stopped
insserv:  loop involving service mathkernel at depth 2
insserv:  loop involving service watchdog at depth 1
insserv: Stopping mathkernel depends on watchdog and therefore on system facility `$all' which can not be true!
insserv: exiting now without changing boot order!
update-rc.d: error: insserv rejected the script header
Posts: 28
Joined: Sun Aug 25, 2013 5:58 pm
by DougieLawson » Sun Jan 26, 2014 11:08 pm
Start by fixing the /etc/init.d/mathkernel script

Either
sudo apt-get purge wolfram*
or
copy the first ten lines from /etc/init.d/skeleton to /etc/init.d/mathkernel
Hacker on ZX80 and Microtan65
Unemployed mainframe database specialist.
Linux hacker since 1995.
RPi owner since 2012.
Twitter: @DougieLawson

Gaffer tape is "The Force", it has a dark side and a light side and it holds the Universe together.
User avatar
Posts: 3638
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
by caporeira » Sun Jan 26, 2014 11:16 pm
The first line on this two files looks the same: "PATH=/sbin:/usr/sbin:/bin:/usr/bin"

Ok, I was used this command: sudo apt-get purge wolfram*
and now is installed. I'm config from this tutorial -> Installing the motion detection software (http://www.codeproject.com/Articles/665 ... nce-camera)

But raspberryip:8080 and 8081 port dosen't work :(
Posts: 28
Joined: Sun Aug 25, 2013 5:58 pm