When you need a mechanism to send certain events to your smart phone one simple way can be via email. The downside of this way is that your phone has to check your email account in short intervals to receive the messages. A better way to get the notifications is the push-service which is implemented in the smartphone operation system. The push-services are using a kind of direct connection between your smartphone and the push-message server. They are faster and generate less data-traffic then the polling of a email-server.

One solution to get push notifications to your smart phone is the app Pushover. The Pushover service itself is free (no monthly fees up to 7500 messages per month), however the client applications are priced at $4.99 (iOS or Android). The Pushover-FAQ contains sample code for numerous programming languages (PHP, Python, etc.) and command line examples on how to send notifications using the Pushover service. Based on this sample code I will show you how to build PIR motion detector which sent push messages to your smartphone.

Hardware setup:

For the motion detection a PIR module "HC-SR501" is used. This modul is powered with +5V and has a 3.3V output. This module could be directly connected to the beagle bone. But  I added a few resistors, a npn transistor and an led as an motion indicator.

PIR Alarm Schematic

The circuit is very simple. When no motion is detected the output of the module is 0V then the transitor is switched off and the led is also off. On Pin GPIO0_7 off the beagle bone a high level or "one" will be read. When motion is detected the output of the module is +3.3V and the transistor will switch on the led. The readback on the GPIO0_7 will be low or "zero".

BeagleBone PIR Alarm Circuit

To test your hardware setup you can use the GPIO subsystem in the sysfs.

Test the input:
cd /sys/class/gpio        #! select GPIO subsystem
echo 007 > export         #! export GPIO0_7 = 0*32+7 => 007
cd gpio7                  #! select gpio 7
echo in > direction       #! set gpio to input
cat value                 #! output the state of the input gpio 7


Getting the User and API Token from Pushover:

Before you can use the Pushover App you need to register on the pushover-webside and get a user key:
Pushover User KeyThen you have to create an Application Token:
Pushover Applicationkey

When you create a API Token select Script as Type and add a description of the Application. It is also possible to add a new icon, which is displayed in the app when a push message is received:
API Token

 Python script for motion detection and sending the push message:

 The python script uses the Adafruit_BBIO library for the GPIO access. To install the latest version of the library you have to follow the next steps:

Installation of the Adafruit_BBIO library:
sudo ntpdate pool.ntp.org
sudo apt-get update
sudo apt-get install build-essential python-dev python-pip -y
git clone git://github.com/adafruit/adafruit-beaglebone-io-python.git
cd adafruit-beaglebone-io-python
sudo python setup.py install
cd ..


Then you can make a new directory and write the python script:

mkdir pir_alarm
cd pir_alarm
nano pir_alarm.py


 

pir_alarm.py
  1. #!/usr/bin/python
  2.  
  3. import datetime
  4. import time
  5. import Adafruit_BBIO.GPIO as GPIO
  6. import httplib, urllib
  7.  
  8. timestamp_alarm = 0
  9.  
  10. def alertPush(body):
  11.        conn = httplib.HTTPSConnection("api.pushover.net:443")
  12.        conn.request("POST", "/1/messages.json",
  13.           urllib.urlencode({
  14.             "token": "Your API Token here",
  15.             "user": "Your user key here",
  16.             "message": body,
  17.             "sound": "spacealarm",
  18.           }), { "Content-type": "application/x-www-form-urlencoded" })
  19.        conn.getresponse()
  20.  
  21.                
  22. def send_alarmmessage():
  23.      global timestamp_alarm
  24.  
  25.      now = datetime.datetime.now()
  26.      timeString = now.strftime("%d.%m.%Y %H:%M:%S ")
  27.      Alarmstring = " PIR Alarm"
  28.      print timeString + Alarmstring
  29.      
  30.      if time.time() > (timestamp_alarm + 60):
  31.         print "Sending push message!"
  32.         alertPush(timeString+Alarmstring)
  33.      timestamp_alarm = time.time()
  34.  
  35.  
  36. def main():
  37.         GPIO.setup("P9_42", GPIO.IN)
  38.         GPIO.add_event_detect("P9_42", GPIO.FALLING)
  39.         try:
  40.            while True:
  41.              if GPIO.event_detected("P9_42"):
  42.                    send_alarmmessage()
  43.                    print("Event!")
  44.                  
  45.         except  KeyboardInterrupt:
  46.                 exit()
  47.  
  48. if __name__ == "__main__":
  49.         main()


The main function of the program sets the GPIO on connector P9 pin 42 to input mode and installs an event which is triggered at the falling edge of the input signal. When the event is detected the send_alarmmessage function is called. This function get the actual time from the system and build the alarm message. When the last alarm is older then 60 seconds the alarm will be sent to you by calling the alertPush function. The alertPush function using a https-request to pushing the message to the server. Before you save the script don't forget to change the userkey and API-Token!

Now you can make the python script executable and start the program:

sudo chmod a+x pir_alarm.py
./pir_alarm.py


Now you should get this output and be able to receive the push messages on you smart phone.

PIR Alarm terminal output

Pushover Notification     Pushover Application