paint-brush
Spy your pet with a Raspberry Pi Camera Serverby@michelparreno
75,901 reads
75,901 reads

Spy your pet with a Raspberry Pi Camera Server

by Michel Parreno4mJuly 14th, 2017
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

This tutorial is the first part of my “ Spy your pet with a Raspberry Pi”. I own a little bunny called ‘Mochi’ and am often worried about him when I am not home. So, I decided to build an interactive cage to connect with Mochi whenever I am at work or traveling.

Company Mentioned

Mention Thumbnail
featured image - Spy your pet with a Raspberry Pi Camera Server
Michel Parreno HackerNoon profile picture

This tutorial is the first part of my “ Spy your pet with a Raspberry Pi”. I own a little bunny called ‘Mochi’ and am often worried about him when I am not home. So, I decided to build an interactive cage to connect with Mochi whenever I am at work or traveling.

Building an interactive cage involves 3 main challenges :

  • Install a spying connected camera,
  • Build an automated food dispenser,
  • Access your camera and food dispenser from anywhere around the world

My first challenge was the spying camera system, which I’m going to describe in this tutorial.

Required equipment

Install the camera on your PI

Raspberry Pi 3

Firstly, turn off your raspberry Pi, and be cautious to avoid any static electricity on your hands. Plug the camera blue ribbon in the Raspberry PI as shown in the above image. To plug the camera, you have to slowly unclip the white plastic part, plug the ribbon, and clip the white part again.

Now, plug the ethernet cable, the keyboard, mouse and screen to your PI. Don’t forget to plug the SD Card containing Raspbian OS ! Finally, turn it on by pluging the power supply to it. After a few seconds you should see :

Raspbian Operating System on your Raspberry Pi

Enabling camera configuration on your PI

In order to use compatible libraries or softwares with your Pi Camera, you first need to enable its configuration on your Raspberry Pi options. Run the following command on your Pi Terminal (screen icon in the navigation bar):

sudo raspi-config

Choose "camera" and select "Enable support for Raspberry Pi camera". Reboot when prompted and you’re done ! Several features should now be available for the camera:

  • raspistill that captures images (available directly with Raspbian)


# Test your camera !raspistill -o cam.jpg

  • raspivid to captures videos,
  • Motion which is what we are going to use

Installing Motion

Motion is a camera signal monitoring software that also have interesting motion detection features.

First, let’s be sure that everything on your Pi is up-to-date. Run the following in the Pi Terminal:


sudo apt-get updatesudo apt-get upgrade

Now, let’s install Motion software on our Pi and activate the official driver for the camera.

sudo apt-get install motion

# Careful, on the v4l2 it's an 'L', not the number 1sudo modprobe bcm2835-v4l2

To complete the process, we also have to tell the Pi to activate the driver after any reboot so our camera will always be available.

sudo nano /etc/modules# at the end of the file, add this line :bcm2835-v4l2

Because we want our camera server to run constantly, let’s set it as a daemon (background process) and start it automatically.

sudo nano /etc/default/motion# in this file, search for 

We are done with Motion installation ! Let’s configure it so we can have a camera server !

Configuring Motion

Create a backup and open the Motion configuration file


sudo cp /etc/motion/motion.conf /etc/motion/motion.conf.baksudo nano /etc/motion/motion.conf

Find the options below and edit them as follows


# Allow motion to run the daemon we've set earlierdaemon on


# Set the logfile (important to debug motion if you webservers crashes)logfile /tmp/motion.log


# we want to be able to access the stream outside off the Pi's localhoststream_localhost off



# disable pictures and movies savingoutput_pictures offffmpeg_output_movies off


# set the framerate of the stream (100 for higher quality)framerate 100



# set the width and height of your videowidth 640height 480


# control de port 8080 by defaultwebcontrol_port 8081

# careful ! don't set the stream_port just like the webcontrol port

Be aware that choosing a high resolution requires a larger bandwidth and your stream will lag if you have a low internet connection. You also have to pick a compatible resolution for the Pi (The stream won’t start if you don’t). Please check the Raspberry Pi available video modes.

OPTIONAL : save motion detection pictures





## OPTIONAL - motion detection## define the number of images saved when a movement is detectedoutput_pictures onpre_capture 1post_capture 1


## define the folder where captures are going (don't forget the chmod rights if necessary)target_dir /<path_of_your_choice>

Testing our camera server from a local computer

First, let’s start our Motion server !

sudo service motion start

By now, you should be able to view the camera stream directly on your Pi. Search for the default web browser in the home menu of Raspbian and check the URL http://localhost:8081. You should see your camera server! But now, we want to be able to spy our pet from any device connected to our Wifi and also from anywhere outside home !

Plug the Wifi dongle to your raspberry Pi and configure the Wifi connection on it.

The official Raspberry PI Wifi Dongle

Run the following command in your Pi Terminal


# will open the network configuration filesudo nano /etc/network/interfaces

On the following file, put your own SSID and Wifi Password.



auto loiface lo inet loopbackiface eth0 inet dhcp


allow-hotplug wlan0auto wlan0



iface wlan0 inet dhcpwpa-ssid "Your Network SSID"wpa-psk "Your Password"

Restart the nerwork interfaces

sudo service networking reload

Now let’s get the local IP adress of your Pi by launching:

ifconfig

In the command line response, find the “inet addr” which will give you the Raspberry Pi IP



wlan0 Link encap:Ethernet HWaddr 80:1f:02:aa:12:58inet addr:192.168.1.8 Bcast:192.168.1.255 Mask:255.255.255.0...

We are ready to test our camera server in all the devices connected to our home Wifi ! Take your phone or your laptop and go to → http://<replace this by your inet addr>:8081. You should see the camera stream !

Conclusion and next steps

Yay ! Now we have our local spy camera stream ! Say Hello to Mochi !

Our little friend is now under surveillance

However, we want to be able to view it from anywhere ! For this, I recommend using a dynamic DNS service. For a detailed explanation, you can check my next tutorial How to access your Raspberry Pi from anywhere.

What about building a food dispenser for your pet ? Get it all done with Build a connected food dispenser with Raspberry Pi.