Pythons, bashs and PIs

So the wife and I recently bought a new Synology NAS (we really were together in the store, yes store, and it was paid from the shared bank account), because the old on got the slows.

A DS 216 II+ it was destined to be. With a cool Intel Celeron x86 CPU, 1 GB of RAM and this in a nice black casing. What’s not to love about that?!

We also have a house (which we live in). And I wanted to “secure things”. Now count 1 & 1 & 1 together: RaspberryPi & new Diskstation & my new found IT-discovery-urge, what do we get? A question!

Can I connect a Raspberry Pi Zero W (with the Picamera) to my Diskstation?

Yes.

And livestream & record the feed in Synology’s Surveillance Station?

Yes.

But how?

With a camera and motion as package. That’s the main thing the Pi then will do. Okay, let’s get into the details. You’ll need:

A Raspberry Pi. I recommend the Zero W, because it’s super small (both in physical size and also energy footprint), and its official case comes with a camera cover. It’s basically meant to be a security cam (among other things).

A Camera for the Pi. Recommendation, go for the official Pi Camera. All following steps will assume it is an official Pi Camera, and physical installation of it in a Pi Zero Case is a breeze.

Other hardware items for the Pi: case, power supply, micro sd-card (we will run a headless installation of the PI, so USB & HDMI-Stuff is not needed).

A PC/Mac to connect to the Pi via SSH/Putty/etc, and configure it. I’m using a Mac here, the workflow shouldn’t be that different on a Windows/Linux machine though.

The Synology SAN in the same LAN as the Pi.

Headless setup of the (new) Raspberry Pi

We start with a factory fresh Pi and an empty sd-card. You an skip this step if your Pi is already set up with an OS and you can connect to it.

Download Raspbian from the official site. We’ll use the lite version for this guide, because the Zero isn’t supposed to do more than running the camera and doing the motion analysis. Desktop and a graphical user interface… pah… pure overkill. Who needs this stuff nowadays?!

With the sd-card now in the Pi, boot it and wait until you can connect to it. You can ping raspberrypi.local to get its IP, or just use exactly this hostname for the first SSH connection.

SSH into the Pi with ssh pi@raspberrypi.local with the default password ‘raspberry’ and run:

$ sudo raspi-config

There we’ll do three things:

Change the password & hostname from the default to something else in order to be secure and to identify the Pi should more of them turn up in your network.

Enable the Camera interface.

Save and confirm the changes (this may result in a reboot, after which you need to connect via the new hostname & new password).

Still in SSH, to get the newest software packages installed run:

$ sudo apt-get update
$ sudo apt-get upgrade

The Pi needs to have a fixed IP, because it won’t actually send anything to the Diskstation. Instead it’s the other way around the Diskstation will pull the feed from Pi, and in order to do that, the Diskstation needs a fixed IP to turn to. My way of choice to configure a fixed IP is (you need to enter the right IP settings for your network and your Pi):

Uncomment mmalcam_name vc.ril.camera(this is the picamera module, and we are using it)

output_pictures center or best saved image with the motion detected

quality 90

ffmpeg_output_movies off because we aren’t actually video-feeding, but only feeding jpegs (and only saving those as well)

locate_motion_mode preview and locate_motion_style redbox will draw a red box around the detected motion area

text_changes on shows the number of pixels changed

target_dir /home/pi/motion_capture this is the location folder into which motion will save images with detected motion

stream_quality 90

stream_localhost off because you want the Synology being able to access the stream

webcontrol_localhost off and you may want to be able to change settings from other machines than the Pi itself

You may also want to play with the Motion Detection Settings (threshold, event_gap, etc.). I leave that to your taste, as it’s mostly for alerting (into which we’ll get in another article). You can also get an example motion.conf in the according GitHub-respository.

We’re almost finished now. Save the configuration changes and run motion with the custom config once manually by:

$ motion -c /motion_conf/motion.conf

Watch for error messages, if all goes well, you should be able to access the feed on http://IP_OF_THE_PI:8081

Now we need to make sure, motion starts on system start. For this we’ll edit the rc.local file with nano and copy the same command into it:

Housekeeping on the Pi, to remove old images (and avoid the disk filling up):

Create a cron job:

0 * * * * find /home/pi/motion_capture/ -iname "*" -mtime +7 -delete

Setting up the Synology Diskstation – Surveillance Station

Okay, last stretch. Really. If you haven’t so far, install the Surveillance Station package on your Diskstation. Once that’s done, and you’re logged into the Surveillance Station module:

Click on IP-Camera and add a new camera. Click Next.

Choose the Complete Setup. Click Next.

Enter the connection data, with port 8081 as the feed port. The Diskstation needs a source path, though it doesn’t really matter what it entered there to my experience. Click Next.

On the Video page of the wizard, disable Audio.

On the Recording page of the wizard, configure how long / much you want to keep the videos. Click Next.

On the Schedule page of the wizard, set if you want to have continous recording or only on motion detected (by the Diskstation, not the Pi), or combined, etc.. Finish the wizard.

That’s it, you should now get a preview from your new Surveillance Pi, and can also access it with the Live View app in the Surveillance Station.

Final Thoughts

By default Synology only licenses 2 cameras per Diskstation (at least with our DS 216 II+), so only two PI’ possible right now. You may want to take this into consideration before going on a Pi with camera shopping spree.

If you want to place the PI outside, make sure the case is weather proof (water & humidity), as well as the typical temperatures in your region (can the PI handle 40°C + or -20°C for example?).

With a small power bank, this thing can be mobile easily though.

This will not directly make the home more safe. Intruders don’t know of your Pi when breaking in (how could they, unless you’ll write a big sign). It may give the authorities something to work though, if intruders did happen (which I hope they don’t).

I’m a total newbie at this. Followed all steps and up until the copy
$ cp /etc/motion/motion.conf ~/.motion_conf/motion.conf , everything seemed to go fine.
The file can’t be copied though. I get this error message:
cp: cannot create regular file ‘/home/pi/.motion_conf/motion.conf’: No such file or directory.
What to do?

The default config you’ve given will put all images into a directory in pi’s home dir.

After a time – this will fill up the disk. When connected to the Synology, do I need to output the pics to disk? Or can that be eliminated? Or do you do some “logrotate” type cleanup of this directory daily, etc.?