Capturing IP camera images to your Raspberry Pi

8 minutes reading time (1680 words)

I've been running the Windows based  Weather Display software for logging all my weather data for years. One of the nice things it did was to manage my webcamera output, and upload it to my website. With the transistion to the Raspberry Pi, I needed to find a way to grab the webcam image and upload it the site.

To solve the problem I decided to split the solution in two - one to download the files I neeed to the Pi, and the other to upload the files to the website. Of course it would make sense to combine everything into one script, which is what we will build out over the course of two articles. Lets look at how we can get those images off the webcam, and automate that process.

 

Getting the images to the Raspberry Pi

I'm using a Reolink IP Webcam. Before you go out and buy a webcam, one of the things you should consider is how easy it is to get an image off it. If you can find a URL you can call then you are good to go. For more ideas on what to look for when buying a camera, have a look here

For the Reolink I have, here's the template to use to get the image I need:

http://(ip address)/cgi-bin/api.cgi?cmd=Snap&channel=0&rs=(any combination of numbers and letters)&user=(user name)&password=(user password)

For example, if your IP address is 192.168.2.119, the username is admin and the password is 123456, then the URL should be:

http://192.168.2.119/cgi-bin/api.cgi?cmd=Snap&channel=0&rs=wuuPhkmUCeI9WG7C&user=admin&password=123456

Once you have your webcam setup  you should be able to use that URL in a web browser to get the image off the camera. Once you have figured out what url you need to use for your camera, substitute in the username, password and then drop the line into the address bar as you would for any other website. If it all works you should see an image from your camera.

 

Downloading the image into a file with wget

Linux OS's  provide a function called wget that can use to do this. Basically it is a way of retreiving and storing a web resource, and will will work fine for our purposes. You can read more about wget on this site. If you don't have wget on your Raspberry Pi for some reason there are instructions on that site also on how to download and install it.

Wget, like many of the commands we are using to do this work is a command line tool. To get to the comand line on a Raspberry Pi, from the Gui, under the main menu, go to Accessories and click on terminal. A terminal window will open.  The general command format for wget is as follows:

wget <OPTION>... <URL>...

As an example, using the URL we have above, we can retreive the image from the webcam as follows, and store it in the file located at /home/pi/wdssd/WeatherD/webcam_images/webcam.jpg:

wget -q -P /home/pi/wdssd/WeatherD/webcam_images -O webcam.jpg "http://192.168.2.119/cgi-bin/api.cgi?cmd=Snap&channel=0&rs=wuuPhkmUCeI9WG7C&user=admin&password=123456"

In this command, 

-q means that wget does't output any logging info (ie works in "quiet"mode). Use -v if you want to see the logging output for troubleshooting purposes

-P means that wget files will be saved to a subdirectory of the active directory, specified as "/home/pi/wdssd/WeatherD/webcam_images"

-O means that wget should write the downloaded file to the name "webcam.jpg"

Then the URL to the source is specified (ie the URL you worked out before to get the image on the webcam. Not that this is in quotes
You should be able to issue your wget command from the terminal and test it out before you go any further. You can validate it works for checking for the files in thelocation specified via the GUI.
 

 You might find that the image is quite LARGE. On many webcams you can request a lower quality (and therefore smaller) image. For example on my Reolink camera I can add the resolution to the end of the URL:

&width=640&height=480

If this isn't suitable then using imagemagick is likely the best option - but that's for another article!

 

Once is not enough - using the crontab

Since I want to get this file regularly from the camera, I could run the above line of code regularly.  However I still need to upload the file somehow, and I'd want to upload the image each time I got it. So it makes more sense to put it all into some sort of script that I run regularly just need to run once and it will get the image and upload it.
 
The way to make things run regularly in the background on a Linux / Raspberry Pi is to use the Crontab. A cron job is a command that runs regularly at specified intervals. To create the cron job, on the terminal window, type
crontab -e
 
You should see a screen similar to the following, except it won't have the last line in white on it:
crontab
 
However the last line should mostly look familiar. It's the wget command we had above. The characters before it state when the cron job should be run - in this case every 15 minutes.  
 

If you want to change your timing the site https://crontab.guru/ will be able to give you the right command

As a reference, here you have a crontab entry parameters meaning:

* * * * * command to be executed
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)
 Once you have made your changes, press CTRL-X, answer yes, press enter and you are done! The wget command will now run at the interval specified.
 
 

Scripting the webcam image capture with bash

There's one problem with crontabs. If you read the commented out text at the top of the crontab editor you would have found that each line will only run one command.  We've eventually got at least two - one set to download the image as above, and the second to upload to the webserver. Time to start scripting!
 

We're going to write what's called a "bash script." Alvin Alexander provides a really simple and clear way to do that, and I've made some minor modifications around the date and DIR. Here' what it looks like:


#!/bin/bash

# modifications by weather.net.nz with credit to alvinalexander.com
# a shell script used to download an image from an IP Cam URL and scp to a website.
# this is executed from a crontab entry.

#Next line sets logging detail
#set -x

#Set Download variables
# Put images in this directory (must exist)
	IMGDIR=/home/pi/myssd/WeatherD/webcam_images

# wget output animated file
	ANIFILE=webcam.`date +"%Y%m%d%H%M"`.jpg
# wget output webcam snapshot
	IMGFILE=jpgwebcam.jpg
# Specify wget log files. If not specified these end up in the script output
	LOGFILEANI=wgetani.log
	LOGFILE=wget.log
# Specify wget download url.  NOTE: &characters are escaped
	URL=http://192.168.2.119/cgi-bin/api.cgi?cmd=Snap\&channel=0\&rs=wuuPhkmUCeI9WG7C\&user=admin\&password=123456


#Now go and get the file(s) from the webcam 
#Options -O is the output file -o is the log output file
	cd $IMGDIR 
	wget $URL -O $ANIFILE -o $LOGFILEANI 
	wget $URL -O $IMGFILE -o $LOGFILE

 

A couple of things to note about the script:

  • If you strike issues with the script you can easily toggle the logging  in this part by removing the comment tag in front of the "set -x" in the script
  • I've also included the ability to create and download files with a date and time in the name. In the future these will be put together in an animation for the day. If you want to remove this, then comment out any lines that have "ANIFILE" or "LOGFILEANI" on them
  • If you look closely at the URL, you will see some extra "\" characters. These are escape characters that essentially tell the bash script to read / recognise the next character literally. If the escape characters weren't there the script would not work as it would not see the following "&" character
To create a file for the script you can either use nano from the command line or Mousepad from the Raspberry Pi Desktop. Copy the code above into your file, make the changes you need to make for your environment and save it all in a file called something like "webcam.sh"
 
Now you can go back to your crontab
crontab -e
and change that big long line that contained your wget statement, with 
 
 */15 * * * * /home/pi/wdssd/WeatherD/webcam.sh  >> /home/pi/templogs/webcamcron.log

 

taking into account any ammendments you need to fit your situation. Again a couple of points to make:

  • Note that  I've specified exactly the script location - your path will be different . If you don't specify the full path, it's likely the cron job will not work
  • I've also specified that any error output goes to a file called webcamcron.log. If there are no errors to report the file remains blank 
The script as it's written can be moved without issue, so now is a good time to do that if you wanted to. For example I moved my script into the same folder that was running consolewd to keep everything tidy. If you have used the script code above then there are no changes to make in the script itself. Just remember to change the paths in the crontab again!
 

 Next step we need to do is the upload. There's a whole new set of things to do for that so check it out in the second part of the article.

 

 Supporting us and feedback

If you think this article was helpful, there are a number of ways you can thank us. Feel free to add a comment and say thanks in the comments section below. Also you are welcome to register on the site. The more members we have the more it encourages us to write these types of articles. Alternatively if you are into all that social media stuff, feel free to share the article using the buttons below, and tell others about the site. Thanks!!

Font size: +

Related Posts