Push Messages

General

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 Key

Then 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
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/python

import datetime
import time
import Adafruit_BBIO.GPIO as GPIO
import httplib, urllib
 
timestamp_alarm = 0
 
def alertPush(body):
	   conn = httplib.HTTPSConnection("api.pushover.net:443")
	   conn.request("POST", "/1/messages.json",
		  urllib.urlencode({
			"token": "Your API Token here",
			"user": "Your user key here",
			"message": body,
			"sound": "spacealarm",
		  }), { "Content-type": "application/x-www-form-urlencoded" })
	   conn.getresponse()
 
			   
def send_alarmmessage():
	 global timestamp_alarm
 
	 now = datetime.datetime.now()
	 timeString = now.strftime("%d.%m.%Y %H:%M:%S ")
	 Alarmstring = " PIR Alarm"
	 print timeString + Alarmstring
	 
	 if time.time() > (timestamp_alarm + 60):
		print "Sending push message!"
		alertPush(timeString+Alarmstring)
	 timestamp_alarm = time.time()
 
 
def main():
		GPIO.setup("P9_42", GPIO.IN)
		GPIO.add_event_detect("P9_42", GPIO.FALLING)
		try:
		   while True:
			 if GPIO.event_detected("P9_42"):
				   send_alarmmessage()
				   print("Event!")
				 
		except  KeyboardInterrupt:
				exit()
 
if __name__ == "__main__":
		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


Pushover Notification


Pushover Application