Friday, July 14, 2017

Building a motion sensing camera with a Raspberry Pi

A Raspberry Pi and a pinoir camera make for a good motion capture camera. There is always an impetus to hack, the impetus in this case came from theft at my company. At night, someone had been walking off with hardware from cubicles. One was just down my aisle and was perhaps too close to home.  Not wanting any of my stuff wandering off, I wanted a camera that would record motion and save video only around the event. I did not want to process 8 hours of raw, mainly motionless video each day when I came in. Currently as it works, each file per night is about 80MB. These files are saved over the LAN on a file server, in this way not polluting the SD card on the Pi. The files can be viewed by VLC and are H.264.

For the motion sensor, an Adafruit PiR sensor is used:

This sensor needs some connectors added, a small crimp connector is added to the bare wires and hot air used to shrink a sleeve around the bare connector. For quick and dirty hacking, my goto language these days is Ruby, this project was no exception. A gem called PiPiper (Silicon Valley fans?) is used to convert GPIO levels to events that Ruby can act on. An event called out by the GPIO going high is generated by the sensor when there is motion, this in turn unblocks a forked raspivid process (raspivid being the canned camera app the RaspberryPi foundation ships). So, via 2 signals SIGSTOP and SIGCONT we can command raspivid to stop and go. raspivid has some buffering already in place so when you say SIGCONT it has the last N seconds of vid ready to go, then the camera is allowed to proceed for some grace period after the event. Finally, via a timer, the SIGSTOP is sent. In this way, smaller video files are generated.

I am using GPIO 18 for the event pin.

The setup looks as above, a PiR connected to a Pi3. On the wall is a 12v IR ring for night illumination, it glows a dull crimson as its photodetector senses darkening ambient conditions.

The code is here:

Some issues:

1) No RTC on the Pi. I have a cheap $8 Banggood kit clock thats in the field of view. That is my source of time. The console output on the Pi helps also, as long as you have the correct time set on the Pi.
2) I recommend a Pi3 as it can do more than just this and is useful to have around. I am cabled up as I found that every 30secs or so the WLAN emits a wpa supplicant pkt that couples with the sensor and generates false events.
% ifdown wlan0

3) Audio would be a plus.

No comments:

Post a Comment