Capturing IP camera images to your Raspberry Pi
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:
https://(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:
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 "https://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"
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:
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
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)
Scripting the webcam image capture with bash
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=https://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
*/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
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.