Use Raspberry Pi to send push notifications to smartphones

Source: Internet
Author: User

Use Raspberry Pi to send push notifications to smartphones

This project explains how to send push notifications from Raspberry Pi to iOS and Android devices. You only need to use a free push app. The main idea here is to use an electromagnetic sensor door to trigger information push events. When the electromagnetic door opens, Raspberry Pi sends a message. In this project, electromagnetic induction doors can be easily replaced with other types of alarm devices, such as PIR motion sensors and infrared fuses.

I am not a Python expert or Raspberry Pi expert. Although I have a lot of software development experience and I was a full-time developer, this is my first Raspberry Pi project and Python application. Therefore, the Python code I wrote may not be the most concise, and there may be other better ways to configure Raspberry Pi. I personally would like to accept constructive criticism and suggestions. If you have any suggestions for improvement, please let me know in the comment bar.

Configure Raspberry Pi to send push messages

The following items need to be completed:

  1. Create a push service on Instapush and install the mobile app
  2. Connect the electromagnetic sensor door to Raspberry Pi
  3. Install the pycurl Library
  4. Load python code
  5. Run a python application
  6. Test to get push notifications
Create a push service on Instapush and install the mobile app

To process push notifications, I use a free push service named Instapush. Instapush has free apps on iOS and Android, and this platform also has an easy-to-use rest api for software developers.

  1. First, go to https://instapush.im/registration.
  2. Download mobile app (iOS and Android)
  3. Log on to the app and use the account you registered on the website.
  4. After logging on to the app, you will find that your device has been connected to the Instapush account in the control panel. Go here to view the https://instapush.im/dashboard.
  5. Click the device tag. I have two devices connected to the Instapush account. See.
  6. Next, click the app tag. Then select Add application.
  7. Select a name for your application and click Add. I named the application "Door Push"
  8. After adding your application, you will enter the event interface. Click Add event
  9. Select a title for your time. We recommend that you do not add any spaces to the event name. I am using "DoorAlert"
  10. You need to add at least one tracker. This is basically a variable used in push notifications. I name it "message"
  11. Finally, enter the message content you want to push. My Python code sends the variable {message} to the Instapush service. Therefore, I suggest you only add {message} to the Message field. Click Add event
  12. Click the Basic Info tab and write down the content of the Application ID and Application Secret fields. This is required when writing Python code. See the example in. Of course, I have processed my ID.
Connect the electromagnetic sensor door to Raspberry Pi

I used a bread kit to make the process easier. I use the GPIO 23rd pin and the grounding pin to connect the electromagnetic induction door. Which line is connected to GPIO and which line is grounded is irrelevant. Below is:

Install the pycurl Library

Our Python program uses a library called pycurl to send API requests to the InstaPush service. Run the following command on Raspberry Pi to install the Python library.

Sudo apt-get install python-pycurl

Python code

The following is the Python code I have compiled. The comments in the code should be able to explain well what I am doing. Name the program doorSensor. py. You can download the source code here.

# ------------- Begin doorSensor. py ------------------#

Import pycurl, json
From StringIO import StringIO
Import RPi. GPIO as GPIO

# Setup GPIO using Broadcom SOC channel numbering
GPIO. setmode (GPIO. BCM)

# 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, 'https: // api. instapush. im/v1/Post ')

# 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 ['event'] = pushEvent
Json_fields ['trackers'] = {}
Json_fields ['trackers'] ['message'] = pushMessage

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
C. perform ()

# Capture the response from the server
Body = buffer. getvalue ()

# Print the response
Print (body)

# Reset the buffer
Buffer. truncate (0)
Buffer. seek (0)

# Door closed detected
GPIO. wait_for_edge (23, GPIO. FALLING)
Print ("Door Closed! \ N ")

# Cleanup
C. close ()
GPIO. cleanup ()

# -------------------- End doorSensor. py --------------------#

Save the Python script on your Raspberry Pi.

Save the Python script to your Raspberry Pi.

Run a Python application

To test whether push notifications can be sent from Raspberry Pi, run the doorSensor. py application first. After the program runs, the electromagnetic sensor door sensors are separated. You will see some content printed on Raspberry Pi's screen. The first line is the command to run the program, and the second line is printed when we open the door. The response received from the InstaPush API service is printed immediately.

Pi @ raspberrypi ~ $ Sudo python doorSensor. py

Door Opened!

{"Msg": "Notification Sent Successfully", "error": false, "status": 200}

Door Closed!

Get push notification

After you open the electromagnetic door for 1 to 2 seconds, you should receive push notifications on iOS or Android devices. Is the PUSH message received on my Samsung Galaxy. IPhone also works as well.

 

How to install Raspberry Pi from Ubuntu (Raspbian "wheezy ")

Install Weston on Raspberry Pi

Linux OS for Raspberry Pi is available

Raspberry Pi (Raspberry Pi) trial note

Introduction to Raspberry Pi (Raspberry Pi) installation, IP configuration, and software source

 

Original article: Mike Haldas Translation: geek fan-chen yu

Http://www.geekfan.net/13505/.

This article permanently updates the link address:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.