Raspberry Pi, Camera, Node.js – Video & Email The Intruder

This post is part 5 of 9 in the series Raspberry Pi
Tweet about this on TwitterShare on LinkedIn0Share on Google+0Share on Reddit0Buffer this pageFlattr the authorEmail this to someonePrint this page

In one of my earlier posts : Raspberry pi, PIR Sensor and Node.js – An IoT Intruder Alert system, I have shown how you can detect intruders with Raspberry Pi and a PIR sensor.  And once the intruder is detected, an email will be sent with the time of incident.

This is good but not that helpful. In this post, instead of just sending an email, we will record a video of the intruder as the “act” is performed and then attach that video along with the email. Sweet right!

Below is a quick demo that show how things work

The video is a bit fast and too shaky. So Let me quickly run you through it. First we start the node program. As soon as a intruder is detected, the buzzer gets triggered. At the same time, we start the recording on the camera. We record a video for 10 seconds and then start sending the same in an email.

Pretty sweet right!

You can find the completed code here.

So let us get started building this IoT Video and Email The Intruder system!

Prerequisites

If you are new to Raspberry pi and have not yet installed Node.js on it, I would recommend going through Getting Started with Raspberry pi and Node.js.

If you are new to electronics devices and circuits, I would recommend going through the video lectures from All About Circuits.

Components needed

  1. 1 – Raspberry pi B+
  2. 1 – Raspberry pi camera
  3. 1 – Breadboard
  4. 1 – PIR Sensor
  5. 9 – Female to Male connectors

If you are new to Raspberry Pi GPIO, please refer this.

I have already written a post that explains how to implement the intruder alert system that sends an email : Raspberry pi, PIR Sensor and Node.js – An IoT Intruder Alert system. I will continue from the output of that post to avoid repetition.

Setup Filezilla

Before we start capturing stuff, we need an easy way to view all our captures. Since we are going to use ssh to run our capture commands, we will need to retrieve the saved files. Of course you can use VNC to connect to the Pi and view the files too. But for the sake of simplicity, we will use Filezilla.

FileZilla is free, cross-platform FTP application software, consisting of FileZilla Client and FileZilla Server. Binaries are available for Windows, Linux, and Mac OS X. It supports FTP, SFTP, and FTPS (FTP over SSL/TLS).

We are going to connect to our pi using Filezilla and access the directories and files.

You can download Filezilla client for your OS from here. We will start using it in a moment.

Setup Raspberry Pi Camera

If you have not already mounted the Camera on the pi, I would recommend following the below video to do so.

Once that is done, you can test the camera. Make sure the camera module is properly mounted before booting the Pi.

By default the camera module is  not enabled, so we need to enable it. Once your Pi is booted completed and you have connected to it via ssh, you can run

sudo raspi-config

This will bring up the config screen, which will look like

Screen Shot 2014-10-25 at 7.40.13 pm

Using the down arrow key, select the Enable Camera option. Then press enter and you should see

Screen Shot 2014-10-25 at 7.40.46 pm

Use the right arrow key to select Enable and press enter. Once the Camera is enabled, you will be take to the config screen.

Screen Shot 2014-10-25 at 7.40.57 pm

Use the right arrow key to select Finish and press enter. You will be asked to reboot, press yes.

Once the reboot is completed, Login to your pi again. Now that we have enabled the camera, lets capture things!

As soon as you ssh into pi, you will be landing inside the /home/pi folder. If you want you can create a folder named media to save all the captures and CD into that folder.

We will first capture a picture. From terminal/putty run

raspistill -o myImage.jpg

The camera should turn on (Red LED next to the lens) and a picture will be taken after 5 seconds. Once that is done, we will get the saved image to our computer to view it.

Launch FIlezilla. Once Filezilla opens up, Go to File > Site Manager. Here we will create a new Site and hook it up to the Pi. This is an one time operation and from next time on, you can select the site and click on connect button directly.

Click on the New Site button from the Site Manager, located at the bottom left hand corner and update it as below

Screen Shot 2014-10-25 at 7.28.34 pmThe Host is the IP address of your Pi and the username/password are pi/raspberry. Now click on connect button to connect to your Pi and you will see a screen like

Screen Shot 2014-10-25 at 7.31.55 pm

The left hand section is your local file structure and the right hand is your Pi. Now, you can navigate to the media folder and drag and drop files from right bottom panel to left bottom panel. And you can view your files on your machine!

So everytime you capture something, you can simply drag and drop from right to left.

Simple and easy!

Now we will capture a video. From terminal/putty run

raspivid -o myVideo.h264 -t 10000

And this starts the camera and records a video for 10 seconds. Once that is done, it will be saved as h264 video.

Again you can use Filezilla to get the video to your local and you can play it using VLC player.

If you want to convert the h264 format to say mp4, you can use FFmpeg. You can download the appropriate version for your OS here.

Once FFmpeg is setup, you can run the below command

ffmpeg -r 30 -i myVideo.h264 -vcodec copy pi_cam_recording.mp4

Now you can play this file with any mp4 player. Simple and easy right!

Now let us use this camera in a real time scenario.

Building the Intruder Detection System

Continuing from my earlier post, we are now in a state where the embedded system sends an email when an intruder is detected. Now we will update that program.

Updated index.js as below

We have updated the  pir.watch(). If the value reads high, and if the recording is not in progress, we will trigger the recording on line 20. And once that operation is completed, we will trigger the email.

The updated mailer.js would be

Do notice line 28. This where we are attaching the video to the email.

Now to test the application, run

sudo node index.js

And the guarding begins! Once the intruder is detected, the camera should start recording and once done, the email will be sent with the attachment.

You will notice some lag between the trigger and when the video actually starts recording. One solution is to position the PIR sensor in such a way that it detects the intruder a bit early and then by the time the recording starts, the intruder is in frame.

Hope this post gave you an ideas as how to use a Raspberry pi camera module along with a real time use case.


Thanks for reading! Do comment.
@arvindr21

Series Navigation<< Raspberry pi, PIR Sensor and Node.js – An IoT Intruder Alert systemRaspberry Pi, Camera and Node.js – Live Streaming with Websockets #IoT >>
Tweet about this on TwitterShare on LinkedIn0Share on Google+0Share on Reddit0Buffer this pageFlattr the authorEmail this to someonePrint this page
  • Nicola Theen

    no longer works, instructions out of date

    • http://thejackalofjavascript.com/ Arvind Ravulavaru

      Can you fix the code?

  • Christopher Tryens

    Thanks so much!

  • Rolf Erikson

    Nothing is saved in stdout but if I change video_path to a file like video.h264 it all works

  • http://www.venkatapathirajum.com VenkatapathiRaju Mandapati

    Brilliant.

    • http://thejackalofjavascript.com/ Arvind Ravulavaru

      Glad you liked it Venkat! Thanks!