How to Send Push Notifications from Raspberry Pi

This project demonstrates how to send push notifications from Raspberry Pi to iOS and Android devices using a free push app. The idea is to trigger the event of sending the push message with a magnetic door sensor. When the door opens, the Raspberry Pi sends the message. The magnetic door sensor can easily be replaced in this project with some other type of alarm device such as a PIR motion sensor, infrared trip wire, or other. You can also setup your RPi to handle multiple sensors and push messages.

Disclosure: I am not a Python coding expert or an expert on Raspberry Pi (yet). Although, I have a lot of software engineering experience and I was once a full time developer, this is my very first Raspberry Pi project and Python application. My main motivation for this project was designing a solution that could work with the CCTV and HD security camera systems that my company supplies. Unfortunately, most CCTV DVRs do not yet support push notifications. So, I thought that an alarm input push notification server based on Raspberry Pi would provide an inexpensive solution to work in conjunction with these systems.

It is likely that the Python code is not the cleanest and there may be better ways to setup the Raspberry Pi. I am certainly open to constructive criticism and suggestions. Please post any comments at the bottom of this blog post if you have any.

 Setup Raspberry Pi to Send Push Messages

Here are the things that need to be done.

  1. Setup Push Service at Instapush & Install Mobile App
  2. Wire Door Sensor to Raspberry Pi
  3. Install pycurl library
  4. Install Python Code
  5. Run Python App
  6. Test and Get the Push Notification

Setup Instapush Account & Install app

To handle the push notifications, I used a free push service called Instapush. Instapush has free apps for iOS and Android and the platform also has a simple to use REST API for software developers.

  1. Sign up for an account here: and login.
  2. Download the apps here:
    iOS App
    Android App
  3. Login to the app(s) using the same login that you created on the website.
  4. After you login to to the mobile app on one or more devices, you will see your device linked to your Instapush account in the control panel. Go here:
  5. Then click on the devices tab. I have two devices linked to me account. Here is what it looks like.
    mobile push devices
  6. Next, click on the apps tab, then Add Application.
  7. Choose a name for your application then click Add. I named my “Door Push”.
  8. After you add your application, you will brought to the events screen. Click Add Events.
  9. Choose a title for your event. I recommend not using any spaces in the event name. I used “DoorAlert”.
  10. You need to add at least one tracker. This is basically a variable that is used in the push notification. I used “message”.
  11. Last, type what you want your push message to say. My Python code passes the {message} variable to the Instapush service so I recommend that you just add {message} to the Message field with nothing else.
    push notification API
  12. Click Add Event.
  13. Click on the Basic Info tab and make note of the Application ID and Application Secret fields. You will need these in the python code. You can see what this looks like below. I blocked out my ID and key.
    push service key

Wire Door Sensor to Raspberry Pi

I am using a breadboard kit to make it easier to wire the door sensor to Pi. I use GPIO pin 23 and a ground pin to wire the door sensor. It does not matter which lead goes to the GPIO and which goes to the ground. Here is what the wiring looks like.

Raspberry Pi magnetic door sensor wiring


Install pycurl library

The python app will use a library called pycurl to send the API request to the Instapush server. Run the following command on your Raspberry Pi to install this Python library.

sudo apt-get install python-pycurl

Python Code

Here is the python application that I wrote. The comments in the code should explain pretty well what is going on. Name your program I am pretty sure that the formatting of the code will be messed up when I paste it into this blog post, so you can download the Python source here.

# ------------- Begin ------------------ #

import pycurl, json
from StringIO import StringIO
import RPi.GPIO as GPIO

#setup GPIO using Broadcom SOC channel numbering

# set to pull-up (normally closed position)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)

#setup InstaPush variables

# set this to Application ID from Instapush
appID = ""

# set this to the Application Secret from Instapush
appSecret = ""

# leave this set to DoorAlert unless you named your event something different in Instapush
pushEvent = "DoorAlert"

# set this to what you want the push message to say
pushMessage = "Door Opened!"

# use StringIO to capture the response from our push API call
buffer = StringIO()

# use Curl to post to the Instapush API
c = pycurl.Curl()

# set Instapush API URL
c.setopt(c.URL, '')

# setup custom headers for authentication variables and content type
c.setopt(c.HTTPHEADER, ['x-instapush-appid: ' + appID,
'x-instapush-appsecret: ' + appSecret,
'Content-Type: application/json'])

# create a dictionary structure for the JSON data to post to Instapush
json_fields = {}

# setup JSON values
json_fields['trackers'] = {}

postfields = json.dumps(json_fields)

# make sure to send the JSON with post
c.setopt(c.POSTFIELDS, postfields)

# set this so we can capture the resposne in our buffer
c.setopt(c.WRITEFUNCTION, buffer.write)

# uncomment to see the post that is sent
#c.setopt(c.VERBOSE, True)

# setup an indefinite loop that looks for the door to be opened / closed
while True:

# door open detected
GPIO.wait_for_edge(23, GPIO.RISING)
print("Door Opened!\n")

# in the door is opened, send the push request

# capture the response from the server
body= buffer.getvalue()

# print the response

# reset the buffer

# door closed detected
GPIO.wait_for_edge(23, GPIO.FALLING)
print("Door Closed!\n")

# cleanup

# -------------------- End -------------------- #

Save the Python script on your Raspberry Pi.

Start Python App

To test the push notification from Pi, run the application. While the app is running. separate the magnetic door sensors from each other. You will see the following printed on the screen of your Raspberry Pi. The first line is the command to start the program. The second line is printed when we open the door. Immediately following is a printout of the response received from the push API service. Last, we print when the door is closed.

pi@raspberrypi ~ $ sudo python

Door Opened!

{“msg”:”Notification Sent Successfully”,”error”:false,”status”:200}

Door Closed!

Get Push Notification

A second or two after you open the door sensor, you should receive the push notification on your iOS or Android app. Here is what the push message looks like when I received it on my Samsung Galaxy. It works just as awesome on my iPhone.

Raspberry Pi push message


Handling Multiple Alarm Sensors & Push Messages

I kept this project very simple by just integrating one alarm sensor with my Raspberry Pi. You may want to have multiple sensors in your project. Maybe you want to have more than one door sensors, PIR motion detectors, or virtually any other type of sensor that can be connected to RPi.

Obviously, you would want the push notification to tell you which sensor was triggered. This is very easy to handle.

By default, I have the pushMessage variable set using the below code.

pushMessage = “Door Opened!”

Simply add some logic to your code and modify the pushMessage variable based on which sensor triggered your alarm.

For example.

pushMessage = “Back Door Opened!”
pushMessage = “Motion detected at front door!”