3

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?

6
  • Your edit substantially changes the nature of your question. Note that this is not a discussion forum, it is a Q&A format and you have received several answers to your original question; the new one seems to be more about basic python programming and belongs on our larger parent site, Stack Overflow. Please take the tour if you have not yet.
    – goldilocks
    Sep 20, 2015 at 17:09
  • Please do. One reason developing a script via back and forth discussion is discouraged is that it's usually less useful to other people down the road than a simple Q&A. E.g., someone who just wants a method of determining whether the camera is being accessed doesn't need to read more than that. The reason making substantial changes to questions is frowned on is hopefully obvious -- unless the people who wrote the answers come back, those answers are now out-of-whack with the new question...
    – goldilocks
    Sep 20, 2015 at 17:42
  • ...And they should not be expected to keep coming back and updating their answers in an open ended way because you keep changing the question. Once you realize you have a distinctly new question, ask it separately. Since this involves the functionality of 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.
    – goldilocks
    Sep 20, 2015 at 17:42
  • i gotcha, thanks. i'll update my question, and delete comments.
    – ken
    Sep 20, 2015 at 17:58
  • hey goldilocks, i actually adjusted my code and came up with a working python script, would it be ok to update here with that, maybe in the form of an answer?
    – ken
    Sep 20, 2015 at 18:50

2 Answers 2

1

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.

1

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.

1
  • sudo lsof /dev/video0 prints information only when the camera is being accessed. this sounds like a good start. thanks.
    – ken
    Sep 19, 2015 at 21:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.