Check out an updated containerized version of this project in https://www.johntonello.com/how-my-cat-helped-me-build-a-better-iot-app/
One of the first projects I ever built on the Raspberry Pi 2 was a motion detector camera to keep track of my cats. The idea was to tune in to the home-based camera while I was away or at work so I could, well, watch them sleep mostly. By combining the motion app with Dropbox, though, I found I could instantly view and share the images remotely, too.
I used motion, a straightforward bit of of software that enables you to capture still images and stream live video, or both. In fact, motion can create and save videos for you, too. It works on any Linux computer, but a Raspberry Pi or NanoPi NEO Plus2 are good choices because they don’t use much power and fit pretty much anywhere. I’ve since upgraded to a Raspberry Pi 3, which has built-in wifi and one less thing to worry about.
For the camera, I used a Logitech USB cam I had. Any USB camera made in the last five years or so should work.
There are quite a few tutorials out there for setting up motion, and I found one of the best is by Carla Schroder over at Linux.com.
By itself, motion works just fine. You can set up your home router so you can access your RPi and live-stream video from outside your house (more on setting of DDNS in a future post), and you can save the pictures or videos to the RPi’s small micro-SD drive, though it’ll fill up fast.
Of course, if you’re just streaming video, you don’t need extra storage at all. Motion lets you turn off picture and video saving. But my goal was to save pictures whenever the camera detected motion (a moving cat, in this case), save it to Dropbox, and have the Dropbox notification on my phone alert me. That way, I figured I would know when to check out the live stream to the cats in action.
Turns out, the Dropbox API worked great for this. Good instructions on setting it up are here: https://learn.adafruit.com/cloud-cam-connected-raspberry-pi-security-camera/dropbox-sync
By using the Dropbox uploader, I was able to let the RPi take as many pictures as it wanted whenever it detected motion. Before tweaking the motion settings, it considered subtle changes in lighting as motion, and took about 5,000 pictures over the course of a single day. With a resolution of 640 x 480, each picture was less than 40k, but that many pictures will quickly fill an RPi’s tiny hard drive.
I tweaked the motion settings and reduced the number of photos dramatically, and I let the RPi upload to Dropbox at will. To keep the RPi from filling up, I set up a cronjob to remove all the files from the RPi folder image folder every hour. Something like:
0 * * * * rm /full/path/to/motion_directory/*
When motion occurred, photos were immediately sent to Dropbox and I could see them on my phone — or anywhere.