Howto

how to

This is a work in progress / unfinished / notes how to, but it should be enough to get your webcam working with fswebcam...

Actual commands to enter are in bold


1/ install base version of Raspbian “wheezy”






2/ run basic config of your new image


sudo raspi-config


configure image size, timezone, keyboard, password to something appropriate for you location etc..


3/ check base video config


lsusb

<response>
Bus 001 Device 002: ID 0424:9512 Standard Microsystems Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp.

ls video*
<response>
ls: cannot access video*: No such file or directory


So no webcam installed or detected yet
Now plug in your usb webcam, wait a minute or two, then check its been detected
lsusb<response>
Bus 001 Device 002: ID 0424:9512 Standard Microsystems Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp.
Bus 001 Device 004: ID 046d:0825 Logitech, Inc. Webcam C270


cd /devls video*
<response>
video0


So your RPI has detected a usb webcam, (eg Logitech, Inc. Webcam C270), and allocated it as device video0


4/ load fswebcam package


sudo apt-get install fswebcam

We should now be able to test the webcam using the fswebcam package


5/ basic tests

its worth having a read thro the MAN for fswebcam,  here for example
to start with we'll just try getting the webcam to take a snapshot.

cd ~
fswebcam test.jpg
<response>


--- Opening /dev/video0...
Trying source module v4l2...
/dev/video0 opened.
No input was specified, using the first.
Adjusting resolution from 384x288 to 352x288.
--- Capturing frame...
Corrupt JPEG data: 1 extraneous bytes before marker 0xd6
Captured frame in 0.00 seconds.
--- Processing captured image...
Writing JPEG image to 'test.jpg'.


If you look in your home directory

ls -all


there should be a file called test.jpg,
So this proves your RPI it can connect to webcam and take a picture !


Now we know it works, its better to run fswebcam from a config file, as it saves typos, and kinder on your keyboard ;)


first create a directory to store config and taken photos


mkdir fswebcam


 cd /fswebcam


create a config file


nano fswebcam.conf
this will open the nano editor, copy and paste  the sample contents below, or edit it to suit yourself.
Note the path for the file, it needs to be the path where you will store your pictures


~~~~~~~~~~~~~~~~~~~~~~~~~
device /dev/video0
input 0
top-banner
title "Picam"
peg 95
save /home/pi/fswebcam/fswebcam.jpg
palette MJPEG

~~~~~~~~~~~~~~~~~~~~~~~~~


Use CTRL-X to exit the editor and save the file.

Now when you run the fswebcam make sure you specify the config file


fswebcam -c /home/pi/fswebcam/fswebcam.conf


your files will be created according the the settings in the config file, and images saved in ~/fswebcam

You can now edit this config file to alter the banner settings, titles, image name etc etc.. lots of detail in the MAN page.



7/ now to upload them to a webserver..


I want my pi-cam to take a picture every minute, and then upload them to a webserver.
I also want it to run headless, and not to need any user interaction.
The ncftp client is good for doing the ftp part from a console.

Install ncftp client


sudo apt-get install ncftp


this is so we can use the command line ncftpput command, for uploading to our webserver


to upload to a webserver/ftp server


ncfftp -u username -p password server_ip filename

this will log onto your named server ( eg 192.168.100.1) and upload the file, all from the terminal.


8/ pulling it all together in a script
I want a single bash script that will take the picture, then upload the image to the webserver. That way I can call it in a cronjob automatically.



create a blank script file


nano capture.sh

 this will open the nano editor, copy and paste  the sample contents below, or edit it to suit yourself.


~~~~~~~~~~~~~~~~~~~~~~~
#!/bin/bash
fswebcam -c /home/pi/fswebcam/fswebcam.conf
echo picture taken
ncftpput -u admin -p admin 192.168.100.111 /picam fswebcam.jpg
echo picture uploaded
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Note the paths, username/password, and the /picam path, this is where the file will be uploaded to on the webserver...


We need to make the shell script executable, so change properties of the file


chmod a+x capture.sh


9/ test it...


./capture.sh

should see the image taken from fswebcam, then the image uploaded via ncftpput


10/ sticking it in a crontab
still to do..
to halt each night.. before the time switch reactivates
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# m h  dom mon dow   command

#* 05 * * * date > /home/pi/restart-log.txt 2>&1
36 * * * * /sbin/shutdown -h now >/dev/null 2>&1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


to take a picture every minute, in crontab -e

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 # m h  dom mon dow   command

# uses individual scripts to take a picture every minute and upload
1-59/10 6-20 * * * ./hb-1.sh
2-59/10 6-20 * * * ./hb-2.sh
3-59/10 6-20 * * * ./hb-3.sh
4-59/10 6-20 * * * ./hb-4.sh
5-59/10 6-20 * * * ./hb-5.sh
6-59/10 6-20 * * * ./hb-6.sh
7-59/10 6-20 * * * ./hb-7.sh
8-59/10 6-20 * * * ./hb-8.sh
9-59/10 6-20 * * * ./hb-9.sh
0-59/10 6-20 * * * ./hb-10.sh

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#!/bin/bash

export GDFONTPATH=~/.fonts/
# takes a screenshot  and then ftps it


fswebcam -d /dev/video0 -i 0 --title "HusBos West" -r 640x480 pi-cam09.jpg
fswebcam -d /dev/video1 -i 0 --title "HusBos SouthWest" -r 640x480 pi2-cam09.jpg

ncftpput -u username -p password ftp.site.com

ncftpput -u username -p password ftp.site.com
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~









No comments:

Post a Comment