Raspberry Pi Camera Setup - Motion detected movie creation (obsolete): Difference between revisions
m (Heiko moved page Raspberry Pi Camera Setup - Motion detected movie creation to Raspberry Pi Camera Setup - Motion detected movie creation (obsolete)) |
|||
(55 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
This article describes the setup of the [[Raspberry Pi]] Camera with motion to take pictures on <ref>guess what! :)</ref>motion. | THIS PAGE IS OBSOLETE - PLEASE CONTINUE READING HERE: [[Home Automation with Raspberry Pi]] | ||
This article describes the setup of the [[Raspberry Pi]] Camera with motion to take pictures on <ref>guess what! :)</ref>motion. If you want to see, how you make a time-lapse movie rather than a motion-triggered, see [[Raspberry Pi Camera Setup for Time-Lapse]]! | |||
== Setup == | == Setup == | ||
Line 17: | Line 19: | ||
=== Make the [[Pi]] take pictures === | === Make the [[Pi]] take pictures === | ||
==== Make PiCam Device accessible ==== | |||
Since <tt>/usr/lib/cgi-bin/</tt> is the default CGI-directory... | Since <tt>/usr/lib/cgi-bin/</tt> is the default CGI-directory... | ||
* make sure, everyone can access the camera device<ref>including web users</ref>: | |||
chmod o+rwx /dev/vchiq | |||
==== Setup script for taking ''a'' picture ==== | |||
* <del>create a folder named <tt>webcam</tt>:</del> | |||
<del>cd /usr/lib/cgi-gbin | |||
mkdir webcam | |||
chown pi webcam</del> | |||
* <del>create this script named <tt>webcam.cgi</tt>:</del> | |||
<del>#!/bin/bash | |||
echo "Content-type: image/jpeg" | |||
echo "" | |||
# | |||
# timeout should not be 0, since images will be to dark and are noisy | |||
#raspistill --nopreview -t 1 -w 1024 -h 768 -o - | |||
# | |||
# Since my cam is mounted wrong, I prefer to flip it :) | |||
raspistill --nopreview -t 1 -w 1024 -h 768 -o - | convert - -flip - | |||
# | |||
#raspistill -t 0 -w 1024 -h 768 -o - | |||
exit 0</del> | |||
<del>* Make it executable</del> | |||
<del>chmod +x webcam.cgi</del> | |||
This won't work very reliably, because this will occur: | |||
stdout: mmal: mmal_vc_component_enable: failed to enable component: ENOSPC | |||
mmal: camera component couldn't be enabled | |||
mmal: main: Failed to create camera component | |||
mmal: Failed to run camera app. Please check for firmware updates | |||
No solution yet, so I decided to don't let [[motion]] execute the cgi, but crond: | |||
* create this script named <tt>takePicture.sh</tt>: | |||
#!/bin/bash | |||
# | |||
# Globals | |||
# | |||
GFOLDERTARGET="/var/www/webcam" | |||
GFILELOCK="${GFOLDERTARGET}/takePicture.lck" | |||
GFILELOCKAGEMAX="60" | |||
# | |||
# | |||
# | |||
# Main | |||
# | |||
# Check locking | |||
if [ -f "${GFILELOCK}" ]; then | |||
LOCKFILEAGE="`stat --format=\"%Z\" \"${GFILELOCK}\"`" | |||
NOW="`date +'%s'`" | |||
AGE="`expr ${NOW} - ${LOCKFILEAGE}`" | |||
if [ "${AGE}" -gt "${GFILELOCKAGEMAX}" ]; then | |||
echo "Lockfile is ${AGE} seconds old, removing it." | |||
rm "${GFILELOCK}" | |||
else | |||
echo "Lockfile is ${AGE} seconds old, exiting." | |||
exit 1 | |||
fi | |||
fi | |||
# | |||
# | |||
# Lock | |||
echo $$ > "${GFILELOCK}" | |||
# | |||
# timeout should not be 0, since images will be to dark and are noisy | |||
# Since my cam is mounted wrong, I prefer to flip it :) (and flop it) | |||
raspistill --nopreview -w 1024 -h 768 -o - | convert - -flip -flop - > "${GFOLDERTARGET}/lastsnap.tmp" | |||
mv "${GFOLDERTARGET}/lastsnap.tmp" "${GFOLDERTARGET}/lastsnap.jpg" | |||
# | |||
# Unlock | |||
rm "${GFILELOCK}" | |||
# | |||
# Exit | |||
exit 0 | |||
* create a folder named <tt>webcam</tt>: | * create a folder named <tt>webcam</tt>: | ||
cd / | cd /var/www | ||
mkdir webcam | mkdir webcam | ||
chown pi webcam | chown pi webcam | ||
* create this script named <tt> | |||
==== Setup script for taking picture''s'' ==== | |||
* create this script named <tt>takePictures.sh</tt><ref>Execution of <tt>/home/pi/Development/motion_webcam/takePicture.sh</tt> takes approx. 7 seconds!</ref>: | |||
#!/bin/bash | #!/bin/bash | ||
# | |||
# Globals | |||
GSNAPSHOTSPERMINUTE=4 | |||
* | GSNAPSHOTINTERVAL="`expr 60 / 4`" | ||
# | |||
# Main | |||
#for i in {0.."${GSNAPSHOTSPERMINUTE}"..1} # fail | |||
#for i in {0..4..1} # works | |||
for ((i=1; i<=${GSNAPSHOTSPERMINUTE}; i++)) | |||
do | |||
TIMEBEFORESNAPSHOT="`date +'%s'`" | |||
echo "Taking snapshot at `date`" | |||
/home/pi/Development/motion_webcam/takePicture.sh | |||
TIMEAFTERSNAPSHOT="`date +'%s'`" | |||
DURATIONOFSNAPSHOT="`expr ${TIMEAFTERSNAPSHOT} - ${TIMEBEFORESNAPSHOT}`" | |||
if [ "${DURATIONOFSNAPSHOT}" -lt "${GSNAPSHOTINTERVAL}" ]; then | |||
DELAY="`expr ${GSNAPSHOTINTERVAL} - ${DURATIONOFSNAPSHOT}`" | |||
echo "Sleeping for ${DELAY} seconds." | |||
sleep ${DELAY} | |||
fi | |||
done | |||
* Add this to <tt>pi</tt>'s crontab: | |||
# PiCam | |||
* * * * * /home/pi/Development/motion_webcam/takePictures.sh > /dev/null 2>&1 | |||
=== Check if it works === | |||
* <del>The snapshot: http://YOUR_PI_S_IPA/cgi-bin/webcam/webcam.cgi</del> | |||
* The snapshot: http://192.168.1.105/webcam/lastsnap.jpg (change to your [[Pi]]'s IPA!) | |||
* The motion archive: http://192.168.1.105/webcam-archive (change to your [[Pi]]'s IPA!) | |||
=== Get [[motion]] to check 'em === | === Get [[motion]] to check 'em === | ||
< | ==== Install <tt>motion</tt> ==== | ||
apt-get install motion | |||
==== Configure <tt>motion</tt> ==== | |||
Edit <tt>/etc/motion/motion.conf</tt>: | |||
<del>netcam_url http://127.0.0.1/cgi-bin/webcam/webcam.cgi</del> | |||
netcam_url http://127.0.0.1/webcam/lastsnap.jpg | |||
and | |||
target_dir /var/www/webcam-archive | |||
and | |||
width 1024 | |||
and | |||
height 768 | |||
and | |||
framerate 2 | |||
and | |||
minimum_frame_time 0 | |||
Don't forget to relaunch motion: | |||
root@raspberrypi:/etc/motion# /etc/init.d/motion restart | |||
==== How do the scripts work? ==== | |||
* The <tt>takePictures.sh</tt> script will be fired every minute and will take, launching <tt>takePicture.sh</tt> a picture avery 15 seconds (4 per minute) | |||
* <tt>motion</tt> will check the change of the created <tt>lastsnap.jpg</tt> and… | |||
** …will create a movie | |||
** …will create a nicly named set of images as archive of the stills | |||
---- | ---- | ||
* Links | * Links | ||
** [http://www.lavrsen.dk/foswiki/bin/view/Motion/FrequentlyAskedQuestions|Motion FAQ] | |||
** http://www.raspberrypi.org/phpBB3/viewtopic.php?f=43&t=43844 | ** http://www.raspberrypi.org/phpBB3/viewtopic.php?f=43&t=43844 | ||
** https://www.modmypi.com/blog/installing-the-raspberry-pi-camera-board | ** https://www.modmypi.com/blog/installing-the-raspberry-pi-camera-board |
Latest revision as of 23:10, 7 December 2018
THIS PAGE IS OBSOLETE - PLEASE CONTINUE READING HERE: Home Automation with Raspberry Pi
This article describes the setup of the Raspberry Pi Camera with motion to take pictures on [1]motion. If you want to see, how you make a time-lapse movie rather than a motion-triggered, see Raspberry Pi Camera Setup for Time-Lapse!
Setup
Get package list
apt-get update
Update all packages
apt-get upgrade
Enable the camera
raspi-config
Then answer the following stupid questions ("really enable?" and "reboot now?"), and you're done.
Configuration
Now we've set up drivers, we need to verknuspel[2] the Raspi Cam with motion. That is a bit tricky, since motion does not support the Raspi Cam. But it supports webcams. So we need to convert our Raspi Cam into a webcam.
Make the Pi take pictures
Make PiCam Device accessible
Since /usr/lib/cgi-bin/ is the default CGI-directory...
- make sure, everyone can access the camera device[3]:
chmod o+rwx /dev/vchiq
Setup script for taking a picture
create a folder named webcam:
cd /usr/lib/cgi-gbin mkdir webcam chown pi webcam
create this script named webcam.cgi:
#!/bin/bash echo "Content-type: image/jpeg" echo "" # # timeout should not be 0, since images will be to dark and are noisy #raspistill --nopreview -t 1 -w 1024 -h 768 -o - # # Since my cam is mounted wrong, I prefer to flip it :) raspistill --nopreview -t 1 -w 1024 -h 768 -o - | convert - -flip - # #raspistill -t 0 -w 1024 -h 768 -o - exit 0
* Make it executable
chmod +x webcam.cgi
This won't work very reliably, because this will occur:
stdout: mmal: mmal_vc_component_enable: failed to enable component: ENOSPC mmal: camera component couldn't be enabled mmal: main: Failed to create camera component mmal: Failed to run camera app. Please check for firmware updates
No solution yet, so I decided to don't let motion execute the cgi, but crond:
- create this script named takePicture.sh:
#!/bin/bash # # Globals # GFOLDERTARGET="/var/www/webcam" GFILELOCK="${GFOLDERTARGET}/takePicture.lck" GFILELOCKAGEMAX="60" # # # # Main # # Check locking if [ -f "${GFILELOCK}" ]; then LOCKFILEAGE="`stat --format=\"%Z\" \"${GFILELOCK}\"`" NOW="`date +'%s'`" AGE="`expr ${NOW} - ${LOCKFILEAGE}`" if [ "${AGE}" -gt "${GFILELOCKAGEMAX}" ]; then echo "Lockfile is ${AGE} seconds old, removing it." rm "${GFILELOCK}" else echo "Lockfile is ${AGE} seconds old, exiting." exit 1 fi fi # # # Lock echo $$ > "${GFILELOCK}" # # timeout should not be 0, since images will be to dark and are noisy # Since my cam is mounted wrong, I prefer to flip it :) (and flop it) raspistill --nopreview -w 1024 -h 768 -o - | convert - -flip -flop - > "${GFOLDERTARGET}/lastsnap.tmp" mv "${GFOLDERTARGET}/lastsnap.tmp" "${GFOLDERTARGET}/lastsnap.jpg" # # Unlock rm "${GFILELOCK}" # # Exit exit 0
- create a folder named webcam:
cd /var/www mkdir webcam chown pi webcam
Setup script for taking pictures
- create this script named takePictures.sh[4]:
#!/bin/bash # # Globals GSNAPSHOTSPERMINUTE=4 GSNAPSHOTINTERVAL="`expr 60 / 4`" # # Main #for i in {0.."${GSNAPSHOTSPERMINUTE}"..1} # fail #for i in {0..4..1} # works for ((i=1; i<=${GSNAPSHOTSPERMINUTE}; i++)) do TIMEBEFORESNAPSHOT="`date +'%s'`" echo "Taking snapshot at `date`" /home/pi/Development/motion_webcam/takePicture.sh TIMEAFTERSNAPSHOT="`date +'%s'`" DURATIONOFSNAPSHOT="`expr ${TIMEAFTERSNAPSHOT} - ${TIMEBEFORESNAPSHOT}`" if [ "${DURATIONOFSNAPSHOT}" -lt "${GSNAPSHOTINTERVAL}" ]; then DELAY="`expr ${GSNAPSHOTINTERVAL} - ${DURATIONOFSNAPSHOT}`" echo "Sleeping for ${DELAY} seconds." sleep ${DELAY} fi done
- Add this to pi's crontab:
# PiCam * * * * * /home/pi/Development/motion_webcam/takePictures.sh > /dev/null 2>&1
Check if it works
The snapshot: http://YOUR_PI_S_IPA/cgi-bin/webcam/webcam.cgi- The snapshot: http://192.168.1.105/webcam/lastsnap.jpg (change to your Pi's IPA!)
- The motion archive: http://192.168.1.105/webcam-archive (change to your Pi's IPA!)
Get motion to check 'em
Install motion
apt-get install motion
Configure motion
Edit /etc/motion/motion.conf:
netcam_url http://127.0.0.1/cgi-bin/webcam/webcam.cginetcam_url http://127.0.0.1/webcam/lastsnap.jpg
and
target_dir /var/www/webcam-archive
and
width 1024
and
height 768
and
framerate 2
and
minimum_frame_time 0
Don't forget to relaunch motion:
root@raspberrypi:/etc/motion# /etc/init.d/motion restart
How do the scripts work?
- The takePictures.sh script will be fired every minute and will take, launching takePicture.sh a picture avery 15 seconds (4 per minute)
- motion will check the change of the created lastsnap.jpg and…
- …will create a movie
- …will create a nicly named set of images as archive of the stills
- Links
- Footnotes:
- ↑ guess what! :)
- ↑ yes, I just coined the term. I hope it will make it! More about verknuspel!
- ↑ including web users
- ↑ Execution of /home/pi/Development/motion_webcam/takePicture.sh takes approx. 7 seconds!