Arduino Yún Motion-Trigger Trespasser Camera

This project consists of a movement sensor (PIR) plus a camera and a Wi-Fi connected device (Arduino Yún was my choice, but it can be easily substituted with a Raspberry Pi). Optionally, speakers can be connected to reproduce an alarm sound.

The purpose is clear. You don’t want your kids to steal your food from the cupboard, or from the fridge, or someone to open your locker, or you want to take pictures of your pet stealing food, or you are Dwight Schrute and you want to finally unmask the coworker that puts your stuff into jelly… so you hide the device in the cupboard/fridge/locker, and when the device detects movement, it will take a picture and post it right to your email! And if you use IFTTT, then you can automatically post the picture of the thief to Facebook or Twitter and show the thief’s face to the entire world!

The project is very simple, it doesn’t require any soldering/electronics skills, and it can be assembled in minutes.

As I said before, my platform choice is Arduino Yún, because it’s very easy to use, and it’s easy to configure the Wi-Fi settings. It’s a bit more expensive than the Raspberry Pi, but if you use the Yún you can give you project to someone else as a gift, and anyone can configure the Wi-Fi settings, which may be a difficult task with the Raspberry Pi, without connecting it to a monitor/mouse/keyboard. But if you like it, a Raspberry Pi version is simple.

Step 1

Arduino basic configuration

First of all, you need to configure your Arduino Yún network settings. This should be pretty easy following this guide.

Why the openssh-sftp-server? Because it would be easier for you to upload and download files from/to the Arduino: now you can use a generic SFTP client (FileZilla, Transmit, or Cyberduck) instead of SCP command.

Then, we have have to install the SSL support package for Python (thanks sbkirby!) Because it’s not included in the default packages.

$opkg install python-openssl

If you have one, I highly recommend to put a micro SD card into the Arduino Yún. It will be automatically mounted in /mnt/sda1.

Then, we will install the USB camera and USB sound card on the Yún.

Step 2

Installing and testing the USB Camera

The UVC package is already available for Linino via opkg, so installing the camera is easy, just connect to your Yún via SSH and type

$ opkg install kmod-video-uvc

We also need a software for taking pictures, I used fswebcam that is super small and very easy to use

$ opkg install fswebcam

Step 3

Take your first picture

Be sure to use the SD card for storage or you will quickly fill the Arduino internal storage. If you are using an SD card, you should see it mounted in /mnt/sda1. If it’s not, put an FAT32 formatted micro SD card into the SD card slot, and reboot your Arduino.

Now plug the camera to the USB host port, and type

$ cd /mnt/sda1
$ fswebcam test.png

If everything is OK, you took your first picture! Take a look at your SD card contents.

This means that now we can take pictures from our Arduino sketch, via the Yún’s Bridge library, in this way

Process.runShellCommand("fswebcam /mnt/sda1/test.png");

Step 4 (optional)

Install the sound card

Open an SSH session to the Yún, and type:

$opkg install kmod-usb-audio
$opkg install madplay

Now you have sound support on the Arduino Yún. I only tried MP3 playback, but you could try recording audio.

To test it just copy an MP3 audio file to the SD card, and type

$cd /mnt/sda1
$madplay yoursound.mp3

This means that now we can take play sounds from our Arduino sketch, via the Yún’s Bridge library, in this way

Process.runShellCommand("madplay /mnt/sda1/test.mp3");

Step 5

The email script

Now we can take pictures and play sounds… but we want more from our Arduino! We want to send them via email.

So, how can we do it? The answer is very easy… Python script! Even if the Arduino Yún integrates the Temboo library, I wanted this project to be as portable as possible, so we will use a very simple Python script to encode the image file and send it to our email.

I’m assuming that you’re using Gmail, but the script can be easily adapted to any SMTP server.

Create a new file, call it “sendemail.py” and paste this code into it

# coding=utf-8
# Copyright (C) 2014 Stefano Guglielmetti
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import smtplib, os, sys
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders
#From address, to address, subject and message body
from_address = '[email protected]'
to_address = ['[email protected]']
email_subject = 'Alert!!! Zombies!!! Ahead!!!'
email_body = 'A non dead intruder has been detected and needs to be eliminated!'
# Credentials (if needed)
username = 'EMAIL_LOGIN'
password = 'EMAIL_PASSWORD'
# The actual mail send
server = 'smtp.gmail.com:587'
def send_mail(send_from, send_to, subject, text, files=[], server="localhost"):
assert type(send_to)==list
assert type(files)==list
msg = MIMEMultipart()
msg['From'] = send_from
msg['To'] = COMMASPACE.join(send_to)
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
msg.attach( MIMEText(text) )
for f in files:
part = MIMEBase('application', "octet-stream")
part.set_payload( open(f,"rb").read() )
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f))
msg.attach(part)
smtp = smtplib.SMTP(server)
smtp.starttls()
smtp.login(username,password)
smtp.sendmail(send_from, send_to, msg.as_string())
smtp.close()
send_mail(from_address, to_address, email_subject, email_body, [sys.argv[1]], server) #the first command line argument will be used as the image file name

Tools

Team leader, software architect and developer, entrepreneur and passionate cook.

Co founded Interact SpA in 1998, and joined Mikamai in 2013

In more than 16 years of experience in the Internet industry, I gained strong problem solving skills, working for very small clients and start-ups up to very big and complex enviroinments, including the major Italian public administration websites and large telecommunication, communication and fashion companies.

I worked with many technologies and many teams, building reliable, scalable and performing solutions, and now I'm totally focused on the Internet of Things.

Advertisement

Sign up for the Make: Membership newsletter

Please enter a valid email

Dozens of projects in every issue covering Robots, Drones, 3D printing, craft and more

Tips and skill-building tutorials with inspiration from the leaders of the maker community