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.
- Raspberry Pi with Raspbian OS (Normally it should be shipped with a SD Card with Raspbian OS)
- A screen with HDMI, usb keyboard, usb mouse, ethernet cable for your raspberry PI
- Pi Camera or Pi Camera NoIR (InfraRed)
- Wifi Dongle
Install the camera on your PI
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 :
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):
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
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 update
sudo 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 1
sudo 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 :
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/motionstart_motion_daemon and activate it
# in this file, search for
We are done with Motion installation ! Let’s configure it so we can have a camera server !
Create a backup and open the Motion configuration file
sudo cp /etc/motion/motion.conf /etc/motion/motion.conf.bak
sudo nano /etc/motion/motion.conf
Find the options below and edit them as follows
# Allow motion to run the daemon we've set earlier
# Set the logfile (important to debug motion if you webservers crashes)
# we want to be able to access the stream outside off the Pi's localhost
# disable pictures and movies saving
# set the framerate of the stream (100 for higher quality)
# set the width and height of your video
# control de port 8080 by default
# 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 detected
## define the folder where captures are going (don't forget the chmod rights if necessary)
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.
Run the following command in your Pi Terminal
# will open the network configuration file
sudo nano /etc/network/interfaces
On the following file, put your own SSID and Wifi Password.
iface lo inet loopback
iface eth0 inet dhcp
iface wlan0 inet dhcp
wpa-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:
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:58
inet 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 !
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.