Hello all,

Well this is something I put together and their may be some mistakes, but should work great. The parameters I wanted was to take a picture using m,y web cam on the PRI-3 and save it to HDD about every 5 minutes and save it for 24 hours; kind of a makeshift security pictures in case something happens it may be caught on camera.

OK here is my current setup on the the Pi -3. Ubuntu Mate 16.04 and is much better than the 15.10 as response is better and has much less overhead. There are 2 USB HDD’s, 1TD HDD drive for O/S and Picture Storage of the web cam, 4TB HDD for media and other backups. A Signalink for my YAAC APRS. If you look at everything it seems like it would be over used, but from watching the mate-system-monitor remotely (with its overhead) not much going on all the time, just when the pictures are taken and last less than 4 seconds and seems to load only 1 processor  during that time. When monitoring remotely the data rate is increased, but other than that much the same as when on the PI’s monitor.

Please always start off with the following:

sudo apt-get update

sudo apt-get upgrade -y

Now for some software information I used for the pictures and how I managed to take all the time (every minute). I use fswebcam software from the repository by:

sudo apt-get install -y

To make sure you have the latest updates to your system.

The “sudo” of course it the superuser command and you will need root’s password. “apt-get” is how you access the repository and “install fswebcam” sort of self explanatory, but just in case, “install” the Software called “fswebcam” and finally “-y” is the answer to the question about if you really want to install it, if you leave it off you will be asked during install. Now the following is a little something I do to check to see if there are packages for your/my install.

sudo apt list > apt.list

This stores the packages into a text file called “apt.list“. Then:

 cat apt.list | grep fswebcam

cat” is that is called concatenate, which for out usage (and there are many more uses) a print the “apt.list” file we just created. now for the “|” vertical bar is the pipe command in Ubuntu or from the older Unix systems (many other systems use this also) that passes out the lines from the previous “cat” command to the “grep” command. now “grep” is a pattern matching program where we are looking for in, this case “fswebcam” package. Now when combined I got:

from executing at the shell or command line shell (Terminal)

cat apt.list | grep fswebcam” I get the following:

fswebcam/xenial,now 20140113-1 armhf

All we need to know is exactly what it is called. usually right up to the “/” is what may be required (try without it first). well from the “sudo apt-get install fswebcam -y” hopefully if everything went OK we have our “fswebcam” program installed and its necessary library’s. BTW: “xenial” is the release name of the current O/S and “xerus” not here, but is the animal. Check “https://wiki.ubuntu.com/DevelopmentCodeNames” to see menacing conventions for Ubuntu. The “armhf” is talking about the computer CPU’s architecture.

Now we have the software installed we need a little more stuff, shell programming to get things going. This is subjective, but First we need a place to store the Pictures we are taking. I use

/mnt/Camera

by using the command

mkdir /mnt/Camera

Make the directory “mkdir” and the path to where we want to store them “/mnt/Camera

and can be either owned by root or your user name. If you need to change it from user name to root, then use:

sudo chown root:root /mnt/Camera

Well we now (or you know) where you want to store your files. mine again is “mnt/Camera“. Lets move on to the shell script file. Mine is called “camera.sh” original I know. Make it anything you will remember or can fine it later. I usually have all the scripts I write into a directory called “/mnt/script“. This comes down from needing to be able to find things I write. Here is the script in its entirety:

/*********************** camera.sh *********************/

#!/bin/sh
# sudo rm -r /var/www/webcam.jpg
#sudo fswebcam -r 1280×720 -d /dev/video0 /var/www/webcam.jpg
#fswebcam –title W4QED –subtitle QTH -r 680×420 -d /dev/video0 /var/www #webcam.jpg

#!/bin/sh

# crontab entry:
# */1 * * * * /mnt/script/camera.sh

# take a picture 920×720 usual resolution
fswebcam -r 1280×720 -d /dev/video0 /mnt/Camera/picture.jpg

# copy it to webpage destination.
cp /mnt/Camera/picture.jpg /var/www/html/webcam.jpg

# make a new filename by date and rename picture.jpg
DATE=`date +%Y%m%d:%H:%M:%S`
mv /mnt/Camera/picture.jpg /mnt/Camera/$DATE.jpg

# delete all files here older than 8 days and should have
# 1440 pictures per day at 8 days = 11520 total pictures
# at approx 27125 bytes each for a total of 312,480,000 bytes kept online.
#find /mnt/Camera/* -mtime +8 -exec rm {} \;

# or, I am now using

delete all files here older than 31 days and should have
# 1440 pictures per day at 31 days = 44640 total pictures
# at approx 27125 bytes each for a total of 1,210,480,000 bytes (1.2GB) kept online
# is changed to 1 per second it would be 72,651,600,00 bytes (72GB) still pictures
find /mnt/Camera/* -mtime +31 -exec rm {} \;

/*********************** end of code *********************/

Lets dissect this a bit:

#!/bin/sh

This is a standard convection to tell the system that you want a shell to execute the following commands. “#!”  get the o/s attention and the “/bin/” is the directory where the shell can be found and it is called “sh” which is the current symbolic link to the system defined shell. That being said, many are symbolically linked to bash shell. We will coma back to the:

# crontab entry:
# */1 * * * * /mnt/script/camera.sh

later. Now the line(s):

# take a picture 920×720 usual resolution
fswebcam -r 1280×720 -d /dev/video0 /mnt/Camera/picture.jpg

refers to the command we are trying to get the system/shell to execute. Note that a “#” is telling the system the following is a comment and is to be ignored by the system, so most people I know use this for comments. I’ll break down the following :

fswebcam -r 1280×720 -d /dev/video0 /mnt/Camera/picture.jpg”

So the “fswebcam -r ” is executing the “fswebcam” program and the “-r “is telling the program the resolution, which I ended up using “1280×720″ resolution of the picture to be taken. now the “-d /dev/video0″ do: “ls -al /dev/vide*/”  to get a list of any already plugged in video sources. This is the device where to get the picture from followed by where to store it and its name: “/mnt/Camera/picture.jpg” Let us continue with the following:

# copy it to webpage destination.
cp /mnt/Camera/picture.jpg /var/www/html/webcam.jpg

The “cp” is the copy command. I use to first store current picture to my web server’s web-page. Check it out http://w4qed.hamshack.info:8080.now the

/mnt/Camera/picture.jpg” is the name of the picture taken along as to where it is located. note there is a space between the file location and name between where I want the file copied to which is “/var/www/html/webcam.jpg” Moving on we have:

# make a new filename by date and rename picture.jpg
DATE=`date +%Y%m%d:%H:%M:%S`
mv/mnt/Camera/picture.jpg /mnt/Camera/$DATE.jpg

I wanted a way to find the files by date worked for me and Date and Time seemed to be easy to figure out for me anyway. So I set a Variable called “DATE” and construct a filename using the “date” convention.  then I set the variable “DATE” by the “=” command. I should make a special note here about just how I came up with this naming and the characters withing it. Try using the following:

date –help

Note “–help” is usually displays a short synopsis on what you can sort of do with the command called in this case “date“. I’m not going into the possibilities of what it can be, just know do the “date –help” if you really want to know more and I do suggest you do it in a terminal window/shell. Also most any command can be used this way, see what “fswebcam –help” does and see what is going on there and you may get an idea as to what I did. The following was commented out, so was left there for what you could do, not what I did.

# delete all files here older than 8 days and should have
# 1440 pictures per day at 8 days = 11520 total pictures
# at approx 27125 bytes each for a total of 312,480,000 bytes kept online.
#find /mnt/Camera/* -mtime +8 -exec rm {} \;

I am currently using:

# or, I am now using

delete all files here older than 31 days and should have
# 1440 pictures per day at 31 days = 44640 total pictures
# at approx 27125 bytes each for a total of 1,210,480,000 bytes (1.2GB) kept on
# is changed to 1 per second it would be 72,651,600,00 bytes (72GB) pictures
find /mnt/Camera/* -mtime +31 -exec rm {} \;
# my current configuration takes a picture every minute and keep it for 31 days. This is a whopping 8.7+GB Disk Storage, but hey I have a 1-TB just for the O/S and sundries.

Now let me try to explain what “find /mnt/Camera/* -mtime +31 -exec rm {} \; ” rius doing. “find /mnt/Camera/*”  is telling the shell/system to “find” all names in the “/mnt/Camera/” directory of course the “*” matches any name. Now “-mtime +31″tells the “find” command to match the pattern with the date the file was created before “+31”. now that list is passed to the command “-exec rm {} \;” then execute the command “rm{}” and delete the list that was found with the entire previous “find /mnt/Camera/* -mtime +31 ” command search and pattern found.

Enough of the camera.sh script. Now I mentioned we would get back to something, Oh yes it was the crontab directive.

# crontab entry:
# */1 * * * * /mnt/script/camera.sh

You should look crontab somewhere else, but here is a very short synopsis. I used the crontab for root by the command:

sudo crontab -e

Now if you have never done crontab editing, you may be asked which editor\ to use, I can’t tell you that, but usually I use nano, it isn’t on all systems and vi or vim is more common, be sure to get use to one of those editors, reading and writing files with it before choosing your editor, they are all different. Now for my short explanation: Add the two lines to the bottom of your crontab file.

“*/1” tells crontab that you want to execute this 1 time a minute. */5 would be every 5 minutes. the next four “* * * *” are for positions of the following “*/1 Minute * Hours, * Day Of Month, * Month, Day OF Week” these are usually referred to as “m h dom mon dow” followed by your command to execute on the previous mentioned pattern. “m” was used up first with “*/“. Then of course what we want done then, execute the script we just entered “/mnt/script/camera.sh“.

This about sums up what I wanted to do and perhaps give you some ideas as to what you may want to do. I am by no means an expert in this, just sharing what I have found out and making an attempt to share this small bit of knowledge.

Again, if you have any questions, let me know. If you see some error’s hey let me know so I can correct them. BTW the spelling error’s are on the house and you can freely use them where ever you want.

Q.E.D.

quod erat demonstrandum.

from my perspective, That which has been shown to be true.

Advertisements