Thursday, June 25, 2015

Setup Raspberry Pi Camera Module as Motion camera

# Install Motion
sudo apt-get install motion

# Install video libraries and codec  (usually libjpeg62 will fetch dependent drivers)
sudo apt-get install libjpeg62


# Install custom motion-mmal driver. Get latest motion-mmal driver 
cd /tmp
wget https://www.dropbox.com/s/jw5r1wss32tdibb/motion-mmal-opt.tar.gz
tar zxvf motion-mmal.tar.gz 
cd motion-mmal 
sudo cp /usr/bin/motion /usr/bin/motion.orig
sudo mv motion /usr/bin/motion
sudo mv motion-mmalcam.conf /etc/motion.conf 
 
 
 # Set permissions
sudo chmod 664 /etc/motion.conf 
sudo chmod 755 /usr/bin/motion 
sudo touch /tmp/motion.log 
sudo chmod 775 /tmp/motion.log
sudo mkdir -p /usr/local/motion-mmal/data 
sudo chgrp -R motion /usr/local/motion-mmal 
sudo chmod -R g+w /usr/local/motion-mmal/
 
 
# Edit /etc/default/motion
start_motion_daemon=yes

# Edit /etc/motion.conf
daemon on
logfile /tmp/motion.log

# Edit /etc/motion.conf according to your preference
 
# Ensure compatibility with Windows Media Player / VLC
ffmpeg_video_codec msmpeg4   
max_movie_time 180    #3 mins
movie_filename %d-%m-%Y-%H-%M
width 640
height 480
target_dir /home/pi/m-video
output_pictures off
text_left Pi %t
stream_maxrate 100
framerate 100
 
# Capture 2 frames before/after motion detection 
pre_capture 2
post_capture 2 
 

# Date formats 
text_right %d-%m-%Y\n%T-%q
text_event %d%m%Y%H%M%S
snapshot_filename %v-%d%m%Y%H%M%S-snapshot
picture_filename %v-%d%m%Y%H%M%S-%q
movie_filename %v-%d%m%Y%H%M%S
 
# Allow view stream from other machine
stream_locahost off 


# Start motion
sudo service motion start

# Example commands
for effect in none negative solarise sketch denoise emboss oilpaint hatch gpen pastel watercolour film blur saturation colourswap washedout posterise colourpoint colourbalance cartoon
    do
        echo $effect
        raspivid -d -ifx$effect
    done
 
# Testing camera
#!/usr/bin/env python
importtime
importRPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
# Set GPIO for camera LED
# Use 5 for Model A/B and 32 for Model B+
CAMLED =5
# Set GPIO to output
GPIO.setup(CAMLED, GPIO.OUT, initial=False)
# Five iterations with half a second
# between on and off
for i in range(5):
    GPIO.output(CAMLED,True)   # On
    time.sleep(0.5)
    GPIO.output(CAMLED,False# Off
    time.sleep(0.5)

# STORE IMAGES/VIDEOS ON A SEPARATE DRIVE e.g. USB Drive
# Create folder for temporary storage of motion files
sudo mkdir /mnt/motion_tmpfs 
 
# Install NTFS driver, if external drive is NTFS-formatted (optional) 
sudo apt-get install ntfs-3g
 
# Create folder for USB Drive 
sudo mkdir /media/USBFlash1
sudo mkdir /media/USBFlash1/shares 
 
# Install samba for windows file sharing (optional)
sudo apt-get install samba samba-common-bin
 
# Configure samba edit /etc/samba/smb.conf 
sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.orig 
sudo vi /etc/samba/smb.conf
 
security = user # uncomment this line
 
# Add this to smb.conf 



[motion]
comment = Backup Folder
path = /media/USBFlash1/shares
valid users = @users
force group = users
create mask = 0660
directory mask = 0771
read only = no
 
# Add permissions
sudo useradd backups -m -G users
sudo passwd backups
sudo smbpasswd -a backups
 
   
# Add to /etc/fstab (if /dev/sda1 is the drive), estimate 10MB per 3 seconds of video
tmpfs  /mnt/motion_tmpfs  tmpfs   defaults,nosuid,size=80M,mode=0777 0 0 
/dev/sda1 /media/USBFlash1 auto user,umask=0000 0 0
  
# Create copy script from temporary folder to usb drive for files older than 5.05 mins
sudo nano /opt/tmpfsToUSBFlash.sh 
 
#! /bin/sh

while :
do
        find /mnt/motion_tmpfs -type f -mmin +5.05 -exec mv "{}" /media/USBFlash1/ \;
        sleep 2
done 
 
# Create boot scripts 
sudo nano /etc/init.d/tmpfsToUSBFlash 
 
 #! /bin/sh
# /etc/init.d/tmpfsToUSBFlash

### BEGIN INIT INFO
# Provides:          Copies images from the motion tmpfs to a USB Drive
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Simple script to start a program at boot
# Description:       Copies images from the motion tmpfs to a USB Drive
### END INIT INFO

# Carry out specific functions when asked to by the system
case "$1" in
  start)
    echo "Starting copy tmpfs to USB Drive"
    sh /opt/tmpfsToUSBFlash.sh 2>&1 &;
    ;;
  stop)
    echo "Stopping copy tmpfs to serv"
    tmpfsPID=`ps auxwww | grep tmpfsToUSBFlash.sh | head -1 | awk '{print $2}'`
    kill -9 $tmpfsPID
    ;;
  *)
    echo "Usage: /etc/init.d/tmpfsToUSBFlash {start|stop}"
    exit 1
    ;;
esac

exit 0 
 
# Make it executable 
sudo chmod 755 /etc/init.d/tmpfsToUSBFlash 
 
# Update rc scripts 
sudo update-rc.d tmpfsToUSBFlash defaults 
 
# edit /etc/motion.conf 
# change target_dir to point to temporary folder
target_dir /mnt/motion_tmpfs

#CLEANUP MEDIA DRIVE, daily (optional)
find /media/USBFlash1/shares/* -mtime +1 -exec rm -f {} \;


No comments: