Simple Webcam on Raspberry Pi

The Raspberry Pi’s low power consumption makes it well suited to the role of always-on server. This post describes how to attach a simple webcam to a Raspberry Pi and have it take a snapshot every few minutes, and how to view the pictures on the web. Just like a traditional webcam.

My Raspberry Pi was purchased from New It in the UK. It was installed with Debian 7 (“Wheezy”) by applying the image “2014-01-07-wheezy-raspbian.zip” downloaded from the raspberrypi.org downloads page. The following procedure was then performed without any further pre-work (other than enabling ssh in the basic setup).

Install Motion

The webcam software we will use is called motion. Install motion as follows.

First, change to root user, because many of the following commands require root rights, and it is a bit easier than typing “sudo” every time.

pi@raspberrypi ~ $ sudo su -
root@raspberrypi:~#

Install motion:

root@raspberrypi:~# apt-get install motion

Many other libraries and programs will be installed at the same time.

Connect a Camera

Plug a USB webcam into one of the Pi’s USB sockets. Then type the following command to see how the Pi reacted to the new hardware.

root@raspberrypi:~# dmesg | tail
[  382.538191] media: Linux media interface: v0.10
[  382.581097] Linux video capture interface: v2.00
[  382.781838] uvcvideo: Found UVC 1.00 device USB Video Camera (0471:2034)
[  382.785808] uvcvideo: Failed to register entity for entity 3
[  382.785840] uvcvideo: Failed to register entites (-22).
[  382.786752] input: USB Video Camera as /devices/platform/bcm2708_usb/usb1/1-1/1-1.3/1-1.3:1.0/input/input2
[  382.794596] usbcore: registered new interface driver uvcvideo
[  382.794632] USB Video Class driver (1.1.1)
[  382.894443] usb_audio: Warning! Unlikely big volume range (=65535), cval->res is probably wrong.
[  382.894479] usb_audio: [5] FU [Mic Capture Volume] ch = 1, val = -32768/32767/1[  382.909879] usbcore: registered new interface driver snd-usb-audio

In this case, I attached an old Philips webcam, and it seems to have been accepted, despite the messages about “failed to register”.

Edit the Motion Configuration File

Move into the motion configuration directory.

root@raspberrypi:~# cd /etc/motion

Before editing the configuration file, take a copy of it. This is always a good thing to do. It means you can move back to the original file if your changes don’t work. The copy command also includes today’s date.

root@raspberrypi:/etc/motion# cp -p motion.conf motion.conf.140119

Now edit motion.conf and make the following changes. Change the line which says

output_normal on

to

output_normal off

and change the line which says

ffmpeg_cap_new on

to

 ffmpeg_cap_new off

and change the line saying

snapshot_interval 0

to

snapshot_interval 60

The effect of these changes is to prevent motion from taking a picture every time it detects movement (which could cause hundreds of images to be generated in a couple of minutes), to prevent motion from taking videos, and instead to just take one picture every 60 seconds.

Test Motion

Type “motion” or “/usr/bin/motion” to start the motion program and test the configuration.

root@raspberrypi:/etc/motion# motion
[0] Processing thread 0 - config file /etc/motion/motion.conf
[0] Motion 3.2.12 Started
[0] ffmpeg LIBAVCODEC_BUILD 3482368 LIBAVFORMAT_BUILD 3478785
...

A ton of stuff will be printed, all about colours, pallets, threads and so on. Finally, after 60 seconds or so, a line like this should appear, showing that motion has taken its first picture:

=[1] File of type 2 saved to: /tmp/motion/01-20140119152800-snapshot.jpg

Every 60 seconds thereafter a similar line will appear, showing another picture has been taken. Motion is basically working.

If your webcam is equipped with an indicator LED, it will now probably be illuminated, showing that the camera is active and transferring data.

Look at those Pictures

By default, pictures are stored in /tmp/motion. cd to that directory, type ls, and you should see the jpg files (open another shell or ssh session for this, so that you can leave motion running in the first window).

root@raspberrypi:/etc/motion# cd /tmp/motion
root@raspberrypi:/tmp/motion# ls
02-20140119160700-snapshot.jpg
02-20140119160800-snapshot.jpg
02-20140119160900-snapshot.jpg
02-20140119161000-snapshot.jpg
lastsnap.jpg

Notice the jpg image files and also a thing called lastsnap.jpg. This is always a soft link to the latest picture. View one of the pictures and verify that the camera is working. How you do this depends on how you are using the Pi. If you are logged in over ssh, open an sftp session to the pi, transfer a picture to your local PC and view it there. If you are using the Pi’s graphical desktop, just double click on the image file or something, and it will probably open in a viewer. I am not sure as I use ssh all the time.

Stop Motion and Tidy Up

That completes the test. In the shell where motion is running, enter ctrl-c to stop it. Also, if a large number of pictures have already been generated, delete some or all of them to save space. Or just leave them.

...
[1] File of type 2 saved to: /tmp/motion/03-20140119162000-snapshot.jpg
^C[1] Thread exiting
[1] Calling vid_close() from motion_cleanup
[1] Closing video device /dev/video0
[0] httpd - Finishing
[0] httpd Closing
[0] httpd thread exit
[0] Motion terminating
root@raspberrypi:/etc/motion# cd /tmp/motion
root@raspberrypi:/tmp/motion# rm *jpg

Install Apache Web Server

If a web server is installed, it is possible to view the webcam pictures remotely. Install the Apache web server as follows.

root@raspberrypi:/# apt-get install apache2

Apache2 will be installed and the software will be started.

In a browser, surf to the IP address of your Raspberry Pi. If you see a mostly white page saying “It Works !”, then Apache is running correctly on the Raspberry Pi. For example, my Pi is using IP address 192.168.1.71. So I start a browser on my PC and surf to http://192.168.1.71/. Alternatively, if you are using the Pi’s graphical desktop, you could start a browser directly on the Pi, eg. Netsurf.

Reconfigure Motion to put Images in the Apache Directory

With the following reconfiguration, it is possible to make motion place its images on the web site, so that you can see them from anywhere.

Edit the motion configuration file again:

root@raspberrypi:/# vi /etc/motion/motion.conf

and change this line

target_dir /tmp/motion

to

target_dir /var/www

and save the file.

Start motion again (just type “motion” in a shell) and wait a couple of minutes for it to take a picture or two. The output will indicate that pictures are being stored in the new /var/www location:

root@raspberrypi:/# motion
...
[1] File of type 2 saved to: /var/www/01-20140119164100-snapshot.jpg
[1] File of type 2 saved to: /var/www/01-20140119164200-snapshot.jpg

Reconfigure Apache so Pictures can be Browsed

Not a reconfiguration exactly, just a simple file move. Rename the default index.html file supplied with Apache.

root@raspberrypi:/tmp/motion# cd /var/www
root@raspberrypi:/var/www# mv index.html index.html.140119

This index.html file is what contains the “It Works!” web page that you saw when testing Apache above. By renaming it, you will be able to see your webcam pictures instead. Check it by surfing again to http://[Pi IP address], or just refresh the page in your browser. In my case, I run a browser on my PC, and surf to http://192.168.1.71/, because my Pi is on that IP address.

What you will see in the browser is a directory listing of /var/www on the Pi, showing all of the image files generated so far. View the latest image by clicking on “lastsnap.jpg“. You should not see a gorgeous snapshot of your desk, wall, garden or whatever, with a neat time stamp in the bottom corner proving when it was taken.

Refresh the browser page at any time to see the latest image (watch the time in the corner of the picture change when you click “refresh”).

Done!

That does it for a basic webcam setup on the Raspberry Pi. There are many options to try with the motion program. By changing the motion.conf file, you can get it to:

– take higher resolution pictures
– take videos
– detect motion (as the name suggests)
– take timelapse videos

…and so on. Before making any changes, I would recommend always taking a copy of motion.conf with a name showing the date. (cp -p motion.conf motion.conf). The motion.conf file is complicated and the options numerous. Having a way of undoing any changes is always a good idea.

Likewise, the file /var/www/index.html, that you have now renamed to /var/www/index.html., can be edited to present a nice web page. If you know some HTML, you could re-write the file to, for example, present the latest snapshot in a frame, or with some text around it, or several images, or thumbnails, or whatever.

Let’s be Sensible

At the moment, your motion installation is taking a picture every 60 seconds. You probably don’t want to take that many pictures, and eventually they will fill your SD card anyway.

Press ctrl-c in the shell where motion is running to stop the program. Then proceed as follows.

Configure a Sensible Snapshot Interval

Edit the motion configuration file again and change the picture interval to something more sensible than 60 seconds. I would suggest either 5, 10 or 15 minutes. For example:

root@raspberrypi:/# vi /etc/motion/motion.conf

Change the line

snapshot_interval 60

to

snapshot_interval 300

for a 5 minute interval, or 600 for 10 minutes, or 900 for 15 minutes.

Remove Old Images

While motion is running, it will continue to generate picture files more or less forever. The file sizes will depend on how posh your webcam is and the resolution configured in motion.conf.

It is a good idea to clean up and remove the old files periodically. You could just do an “rm” every so often in the output directory /var/www. If you want clean up to happen automatically, proceed as follows.

Automated Image Clean Up

Create a crontab and enter job details as follows.

Note: The first two commands here are used to set the editor to “vi“. By default, the crontab -e command will bring up a nano editor session, and I don’t know how to use nano, so I set the EDITOR variable make it use vi instead. If you know nano, go straight to crontab -e, and use nano.

root@raspberrypi:/# EDITOR=vi
root@raspberrypi:/# export EDITOR
root@raspberrypi:/# crontab -e

… an editor session will be opened. In the editor, go to the end of the file and add this line:

15 * * * * (date; ls /var/www/*snapshot.jpg | head -n -20 | xargs rm -v) >> /tmp/images_deleted 2>&1

then save the file.

Now that the above cron job is in place, it will be executed at fifteen minutes past the hour, every hour. All snapshot images except for the 20 most recent ones will be deleted. If you want to keep more images, say 50 or 100, type crontab -e again and change the “-20” to “-50” or “-100”.

Cleanup Log File

In order to see what files the cron job has deleted, look at its log file /tmp/images_deleted after a few hours. For example:

root@raspberrypi:/var/www# cat /tmp/images_deleted
Sun Jan 19 17:32:01 UTC 2014
removed `/var/www/01-20140119164100-snapshot.jpg'
removed `/var/www/01-20140119164200-snapshot.jpg'
removed `/var/www/01-20140119164400-snapshot.jpg'
removed `/var/www/01-20140119164500-snapshot.jpg'
removed `/var/www/01-20140119164600-snapshot.jpg'
removed `/var/www/01-20140119164700-snapshot.jpg'
removed `/var/www/01-20140119164800-snapshot.jpg'
removed `/var/www/01-20140119164900-snapshot.jpg'
removed `/var/www/01-20140119165000-snapshot.jpg'
removed `/var/www/01-20140119165100-snapshot.jpg'
removed `/var/www/01-20140119165200-snapshot.jpg'
removed `/var/www/01-20140119165300-snapshot.jpg'
removed `/var/www/01-20140119165400-snapshot.jpg'
removed `/var/www/01-20140119165500-snapshot.jpg'
Sun Jan 19 17:33:01 UTC 2014
removed `/var/www/01-20140119165600-snapshot.jpg'
Sun Jan 19 17:34:02 UTC 2014
removed `/var/www/01-20140119165700-snapshot.jpg'

If you noticed that my log file shows the cron job running every minute and not once an hour, – yes, that is because I was just testing the job to make sure it worked right.

Running Motion in the Background

If the motion program is running in a shell, it will likely die when you either close the shell or logout of your Pi. If you want it to run all the time, run it “in the background” instead.

If motion is running, type ctrl-c to stop it.

Invoke motion as follows:

root@raspberrypi:/# nohup /usr/bin/motion > /tmp/motion.log 2>&1 &
[1] 4880

It is now running with a process id of 4880 (in the example). Check the log to verify:

root@raspberrypi:/etc/motion# tail /tmp/motion.log
[1] mmap information:
[1] frames=4
[1] 0 length=153600
[1] 1 length=153600
[1] 2 length=153600
[1] 3 length=153600
[1] Using V4L2
[1] Resizing pre_capture buffer to 1 items
[1] Started stream webcam server in port 8081
[1] File of type 2 saved to: /var/www/01-20140119184000-snapshot.jpg

You can log out of the Pi, and it will continue to take a picture every 5 minutes (or whatever) and display it on the web site, while logging its output to /tmp/motion.log.

Conclusion

I hope this guide has been useful and not too long or fiddly. You should now have a basic working webcam on the Raspberry Pi that can be viewed in a browser.

If you want to be able to view the webcam from across the internet, ie. from locations other than your home network, it is necessary to forward a port on your internet router. That is beyond the scope of this article. However it is quite easy to do and many guides exist on the internet.

Footnote

The above article avoids some of motion’s more advanced features, eg the built in “live” webcam server, the motion detection, and the recording of videos. These features can be explored by the more advanced motion user, but they are not suitable for a introductory article, they put a higher CPU load on the Pi and can take large amounts of storage space. Also the “live” streaming feature can max out your ADSL upload capacity if viewed remotely.

UPDATE 19th February 2015 – the recently released quad core Raspberry Pi 2 could probably handle motion’s video capture features, so long as enough storage is provided.

47 thoughts on “Simple Webcam on Raspberry Pi

    • Hello,

      I had the same problem. You must write “sudo chmod 777 /etc/motion/motion.conf” to change the permission to the conf file.

      Martin

  1. Not sure what problem you are having Shree, but you need to be root user for the above procedures to work. Permission denied when editing motion.conf might be because you are not logged in as root.

  2. Hi, I’d like to know if it is possible to take a video with the same tool or with something like that. Thanks for the guide, really clear.

  3. Hi Alex, yes the motion tool can capture videos as well as still images. For example, it can record a short video when motion is detected. You have to edit the motion.conf file to enable videos.

  4. hi,
    Nice tutorial!
    It would be nice if you could add a description how you would build in a photo gallery.
    Something like a folder per day or …..

  5. Great tutorial!
    A couple of questions after testing it a bit:
    1) Is it possible to switch off the video output of motion (so it only does the webcam stills every 10 minutes)?
    My setup works fine for the still images but I am afraid the video handling will bog down the pi unnecessarily….
    2) Another thing:
    How can I make motion survive a reboot (start up again afterwards)?
    I tried editing the motion.conf file and setting this:
    daemon on
    But it just makes the motion program switch to the background mode when started and return the user to the shell just like the nohup command you have listed.
    But what can be done to make it start whenever the Pi restarts???

    • I1) Yes you can set up motion to take videos as well as regular stills. Just turn on the following option in the config file:
      ffmpeg_cap_new on

      Along with
      output_normal off

      and
      snapshot_interval 600

      motion should start taking snapshots every 10 minutes as well as videos when motion is detected. I haven’t actually tested it though.

      (2) You should be able to put a script under /etc/init.d or add a motion command to /etc/rc.local. Just google for “starting services automatically on pi” or something, there will be tons of hits.

  6. Referred here from the Pi forum:
    http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=99510&p=697781#p697781

    It seems like the cron job to prune the snapshot files to the 20 latest ones does not work. Instead it deletes the newest files and leaves the old files in place.
    I have looked at the /tmp/images_deleted file and this is what I see (notice that I have a 10 minute snapshot interval so there are 6 files created per hour):

    Tue Feb 17 21:15:01 CET 2015
    removed `/var/www/webcam/20150217202000-snapshot.jpg’
    removed `/var/www/webcam/20150217203000-snapshot.jpg’
    removed `/var/www/webcam/20150217204000-snapshot.jpg’
    removed `/var/www/webcam/20150217205000-snapshot.jpg’
    removed `/var/www/webcam/20150217210000-snapshot.jpg’
    removed `/var/www/webcam/20150217211000-snapshot.jpg’
    Tue Feb 17 22:15:01 CET 2015
    removed `/var/www/webcam/20150217212000-snapshot.jpg’
    removed `/var/www/webcam/20150217213000-snapshot.jpg’
    removed `/var/www/webcam/20150217214000-snapshot.jpg’
    removed `/var/www/webcam/20150217215000-snapshot.jpg’
    removed `/var/www/webcam/20150217220000-snapshot.jpg’
    removed `/var/www/webcam/20150217221000-snapshot.jpg’
    Tue Feb 17 23:15:01 CET 2015
    removed `/var/www/webcam/20150217222000-snapshot.jpg’
    removed `/var/www/webcam/20150217223000-snapshot.jpg’
    removed `/var/www/webcam/20150217224000-snapshot.jpg’
    removed `/var/www/webcam/20150217225000-snapshot.jpg’
    removed `/var/www/webcam/20150217230000-snapshot.jpg’
    removed `/var/www/webcam/20150217231000-snapshot.jpg’

    As you can see by the cron job timestamp and the timestamp built into the name of the snapshot files it actually deletes the most recent files….

    • You’re right. It is deleting all snapshots except for the earliest 20. What’s needed is to delete all snapshots except for the latest 20.

      Here is a corrected cron job. Article updated accordingly. Cheers.

      15 * * * * (date; ls /var/www/*snapshot.jpg | head -n -20 | xargs rm -v) >> /tmp/images_deleted 2>&1

  7. motion directory (nor its files) never gets created. I’m using the first part of the tutorial (i.e. not using Apache etc).It seems to be repeating:
    [0] Thread 1 – Watchdog timeout, trying to do a graceful restart
    [0] httpd – Finishing
    [0] httpd Closing
    [0] httpd thread exit
    [0] Thread 1 – Watchdog timeout, did NOT restart graceful,killing it!
    [0] Calling vid_close() from motion_cleanup
    [0] Closing video device /dev/video0
    [0] Motion thread 1 restart

    PS email me if you want me to send you the complete Terminal output.

  8. Hi John, I am not sure what is causing those errors. As you are not following the procedure above, unfortunately it is not really possible for me to say what the problem might be. Hope you get it fixed anyway.

    • I’ve never managed to make Motion work with more than one camera on the Pi, Ahmed. It seems to be a Pi/USB limitation (works on other systems). I would stick to just one camera. You could always get a second Pi to run another.

  9. Have got Motion running on my Pi but want to change target_dir from /tmp/motion (default).
    When I try to change to a folder I have created (e.g./home/pi/pictures) Motion will not run. Changing back to /tmp/motion solves the problem. What else do I have to do to change the target_dir?

    • Hi Randy. No it can’t be used directly with the Pi camera unit, only a USB camera. I did experiment with it by having both a USB camera and the Pi Camera attached, then setting up an action in the motion.conf file to get the pi camera to take a photo every time the USB camera detected motion. The results were patchy, mainly due to the time delay in capturing a picture with the Pi camera – usually it was too late to capture the source of the motion. Also, of course, the Pi camera image lacks the helpful Motion legend and box drawn around the moving elements.

      Getting the pi camera to take a short video instead was a bit more successful, but can consume a lot of storage space very quickly.

      However there is a version of Motion compiled to use the Pi camera. It’s called motion-mmal – just Google that and you will get it straightaway. I haven’t tried it personally though.

  10. Hi, this works well using an old logitech webcam. I am taking a picture of my garden every 10 mins. The webcam shows only a black picture after the sun has set, although street lighting should give some view. when the sun is shining, but not directly on the camera, the webcam pic is totally white. what do i adjust in motion.conf to show a picture when the sun is shining and when it is almost dark. it works ok if the sky is overcast during the day. thanks

  11. bought a new webcam which does the job, logitech c310, using a powered usb hub with wireless dongle and usb stick.
    my question is how to save motion jpg pics to the apache server to view online, as well as the usb stick. i want to reduce read/write on the sd card, so have the cron job mentioned above to delete all except the last 20 pics. i can’t get motion to save to two locations, it is either one or the other. i tried the & & code, but it defaults to the apache www file, and ignores the second part media/usb.
    has anyone tried this? any ideas on saving to two locations?
    thanks

    • Hi Jules,

      To answer your question about taking pictures when the sun is shining directly on the camera, it would seem to be an issue with the camera rather than motion. There are many motion settings to adjust the picture, but I don’t know of one what would help with that problem. Trying a different camera (as you have done) is probably the best idea.

      Regarding the C310, it is the same camera that Ahmed was using above. It isn’t quite clear what you are trying to do, but it seems you have one camera and you want to place the pictures in two different locations. I don’t think Motion has a function for that. You could try creating a script to copy the pictures over, but that is not a very good solution. And it won’t reduce the writes on your SD card.

      If you want all pictures to be stored on the USB stick only, you could move /var/www to the stick, for example by simply stopping apache and mounting the stick at /var/www. If you are thinking that your SD card will fail because of too many writes, I wouldn’t worry about that. I have some Linux systems based on SD cards that have been running for years, and are written too constantly (more than 10,000 writes a day), without a problem.

      • thanks for the ideas, i have everything sorted now. upgraded camera to logitech c920. pi boots to sd card only, then motion runs from the usb stick.

        a few odd results, i had to change my router after 6 years, (overheated) so a lot of fiddling with the new router which of course has a different ip address.

        motion pics are stored every 10 mins and can be viewed on my local ip.
        but if i try to access photos from outside my lan, with the router ip, the webcam updates every few seconds, despite motion config changed to snapshot interval of 600 secs!

        not sure what i have changed with the new router, any ideas? thanks

        • Hi Jules, it’s good to hear you made progress. Also good to have confirmation that the C920 camera works properly with the Pi.

          Regarding your router, it seems that the motion web site behaves differently depending on whether you access it from your LAN or from outside. I don’t know your exact setup, but to access the site from outside, you must have forwarded a port on your router. If so, check the forwarding again – if you pointed the port to port 8081 on the Pi (instead of 80), it might be connecting you to the built in Motion “live stream”, which updates every few seconds.

          You could confirm by turning off the live stream in the motion log file. Edit the motion.conf file and look for the “Live Webcam Server” section. Change the line:

          webcam_port 8081
          to
          webcam_port 0

          and restart motion.

  12. hi, have done a reinstall of motion after a problem. Original was working well from last Nov 2015 following your instructions.
    I have a problem with the new install!
    sudo apt-get install motion does download and install it, but it has a suffix I have not seen before.
    I have motion 3.2.12+git20140228 which seems to be different from the original.
    When I go into motion for the first time to make the changes, some lines are not found.
    Example: webcam_localhost (not found)
    output_normal (not found)
    ffmpeg_cap_new (not found)
    It also appears to have daemon on by default.
    My webcam did not take any pics after 60 secs during a test setup.
    Is this a different version of motion?
    How can I get the original one 3.2.12 without the git part and date from 2014 which seems to be causing a problem?
    Has anyone else noticed this?
    Thanks for your help and advice,
    Jules

  13. Regarding my last post, i have since done some research and the latest motion does have changes to some text lines, making the original install more complicated. i cant find ffmpeg_cap_new on this new version, which is installed by default by sudo apt-get install motion.
    How do i install the previous version 3.2.12 which was working fine?
    I think wget is involved somewhere, but not sure how to download and open the tar.gz file.
    Any ideas? thanks,
    jules

  14. There are a lot of configuration changes in the latest motion (3.2.12+git20140228)
    This will effect anyone following the above webcam instructions.
    I have made all the changes I could find, but now there is a problem with changing the file on Apache so pics can be viewed on it using local network. The “it works” Apache page is shown, but can’t get pics to be shown on it.
    Here are the main changes on motion:

    output_normal > output_pictures
    ffmpeg_cap_new > ffmpeg_output_movies
    webcam_localhost > stream_localhost
    webcam_port > stream_port
    control _localhost > webcontrol_localhost
    /tmp/motion > /var/lib/motion

    Also daemon is on by default
    For some reason, I am getting a live webcam stream when none is intended.
    If anyone can get this working, I would be pleased to hear what you did…

    jules

    • Hi Jules

      Thanks for coming back with all this new information. It seems that motion, after being abandoned by the developer for years, is now being actively developed once more. This is excellent news as we can look forward to lots of improvements.

      However, as you point out, the version of motion in the Raspbian repos is now a 2014 version (3.2.12+git20140228-4) instead of the 2010 version used in the article above (plain old 3.2.12-3.4). There are many changes to option names and thank you for pointing those out. Lots more information is available at the new Motion home page. There is also a Roadmap, a list of config options and release notes.

      The above article really need to be rewritten now to adapt to the new motion. I’ll do that if I get time. About downloading the original version, look around the site and it is available in source code, but I couldn’t find the binaries. The best course of action might be to continue looking at the new version until it works the way you want. NB the way of turning off the live stream was to set the port number to zero in the old version. Not sure what it is now.

      Cheers,
      Jim.

  15. Glad you saw my comments about the new version of motion, and I look forward to your rewrite if time allows.
    This is the easiest to follow use of motion for snapshot pics I have come across. Most are for live webcam streaming.
    One other point! I also think that the latest Raspbian is not compatible in some way. Since May, Jessie seems to be the one to download, rather than Wheezy.
    With changes, I have got the latest motion to work taking snapshots every 900 s. I have the Apache server up and running “it works”, but the file move you showed no longer works to show webcam snapshots on Apache. The updated page asks the viewer to change the html.

    This command no longer works:
    mv index.html index.html.(for me 16.06.07)
    it returns with error mv:cannot stat ‘index.html’: no such file or directory

    The live stream seems reluctant to turn off! I see it updating every 2-3 seconds, must be reducing my bandwidth.

    I can see the last 20 jpg snapshots on var/www# ls so they are being taken and stored, but not to the Apache server. I cant view them on my local network.

    • Hi Jules. The live stream won’t take any bandwidth unless you or somebody else is actually watching it. Regarding the file move, note that under Jessie, the default location for html files has changed from /var/www (as it was in wheezy) to /var/www/html. The above procedure is written for wheezy, so does not take that into account.

  16. So I have changed /var/www to /var/www/html in motion.conf
    Then I applied the move
    root@raspberry pi:/var/www/html mv index.html index.html.160607
    and get the message mv: cannot stat ‘index.html’: No such file or directory

    I can still see snapshots at /var/www/html# ls

    But when looking at the default apache2 page, I still get t he default ‘it works’,
    can you explain this? i am getting close i think….

    one extra thing, the latest apache2 also has changes!
    Thanks, Jules

    • Hi Jules, if the mv command responds “cannot stat ‘index.html’: No such file or directory”, it simply means that the file you are trying to move (index.html) is not in your current working directory, so mv doesn’t know what file you mean. Without actually being there it is difficult to provide more detailed guidance. It sounds a bit like you might benefit from reviewing the basics of navigating the directory structure and copying/moving files, using commands like mv, cp, pwd and cd. These are skills you will really need when setting up software on the Pi. There are plenty of free guides out there.

  17. So, this is an awesome article! Worked very well for what I wanted to do. I see that the delete after so many pictures (20) in the instructions happens to generate an error in the log file. I do not know why, as I am as green as grass when it comes to this stuff. Perhaps someone could shed a bit of light on the problem. Of course now I cannot find the log file I was looking at with the error so I can’t do the copy/paste.
    I am trying to get this line to run in crontab:
    10 * * * * find -path ./var/www/html -size -12k -type f -delete
    The purpose of the file is to delete black frames as they appear as .jpg generated under about 12k. I have this line before (10 min) the one that does the oldest file deletes (15 min). If anyone has an idea why my 12k line does not appear to work please leave a note. Thanks.

    • Thanks Gill. Without seeing the error message, it is a bit difficult to say what the error might be. Looking at your “find” command though, it seems a bit strange. I am not sure why it contains the “-path” flag. And there should be no dot in front of “/var/www/html”. If you are trying to delete files under /var/www/html that are smaller than 12k, try something like this:

      find /var/www/html -size -12k -type f -delete

      If you want to be a bit safer, and avoid deleting something by accident, perhaps add a bit to make sure it deletes only files ending in “jpg”, ie. pictures, like so:

      find /var/www/html -name ‘*jpg’ -size -12k -type f -delete

      Cheers,
      Jim.

      Note the path should not begin with a dot, as

      • Jim, thanks for the reply! I fiddled around with the line of code and eventually went with something a bit different.
        10 * * * * (find /var/www/html/*snapshot.jpg -size -13k -type f | xargs rm -f)
        15 * * * * (date; ls /var/www/html/*snapshot.jpg | head -n -100 | xargs rm -v) >> /tmp/images_deleted 2>&1
        20 * * * * (date; ls /var/www/html/*.avi | head -n -20 | xargs rm -v) >> /tmp/images_deleted 2>&1
        The 20 line is my next challenge and a modification of the 15 line to deal with the the .avi files generated when motion capture is running and not just the hourly snapshot. I am playing again. I did not have a lot of success with the ‘find’ and attribute that lack to inexperience. With Linux there seems to be more than one way to do just about everything. The 10 line appears to be working quite well now as I am getting hourly pictures from 8am to about 4pm which is daylight hours here at this time of year. Thanks again for sending me a reply and I am inclined to try ‘find’ again at a later date as it bothers me as to why it did not work as expected. I did find out that the ./ was a problem.

  18. I set the snapshot_interval to 3600 but I’m getting 3 images per second. The images are not being saved as -snapshot.jpg, just a date time stamp and a number.jpg. Is there a wayt o fix this? Thanks.

    • Hi Tom, pardon the long delay in replying. Motion performs several functions at the same time. For example: taking snapshots at regular intervals, taking snapshots in response to motion, taking video in response to motion. The 3 images per second you are seeing are likely due to some other function being active. Make sure all things are disabled in the motion configuration file except the function you want. This is why, in the procedure above, options such as “ffmpeg_cap_new” and “output_normal” are set to “off”.

      I would recommend restoring the original configuration file (from the backup copy made in the first part of the procedure), and making the changes again, in case there was some mistake. Then restart motion.

  19. Hi,
    Returning to this after a few years. Worked for me with the original settings, but
    many changes with Debian 9 Stretch, Apache2 and Motion all being upgraded.
    Have moved house, and now have a mountain view to take snapshots of using webcam.
    I have been playing with the file move from motion to Apache2, but here is where I get stuck……
    Running as Root. I made a new folder for Webcam. Checked all permissions and all are the same, owned by root. Checked with “namei -m /home/pi/webcam”
    Pics saved to home/pi/webcam and can be see there when I do “ls -al”
    The file move……root@raspberry pi: /var/www/html# mv index.html index.html.260218 takes place. I was getting a “can’t stat” error in the past.

    When I go to localhost, I can see “Index of/ ” and a folder called index.html.260218
    but no jpg pics!
    I have checked that document root in Apache sites available is /var/www/html
    Haven’t played around with the Apache2 conf files yet. My meddling messes up and I have to download and start again!
    Any ideas where I am going wrong?
    Thanks.

    • Hi Jules. The above article is rather out of date now, and could do with a re-write. Thanks for coming back and posting your solution. Cheers,
      Jim.

  20. Fixed!
    It needed a quick apache configuration change. In apache2/sites-enabled change DocumentRoot to /home/pi/webpics (or your choice), and also add a Directory Block underneath.
    I work in root to keep all permissions the same.
    Motion target directory changed to /home/pi/webpics
    My file move was wrong!
    Made new directory called webpics in home/pi
    CD to /home/pi# mv webpics /var/www/html
    localhost in a browser on your LAN (I use Firefox) will give jpg pics. “Index of/” and they are all listed. Easy-peasy when you know how. I struggled for days to get it right.
    Hope this helps.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.