Page 1 of 2

Motion - Ip Camera Possible

Posted: Wed May 15, 2013 12:09 pm
by kiteboywales
Havent used the pi for a while but ordered a camera module yesterday and should be here tommorrow
Been waiting for it for a while tbh

Basically all I want to do is set up a basic IP cam to capture movement and pump the stream out to the web
Does motion do this - I know its picks up usb ok, just not sure if it will pick up the camera board??
I assume this will be a popular project so info will come soon enough - will have a tinker anyway

Thanks for any Info

Re: Motion - Ip Camera Possible

Posted: Wed May 15, 2013 12:16 pm
by piglet
Every time recently I've tried Motion on a Pi I've had a segmentation fault.

Re: Motion - Ip Camera Possible

Posted: Wed May 15, 2013 12:57 pm
by ghans
You can use the command-line in the camera post for that.

No , motion won't detect the camera. No Linux application will
recognize the Raspi camera as webcam , if it is not written
beforehand to work with it.

ghans

Re: Motion - Ip Camera Possible

Posted: Wed May 15, 2013 1:27 pm
by mikerr
Motion is actually quite flexible on input, as well as using the normal V4L and UVC camera input
it can also use IP cameras by putting netcam_url in motion.conf:

So, we can workaround the lack of V4L driver by making a "local" ip camera out of the raspi camera.

motion.conf:

Code: Select all

netcam_url http://127.0.0.1/cgi-bin/raspicam 
- get the Raspi camera to dump jpegs into a local webserver running on the pi:

/usr/lib/cgi-bin/raspicam

Code: Select all

raspistill -o -

Re: Motion - Ip Camera Possible

Posted: Thu May 16, 2013 5:44 pm
by scorp
mikerr wrote:
- get the Raspi camera to dump jpegs into a local webserver running on the pi:

/home/www/cgi-bin/raspicam

Code: Select all

raspistill -o -
mikerr, can you explain in more details how to do that? When I run raspistill it sends garbage on the screen

Re: Motion - Ip Camera Possible

Posted: Fri May 17, 2013 5:13 am
by ghans
mikerr ,
that's a nice solution !

But should raspicam be a bash script ?

Like this , perhaps :

Code: Select all

#!/bin/bash
raspistill -o
ghans

Re: Motion - Ip Camera Possible

Posted: Fri May 17, 2013 7:41 am
by mikerr
Yes, the full script to put in cgi-bin is a bit more expanded than that too:

/usr/lib/cgi-bin/raspicam

Code: Select all

#!/bin/bash
echo "Content-type: image/jpeg"
echo
raspistill -t 0 -w 1024 -h 768 -o -
motion can't handle the full 2592x1944 frame, so I'm reducing it there, and -t 0 captures a frame immediately.

You may need this too:

Code: Select all

 sudo chmod 777 /dev/vchiq
I can confirm motion does actually work with that !

Note that's only about 2 fps, due to starting/stopping raspistill per image.
Motion doesn't support h264 that raspivid outputs, it only supports jpeg or mjpeg.

Re: Motion - Ip Camera Possible

Posted: Fri May 17, 2013 3:06 pm
by SN
a simple solution to the flickering on the screen (because -nopreview is broken) is to make the preview size very small and place it to the left and above the screen so only ONE pixel is updated on the console like so

/opt/vc/bin/raspistill -t 0 -w 1024 -h 768 -p -31,-23,32,24 -o -

Re: Motion - Ip Camera Possible

Posted: Fri May 17, 2013 3:33 pm
by jamesh
SN wrote:a simple solution to the flickering on the screen (because -nopreview is broken) is to make the preview size very small and place it to the left and above the screen so only ONE pixel is updated on the console like so

/opt/vc/bin/raspistill -t 0 -w 1024 -h 768 -p -31,-23,32,24 -o -
If you are up for recompiling, there is a fix for the preview issue in the thread about it.

Re: Motion - Ip Camera Possible

Posted: Fri May 17, 2013 3:36 pm
by SN
jamesh wrote:
SN wrote:a simple solution to the flickering on the screen (because -nopreview is broken) is to make the preview size very small and place it to the left and above the screen so only ONE pixel is updated on the console like so

/opt/vc/bin/raspistill -t 0 -w 1024 -h 768 -p -31,-23,32,24 -o -
If you are up for recompiling, there is a fix for the preview issue in the thread about it.
I know, but sadly I'm not 'up for it' ;-)

Re: Motion - Ip Camera Possible

Posted: Sat May 18, 2013 3:10 pm
by mjb
Motion isn't recognising my camera. I can capture stills with raspistill but I don't think the cgi script is working properly as I get a broken image link when browsing to /cgi-bin/raspicam. Does anyone have any suggestions?

Thanks

Re: Motion - Ip Camera Possible

Posted: Sat May 18, 2013 11:53 pm
by peewee2
likewise, I get the same error,

running wget, I can see the file is only 32 bytes.

I tried using nginx and creating a php script to run raspistill as a system call.

<?php
system ("raspistill -t 0 -ex night -hf -o -");
?>

it works as expected when run from the php shell

ie
php raspicam

but when called from a browser or wget I get

* failed to open vchiq instance

Re: Motion - Ip Camera Possible

Posted: Sun May 19, 2013 8:48 am
by scorp
May be

Code: Select all

chmod 777 /dev/vchid
can help

Re: Motion - Ip Camera Possible

Posted: Sun May 19, 2013 11:39 am
by dozencrows
Rather than blanket change the permissions on /dev/vchiq, it might be safer and more robust to add the user account under which the web server runs to the video group, then reboot.

E.g. for me using nginx, this was:

Code: Select all

sudo usermod -a -G video www-data
sudo shutdown -r now

Re: Motion - Ip Camera Possible

Posted: Sun May 19, 2013 1:32 pm
by peewee2
yes, brain fade - it was late last night, I eventually just added www-data to the video group in /etc/group.

replacing the bash script seems to finally get it working for me ( opens ok in a browser):

#!/usr/bin/perl
select STDOUT;
$|=1;
print "Content-type: image/jpeg\n\n";
system "raspistill -t 1000 -ex night -hf -o -";

next step configure motion.

Pete

Re: Motion - Ip Camera Possible

Posted: Tue May 21, 2013 1:36 am
by Sarge
this was/is a good thread . it got me going on the road to using motion with my raspicamera. :?

there were some dots for me to connect however, from the discussion above, since I'd never done webservers and thought that "cgi" had something to do with computer graphics.

there is good article on setting up a simple server in the last issue of Magpi by Jason Davies, "Setting up a simple intranet". This will get your apache2 server running.

then I found the following on enabling a cgi scripts for apache
http://www.techrepublic.com/blog/doityo ... rver/1066

and i did need to make the permissions change suggested above

Code: Select all

sudo chmod 777 /dev/vchiq
i'm not sure what this permission changes means but suspect i have to do it on every reboot. first i had the raspicam server working, then it wouldn't post pictures, then I eventually figured out that i had to give the permissions again.

now begins the fun with motion.

Sarge

Re: Motion - Ip Camera Possible

Posted: Tue May 21, 2013 3:12 am
by decrot
Are we opposed to using external hardware sensors? Because why wouldn't you guys just wire a motion detector
http://compare.ebay.com/like/1810526712 ... s&var=sbar

and wire it up via gpio and write a bash script to toggle the camera on/off with the motion detector?

Re: Motion - Ip Camera Possible

Posted: Tue May 21, 2013 9:18 am
by Nu7s
Isn't the following aproach easier:

You let the raspicam take pictures every x msec and store it into 1 folder.
When there is motion, you copy these pictures to a seperate "locked" folder.

When space on the first folder gets low, you delete the oldest pictures. The interesting pictures remain safe in the second folder until you review them manually.

This is how the G-sensor in a car-sensor goes about it, to make sure in case of an accident the images are stored safely.

Re: Motion - Ip Camera Possible

Posted: Tue May 21, 2013 10:00 am
by kevsan2402
What I have been doing that seems to work well is;

Use motion with a 'normal' webcam at low res, when motion is triggered, use the 'on_motion_detected' hook to call the raspistill command which then stores a full res image.

not the most elegant solution, but works well so far.

Re: Motion - Ip Camera Possible

Posted: Tue May 21, 2013 5:33 pm
by Sarge
@decrot,
yep, I was thinking along those lines as well but was steered to the motion solution by others on the forum.
my guess is the camera is going to have a larger field of view and certainly a greater range.
the typical PIR sensor has a maximum sensing range of 5 - 7m, the long range version Panasonic sensors have 12m range. I think the camera should be better.
also, motion is continuously taking pictures and comparing pixels to determine change/motion. since it holds on to frames, it is theoretically* able to rewind and capture the whole sequence of events.
since my goal is to view wildlife across an open field, these features would be nice.

S

* I mean the documented features say it is possible. I just haven't implemented it yet.

Re: Motion - Ip Camera Possible

Posted: Tue May 21, 2013 10:22 pm
by dozencrows
@Sarge - if you add the web server's user account (usually www-data) to the video group as described above, that will ensure you have the correct permissions even after a reboot.

Re: Motion - Ip Camera Possible

Posted: Thu May 23, 2013 3:36 pm
by dozencrows
I've had some initial success creating a version of motion which has an "MMAL" camera module and options - if you want to try using it, I've shared it here: https://www.dropbox.com/s/xdfcxm5hu71s9 ... mal.tar.gz

Please be aware this is very early and experimental:
[*] Based on motion SVN trunk at revision 557 - note that conf file syntax is different to the release version.
[*] There is an included config file which works for me.
[*] None of the camera setting options from raspivid/raspistill are currently supported (but hflip is on by default)
[*] You may well run out of memory if you try adjusting the image size and framerate (ENOSPC error)
[*] MySQL and PostgreSQL support are not present.
[*] Should work with the usual motion required packages as available in the Raspbian repos (see http://www.lavrsen.dk/foswiki/bin/view/ ... stallation)

Let me know how you get on if you try it! And whether it'll be worth me putting in more effort (more options, optimisation, packaging properly and sharing the source).

Re: Motion - Ip Camera Possible

Posted: Thu May 23, 2013 5:02 pm
by kmdm
Hi,

Thanks for motion-mmal, please could I get a copy of the source code?

Thanks,
Kenny

Re: Motion - Ip Camera Possible

Posted: Thu May 23, 2013 5:43 pm
by mikerr
Well that's fantastic work, thanks !

Re: Motion - Ip Camera Possible

Posted: Thu May 23, 2013 7:25 pm
by dozencrows
Thanks for the replies!

I'll look into getting the source up on to Github over the next few days (something I've not done before). I also need to sort out the build process; I've currently got it cross-compiling through Eclipse which won't suit everyone. The additional Pi Videocore libs & headers have made it a bit awkward for me personally to use autotools; I'll have a look at cmake as per the rest of the Pi userland stuff.

I'll also do some profiling - the performance is only a bit better than the cgi approach, and some quick experimentation suggests that the ffmpeg and jpeg encoding in software might be proving costly - and I may be doing something "unperformant" in my own code too!

There may be a way to use MMAL to do image encoding from motion via the Pi's GPU to improve speed, but that might mean messing with how motion works. The current implementation fits in quite nicely with motion's architecture and main loop, even down to the MMAL interface returning image frames in the format that motion uses internally.