Add Push Notifications to MotionEye OS

Get mobile notifications when movement is detected by your home CCTV!

Add Push Notifications to MotionEye OS

MotionEye OS is perfect for using your Raspberry Pi as a CCTV home security system. If you want to know if it detects movement when you’re out as part of a home security system, then read on…

MotionEye OS

For those of you unfamiliar with MotionEye OS, it is a dedicated operating system for CCTV systems created by Calin Crisan and distributed free via GitHub. It is very quick to set up and is perfect for your Raspberry Pi due to its lower resource requirement. Those of you who read The MagPi Magazine may recognise this guide as it was featured in their Raspberry Pi 3 launch edition in March 2016, after I originally wrote this for Pi Supply’s Maker Zone! If you want to download the PDF version of the brochure, then you can check it out here.

One benefit of MotionEye OS is its ability to detect motion and capture images and movies of what triggered it. You can even access a live stream of your camera online, even when you’re not home, which is handy if you want to check in every now and then. If you’re not home, being notified of any movement is very useful and MotionEye OS has a nifty option for custom notifications.

This guide will assume you have already set up and configured MotionEye OS and requires a Pushover licence, which costs $4.99 / £3.99. For help, check out the MotionEye OS wiki here.

What You’ll Need

Steps

1. Create an Application in Pushover

Pushover has a great, easy to use API and before we start we need to register an application with them. To do this, go click on “Register Application” under the “Your Applications” heading on their website. Give your app a name – something like RaspiMotion – and then make sure the type is “Application”. Give your app a quick description (i.e. “Push notifications sent by my Raspberry Pi”) and, if feeling creative, upload a custom icon which will show in your Pushover client app whenever a notification is sent.

Create an app in Pushover

2. Get you API Token and User Key

Once you have created your application, you should have access to an API Token/Key. This is a combination of numbers and letters – please keep this a secret! You’ll also need your user key, which is shown once you log in to Pushover’s website.

Okay, now you have an app and your API and user keys. You’ll now need to download (or recreate if you so wish) a simple Python script to tell your Raspberry Pi to work its magic once the script is called upon by MotionEye OS.

3. Create your Python script

MotionEye OS is not like Raspbian. You cannot use certain commands as you would normally (such as git clone), so we’ll have to create our Python script manually (or you can drag and drop using WinSCP if preferred). We also do not need to use sudo as we’re already logged in as root by default. Our script needs to live in the data folder, so let’s go there and create pushover.py using Nano:

cd /data
nano pushover.py

Then once here, you’ll need to copy and paste or type in my code, whilst also including your API Token and User Key where required.

My code can be found over at my GitHub repository, but you can also use the following here:

#!/usr/bin/python

#################################################################################
# Created by Wesley Archer (aka. Raspberry Coulis) to enable push notifications #
# via Pushover.net in MotionEyeOS. See Pushover's API FAQ for more detailed use #
# Also featured on Pi-Supply.com's Maker Zone.                                  #
#                                                                               #
# Follow me:    @RaspberryCoulis                                                #
# Visit me:     raspberrycoulis.co.uk                                           #
# Email me:     [email protected]                                    #
# Like me:  facebook.com/raspberrycoulis                                    #
#################################################################################

import httplib, urllib

conn = httplib.HTTPSConnection("api.pushover.net:443")
conn.request("POST", "/1/messages.json",
  urllib.urlencode({
    "token": "APP_TOKEN",                       # Insert app token here
    "user": "USER_TOKEN",                       # Insert user token here
    "html": "1",                                # 1 for HTML, 0 to disable
    "title": "Motion Detected!",                # Title of the message
    "message": "<b>Front Door</b> camera!",     # Content of the message
    "url": "http://IP.ADD.RE.SS",               # Link to be included in message
    "url_title": "View live stream",            # Text for the link
    "sound": "siren",                           # Define the sound played
  }), { "Content-type": "application/x-www-form-urlencoded" })
conn.getresponse()

4. Make your script executable

As with the majority of scripts used on a Raspberry Pi, we need to make sure it can be executed otherwise it is nothing more than a fancy collection of text! You can do this either from the command line, or from within WinSCP. From the command line, make sure you are in the data folder and then type:

chmod +x pushover.py

Or using WinSCP, select the pushover.py file in the data folder then press F9. In the window that appears, change the permissions to 0755 and then click “OK” to confirm.

5. Configure MotionEye OS to use your script

Now that we have our script, we need to tell MotionEye OS to use it when it detects motion. To do this, log in and then go to the “Motion Notifications” menu and then turn on “Run A Command”. You then need to specify which command to run, which will be the Python script you just created. This will be “/data/pushover.py”. Click “Apply” once done to confirm the changes.

Motion notifications in MotionEyeOS

6. Test it out!

Hopefully by now, you have created your Python script, made it executable, told MotionEye OS to use your script when it detects motion and have the Pushover app installed on your smartphone or tablet. We now need to test that it works! Wave your hand in front of your Raspberry Pi Camera (or you can do a dance if you are feeling energetic!) and then shortly afterwards, you should receive a notification via Pushover warning you that motion has been detected!

Motion detected!

Feel free to experiment with the script to customise the message displayed and sound played in Pushover. Their API documentation is really simple to follow, so be brave and have a play!