How to send Push Notifications from Raspberry Pi to Smart Phone

How to send Push Notifications from Raspberry Pi to Smart Phone

Nowadays Home security is very important and there are many surveillance systems available in the market to monitor your home or office from anywhere. But these IP based security systems are very costly, and the cheaper once only stream data locally.

So here we are building a simple yet useful Home Security System using Raspberry Pi and PIR Sensor. This system will detect the presence of Intruder and quickly alert the user by sending a Push notification to their phone. Raspberry Pi is used to control the whole system and PIR sensor used to detect any motion. This system can be installed near the door of your home or office and you can get the live notification if someone enters in your house. 

Raspberry Pi is the most suitable microcontroller for IoT projects as it has inbuilt Wi-Fi to connect with internet easily. To start using Raspberry pi, either you should have a display monitor which can be connected using HDMI cable or you can access it using SSH terminal or VNC server. In our previous projects, we controlled Raspberry Pi GPIO with various IoT platforms.

 

What is Push Notification and How it Works?

Push notifications are just like instant messages that pops up on a smartphone or  desktop/laptops. These messages are pushed by a backend server which contains some information related to latest offers, promotions, advertisements, scores etc. Almost all mobile platforms support push notifications and most of the apps send notifications as push messages like Facebook, amazon, chrome etc. However you can turn off push notifications from a particular app. This service help the user to continue their work while the important information pop-ups on screen.

In Android device, push messages are shown with the app icon and a message in the status bar. When the user taps on that icon then it directs on that particular application.

 

How Push Notification works?

There are some servers which are used to send data to your device like GCM (Google Cloud Messaging) for Android, MPNS for Microsoft and APNS for Apple.

How Push Notification works

 

Below steps shows how push notification works on Android devices:

  1. First registration takes place of android device on the GCM server by sending sender ID and application ID.  
  2. When registration is successful, a registration ID is issued to android device by the server.
  3. Then the received registration ID is sent to the server which is handling that particular application.
  4. The app server will store registration ID in the database for further use.
    • The app server sends a message to GCM server along with device registration ID whenever push notification is needed.
    • Using that device registration ID GCM server will deliver that message to respected mobile device.

 

Now a server is needed to host the push notification service. For this we have many platforms like Pushetta, Pushsafer, PushBullet etc. From all these services, Pushbullet is easy and free to use. Pushbullet has prebuilt libraries for python which makes it easy to use it with Raspberry pi.

 

Setup Pushbullet account

1. First create an account on Pushbullet website.

Setup Pushbullet account

 

2. Now, we need Access token key from the Account setting. So, click on Settings then click on Account and scroll down. You will find Create Access token button.  Click on it and a key will appear. Copy this key, it will be needed in the code.

Create Access token in PushBullet Account

 

To show the push notification, install the Pushbullet app in an android device. Now, sign in with the same account that you have used to get access tokens. Now push messages will start to appear on the given screen.

PushBullet App

 

Installing Pushbullet Libraries on Raspberry Pi

Run the following commands to install required library for using Pushbullet on Raspberry Pi.

sudo apt-get update
sudo apt-get upgrade
sudo pip install pushbullet.py

 

Now, we are ready to write the python code detect the motion and send push notification whenever someone enters in the room.

 

Components Required

  1. Raspberry Pi with Raspbian OS installed in it.
  2. PIR sensor
  3. Jumper Wires

 

Circuit Diagram

Here in this project, only the PIR sensor is connected with Raspberry Pi. Here the signal pin of PIR sensor is connected to the pin number 11 of Raspberry Pi as shown below.

Circuit Diagram for Interfacing PIR with Pi for Sending Push Notification

Circuit Hardware for Interfacing PIR with Pi for Sending Push Notification

 

Code and Explanation

Complete python code with a working video is given at the end of this project.

First import all the important libraries for using RPi gpios, pushbullet and time.

from pushbullet import Pushbullet
import RPi.GPIO as GPIO
from time import sleep

 

Now, set GPIO mode and pin number for attaching PIR sensor.

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.IN) 

 

Enter Access token that we have copied while creating account on Pushbullet and print all the devices that are using your account.

pb = Pushbullet("Your Access token")
print(pb.devices)

 

Now, in while loop check for any motion detection by checking the PIR output pin.

while True:
   i = GPIO.input(11)
   if i == 0:
       print "no motion"
       sleep(1)

 

If someone comes in the range of PIR sensor then value will be ‘1’. Now, enter the device name from Pushbullet app. Device name can also be found while running this script as shown the in below snapshot. Then enter the message that you want to send as a push notification.

elif i == 1:
      print "motion"
      dev = pb.get_device('Your device name')
      push = dev.push_note("Alert!!", "Someone is in your house")
      sleep(1)

 

Now finally copy the complete code given at the end of this project and save the file with .py extension. Then run this script using below command

python filename.py

 

Make sure you have connected PIR sensor correctly.

Testing PIR Sensor with Pi

 

Whenever someone comes near to the PIR sensor then Motion message will be displayed on the terminal and you will get a push notification as shown in below snapshots.

Sending Push Notifications from Raspberry Pi to Smart Phone

Code

from pushbullet import Pushbullet
import RPi.GPIO as GPIO
from time import sleep
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.IN) 
pb = Pushbullet("your access token")
print(pb.devices)

while True:
    i = GPIO.input(11)
    if i == 0:
        print "no motion"
        sleep(1)
    elif i == 1:
        print "motion"
        
        dev = pb.get_device('Oppo F1s')
        push = dev.push_note("Alert!!", "Someone is in your house")
        sleep(1)
 

Video

4 Comments

Nice! What a clear (and beautifully simple) way to get 'alerts' from a Pi to a phone.
I'd previously tried a couple of methods (PyQt, email) but in comparison they were complicated and/or required compromises to work.
Thank you!

Thanks, just what I was looking for. Super simple and very quick to get going. I used a battery powered motion sensitive light I got from the supermarket, opened it up an soldered a few wires to the board. Changed the code a bit so it didn't fire too many notifications. I've got it placed in front of the cat litter tray so I know how often and when she uses it.