I have a NoIR cam working with my Pi2 using the uv4l driver. I also have an external 12v IR led array that I would like to set up with the Pi. My plan was to set up a Pi controlled relay to turn on and off power to the led array so that it wasn't on all the time. Optimally, I'd like to have the relay triggered by some event created by a connection to the camera, in other words, for the leds to only be on when the cam is being viewed. does anyone know if this is possible, to have a connection to the camera be a triggering event for GPIO? The GPIO to the relay stuff I can figure out, it's just that I'm a total linux noob, so I'm not sure about how connections to the camera are registered, and how i can tap into that info to use it as an event. Any ideas?
2 Answers
How easy this is depends on how raspivid
works. I don't have my camera connected right now so I cannot check without digging into the source, but it is probably easier for you to just try my suggestion yourself.
The camera does have a device node, probably /dev/video0
if there is only one camera connected. If raspivid
uses this, lsof /dev/video0
should indicate whether any userspace processes are accessing it.
However, using mmap()
hacks on /dev/mem
instead of kernel drivers is a common practice on the pi, and if raspivid
does something like that, there will be no way to tell when it is running unless you just look for an application with that name (e.g., ps -C raspivid
), which is only a problem if you are worried someone is going to use the system and try and circumnavigate your check, since that would then be as easy as copying raspivid
and running it with a different name.
As for doing this programmatically, running strace
on lsof
leads me to believe it actually checks all the file descriptors for all running processes via /proc
(which is probably why it is sometimes used with the setuid bit set, or else will not provide all the answers to an unprivileged user). You could do something like this yourself, or just parse lsof's output.
For more information, see man lsof
. It is a widely used tool with copious online commentaries.
You can easily do it by using lsof
(and UV4L):
sudo lsof /dev/video0
The above command prints out some lines if and only if the camera is being accessed. Based on the presence of the output, you can easily write your script to turn on your leds via GPIOs.
See the lsof
manual to know how to run it in repeated mode for automatic, efficient polling every X seconds.
Note that raspivid
does not create or use any /dev/video nodes, so lsof
won't work with it.
-
sudo lsof /dev/video0
prints information only when the camera is being accessed. this sounds like a good start. thanks.– kenSep 19, 2015 at 21:46
lsof
invoked programmatically, you might try and ask about it on Unix & Linux (which is also larger and busier), although first you should confirm that the exact same call made from the command line at the same time gives a different return value.