You Can’t Touch This! – An Evil Arduino Based Alarm System

You can’t touch this!

title

This was my entry for the Connected Home Project Contest by Make Magazine

This project consists in a movement sensor (PIR) plus a camera and a wifi connected device (Arduino Yún was my choice, but it can be easily substitued with a RaspberryPi). 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 locket, or you want to take pictures to 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 into the cupboard/fridge/locket, and when the device detects some 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, 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 wifi settings. It’s a bit more expensive than the RaspberryPi, but if you use the Yún you can give you project to someone else as a gift, and anyone can configure the wifi settings, while this may be a difficult task with the RaspberryPi, without connecting it to a monitor/mouse/keyboard. But if you like it, I can easily make a Raspberry version.

So, here are the ingredients

  • Arduino Yún
  • PIR Sensor
  • UVC compatible USB Camera (don’t worry: almost all the USB cameras are UVC compatible)
  • Micro USB power adapter or USB Battery pack
  • Recommended: micro SD card
  • Optional: USB Sound card
  • Optional: USB HUB (if you want both the USB Camera and the USB Sound card)

totalone

Ok, let’s start!

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

Then test the connection to your Arduino: in your browser type http://arduino.local, you will see the Arduino web interface

If it works, open SSH session

$ssh root@arduino.local

the default password is “arduino”

Then let’s install some useful packages

$opkg update
$opkg install openssh-sftp-server

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

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 Soundcard on the Yún

Step 2

Installing and testing the USB Camera

The UVC package is already available fot Linino via opkg, so installing the Camera is a very easy step, just connect to your Yún via ssh and type

$ opkg update
$ opkg install kmod-video-uvc

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

$ opkg install fswebcam

Step 3

Take your first picture

First of all, be sure to use and SD card for storage or you will quickly fill the Arduino internal storage, and you don’t want to do that. If you are using an SD card, you should see it mounted in /mnt/sda1. If it’s not, put a 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 to 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 yun, and type:

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

End of the story. Now you have sound support on the Arduino Yún. I only tried MP3 playback, I didn’t try to record some audio yet, but I’ll try soon!

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");

We’re learning a lot of interesting things!! 🙂

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? The answer is very easy… python script! Even if the Arduino Yún integrates the Temboo library, I wanted this project to be as much 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 are 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    = 'FROM_ADDRESS@EMAIL.COM'
to_address      = ['YOUR_ADDRESS@EMAIL.COM']
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

now you have to customise the email settings

#From address, to address, subject and message body
from_address    = 'FROM_ADDRESS@EMAIL.COM'
to_address      = ['YOUR_ADDRESS@EMAIL.COM']
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'

so change the from address, to address and so on to match your settings

Now you can upload the script to the Arduino Yún SD card via SFTP (or SCP, if you prefer), then open an SSH session to the Yún and test it typing

$cd /mnt/sda1
$python sendemail.py test.png

And in a few seconds you will receive an email with the picture attachment… amazing!

Step 6

Let’s build the circuit!

Don’t worry, this is supereasy, there is nothing to solder, just assemble few parts

I used this PIR sensor, because it’s reliable and easy to use.

Just follow this scheme

image

We’ve connected the PIR sensor, and a LED that will turn on when the device detects some movement

Step 7

Now it’s time to add the Arduino Sketch

Copy this code to the Arduino IDE and upload it to your Yún

/* 
 * Switches a LED, takes a picture and sends it via email
 * according to the state of the sensors output pin.
 * Determines the beginning and end of continuous motion sequences.
 *
 * @author: Stefano Guglielmetti / stefano (at) mikamai (dot) com / http://jeko.net
 * @date:   feb 5, 2014  
 *
 * based on the example by Kristian Gohlke / krigoo (_) gmail (_) com / http://krx.at
 * http://playground.arduino.cc/Code/PIRsense
 *
 * stefano guglielmetti (cleft) 2014 
 *
 * released under a creative commons "Attribution-NonCommercial-ShareAlike 2.0" license
 * http://creativecommons.org/licenses/by-nc-sa/2.0/de/
 *
 *
 * The Parallax PIR Sensor is an easy to use digital infrared motion sensor module. 
 * (http://www.parallax.com/detail.asp?product_id=555-28027)
 *
 * The sensor's output pin goes to HIGH if motion is present.
 * However, even if motion is present it goes to LOW from time to time, 
 * which might give the impression no motion is present. 
 * This program deals with this issue by ignoring LOW-phases shorter than a given time, 
 * assuming continuous motion is present during these phases.
 *  
 */

#include <Bridge.h>

/////////////////////////////
//VARS
//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 10;        

//the time when the sensor outputs a low impulse
long unsigned int lowIn;         

//the amount of milliseconds the sensor has to be low 
//before we assume all motion has stopped
long unsigned int pause = 5000;  

boolean lockLow = true;
boolean takeLowTime;  

int pirPin = 6;    //the digital pin connected to the PIR sensor's output
int ledPin = 13;

Process p;
String imageName;

/////////////////////////////
//SETUP
void setup(){
  Bridge.begin();
  Serial.begin(9600);
  pinMode(pirPin, INPUT);
  pinMode(ledPin, OUTPUT);
  digitalWrite(pirPin, LOW);

  //give the sensor some time to calibrate
  Serial.print("calibrating sensor ");
  for(int i = 0; i < calibrationTime; i++){
    Serial.print(".");
    delay(1000);
  }
  Serial.println(" done");
  Serial.println("SENSOR ACTIVE");
  delay(50);
}

////////////////////////////
//LOOP
void loop(){

  if(digitalRead(pirPin) == HIGH){
    digitalWrite(ledPin, HIGH);   //the led visualizes the sensors output pin state
    if(lockLow){  
      //makes sure we wait for a transition to LOW before any further output is made:
      lockLow = false;            
      Serial.println("---");
      Serial.print("motion detected at ");
      Serial.print(millis()/1000);
      Serial.println(" sec"); 

      imageName = uniqueFileName("png"); //generate a new, uniqe file name

      p.runShellCommand("fswebcam /mnt/sda1/" + imageName); //takes the picture
      while(p.running()); //wait till the process ends
      p.runShellCommand("madplay /mnt/sda1/sounds/sirena.mp3"); //play the siren sound
      while(p.running()); //wait till the process ends
      p.runShellCommand("python /mnt/sda1/sendemail.py /mnt/sda1/" + imageName); //sends the picture via email
      while(p.running()); //wait till the process ends

      delay(50);
    }         
    takeLowTime = true;
  }

  if(digitalRead(pirPin) == LOW){       
    digitalWrite(ledPin, LOW);  //the led visualizes the sensors output pin state

    if(takeLowTime){
      lowIn = millis();          //save the time of the transition from high to LOW
      takeLowTime = false;       //make sure this is only done at the start of a LOW phase
    }
    //if the sensor is low for more than the given pause, 
    //we assume that no more motion is going to happen
    if(!lockLow && millis() - lowIn > pause){  
      //makes sure this block of code is only executed again after 
      //a new motion sequence has been detected
      lockLow = true;                        
      Serial.print("motion ended at ");      //output
      Serial.print((millis() - pause)/1000);
      Serial.println(" sec");
      delay(50);
    }
  }
}


/* A simple function to generate unique timestamp based filenames */

String uniqueFileName(String ext){
  String filename = "";
  p.runShellCommand("date +%s");
  while(p.running()); 

  while (p.available()>0) {
    char c = p.read();
    filename += c;
  } 

  filename.trim();
  filename += "." + ext;

  return filename;
}  

After 10 seconds (calibration time) it will start working and making pictures!!!

This is the assembled project

assembled

All the files of this project are available in my GitHub account

…and check the Youtube Video again!

I really hope you enjoyed this project!

Cheers, Stefano

Arduino Yún social photo camera

After making an USB Audio card work with Arduino Yún, the logical second step was try to use an USB camera instead, and it worked! I was so amazed by this new huge set of possibilitities to create, that I started a little weekend project: to build an Arduino based Facebook photo camera

I was so into this project that I didn’t realize that, even if the single steps are very simple, they’re quite a lot! So I decided to write this step-by-step tutorial and to split it into … articles.

Just to be clear:

The goal

To build photocamera that shoots pictures and publishes them directly to a specific Facebook Album.

Ingredients

The Steps

  1. Install the USB Camera on Arduino Yún
  2. Take your first picture
  3. Create the Facebook Application
  4. Post your first picture with the Temboo API
  5. Install the Temboo SDK on Arduino Yún
  6. Make the python script and publish your first photo to Facebook from Arduino
  7. Create the Arduino sketch
  8. Put everything together

1. Install the USB Camera on Arduino Yún

I assume that your Arduino Yún is already configured for wi-fi (or connected via the Ethernet port), and that you have SSH access to it, if not, follow this link to configure the wifi network and this link to connect via SSH.

The UVC package is already available fot Linino via opkg, so installing the Camera is a very easy step, just connect to your Yún via ssh and type

$ opkg update
$ opkg install kmod-video-uvc

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

$ opkg install fswebcam

Now we can test it!

2. Take your first picture

First of all, be sure to use and SD card for storage or you will quickly fill the Arduino internal storage, and you don’t want to do that. If you are using an SD card, you should see it mounted in /mnt/sda1. If it’s not, put a 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!

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");

Now we will learn how to share them on Facebook using Temboo.

3. Create the Facebook Application

This is easy, but you must be a Facebook developer to do that. That simply means that you have to accomplish these easy steps before starting. Done? ok! So now let’s create a blank Facebook Application

image creation

and configure it in this way:

configuration

 Create a Temboo Application

First of all, register on Temboo. When you’re in, create a new application, then go to https://temboo.com/library/Library/Facebook/Publishing/UploadPhoto/

add new credentials for the new Facebook app.

temboo config

Choose oAuth wizard, go directly to the third step and copy and paste the app id & app secret from your Facebook application.

Go to the next step, click on launch authorisation and grant the authorisation to the Facebook application.

Now you have the access token

Click on use these values and save, copy and paste the access token in the access token form field if needed.

Now you need the target album id. to get an album id, just go on Facebook, open an album (not a single photo) and the URL will look like this.

albumid

The album id is the highlighted part. copy and paste it into the AlbumID field on Temboo

I also choose to open the Optional Input section and to put a custom message 🙂

message

If you take a look to the sample code, Temboo has made pretty much all the job for you, but we need to make some changes, we will make them later. Now we need some gratification, so let’s post our first picture!

4. Post your first picture with the Temboo API

You already put your Access Token and the desired AlbumID. The only thing that’s missing is the Base64 Encoded image. So go to http://www.freeformatter.com/base64-encoder.html and paste the URI of the image you want to encode, then copy the encoded string and paste it into the Temboo interface.

https://i0.wp.com/i.imgur.com/DT1Ij8U.png?w=640

Click TRY IT OUT and voilà, the image it’s magically posted on your album!

5. Install the Temboo SDK on Arduino Yún

We will use the Temboo API from a Python script, so we need to download the SDK. The download page is here https://temboo.com/download, all we have to do is download the Python SDK and unzip it into the root folder of your Micro SD card (it will unzip into a /temboo folder).

A very important step is installing OpenSSL support for Python, as the Temboo libraries will try to use SSL to publish the photos.

$ opkg update
$ opkg install python-openssl

6. Make the python script and publish your first photo to Facebook from Arduino

As I wrote before, the generated code from Temboo is a good starting point, but we need to make some modifications in order to make it work.

# Instantiate the Choreo, using a previously instantiated TembooSession object, eg:
# session = TembooSession('jeko', 'APP_KEY_NAME', 'APP_KEY_VALUE')
uploadPhotoChoreo = UploadPhoto(session)

# Get an InputSet object for the choreo
uploadPhotoInputs = uploadPhotoChoreo.new_input_set()

# Set credential to use for execution
uploadPhotoInputs.set_credential('ArduinoCamera')

# Set inputs
uploadPhotoInputs.set_Message("This photo has been shot and published with an Arduino Yún")
uploadPhotoInputs.set_AlbumID("")

# Execute choreo
uploadPhotoResults = uploadPhotoChoreo.execute_with_results(uploadPhotoInputs)

Download my version of the script and copy it into the root folder of the SD card. Here’s what I did.

Step number one, the encoding. I’m italian and we use a lot of accented characters, so I’ll use UTF-8 as the default script encoding, so I added

# coding=utf-8

at the top of my script. Now we need to read the photo from the disk, and to base64 encode it, so we will need the base64 and the sys libraries.

import base64
import sys

The Temboo libraries are not included in the generated script by Temboo, so we will have to add them

from temboo.core.session import TembooSession
from temboo.Library.Facebook.Publishing import UploadPhoto

The script will read the first argument, and use it as the name of the image file to encode

with open(str(sys.argv[1]), "rb") as image_file:
    encoded_string = base64.b64encode(image_file.read())

In this line you have to put your Temboo’s account name, and the app name and key (from this page)

session = TembooSession('ACCOUNT_NAME', 'APP_KEY_NAME', 'APP_KEY_VALUE')

You’ll also need to put yout access token, message (if you want one) and Album ID into the script

uploadPhotoInputs.set_AccessToken("ACCESS_TOKEN")
uploadPhotoInputs.set_Message("MESSAGE")
uploadPhotoInputs.set_AlbumID("ALBUM_ID")   

The rest of the script is basically identical to the Temboo’s example.

We can now try to publish our first photo with the script!

$fswebcam testpic.jpg
$python sendphoto.py testpic.jpg 

At this point I strongly hope that it’s working for you as it’s working for me (otherwise contact me and we will try to solve the problem together) because you have just published your first picture to Facebook with Arduino! The only missing step is to make an Arduino sketch to take and send the picture without using SSH

7. Create the Arduino sketch

I started from the basic “Button” example, as I wanted the camera to be triggered by a simple Pushbutton. The circuit is the same of the button example. Just download this arduino sketch

you can customise the LED and Button pin if you want

const int buttonPin = 2; // pin for the pushbutton 
const int ledPin =  13; // pin for the status led

as well as the path for the photos and the Python script (important! this folder must contain the Temboo SDK)

String path = "/mnt/sda1/"; // path of the images and script file 

The setup is pretty simple, we will use the Bridge library to invoke the Python script, and the Serial library for a very basic debugging

void setup() {
    Bridge.begin(); // The bridge library is used to run processes on the linux side
    pinMode(ledPin, OUTPUT);      
    pinMode(buttonPin, INPUT); 
    Serial.begin(57600); // Serial port for debugging
    filename = "test";
}

Then, when the pushbutton is pressed, the script generates a new timestamp based filename running the “date +%s” command

if (buttonState == LOW) {
    filename = "";
    p.runShellCommand("date +%s"); //generate a timestamp
    while(p.running()); // wait until the command has finished running

    while (p.available()>0) {
        char c = p.read(); //reads the first available character
        filename += c; //and adds it to the filename string
    } 

    filename.trim(); //used to avoid trailing spaces or newline characters
    filename += ".png"; //finally I add the png extension

This part of the code will take the picture and send it to Facebook using the Bridge library

p.runShellCommand("fswebcam " + path + filename); //let's take the picture
while(p.running());  //waits till the picture is saved
Serial.println("picture taken"); 

p.runShellCommand("python " + path + "sendphoto.py " + path + filename); //sends the picture to facebook 
while(p.running()); 
Serial.println("picture sent"); //last debug message    

8. Put everything together

Ok! Everything is ready! Just upload the code to Arduino, push the button and… Smile!! You’re on Facebook! 🙂

I really hope that you enjoyed this tutorial, it opens a lot of possibilities! Trap cameras, cameras triggered by doors, by sounds or any kind of sensor… I’d really like to see your projects and share them!

Cheers and happy hacking!

Stefano