Date: 2024-03-25
This is an older post (2016-07-11) from my previous blog, but I wanted to
bring it over. Most of the setup is long gone, but it was a fun project.
I've changed some formatting from the original post, but most of the content
has stayed the same.

I've set up a motion-sensor camera a while back, and I wanted to explain
how I set it up. I put it on a raspberry pi, with an infrared camera
via a ribbon cable. The camera isn't great, but it's only $33, for a night-
vision camera, so I can't complain too much. Honestly, the worst part is that
there's no real pre-made cases that fit the night-vision addons. I've got a
ghetto fix via velcroe tape. I'd take a picture, but it's too shameful to post
publicly! I plan on building a case out of wood, but like most of my projects,
I have no idea when I'll have time to get to it.

Motion was interesting to set up, and it wasn't difficult. One of my bigger
issues was wanting to store my pictures on a NAS that I have. But now that
I've set it up, I've been running this for probably over a year now, and it's
been pretty reliable. My config is not too far from ordinary, but I
wanted to highlight a few things:

* mmalcam_name vc.ril.camera: I had to compile a custom motion binary. It's
honestly been a while, and I've reinstalled since then, so I really don't
remember much of why I had to.

* input -1: Even though it's a ribbon, it's considered a usb camera due to the
custom binary.

* ffmpeg_output_movies off: I don't use video. I'd rather just have stills, to
save on space, and, as I'll show later, I email the pictures to myself.

* target_dir /mnt/camera/: I have this mounted to a NAS, so I can have a small
sd-card on the rpi. I like keeping backups of the pictures anyway.

* on_picture_save: This script is really the biggest part I wanted to explain.
I'll break it down:

* if [ ! -f /tmp/.motion.lock ]; then touch /tmp/.motion.lock - If the lock file
exists, just quit. This means that motion has already started this process. If
it doesn't exist, make it

* sleep 5 - sleep for 5 seconds because I had some issues with it trying to send
too fast after saving

* ls /mnt/camera/%Y%m%d%H%M* > /tmp/ls - Make a list of all the pictures for the
current minute

* for file in $(ls /mnt/camera/%Y%m%d%H%M* | shuf -n 5) - Pick 5 random pictures,
and then continue on

* attach="$attach -a $file"; done; echo "" | mail $attach -s "security camera" -r
"motion@thekyel.com" "kyelw@thekyel.com" - attach the files, and send them to
my email

* rm /tmp/.motion.lock - remove the lock file

There's nothing super special here, but there's a lot of little things going on. Now you know!

Anyway, It's been working out well. I'm pretty happy with this setup. It works
over wifi just fine, and makes me feel better than any chinese closed-source
system would.