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.
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.
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”.
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:
Then you have to create an Application Token:
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:
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
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.