winka
Posts: 10
Joined: Mon Jun 09, 2014 2:24 pm

Connecting a pi to a wireless IP camera

Mon Jun 09, 2014 2:41 pm

HI guys, New around here

I've been playing around with my pi for several months now, using the likes of Raspbmc or openELEC.

I've recently installed a wireless IP camera outside which is connected to my home network.

I can view images via my pc and also record to my pc.

My question is this: Is it possible for my Pi to connect to my camera and maybe record images / video to an external HHD?

Thank you

Graham

anita2r
Posts: 226
Joined: Sun Dec 23, 2012 6:55 pm
Location: Ottawa, Canada

Re: Connecting a pi to a wireless IP camera

Mon Jun 09, 2014 9:46 pm

Hi,

It's certainly possible.
There are lots of ways to do this in a Linux environment.

I happen to use perl a lot, so here is a very simple perl script that captures and saves a still image from a network attached camera.

The source in this case is the IP address of the camera and you will need to look at the camera documentation to find out the file it uses to serve still images - in this case (a D-link camera) it is just 'Image.jpg'

The destination address will be a folder on your attached hard drive, or just a local folder for testing it - as in this example.

Code: Select all

#!/usr/bin/perl
#
use LWP::Simple;
#
use strict;
#
# variables
my $source;
my $dest;
#
# setup source/destination;
$source = "http://192.168.0.156/Image.jpg";
$dest = "/home/Cam156.jpg";
#
# save image to folder
getstore($source, $dest);
#
exit 0;
There are lots of perl date commands that allow you to add a date/timestamp to the destination file name.
Then create a loop with a delay and capture images at set intervals.

Regards

anita2R

winka
Posts: 10
Joined: Mon Jun 09, 2014 2:24 pm

Re: Connecting a pi to a wireless IP camera

Tue Jun 10, 2014 9:17 am

Hi Anita

Thanks for you reply,
I'm sorry, but that means nothing to me.
Is this running the "motion" prog?
Is this on the Raspbian os?

Sorry im a newbie to all this, so not up on all the text / programming

Graham

anita2r
Posts: 226
Joined: Sun Dec 23, 2012 6:55 pm
Location: Ottawa, Canada

Re: Connecting a pi to a wireless IP camera

Tue Jun 10, 2014 3:08 pm

winka wrote:Hi Anita

Thanks for you reply,
I'm sorry, but that means nothing to me.
Is this running the "motion" prog?
Is this on the Raspbian os?

Sorry im a newbie to all this, so not up on all the text / programming

Graham
Is this running the "motion" prog - no
I was using a script which is a series of simple commands which are interpreted by the perl language - the first line of the script contains the instruction to send the lines of the script to perl.
perl then does the rest - in this case capturing and saving an image.
perl is a programming language that has a a vast number of modules that can carry out almost anything you want to do (perhaps I exaggerate) .
In this case I have demonstrated using a very short perl script to capture an image from a network attached camera.

Perl can be loaded onto any machine running a Linux based operating system (as well as onto machines running Windows).
So 'Is this on the Raspbian os?' - the answer is yes it happens to be on a Pi with Raspbian, but that is not relevant - because perl could be used on other operating systems, typically Linux based, (I also run perl on Ubuntu for example).

There are likely numerous ways of achieving what you want to do.
You mention 'motion' - motion is also a program that runs on Linux based operating systems
It is very specifically targeted at cameras and as such has many options for using a camera, detecting motion, responding to motion and such things as emailing images and streaming images onto the network (it contains a web server).

There are many web sites about 'motion', but you could start here: http://www.lavrsen.dk/foswiki/bin/view/Motion/WebHome
'motion' normally attaches itself to a webcam on the machine it is running on - the default is /dev/video0.
(The folder '/dev' is where linux shows autodetected attached devices and attached file systems)

I haven't used motion to take output from a network-attached camera, but I see that there is a configuration option named 'netcam_url' that will take the address of a networked camera, so presumably it can use a network-attached camera rather than a usb-attached webcam, or an on-board webcam.

Regards

anita2R

anita2r
Posts: 226
Joined: Sun Dec 23, 2012 6:55 pm
Location: Ottawa, Canada

Re: Connecting a pi to a wireless IP camera

Tue Jun 10, 2014 4:09 pm

Hi,

I just followed-up on the issue of 'motion' using a network-attached camera.

After installing motion on my laptop and making a few changes to the 'motion.conf' file, I was able to get motion to respond to one of my network-attached cameras and save frames when motion was detected.

The relevant line in the config file was:
netcam_url http://192.168.0.156:30156/mjpeg.cgi
(note that you need to remove the comment marker ';' at the start of the line)

This url grabs the camera's video stream output - that's the 'mjpeg.cgi' bit at the end.
This will be different for different makes & models of camera - look at your camera documentation.
Obviously the IP address needs to be changed to the one used for your camera.

Regards

anita2R

winka
Posts: 10
Joined: Mon Jun 09, 2014 2:24 pm

Re: Connecting a pi to a wireless IP camera

Tue Jun 10, 2014 5:02 pm

Wow

Your so knowledgeable on this subject.

Thanks for shedding some light on to this for me, much appreciated.

I will have a little play about as i do have the camera http://.address

Again Thanks for you input

Graham

anita2r
Posts: 226
Joined: Sun Dec 23, 2012 6:55 pm
Location: Ottawa, Canada

Re: Connecting a pi to a wireless IP camera

Tue Jun 10, 2014 8:28 pm

You're welcome,

The following is from notes I made last year when setting up motion on a Pi running Raspbian with a USB-attached webcam, so it was not originally used for linking to a newtwork-attached camera, but hopefully it will give you some hints about getting motion running.
I added some extra notes as you said that you were new to the Pi, and therefore probably new to the Linux environment.

If you do try using motion, i found it helpful to set it up to run under my 'username' rather than its default user 'motion'
Anyway to install motion:

Update & upgrade:
sudo apt-get update
sudo apt-get upgrade

Install 'motion':
sudo apt-get install motion
accept the extra space request

Note the warning:
'Not starting motion daemon, disabled via /etc/default/motion'

Then modify the 'motion' configuration file:
sudo vim /etc/motion/motion.conf
(note that I am using the text editor called vim. You might be using another one.
Wherever I have 'vim', replace it with the name of your editor
Don't edit the files using a windows editor as the line ending/newline characters are different

Edit these lines:
process_id_file /var/run/motion/motion.pid
and replace it with:
process_id_file /home/<userename>/motion.pid
this will make 'motion' run as user:<username>

change the target directory - this is where you can use an external hard drive
target_dir /your/path/to/a/folder

In Linux operating systems, drives, or folders are 'mounted', so your
path is to a mount point. You use the /etc/fstab file to create new mount points
these can include network-attached storage, hard drives or a memory stick on a usb
or even a shared hard drive on a PC

snapshot_filename CamName-%Y%m%d-%H%M%S
Snapshots saved by 'motion' can have a stem name (change 'CamName' to suit) and a timestamp
the format list is fairly obvious - in this case all snapshots will be saved with a full
numerical date followed by ' -' and the time

write/save & quit the configuration file
There are many other items that can be configured in this file. When using the Pi, you may need to limit
framerate and quality so as not to overload the processor

Modify /etc/default/motion
sudo vim /etc/default/motion
change the startup line:
start_motion_daemon=yes
write/save & quit

Modify the init.d motion script to make <username> the owner instead of 'motion'
<username> is your username - the name you log-on with
cd /etc
sudo vim init.d
scroll down to 'motion' and hit enter
edit the following lines in the motion script:
chown motion:motion /var/run/motion
if start-stop-daemon --start --oknodo --exec $DAEMON -b --chuid motion ; then
to:
chown <username>:<username> /var/run/motion
if start-stop-daemon --start --oknodo --exec $DAEMON -b --chuid <username> ; then
write/save and quit

add <username> to the 'video' group:
edit the group file:
sudo vim /etc/group
add <username> to the group 'video'
write/save & quit

Make the motion.conf file accessible
sudo chown <username> /etc/motion/motion.conf
sudo chmod 0770 /etc/motion/motion.conf
(chown is the program that CHanges the OWNer of a folder, file or program)
(chmod is the program that CHanges the MODe of a folder, file or program -
the mode is a set of permissions which specify who can read, write or execute the files
when you do 'ls -l' in a terminal you get a list of folders and files with permissions e.g. drwxrw-r--
where d shows its a directory, rwx means the owner can read, write and execute, rw- means that
anyone in the specified group can read and write but if it was a program could not execute it
and the final triplet r-- means that any user who is not the owner or is not a member of
the group can only read the folder or file)

Start the 'motion' daemon:
sudo /etc/init.d/motion start
or for testing justrun motion directly:
sudo motion -s
(stop it by using Ctrl-C)
Regards

anita2R

winka
Posts: 10
Joined: Mon Jun 09, 2014 2:24 pm

Re: Connecting a pi to a wireless IP camera

Wed Jun 11, 2014 2:35 pm

Thank you very much for your input.

Greatly appreciated.

Regards

Graham

maxisgsm
Posts: 2
Joined: Thu Jan 15, 2015 2:20 am

Re: Connecting a pi to a wireless IP camera

Thu Jan 15, 2015 2:32 am

Hello all,

I'm new here and want to find this type of project for my PI B+.

here my situation,

i've PI, DCS-920 CCTV and DNS-320 NAS ....

how to make my PI connect to DCS and store the motion/image into NAS??

anita2r
Posts: 226
Joined: Sun Dec 23, 2012 6:55 pm
Location: Ottawa, Canada

Re: Connecting a pi to a wireless IP camera

Thu Jan 15, 2015 4:28 pm

maxisgsm wrote:Hello all,
I'm new here and want to find this type of project for my PI B+.
here my situation,
i've PI, DCS-920 CCTV and DNS-320 NAS ....
how to make my PI connect to DCS and store the motion/image into NAS??
Hi,

There are several steps:
1. Get the DCS camera, the Network attached storage device and the Pi running on the same network - all need to have IP addresses in the same range, such as between 192.168.0.1 and 192.168.0.254
Both the camera and the NAS have web page interfaces where you can change the ip addresses from the defaults to ones you choose. For a small home network, fixed IP addresses work well. Using dynamic ip address allocation for the camera and the NAS - the DHCP option on the network setup pages - will make it much more difficult to get this to work and to keep working after reboots as the ip addresses will change.

2. Setup the DCS camera with the video settings you want and test that you can access the video stream. If the camera has an ip address of 192.168.0.100 and you haven't changed the default port address (80), using a laptop on the same network or a web browser on your Pi, point the browser to http://192.168.0.100/mjpeg.cgi
You should see the video streaming in your browser - just the image, no text around it (this is different to using the DLink software on the DCS which requires java and displays the video inside a box with DLink graphics and so on)

3. Setup the NAS, using its web page to create a user - use the same name and password as you are using on your Pi. Then browse to the NAS and create a folder say 'CameraData', and make it available on the network. You may be asked for your NAS username and password to do this.

4. Create a mount point on your Pi, to mount the NAS folder. You will need to edit the Pi's /etc/fstab file and create an entry that looks like this:

Code: Select all

//192.168.0.200/CameraData  /mnt/CameraData    cifs   credentials=/boot/CredFile,uid=1100,gid=1100      0       0
In the above line the first part is the ip address of the NAS device - 192.168.0.200 in this example - the /CameraData is the name of the folder you created on it.
/mnt/CameraData is a folder on your Pi where the NAS folder will appear - this is the mount point. You will have to create the folder 'CameraData' (sudo mkdir /mnt/CameraData) and change it's ownership and permissions to make it read/write/executable by you.
'cifs' is a file system that works for mounting folders on external drives - I can't remember if it is already available on the Pi or whether you have to install it.
The next section contains parameters for connecting to the NAS folder. In this example, the username and password required to log on to the NAS folder are in a simple text file '/boot/CredFile', alternatively you can include username and password - but it leaves them rather exposed for anyone with access to your fstab file to see. uid and gid are the user and group id's that the mounted folder will operate under - look in the /etc/passwd file for the numeric values after your name. Leave the '0 0' as shown - it's not important for this setup, but must be there on the same line.
Mount the folder this one time by entering sudo mount -a
(The folder on the NAS will be mounted automatically each time the Pi boots up)
Create a file in the NAS folder, e.g. touch /mnt/CameraData/test.txt
Now see that its there: ls /mnt/CameraData

5. Now setup motion to capture and save the camera stream. My previous posts give information on setting up motion, and there's lots of information available elsewhere about the program. You will have to install it first. One of the posts decribes the configuration file line that needs to be edited to capture the networked DCS camera stream. For example: netcam_url http://192.168.0.100/mjpeg.cgi (you don't need a port number when using the default port 80)

Once you have this working, you have the issue of your NAS filling up.
You can lower the quality settings in the Camera video settings page, or you can use motion to limit how long the video clips are or when they are captured.
Another option is to just capture and store still images from the video stream at say 1 second intervals.
Finally write a script which deletes captured video or captured images after a certain number of days or weeks. Then the amount of storage required never increases once the initial say 1 week of video/images have been captured.
With the NAS folder mounted on your Pi (/mnt/CameraData) you can use bash scripts or scripts in other languages running on the Pi, to manipulate your stored video / images, not only deleting them but also making images available on a web page if your Pi is running web server software. For example if you create a web page with some buttons and java script, you can interactively bring images or video onto your web page.

Regards

anita2R

maxisgsm
Posts: 2
Joined: Thu Jan 15, 2015 2:20 am

Re: Connecting a pi to a wireless IP camera

Sat Jan 17, 2015 3:02 am

Thank you anita,

I'll try it later .... now very busy with new coming project :D :D :D :D

anita2r
Posts: 226
Joined: Sun Dec 23, 2012 6:55 pm
Location: Ottawa, Canada

Re: Connecting a pi to a wireless IP camera

Sat Jan 17, 2015 5:33 am

maxisgsm wrote:Thank you anita,

I'll try it later .... now very busy with new coming project :D :D :D :D
I also am busy with several projects and spent quite a lot of time putting together information in response to your question.
If you are too busy on other projects then please don't ask for help on something you aren't going to do.
anita2R
PS I am not anita - I am anita2R

untg
Posts: 5
Joined: Mon Sep 24, 2012 12:11 pm

Re: Connecting a pi to a wireless IP camera

Thu Jan 29, 2015 1:51 am

A great option anita2r. There are a couple of things I do extra, I create a temporary /var/tmp/ directory like so:

Code: Select all

tmpfs /var/tmp tmpfs nodev,nosuid,size=100M 0 0
in /etc/fstab

I use this to copy the photos locally which are captured and then I run a script to move them based on day/hour.

The code below I call move.pl and create a cron entry like so:

Code: Select all

*/2 * * * * /usr/bin/perl /root/move.pl
This is move.pl

Code: Select all

#!/usr/bin/perl

use strict;
use warnings;


my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst, $year);
my ($localPath , $hour, $localPath, $dirHasStuff, $move, $make);

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year += 1900;
$localPath = "/mnt/external/"; # Change this to where you want the images moved.
$hour =~ s/^(\d)$/0$1/; # Hour from localtime is a single digit format so doesn't sort well, this regex will fix that.
$localPath .= "$year$mday$hour/"; # A directory of images for every hour of every day.
$dirHasStuff = `ls /var/tmp/ | grep jpg`; # See if there are images captured that need moving.

if (-d $localPath) { # Check if the path already exists.
  $move = `mv -u /var/tmp/*.avi $localPath`;
  $move =  `mv -u /var/tmp/*.jpg $localPath`;

} else { # This means localPath doesn't exist, so create it first but run through only if there are images in the dir.
  $make  = `mkdir $localPath` if $dirHasStuff;
  $move = `mv -u /var/tmp/*.avi $localPath` if $dirHasStuff;
  $move =  `mv -u /var/tmp/*.jpg $localPath` if $dirHasStuff;

}
/var/tmp/ is in memory which means it isn't writing constantly to the local flash.

I've made move.pl so it won't create a directory for every hour, only when there are pictures to save.

I've configured /home/pi/motion-mmalcam.conf to copy the captured images to /var/tmp/

Matthudson16
Posts: 1
Joined: Mon Apr 25, 2016 10:00 am
Location: Ottawa, Canada

Re: Connecting a pi to a wireless IP camera

Mon Apr 25, 2016 10:11 am

Could this be done with pi3 wifi built in? I'm very new a Pi mastered arduino. I have a camera GPIO Pi3 loads of micro sd's plus adapters. How do I set up like Pi2 for wireless ip cam, just so I get a feel for the code? I don't think it's hard but a pro telling me what todo would be better haha? Can any help ip cam using GPIO camera

alfinsyuqra
Posts: 28
Joined: Wed Jul 26, 2017 2:55 pm

Re: Connecting a pi to a wireless IP camera

Fri Aug 11, 2017 8:03 am

hy all ,

i also have a problem about how to install my wireless ip camera to raspberry ,
my camera is TP-Link SC3171G , and it's only have wireless connection and RJ45 port , and dont have USB port connection ,
so can someone help me , tell what should i do to set my ip camera to the raspberry ,

thanks a lot.

Vaibhav05
Posts: 1
Joined: Mon Jan 14, 2019 9:53 am

Re: Connecting a pi to a wireless IP camera

Mon Jan 14, 2019 10:04 am

hello anita2R
i am having wifi IP camera (UNICAM camera) now i wants to capture images using raspberry pi using IP address of my camera . how can i do that . later i wants to do some image processing task on that like face recognition so any suggestions.

Return to “Automation, sensing and robotics”