CHAPTER
16

Setting Up a Home Surveillance System

In This Chapter

Even at the low end, a home surveillance camera typically runs around $1,000–and that’s just for a simple camera you can access remotely via the internet. With the Raspberry Pi, however, you can build a similar system for a little over $100.

In this chapter, you learn how to turn your Raspberry Pi into a camera system you can monitor locally, on your home computer, or remotely, online. You’ll also set it up so you get an email notification anytime the camera senses movement, which makes it possible to use a DIY security system to protect your home.

For this project, you need to pick up a few extra parts. You need a camera (about $29) and a housing for the camera (about $10). You also need a set of standard gear for your Raspberry Pi, like a power supply, a SD card, and a Wi-Fi adapter.

Using the official Raspberry Pi Camera Module, which hooks right into your Raspberry Pi without any special equipment or skill, is quick and easy. Even the housing is simple enough that you don’t need to worry too much about any extra skills required to put it all together. For the overall setup, you’ll be using a lot of skills you’ve already learned in previous chapters, including installing Raspbian, hooking up a Wi-Fi adapter, and downloading other software.

By the end of this chapter, you’ll have a pretty nice little home security camera you built yourself from scratch.

What a Raspberry Pi Surveillance System Can Do

At a glance, the Raspberry Pi doesn’t look like much of a security system, but you’ll be surprised at what you can do with it and a few extra parts. When you finish this project, you’ll have a camera you can mount outside your home or office, within range of your Wi-Fi network, that records video when it senses movement. You’ll also be able to access a live stream remotely or retrieve any saved video later. And you’re going to tuck the Raspberry Pi and the camera inside a fake camera housing, so the whole thing will look legit to anyone who comes across it.

If you’re using a Raspberry Pi Model B, B+, or 2, you’ll get HD video. If you’re on the Model A+, you’ll have SD video, but it’s still perfectly capable for this project.

HARDWARE HELPER

The Raspberry Pi Foundation released the Raspberry Pi Camera Module in 2013. It’s a 5-megapixel camera that’s pretty simple, but it gets the job done. You won’t be able to add filters, zoom in or out, or change lenses, but as a point-and-shoot camera, it works well.

This isn’t a full-blown security system by any means, but it can give you peace of mind that your house is being monitored when you’re away—at a fraction of the cost. Plus, you have full control of the setup process, so you can alter any settings you want.

What You Need

As mentioned, to complete this project, you need a few more components beyond the usual stuff necessary to operate your Raspberry Pi.

Be sure you have an extra SD card, a power supply, and a Wi-Fi adapter. And because you’ll be storing HD video on the SD card, consider getting a card that’s at least 32GB in size.

You’ll need to get a few other parts. Let’s take a closer look at exactly what you’ll want to pick up before you start this project:

Raspberry Pi: Model B, B+, or 2 is recommended if you want to record HD video, but the Model A+ works if you don’t mind SD. Also pick up a power supply if you don’t already have one.

Camera module: The Raspberry Pi Camera Module is a custom camera made specifically for the Raspberry Pi by the Raspberry Pi Foundation. It attaches to the Raspberry Pi via a ribbon cable that connects to the top of the Raspberry Pi. The board itself is actually pretty tiny. It’s about 25 by 20 millimeters and weighs only 3 grams. The camera is a 5-megapixel camera that’s capable of taking still images at 2592×1944, 1080p video, 720p video, and 640×480 video. The ribbon cable only transmits pixel data, so it’s surprisingly fast. It costs $29.95 as of this writing.

HARDWARE HELPER

The camera module comes in two variations–the normal version that shoots regular video, and the Pi NoIR, which can record in low light using infrared. The setup process for both cameras is the same, so go with whichever one you think is best for your needs.

Camera housing: You’ll also need to purchase a housing for your camera system. Perhaps surprisingly, there’s a large market of dummy security cameras, and many are less than $10. These camera housings are made to look like a real security camera, but they’re hollow inside with no camera or other electronics inside—ideal for housing your Raspberry Pi and camera module. Find one that can fit your specific model of Raspberry Pi inside. You should be able to purchase a camera housing at a department store near you or online. For this project, I used one from UniquExceptional I purchased from Amazon for $8 (amazon.com/gp/product/B004D8NZ52).

SD card: Your SD card is not only going to store Raspbian for this project; it’s also going to store the video you record on your camera, so the bigger the better. Get at least a class 10 32GB SD card for this project; you can go up to 128GB if you want. (If you’re not sure an SD card is compatible, you can check it at elinux.org/RPi_SD_cards.)

That’s it for the extra stuff you’ll need. Depending on the size of your housing, you might also want to mount your Raspberry Pi inside the camera housing more securely, so be sure you have hook-and-loop tape or a similar adhesive to do that.

Camera Module Sources

Like the Raspberry Pi, it’s best to purchase your Raspberry Pi Camera Module from a reputable seller. Its list price is $29.95 as of this writing, so don’t spend more than that.

If you can, it’s best to buy a housing from a store local to you. That way, you can bring your Raspberry Pi into the store and eyeball it to be sure it’ll fit inside the housing. This isn’t required, of course, but it does help ensure you don’t waste $10 on housing that’s too small.

If you’re having trouble tracking down a housing locally, here are a few online stores you can try:

Adafruit: Adafruit (adafruit.com) typically has the camera module in stock and is a great resource for all things Raspberry Pi–related, including the camera. You can order it from adafruit.com/product/1367.

SparkFun: SparkFun (sparkfun.com) is an electronics and hardware distributor that sells all kinds of fun stuff, including several Raspberry Pi accessories. You can order the camera module from sparkfun.com/products/11868.

MCM Electronics: MCM Electronics (mcmelectronics.com) is one of the official distributors of the Raspberry Pi so it should come as no surprise it also carries the camera. Find it at mcmelectronics.com/product/28-17733.

With those options, you should have no problem tracking down a Raspberry Pi Camera Module.

PI POINTER

Be sure the housing you purchase is capable of being mounted to a wall or the side of a building. If you’re planning on using it outdoors, it’s good to check that it’s listed for outdoor use. That might not mean it’s waterproof, but because it’s plastic, it should be fine. That said, many cases are cheap plastic, so don’t expect them to hold up under extreme weather conditions.

Storage Considerations

The Raspberry Pi’s hard drive is just an SD card, so you’re limited to the space supported on a card for video storage. Fortunately, you have a few different options for dealing with this.

As one option, you can select a smaller video size during the setup process. If you shoot in SD, your videos will take up less space on your SD card and so you have room for more videos. You also can get a bigger SD card, but even that will fill up fast if you’re not deleting the camera’s footage daily or transferring it to another drive.

Provided you have another computer that’s connected to the same Wi-Fi network as your Raspberry Pi, you can set up a system to automatically dump the security video onto your personal computer. I explain how in this chapter.

Video Limitations

It’s also worth noting that the HD video itself isn’t perfect. At 1080p, you only get a maximum of 30 frames per second, which is pretty slow for a security camera but should be fine for catching any intruders in your house. You can bump that up to 60 frames per second if you drop the video quality down to 720p. Which you use is really up to you. If you want a truly high-quality HD stream, use the 1080p video.

Recording at this higher quality, however, takes up a lot of space. A 1-hour video recorded at 1080p at 60 fps takes up about 11GB of space—far too much for just an SD card. In this chapter, I show you how to set up your video recording system so you’re not wasting too much space with high-quality video you might not need. You can drop down your frames per second and the quality so you can store more video at the sacrifice of a little quality.

Note you won’t be recording live video with this project. Instead, you’ll set up your camera so it shoots at a very low frame rate. This makes the video choppy, but it also means you can save a lot more information on the SD card. Unless you’re using your camera to capture Batman, this shouldn’t be a problem.

PI POINTER

It’s important to note that this security system doesn’t have many of the conveniences of a commercial system. No security guard will come by your house if something goes wrong, and it’s probably not as reliable as those $1,000 systems. So although it’s probably just fine for your personal needs, don’t try to secure a bank with it.

Camera Limitations

Unfortunately, there is a downside to this system: you can only use one camera at a time. You can certainly set up multiple cameras with multiple Raspberry Pis, but they won’t be linked together like a modern security system.

To create a multiple-camera system, you’ll need to purchase multiple Raspberry Pis and multiple camera modules. This is still a lot less expensive than a commercial security system.

Setting Up and Configuring Raspbian

Before you assemble the camera module, housing, and other hardware, you need to get Raspbian up and running properly.

Installing Raspbian

This project uses Raspbian as the basis because it’s preloaded with the bulk of the software you’ll need. Refer to Chapter 8 to install it if it’s not already on your SD card.

Then, come back here because you need to adjust a few settings.

Setting Up SSH

Your camera’s likely going to need to be wireless and not connected to a television of any kind, so you’ll want to set up SSH so you can log in to your Raspberry Pi remotely.

Here’s what to do:

  1. At the command line, type in sudo raspi-config, and press Enter to get to the Raspberry Pi configuration menu.
  2. Scroll down to Advanced Options, and press Enter.
  3. Select Enable SSH, and press Enter.
  4. Select the Enable option.
  5. Press Esc to get back to the command line.

When you have SSH enabled, you’ll need to set up your Wi-Fi card.

Enabling Wi-Fi

I covered establishing your Wi-Fi connection in Chapter 11, but here’s the short version if you need a refresher:

  1. From the command line, type in sudo nano /etc/network/interface, and press Enter.
  2. Edit the file so it matches the following, replacing YOUR ROUTER NAME and WIFI PASSWORD with your network information:

    allow-hotplug wlan0

    iface wlan0inet dhcp

    wpa-ssid "YOUR ROUTER NAME"

    wpa-psk "WIFI PASSWORD"

  3. When you’re done, press Ctrl+X to exit and save.
  4. Restart your Wi-Fi interface to be sure everything takes effect. Type in sudo
    /etc/init.d/networking restart
    , and press Enter.

Your Wi-Fi card is set up, and SSH is enabled. You have one more thing to do before you start assembling hardware.

Assigning Your Raspberry Pi a Static IP Address

Because you’re going to be accessing your Raspberry Pi remotely all the time, it’s smart to give it a static IP address so you can easily find it on your network each time you boot your Raspberry Pi. The process is really easy to do from the command line:

  1. Type ifconfig, and press Enter.
  2. Look for the section titled wlan0.
  3. Write down the numbers listed after inet addr (you’ll use this for the “address” section in step 8), Bcast (for the “broadcast” section in step 8), and Mask (for the “netmask” section in step 8).
  4. Type in netstat -nr, and press Enter.
  5. Write down the numbers listed under Gateway Address (for the “gateway” section in step 8) and Destination Address (for the “network” section in step 8).
  6. Type in sudo nano /etc/network/interfaces, and press Enter.
  7. Look for the line that reads iface wlan0 inet dhcp, and edit it to iface wlan0 inet static.
  8. Right below that line, type in the following, substituting the numbers you wrote down in steps 3 and 5 for the X’s:

    address xxx.xxx.x.xx

    netmask xxx.xxx.xxx.x

    network xxx.xxx.x.xx

    broadcast xxx.xxx.x.xx

    gateway xxx.xxx.x.xx

  9. Press Ctrl+X to exit and save the changes.
  10. Restart your Wi-Fi interface one more time. Type in sudo /etc/init.d/networking restart, and press Enter.

You just set a static IP address for your Raspberry Pi so it’s always accessible at the same place on your network.

PI POINTER

Every time your Raspberry Pi logs on to your home network, it needs to find an IP address. This can change every time you power on your Raspberry Pi, so picking a static IP address ensures it’s in the same place every time, which makes it easy to find.

Updating Raspbian

Because you’re using a lot of external components, you’ll want to ensure your Raspberry Pi and your camera module are both up to date. You can do this from the command line.

Here’s how:

  1. Type in sudo apt-get install rpi-update, and press Enter.
  2. Type in sudo rpi-update, and press Enter. This updates Raspbian if there’s anything to update.
  3. Type in sudo apt-get update, and press Enter.
  4. Type in sudo apt-get upgrade, and press Enter. This ensures all your software is up to date, including drivers for the camera.

Now everything’s ready to go. You just need to check that SSH works properly.

Connecting to Your Pi Remotely

With SSH on and Wi-Fi enabled, you can now log in to your Raspberry Pi remotely from your computer. You’ll do this through SSH with some additional software.

Windows:

On Windows, you need to download some software before you can SSH into your Raspberry Pi.

Here’s what to do:

  1. Head to www.chiark.greenend.org.uk/~sgtatham/putty/download.html and download PuTTY.
  2. When the file is downloaded, open putty.exe.
  3. Enter your Raspberry Pi’s IP address you assigned earlier under Host Name.
  4. Tap Enter to open the command line.
  5. Enter your Raspberry Pi’s username and password.

Your Raspberry Pi and Windows computer are now connected. You can control your Raspberry Pi via your Windows PC as if you were in front of your Raspberry Pi itself. Test this out before you assemble your hardware so you don’t have to disassemble it to troubleshoot later.

Mac:

If you own a Mac, you already have SSH built into your computer and you can access your Raspberry Pi using the Terminal app. Here’s what to do:

  1. Launch Terminal on your Mac from Applications > Utilities > Terminal.
  2. Type in ssh pi@yourIPaddress, and replace yourIPaddress with the IP address you gave your Raspberry Pi. For example, ssh pi@192.168.1.119.
  3. When you’re prompted for your Raspberry Pi password, type it in and press Enter.

That’s it. You can control your Raspberry Pi from Terminal on your Mac just like you would from the command line on your Raspberry Pi. You should also see your Raspberry Pi in Finder, so you can drag and drop files onto it. Before you assemble your hardware, test this connection so you don’t have to disassemble it to troubleshoot if problems arise later.

PI POINTER

When you use SSH, you control your Raspberry Pi from the command line just like you would if you were sitting in front of your Raspberry Pi. All the same commands work here. Just remember, if you reboot the Pi, you’ll have to reconnect.

Assembling the Hardware

When you have everything connected and the preliminary setup finished, you can assemble your camera. As with any construction-type project, pay close attention to what you’re doing, and always keep an eye on your small parts so they don’t disappear.

Connecting the Camera Module

First, let’s get that nifty camera module installed. Here’s what to do:

  1. Look at your Raspberry Pi Camera Module. You should see a blue strip at the end of the ribbon cable. On the opposite side of that is a bit of metal. You’re going to connect that metal part to your Raspberry Pi.

    The metal strip on the end of the camera module is how you attach the camera to your Raspberry Pi.

  2. Connect the ribbon cable to your Raspberry Pi’s Camera Module insert. It’s located right next to the Ethernet. Insert it so the metal on the ribbon cable touches the metal on the board. (The blue bit should be facing the Ethernet connector.) You might need to use a little bit of force to insert it.

Connect the camera module’s ribbon cable to your Raspberry Pi.

Fitting Your Raspberry Pi into the Camera Housing

Next, you’ll fit your Raspberry Pi, with the camera module attached, into the empty camera housing. This process will be a bit different depending on which camera housing you purchased, but you should still be able to follow along with these instructions and your specific housing.

The nice thing about these dummy cases is that they look like cameras, but they’re basically empty inside. So taking them apart and putting them back together again is incredibly simple.

If you purchased the same case from Amazon I shared earlier in this chapter, you just need to unscrew the four screws in the front and the four in the back to gain access to the inside of the housing. You can remove the wires inside because there’s no reason to have a light on. Then, place your Raspberry Pi inside the housing and push the camera module into the housing’s front plate. You can secure it with adhesive tape or hook-and-loop tape now if you want. Then close the case, ensuring your power cable is connected and hanging out of the case.

Fit your Raspberry Pi into the empty camera housing.

Every housing is going to be a bit different, so you might have to play around with your model here to see what works best. Just be sure you keep track of the screws as you take apart the housing because you’ll have to put it back together again if you want it to actually look like a camera.

Setting Up Your Software

Now that everything’s put together, it’s time to tackle the software portion and get your camera working properly. This means installing a bunch of drivers, changing some configuration files, and taking a look at a few different ways to view your video stream.

Enabling the Camera

Getting the camera up and running isn’t difficult. You’ll do this in the raspi-config menu. Here’s what to do:

  1. Open the command line.
  2. Type in sudo raspi-config, and press Enter.
  3. Scroll down to the Enable Camera option, and press Enter.
  4. Select Enable.

Enabling the camera on your Raspberry Pi is pretty simple.

Exit out of the raspi-config menu. Your camera is now set up.

PI POINTER

This is always how you enable the camera, so if you decide to tackle a different camera project, the camera setup will be the same for that project as it is here. You can do lots of fun stuff with the camera. I share several more projects in Chapter 18.

Installing Motion-Detection Software

The camera is pretty cool, but right now, it’s still completely useless. To fix that, you need some additional software—in this case, motion-detection software so the camera knows when to record.

You’re going to use Motion. Motion is open-source software that works as a motion detector. When it’s installed, it triggers your camera to record only when it sees movement.

Here’s what to do:

  1. Open the command line.
  2. Type in sudo apt-get install motion, and press Enter. This downloads the Motion software.
  3. Motion isn’t currently set up to work with the Raspberry Pi Camera Module, so you’ll need some additional files. This part’s a little tricky to type in, so be careful. Start by typing in cd /tmp, and press Enter.
  4. Type in the following and press Enter:

    sudo apt-get install -y libjpeg62 libjpeg62-dev libavformat53 libavformat-dev libavcodec53 libavcodec-dev libavutil51 libavutil-dev libc6-dev zlib1g-dev libmysqlclient18 libmysqlclient-dev libpq5 libpq-dev

  5. Now you need to get the most current link for the Raspberry Pi version of the Motion software. Head to github.com/dozencrows/motion/tree/mmal-test in your computer’s browser, and scan the page to find a download link. It should look something like https://www.dropbox.com/s/jw5r1wss32tdibb/motion-mmal-opt.tar.gz. Make a note of that link.
  6. Type in the following, replacing the link with the one you got in step 5, and press Enter:

    wget https://www.dropbox.com/s/jw5r1wss32tdibb/motion-mmal.tar.gz

  7. To unzip the file you just downloaded, type tar zxvf motion-mmal.tar.gz and press Enter.
  8. Now, you need to modify the version of Motion you downloaded in step 1 so it integrates this new software. Type in sudo mv motion /usr/bin/motion, and press Enter.
  9. Type in sudo mv motion-mmalcam.conf /etc/motion.conf, and press Enter.
  10. Type in sudo nano /etc/default/motion, and press Enter to open the text editor.
  11. When you’re in the text editor, change start_motion_daemon=no to
    start_motion_daemon=yes. Then press Ctrl+X to exit the editor.

Starting the motion detection takes a few steps but isn’t overly difficult.

That’s it for installing the motion detection software. Customizing it for your use is next.

Adjusting Your Motion-Detection Software

Now to fine-tune the motion detection software. I show you how to set it up so it works well on a Raspberry Pi operating in a pretty low-key neighborhood, but you might need to alter some of these settings for your specific use.

First, you need to get your permissions in order. Because you’re using a modified build of Motion, you need to give it proper permissions before you can proceed.

Here’s how:

  1. At the command line, type in sudo chmod 664 /etc/motion.conf, and press Enter.
  2. Type in sudo chmod 755 /usr/bin/motion, and press Enter.
  3. Type in sudo touch /tmp/motion.log, and press Enter.
  4. Type in sudo chmod 775 /tmp/motion.log, and press Enter.

This should set all your permissions.

PI POINTER

Remember, when you’re editing configuration files, be careful to type everything exactly as you see it here. One small typo, and the whole system won’t work. Fortunately, if something is wrong when you try to launch Motion, you’ll get a list of errors. You can usually troubleshoot where you made a mistake by comparing the lines you’ll find here and there.

Now to actually edit that configuration file:

  1. To open the file, type in sudo nano /etc/motion.conf, and press Enter.
  2. Now you’re in the configuration file. You have a ton of options to edit here, so let’s go through each of the important ones:

    The motion configuration file has plenty of options to configure.

    Daemon off: Change this to daemon on. This ensures the motion detector is always running.

    Logfile /home/pi/motion.log: Change this to logfile /tmp/motion.log. This ensures you don’t have to actually log in to your Raspberry Pi for it to record.

    Width 640: If you want to record HD video, change this to width 1280. This will record in HD width.

    Height 576: Because you changed the width, you also need to change the height. Change this to height 720 for HD video.

    Pre_capture 0: With motion-sensor software, it’s useful to record a little before the motion and also a little after. Change this to pre_capture 2 to record 2 seconds before the motion.

    Post_capture 0: This is what’s captured after movement. Change this to post_capture 2 to record 2 seconds after the motion.

    Max_movie_time 0: This is the maximum amount of time the camera can record. Because you don’t need to record forever, you’re going to change it to read
    max_movie_time 600. This makes the maximum record time 600 seconds, or 10 minutes.

    Ffmpeg_video_code mpeg 4: This is the format the camera records in. You’re going to adjust this so it’s a little more compatible with other software. Change it to
    ffmpeg_video_code msmpeg4.

    Locate_motion_mode off: Your camera can find and draw a box around any motion it sees. If you want that box, change this to locate_motion_mode on.

    stream_localhost on: This tells your Raspberry Pi to not send the video outside your network. If you want to access it remotely from anywhere, change this to
    stream_localhost off.

    Stream_auth_method 0: If you want to access your video stream remotely, you’ll want a password. Change this to stream_auth_method 2 so it requires authentication.

    Stream_authentication username:password: This is your username and password for your live video stream. Type in whatever username you want in place of username and whatever password you want in place of password.

    PI POINTER

    The configuration file here is pretty large, but it’s easy to navigate. Remember, anything that’s proceeded with a # is a comment, so don’t worry about editing those lines. Instead, you’re looking for the lines without that symbol.

  3. When you’re done editing, press Ctrl+X to exit and save.
  4. Reboot the system by typing in sudo reboot.

Those are the core settings, but as you’re going through the configuration file, you’ll probably notice some other things you want to tweak. The description for each configuration change is in the file itself, so take your time going through it and change whatever you like to match your needs.

You should now be able to access the live stream of your video by pointing your web browser to YOURRASPBERRYPIIPADDRESS:8081. For example, you might use something like 192.168.1.119:8081.

Your Raspberry Pi–powered camera can detect and record anything that moves within its view, such as in a tree outside your window.

PI POINTER

I had trouble getting the video stream to work in both Google Chrome and Internet Explorer. If you’re having trouble, try another web browser like Safari or Firefox. You should also be able to access this video on your smartphone, provided it’s on the same Wi-Fi network as your Raspberry Pi.

Storing Videos on Your Computer

Your stream is up and running, and the camera is recording video whenever it senses movement. But your Raspberry Pi doesn’t have a ton of storage, even if you purchased a large SD card. You might want to set it up so all the recorded video is automatically sent over to your home computer, where you have more storage capacity.

Windows:

Here’s how to put that video transfer in place in Windows:

  1. Create a folder to share with your Raspberry Pi. Call it something like Surveillance so it’s easy to remember.
  2. Right-click the folder, and select the Share option.
  3. Select the Everyone option, and change the Permission Level to Contributor.
  4. Click the Share button, and make a note of the network path. It should read something like \\yourcomputer\users\documents\surveillance.
  5. Head back to your Raspberry Pi’s command line. Type in sudo nano /etc/fstab, and press Enter. This loads a startup configuration.
  6. Add this line to the configuration file, substituting in the information you wrote down in step 4:

    //PATHFROMSTEP4 /mnt/surveillance cifs
    username=YOURFOLDERUSERNAME,password=YOURFOLDERPASSWORD,iocharset=ut
    f8,file_mode=0777,dir_mode=0777 00

  7. Your Raspberry Pi now has access to that shared folder on your Windows computer.
    You just need to change your camera’s settings so it knows to save videos there. Type in sudo nano /etc/default/motion.conf, and press Enter.
  8. Find the line that says target_dir, and change it to target_dir /mnt/surveillance.
  9. When you’re done, press Ctrl+X to save and exit.

Your Raspberry Pi will now save all the videos and images it records right on your Windows computer for easy access.

Mac:

Storing videos on a Mac is pretty simple to do as well. You’ll just need to share a folder with the Raspberry Pi. Here’s how:

  1. Create a folder to share with your Raspberry Pi. Call it something like Surveillance so it’s easy to remember.
  2. Open System Preferences by clicking the Apple logo in the top menu bar and selecting System Preferences.
  3. Navigate to the Sharing option, and click it.
  4. Click the + button next under Shared Folders, and select the folder you want to share.
  5. Now head back to the main System Preferences panel, and select Network. Look for your IP address here. You’ll need it in step 8.
  6. Head over to your Raspberry Pi, and pop into the command line. Type in sudo
    apt-get install afpfs-ng
    , and press Enter. This installs the file-sharing protocol your Raspberry Pi needs to access your Mac.
  7. Type in sudo nano /etc/fstab, and press Enter.
  8. Add these lines to the configuration file, substituting your own information for YOURMACUSERNAME, MACPASSWORD, YOURMACIPADDRESS, FOLDERFROMSTEP3, and YOURUSERNAME:

    afpfs#afp://YOURMACUSERNAME:MACPASSWORD@YOURMACIPADDRESS/FOLDERFROMSTEP3/
    mnt/surveillance fuse
    user=YOURUSERNAME,group=fuse 0 0

  9. Save and exit the file by tapping Ctrl+X.
  10. Next, type in sudo nano /sbin/afps, and press Enter.
  11. Type the following into the blank text file:

    #!/bin/bash

    mount_afp $3 $4 $1 $2

    Tap Ctrl+X to save and exit the file when you’re done.

  12. Now to make the file you just created an executable program, type in chmod +x
    /sbin/afpfs
    and press Enter.
  13. Your Raspberry Pi has access to that shared folder on your Mac computer; you need to change your camera’s settings so it knows to save it there. Type in sudo nano
    /etc/default/motion.conf
    , and press Enter.
  14. Find the line that says target_dir, and change it to target_dir /mnt/surveillance.
  15. When you’re done, press Ctrl+X to save and exit.

Your Raspberry Pi should automatically connect to your Mac so it can store information there.

PI POINTER

The Mac configuration is a little trickier because Macs use a different type of file sharing than Windows or Linux. Pay careful attention to what you’re doing here to get it right. There’s always a chance this won’t work if any of the sharing preferences get changed, so if you’re struggling, additional resources are available online.

Accessing Saved Videos

If you set up your Raspberry Pi to automatically save videos to your personal computer, you can access all your saved footage there. If not, you’ll need to pull it off the Raspberry Pi directly. You can do that remotely, too.

Windows:

On a Windows computer, you can access your saved videos through SSH software called WinSCP. Here’s how:

  1. Download WinSCP from winscp.net/eng/index.php.
  2. Launch WinSCP. In the Connect to: box, type in your Raspberry Pi’s IP address.
  3. Type in pi for the username.
  4. Click the Connect button.
  5. When prompted, enter your password.
  6. Now that you’re connected to your Raspberry Pi, navigate to the /tmp/ folder. Your videos are saved here. You can drag them to your desktop or play them remotely.

If you need to clear up space, you can delete old videos from this folder.

Mac:

To access files on your Raspberry Pi from your Mac, you’ll need additional software called Fugu SSH. Here’s what to do:

  1. Download Fugu SSH from sourceforge.net/projects/fugussh.
  2. Open the software. In the Connect to: box, type in your Raspberry Pi’s internal IP address.
  3. Type in pi for the username.
  4. Click the Connect button.
  5. When prompted, enter your password.
  6. Now that you’re connected to your Raspberry Pi, navigate to the /tmp/ folder, where your videos are saved. Drag them to your desktop, or play them remotely from here.

If you need to make room on your SD card, you can remove old videos from the /tmp/ folder.

Remotely Reviewing Footage

If viewing and accessing your surveillance video from your home computer isn’t enough for you, you also can remotely access it online. To do this, you’ll need to register a DNS address for your Raspberry Pi and set up some port forwarding on your router.

Setting Up a DNS Address for Your Raspberry Pi

You have many options for getting a DNS address. In this project, we’ll use DNSdynamic (dnsdynamic.org). In Chapter 13, I share a few alternative options.

  1. Head to dnsdynamic.org.
  2. Check for a domain availability by typing in a domain name in the check availability box. You can use whatever you want here. Follow the instructions, and you’ll get a host service that reads something like myraspberrypicamera.dnsdynamic.com. You’ll also get some more configuration settings. Hold on to these for use in step 7.
  3. Head back to your Raspberry Pi, and open the command line.
  4. Type in sudo apt-get install ddclient, and press Enter. This installs the dynamic DNS client. Wait for it to download and install.
  5. When it’s finished, it’ll run automatically. Select Other if you’re using DNSdynamic.
  6. Enter the address of the DNS server you registered in step 2.
  7. Now you need to edit the file to include your information. Type in sudo nano
    /etc/ddclient/ddclient.conf
    , and press Enter.
  8. Then type in all the information you gathered in step 2.
  9. Finally, add one more line to the top of the file that reads daemon=600. This tells your internet service provider to check the address every 600 seconds, or every 10 minutes.
  10. Press Ctrl+X to save and exit the file.
  11. Type in ddclient. This launches the dynamic DNS client so your Raspberry Pi is accessible online.
  12. To be sure ddclient is always running when you reboot your Raspberry Pi, type in sudo /etc/init.d/ddclient start and press Enter. This adds ddclient to your boot process.

Your Raspberry Pi now has a permanent address on the internet. You’re almost done. You just need to tell your router it’s okay to broadcast that signal.

PI POINTER

Dynamic DNS is a great way to give your Raspberry Pi a permanent address online. Your internet service provider constantly changes your IP address, so a dynamic DNS ensures you can always find your Raspberry Pi at the same address every time. Plus, it’s a lot easier to remember a phrase you come up with yourself than it is to remember a string of seemingly random numbers.

Enabling Port Forwarding on Your Router

Before you can access your Pi’s camera remotely, you need to set up port forwarding on your router. Every router is a little different, so consider this a loose guide to get you started:

  1. Access your router from your computer’s web browser. It’s typically 192.168.1.1 or 10.0.0.1.
  2. When prompted, type in your router’s username and password.
  3. Look for a tab or setting that reads NAT/QoS, and click it.
  4. Find the Port Forwarding tab, and click it.
  5. Typically, you’ll need to click an Add button to add a new port forwarding address.
  6. In the boxes, enter your Raspberry Pi’s IP address under Address and 8081 under Port.
  7. Save it, and exit your router.

As mentioned earlier, all routers are a little different, so if you can’t find your router’s port forwarding section, refer to your owner’s manual.

With that, your Raspberry Pi is accessible over the internet. You should be able to access your camera feed by going to nameyoupickedabove.com:8081.

PI POINTER

Every internet service provider varies in the settings they allow. If port 8081 doesn’t work for you, try another like 80, 20, or 8080.

Setting Up Email Alerts

A motion-sensing camera is only so good without some kind of alert system. Fortunately, you can set up your Raspberry Pi to send you an email every time the camera detects motion. This way, when you get an alert, you know to boot up your remote camera and see what’s going on.

Here’s what to do:

  1. From the command line on your Raspberry Pi, type in sudo nano /etc/motion.conf, and press Enter.
  2. Edit the line on_picture_save value so it reads on_picture_save mpack -s Alert %f youremail@youremail.com, replacing youremail@youremail.com with your personal email address. This ensures you’ll get an email alert any time the camera detects motion and snaps a picture or video.
  3. Press Ctrl+X to save and exit the document.

Get ready to check your email because now you’ll get alerts when your Raspberry Pi camera senses motion.

Mounting Your Camera

Now that everything is set up and working on the software side, you can get the hardware in place.

When you’re deciding where to mount your camera system, look for the most trafficked area. This is probably your front door, but it really depends on what you’re trying to capture on film. Be sure it’s located within Wi-Fi range of your router and that your power cord reaches an electric outlet. It’s also best to keep it someplace out of the weather. It’s in a case, sure, but that doesn’t mean you want to leave it out during a hailstorm. Finally, point the camera’s eye at what you want to film. You might need to test it and adjust it a bit before you’re finished.

With your camera in place where you want it, plug in your Raspberry Pi and boot up your browser to check the feed.

The Least You Need to Know