Pushetta: Send push notifications from Your Raspberry Pi

By on January 23, 2015

Web site:

http://www.pushetta.com

Project Summary:

Use a Raspberry Pi to send push notification to Android and iOS smartphones. We present a simple intrusion detection system which send a push notification when someone enters in a room.

Full Project:

Some time ago sending a realtime notifications from and embedded system was a problem with few solutions, the one most widespread was SMS. This is an effective solution because knowing the phone number of recipient is all You need to send it a few words. On the other side there are some issues with SMS:

  1. You must pay to send messages
  2. You need to know in advance all the recipients’ numbers
  3. You can send a limited number of characters
  4. You can’t get information about read messages

Today there are more options to solve the task and the one I like more is push notifications. Major mobile operating system implements some system to handle push notifications, Apple was the first to introduce them and in a short time all others OS did.

Pushetta, push notifications from the cloud

What I like to present here is Pushetta, a system made to make trivial use of push notifications, and to dimostrate it I made a simple example using a Raspberry Pi.

First of all You need to register on Pushetta web site, it’s free and it’s a mandatory step to be able to send notifications (http://www.pushetta.com).

Pushetta signup

Pushetta signup

After registered it’s time to login and create your first channel, a channel is something like a topic where your notifications are sent . User want to receive notifications have to subscribe channel interested in.

Schermata 2014-11-20 alle 21.26.56

Creating a channel requires few data: an image to identify it, a name and a short description are the essential ones. Channel can be public or private, at this time only public channels are available, hidden flag make it invisible from search.

After a channel is created we are ready to start pushing notifications, now it’s time to use or Raspberry Pi. Pushing notifications with Pushetta can be made in many ways, using curl from command line or from all major languages, we’ll use python in our project.

Send a notification when someone is in the room

We’ll use Raspberry Pi as intrusion detection system, our objective is to push a notification when someone get in a room. First we need to prepare the SD, I use raspbian in this project (it can downloaded from here and here there are instructions to install it).

Installed raspbian it’s time to make a simple circuit to detect when someone enters the room. I use a PIR (Passive InfraRed) sensor, this kind of sensor uses infrared light to detect temperature changes in a specific range (check here form a detailed explanation). Given that human body is warmer than the surrounding environment it can detect when someone move around.

PIR sensor

PIR sensor

This sensors are really simple to interface with, all we need it to give it power and check OUT pin from a Rapberry Pi GPIO. When OUT pin goes high (+3.3V) something is in range.

raspi-gpio

I made this simple circuit using Raspberry model B but it can be simply adapted for other models. Now we can start writing our first lines of code to read PIR status. I’ll use a Python module very useful that make it trivial to use GPIO on Raspberry Pi, this module is called RPi.GPIO. First of all we need to connect to Raspberry Pi via ssh and download the module.

$ wget https://pypi.python.org/packages/source/R/RPi.GPIO/RPi.GPIO-0.5.8.tar.gz
$ tar xvzf RPi.GPIO-0.5.8.tar.gz
$ cd RPi.GPIO-0.5.8/
$ sudo python setup.py install

 

If You get an error like “Python.h: No such file or directory” You need to install Python dev packages with:

$ sudo apt-get install python-dev

Now we are ready to write the Python code, is very important to use a pinout reference when write the code, one really good is here.

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)

# In BCM mode pin 7 is identified by id 4
PIR_PIN = 4
GPIO.setup(PIR_PIN, GPIO.IN)

try:
        print "Reading PIR status"

        while True:
                if GPIO.input(PIR_PIN):
                        print "Motion detected!"

except KeyboardInterrupt:
        print "Exit"
        GPIO.cleanup()

 

By default GPIO are accessible only to admin users so to use the script launch it with sudo (something like “sudo python test_pir.py”).

Now, if all works as expected we’ll see in console “Motion detected!” every time PIR detects some movements and next step is to push a notification in this condition. Looking a Pushetta’s doc page there is a Python sample with everything we need. I used a polling approach, that is checking continuously the status of OUT pin in a infinite loop. This isn’t the best approach, a better one is using interrupt but is more complex and I prefer to get focused on our target.

First we need to imports some modules in our script.

import urllib2
import json

 

And define the function to send the notification.

def sendNotification(token, channel, message):
	data = {
		"body" : message,
		"message_type" : "text/plain"
	}

	req = urllib2.Request('http://api.pushetta.com/api/pushes/{0}/'.format(channel))
	req.add_header('Content-Type', 'application/json')
	req.add_header('Authorization', 'Token {0}'.format(token))

	response = urllib2.urlopen(req, json.dumps(data))

 

Now it’s only matter to combine everything and you’re done, the full working example can be downloaded from here.

 

 

Software & Code Snippets:

import urllib2
import json
import RPi.GPIO as GPIO
import time


def sendNotification(token, channel, message):
	data = {
		"body" : message,
		"message_type" : "text/plain"
	}

	req = urllib2.Request('http://api.pushetta.com/api/pushes/{0}/'.format(channel))
	req.add_header('Content-Type', 'application/json')
	req.add_header('Authorization', 'Token {0}'.format(token))

	response = urllib2.urlopen(req, json.dumps(data))


GPIO.setmode(GPIO.BCM)

# In BCM mode pin 7 is identified by id 4
PIR_PIN = 4
GPIO.setup(PIR_PIN, GPIO.IN)

try:
        print "Reading PIR status"

        while True:
                if GPIO.input(PIR_PIN):
                	sendNotification("4b6e163b7935271b924dbf2fdc7b4b528bd6c6db", "Sample Channel", "Motion detected")
                        print "Motion detected!"

except KeyboardInterrupt:
        print "Exit"
        GPIO.cleanup()

About guglielmino

3 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *