Introduction

In this tutorial, I am gonna show you how to build a simple application that detects motion and sends notification to the your smart phone using raspberry pi and camera module. If there are something or someone moving in the camera setup field, you will get notified to your phone or web browsers.

The notification comes with text, still image, and video. that you can view it right on iOS or android.
The application is pretty simple. It utilises the free software and services such as picamera, ffmpeg, and pushbullet.

I will walk you through the architect, flow chart, and explain the main python functions. At the end of this video, you can build your own application or customize the already built one as you wish.

Let start!

Specification

This section explains the specification of application including app structure, and general flow chart. You will have a clear vision about what application does.

Application Structure

There are 3 actors play together to build up the application. (1) RaspberryPi&Camera module as the data source that continuously scan for motion and recording video at the same time, (2) PushBullet as the message-broker to send messages(text, still image, and videos) back and forth between RaspberryPi and Mobile/Web, and (3) mobile/web acts as “notifyee” who receives the still image and video of motion. The communication is two ways though. It means you can send a command from mobile or web to Raspberry Pi for particular action such as “@snap” (for instantly take a photo) or “@check” (to get current system information of Pi).

General Flow Chart

When the script launch, it takes 2 seconds to warm up the camera to make sure everything works properly. Then jump right into motion scanning. If moving happens, The Pi will capture a still image and send notification to phone immediately. At the same time, Pi keeps recording the moment for a few more seconds. After that a mount of time, if moving is still there, Pi re-capture a new image and send a new notification. Until no motion anymore, the recorded videos will be pushed to mobile to confirm the motion end. The system goes into motion scanning state. The programe loops again and again as this flow.

Just Work

If you can’t wait to see it works for you, this section is for you. You will understand the source code later. For now, everything you need is to make the application up running.

Step1: Setup Pushbullet

First, you need to register a pushbullet account get the key for notification. Goto https://www.pushbullet.com, you can either register with google email or facebook. In my case, I use gmail. After successful signup, Let login and goto Settings–> Account to create a API access token. We need this token for Raspberry Pi to push notification. Just note them down some where.

Second, on mobile, the bushbullet application is ready for both iOS and Android. So, install it. Make sure you login with the pushbullet account that you just registered.

Step2: Download software

I recommend you download and install the ready-to-work image in Resources section to save your ton of time for setup necessary software and libraries. Otherwise, you can setup from the brand-new Jessie. That’s fine.

This application builds on top of standard Raspbian Jessie released on 2017-04-10 that downloads from official website of raspberry pi. You also need ffmpeg for video convert from h264 to mp4 to view directly on your phone or web browser, and pushbullet python library for notification. One note for ffmpeg that it is not ready for install via “apt-get” command on Jessie, you have to compile it from source code. The compilation time takes about 3 hours. The source code of application is mostly written in Python.

Now let download the ready-to-work image, this file size is huge, it’s about 2.7GB. So now should be the coffee time.

Step3: Flash Image to Pi

Oh right, you have downloaded the image successfully, now let unzip and flash it to your SD card. I used the sandisk micro sd card 8GB. I’m using MAC. In case you don’t know how to flash image to SD card, check this tip. If you’re on windows, you can flash with WIN32DISKIMAGER. The flash takes about 20 minutes, coffee again. (make sure you don’t drink too much)

Step4: Enter the pushbullet access token

Now is show time, let take a look how it works. Insert the SD card, power Pi up. The application source code is placed right on the Desktop. You only job now is enter the pushbullet key as following:

Save the main.py and reboot the pi. Enjoy the result!

Step5: Play around

Take a look at pushBullet application on your phone, you should see a message that tells The application is up running. The first message should be “PiCameraNotifier app starts!”. Right swipe that message to bring you to chat screen of PushBullet. Later on, all images, videos, and texts from RaspberryPi will be sent to this chat screen. Now let try moving something in front of camera to see the magic happens. Use QuickView on iOS to view motion video. Here is my example.

Under The Hood

You want to learn deeply how source code works, this section is for you. In this section, I will explain very detail about how the code works along with its flowchart. You will be able to customize the code to add new commands, change the video or image size, store files, or what ever you need.

There are 2 python files you need to care of, they are main.py and push.py. You may guest the function of each file by its name, right? main.py is all about logic of application and push.py takes care sending notification and receive the command.

main.py

At app launch, I create two important objects – camera and notificationHandler. Those objects are used in all functions below.

We initialise camera for motion detection and recoring ahead into circularIO. The reason we need to record ahead is because we want to have a video about the moment before motion detected. We have no way to predict when something will happen, so we have to record in advance.

When motion detected, the function didDetectMotion() will be called. Pi will capture a still image and send notification to mobile via captureImage() function. Then It keep recording 2 more seconds to complete a video. That video will be pushed when the scheduled timer expired. Take a look at writeVideo() function.

In case you send a command to Pi, “@snap” for example, the function didReceiveCommand will be called and proccess accordingly. The same operation goes for “@check” command.

push.py

Now let take a look on push.py that functions transmit and receive the notification.

For Transmition, The sending out messages are text, image, and videos. To minimize the crazy things happen on sending, I use serial queue to sending one after one. You can leverage the CPU by changing the maximum thread number to 2 or more

For Receiving, I use the “@” character to mark it as a command. Here I have defined two commands “snap” and “check” as you seen. You can define your own commands by copy those lines of code.

To feel the code better, you should checkout the repository and play arround with it. Try customize the things like camera resolution, video duration, text messages, or add new command.

Log

I write log to file /home/pi/Desktop/PiCameraNotifier/run.log just in case if error happens, or you want to check the status of running. You can change the file path by modifying the variable LOG_FILE_PATH in main.py. Here is an example of log info.

05/24/2017 02:37:06 PM =========== app launched ========
05/24/2017 02:37:07 PM ### Setup Notification Listener
05/24/2017 02:37:07 PM Starting new HTTPS connection (1): api.pushbullet.com
05/24/2017 02:37:11 PM Start main
05/24/2017 02:37:13 PM ### Initialize Camera
05/24/2017 02:37:13 PM cameraInitialize: for (1) motion detection, and (2) circularIO recording
05/24/2017 03:21:27 PM motion just detected
05/24/2017 03:21:27 PM push image…

Auto launch the app at Pi boot/reboot

I think you wonder why the application is up running automatically after reboot the Pi as we do in above step, don’t you? I use the systemctl to config the PiCameraNotifier as a service that runs in background. Here is the configuration file /lib/systemd/system/PiCameraNotifier.service

Resources

Summary

We have a real application that detects motion detection and notify to smart phone, run on Raspberry Pi 3. This application should work normally with raspberry pi zero or Pi 2.
You may wonder if is it realtime. Nah It’s not realtime, but fast enough to be aware what happen in the camera setup place.
This application does not require coding skill, but You can modify the source code as your need to add more features or customise the function.
We have very clear process for real product development. We know architect, flowchart, source code. and How those things dance together.

In case you get stuck somewhere, write your question. I will read and answer.

If you like the video, please subscribe the channel, share, and thumps up. That will inspire me to produce more helpful videos. Have a good time!

Update 1 : (01 June 2017)

+ While you testing the app, it may run out of Pushbullet quota – 500 message per month for a free account and you can’t receive notification anymore. To overcome this issue, I think you can do two things: (1) register multiple accounts and switch between them, or/and (2) customize the source code to not push text anymore, only push images and videos.

+ One more think I experienced when making the tutorial that If we push too many messages in a short period, the Pushbullet server will return that it temporarily holds the notification. Again, to overcome this issue, you can do the 2 things I mentioned above.

 

Build a Camera Alert Application With RaspberryPi 3 and iOS/Android Pushbullet app
  • https://twitter.com/firdoshnd

    I keep getting “Your merge fields invalid” when trying to download image file…. please help

    • I have fixed the issue. It should work now. Thanks!

  • https://www.facebook.com/app_scoped_user_id/1449697425094857/

    after I download the file I get an error at the very end. I tired sevral times, and on other computer, same problem, end of file does not complete the download.

    • https://www.facebook.com/app_scoped_user_id/321990928230737/

      Oop, sorry for that. Let me try another distribution system rather than Google Drive. I’ll inform you after uploading complete.

    • https://www.facebook.com/app_scoped_user_id/321990928230737/

      hi David, I’ve just moved the pi image file from Google Driver to MediaFire. You can try it now.

      • https://www.facebook.com/app_scoped_user_id/1449697425094857/

        Kenny, OK thank you. yeah, I just tried again, and got the same error from MediaFire. I took a screenshot of the error

        • https://www.facebook.com/app_scoped_user_id/321990928230737/

          hi David, I think the root cause of error is not about the file itself, it’s about the unzip software. I google around and see this link. https://www.technipages.com/fix-an-unexpected-error-zip . Let try to use another tool to unzip the file.

          • https://www.facebook.com/app_scoped_user_id/1449697425094857/

            Kenny, I just used winRAR to unzip the file, and its looks good, I’m going to work on transferring to the img to the sd card, and let you know how it goes. Thank you for getting back to me.

          • https://www.facebook.com/app_scoped_user_id/1449697425094857/

            Hi Kenny, I got the image onto the SD card, and fired it up. It did the setup perfect, then I put my Pushbullet Token into the Main.py. rebooted, and nothing happened. Well I just realized you are using a Pi 3, and I was using a Pi Zero W last night. I thought it would work on either Pi, but that may not be the case. I will try it in a Pi 3 shortly, and see what happens. I let you know, Thanks again.

          • Actually i don’t have RPi Zero to test it out. I’m ordering two Pi Zero then I can check and feedback to you also. Nice day!

          • https://www.facebook.com/app_scoped_user_id/1449697425094857/

            Hi Kenny, Tonight I setup Raspberry Pi 3B with your i mage file, It worked perfectly, then setup my PushBullet Token, and its working great! So I was trying to find where I can modify script code to not send the jpg, or video, and then add Apache web server, to look in on the files, and live stream. Thank You
            David

          • https://plus.google.com/108410369126236293466

            Any luck with the Pi Zero? With the size of the Zero being so small it would be perfect for this application. Excellent idea and tutorial by the way!

          • https://www.facebook.com/app_scoped_user_id/1449697425094857/

            Hi Kenny, Tonight I setup Raspberry Pi 3B with your image file, It worked perfectly, then setup my PushBullet Token, and its working great!
            Thank You
            David

  • https://www.facebook.com/app_scoped_user_id/1300333313416559/

    why i can’t receive the notification of the detect motion for my own device after i install all the steps given?i had choose this project for my final year project.

    • Could you please send the logs content: run.log in /home/pi/Desktop/PiCameraNotifier so that I can understand what really happen ?

  • https://plus.google.com/102293657992127043684

    where can i find the ready to work imgage or resources

    • https://www.facebook.com/app_scoped_user_id/321990928230737/

      Click on the button “Download PiCameraNotifier_V1.img” to download. Please note that you should use WinRAR to unzip the file because its file size is larger than 2GB.

  • https://twitter.com/isabella085

    will this work on raspberry pi 2?

    • https://www.facebook.com/app_scoped_user_id/321990928230737/

      In theory it should work on RPi2. But I didn’t test on Pi2 yet. Can you give it a try?!

  • https://plus.google.com/116512187101027344843

    currently lost on part three. I downloaded your Image file PiCameraNotifier_V1 and flashed it to a fresh (reformatted) micro sd card. now, ofc my PI-2 won’t boot. I know,
    I took that risk. My question or concern is about this line of code

    ‘Flash Pi Image To SDCard
    Shell $> sudo dd bs=1m if=/[your_dowload_path]/iotbreaks_PiCameraNotifier_V1.0.img of=[The SD Card]’

    primarily, why is in step three, where we flashed the sd card and before we boot the PI?

    so, when do we boot the Pi and How do we boot the PI? is the PI os already on the SD card from before flashing it or is it part of PiCameraNotifier_V1?

    • https://www.facebook.com/app_scoped_user_id/321990928230737/

      >>primarily, why is in step three, where we flashed the sd card and before we boot the PI?
      I simply mean we have to flash the OS into sd card before plugging it to Pi. That’s it. If your sd card already have OS on it, then just plug it in, and do the rest (clone git, setup pushbullet…)

      • https://plus.google.com/116512187101027344843

        Thanks for responding. Still lost at step three. Went and got a pi3. Installed Raspbian (from noobs). Took the sd card from the pi and flashed your picameranotifierv1.0 to the SD card and got the same.problem.l.had yesterday…ugh. My pi won’t even.boot now 🙁 … you see attached picture of my screen?. First.line is picameranotifier.service lacks ExecStart setting……about 20 more errors and hung boot. I’m not getting it. Dumb question…..how can l flash your picameranotifier to the SD card without destroying the raspbian os? (which is.on the sd card)

        • https://www.facebook.com/app_scoped_user_id/321990928230737/

          >> Installed Raspbian (from noobs)
          ==> I think you should try using the image PiCameraNotification_v1.img instead of the default Raspbian OS. You should do as following:
          – Format your SD card as FAT32 or ExFAT
          – Flash PiCameraNotification_v1.img into the SD card
          – Then plug the card in Pi3, power it up.

          • https://plus.google.com/107155090273228600990

            is there any way to stop the camera from capturing the image and video ? i mean, stop it for 2 – 3 minutes from sending us notification because my motion detector is way too sensitive, even a breeze of air it will captured and the notification goes like crazy 🙁

          • https://www.facebook.com/app_scoped_user_id/343383742762305/

            Hi Did you manage to get this working?

          • I think we should care about the sensitiveness of motion capture. Please read the source code to see the threadhold of image diff to decide trigger notification or not.

  • https://www.facebook.com/app_scoped_user_id/10211693233238892/

    Dear Kenny i follow all steps and work ok… but i’m still freeze at step “Auto launch the app at Pi boot/reboot” but how to add configuration file /lib/systemd/system/PiCameraNotifier.service in raspian os ?

  • https://www.facebook.com/app_scoped_user_id/10212282495630436/

    Hello, Imma newbie here. Do I still need to flash an SD card with the downloadable IMG file or can I get the code on github and run in with a default raspian OS? Sorry I’m very new to this and I chose to replicate your work as part of my finals project . Hope for the help >.<

  • https://www.facebook.com/app_scoped_user_id/10212282495630436/

    I was able to replicate everything . Very nice work! Would there be any way that a PIR sensor would be able to play the trigger instead of the Pi Camera to be rid of false positives? Just a thought. I would really like to see that in the future! Cheers!

    • https://www.facebook.com/app_scoped_user_id/343383742762305/

      Hi Have you made any progress?

  • https://www.facebook.com/app_scoped_user_id/343383742762305/

    Hi V1.img file showing me 12 to 14 hours to download even though I have a very fast internet connection, sure this cannot be right? Is there not a faster way of downloading this file?

    • Sorry that I’m using the free service. So it’s the limitation.
      Let me consider to use DNS for the file.

  • https://www.facebook.com/app_scoped_user_id/343383742762305/

    Hello Kenny where are you? many questions unanswered…

    • Hi Diakus, I’m here :D.
      Most of questions are too vague, not enough information for me to answer.
      Please add as detail as possible so that I can help.

  • https://plus.google.com/109728250930879585728

    After sending @snap command , notification stopped and not able to receive any thing from raspberry pi

  • https://plus.google.com/115408112685452524436

    Cannot even run..I already follow the steps..need some help

  • https://plus.google.com/117572249739921096509

    plz i want pdf download link not picamera.img download link any one can help me

  • https://plus.google.com/103936934128788477305

    Prob a silly question but how to you save the main.py? ctrl + x doesnt do anything…. cheers.

    • If you’re using VIM editor, then type :w for saving the main.py

  • https://plus.google.com/117692071842442940686

    Hi Kenny, I am using windows and anytimes I tried to extract the ready to work file to my SD card I got an error message! what should I do? thank you.

    • Because the file is over 2GB, so please use the proper extract tool on Windows

  • https://plus.google.com/117572249739921096509

    can use web camera ?

    • No, unfortunately.

  • https://plus.google.com/117692071842442940686

    Hi, Kenny, I think your file is corrupted! I tryied to extract it into my sd card but it is not working. this error appeared 0x80004005

    • hi Issa, maybe the file is corrupted while downloading. I don’t change anything till now.
      Please try again at the time when the internet is not so busy.

  • Hi Kenny, thank you for the video. I managed to flash the PICameraNotifier image onto my SD card but it won’t boot up at all. All i see is the rainbow window with a lightning icon on the top right. I am very new to Raspberry and this is my first project. How can i fix this? Thank you.

  • https://plus.google.com/109006661524694147466

    Hi!

    File “./main.py”, line 8, in
    from pushbullet import Pushbullet
    File “/home/pi/Desktop/PiCameraNotifier/pushbullet/__init__.py”, line 4, in
    from .listener import Listener
    File “/home/pi/Desktop/PiCameraNotifier/pushbullet/listener.py”, line 9, in
    import websocket
    ImportError: No module named websocket

  • I burned the image on SD card but when I insert into Pi,all I see is the rainbow color and nothing happens. Someone also said that he has the same problem. It looks like the image file is corrupt. Please fix it. Thanks.

Share This

Subscribe To Our Newsletter

Join our mailing list to receive the latest news and updates from our team.

You have Successfully Subscribed!

Subscribe

To download the latest tutorials

You have successfully download

Enter Email To Download

Join our mailing list to receive the latest Application for free from IoTBreaks.

You have Successfully Subscribed!

flash Raspbian to SDCard

Enter Email To Download

Join our mailing list to receive the latest Application for free from IoTBreaks.

You have Successfully Subscribed!

Subscribe To Our Newsletter

Join our mailing list to receive the latest news and updates from our team.

You have Successfully Subscribed!