歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> 用樹莓派給智能手機發送推送通知

用樹莓派給智能手機發送推送通知

日期:2017/2/28 14:25:08   编辑:Linux教程

本項目說明了如何從樹莓派發送推送通知給iOS和Android設備,只需要用到一個免費的推送app即可。這裡的主要思想就是利用一個電磁感應門來觸發推送信息的事件。當電磁門打開時,樹莓派就發送消息。在這個項目中,電磁感應門可以很容易替換成其他類型的告警設備,比如PIR運動傳感器,紅外引信等。

作者聲明:我不是個Python專家,也不是樹莓派的專家。雖然我有過很多軟件開發的經驗,而且也曾是個全職的開發者,但這是我的第一個樹莓派項目和Python應用。因此,我寫的Python代碼很可能不是最簡潔的,而且也可能會有其他更好的方式來配置樹莓派。我個人很樂意接受建設性的批評和建議。如果有任何改進的建議,請在評論欄中告訴我。

配置樹莓派發送推送消息

下面各項就是我們需要完成的:

  1. 在Instapush上建立推送服務,並安裝移動app
  2. 將電磁感應門連接到樹莓派上
  3. 安裝pycurl庫
  4. 加載python代碼
  5. 運行python應用
  6. 測試,獲取推送通知

在Instapush上建立推送服務,並安裝移動app

要處理推送通知,我使用了一個名為Instapush的免費推送服務。Instapush在iOS和Android上有免費的app,而且這個平台上也有一個易於使用的REST API供軟件開發者使用。

  1. 首先,在https://instapush.im/注冊並登陸。
  2. 下載移動app(iOS版,Android版)
  3. 登陸到app上,使用你在網站上注冊的賬戶即可
  4. 在app上登陸後,你會發現控制面板中已經顯示你的設備已連接到Instapush的賬戶上了。去這裡查看https://instapush.im/dashboard.
  5. 然後點擊設備標簽。我有兩台設備都連接到了Instapush的賬戶上,見下圖。
  6. 接下來,點擊app標簽。然後選擇添加應用。
  7. 為你的應用選擇一個名稱,然後點擊Add。我把應用命名為“Door Push”
  8. 添加了你的應用之後,你會進入事件界面。點擊添加事件
  9. 為你的時間選擇一個標題。我建議在事件名中不要加入任何空格。我用的是“DoorAlert”
  10. 你需要添加至少一個tracker。這基本上就是一個用在推送通知中的變量。我給它命名為“message”
  11. 最後,輸入你想要推送的消息內容。我的Python代碼將變量{message}傳給Instapush服務,因此我建議你只把{message}添加到Message字段即可。 點擊添加事件
  12. 點擊Basic Info標簽,記下Application ID和Application Secret fields這兩個字段的內容。在編寫Python代碼時需要用到這些。可以參考下圖中的示例。當然,我把我的ID做了些處理。

將電磁感應門連接到樹莓派上

我使用了一個面包板套件來讓這個過程變得簡單些。我使用GPIO的第23號管腳以及接地管腳來連接電磁感應門。哪條線接GPIO,哪條線接地無關緊要。下面是示意圖:

安裝pycurl庫

我們的Python程序需要使用一個稱為pycurl的庫來發送API請求給InstaPush服務。在樹莓派上運行下面的命令來安裝這個Python庫。

sudo apt-get install python-pycurl

Python代碼

下面就是我編寫的Python代碼了。代碼中的注釋應該能很好的解釋我在做什麼。將程序命名為doorSensor.py。你可以在這裡下載源代碼。

# ------------- 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.

將Python腳本保存到你的樹莓派上。

運行Python應用

要測試是否能從樹莓派上發送推送通知,先運行doorSensor.py應用。程序跑起來之後,將電磁感應門的傳感器分開。你會看到樹莓派的屏幕上會打印出一些內容。第一行就是運行程序的命令,而第二行就是當我們打開門的時候所打印的。緊跟著會打印出從InstaPush的API服務接收到的響應。

pi@raspberrypi ~ $ sudo python doorSensor.py

Door Opened!

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

Door Closed!

獲取推送通知

在你打開電磁門的1到2秒後,你應該在iOS或者Android設備上接收到推送通知。下圖就是在我的三星Galaxy上所接收到的推送消息。iPhone上也工作的一樣好。

怎樣從Ubuntu安裝樹莓派系統( Raspbian “wheezy”) http://www.linuxidc.com/Linux/2014-12/110174.htm

Raspberry Pi 樹莓派上安裝Weston http://www.linuxidc.com/Linux/2013-06/86685.htm

用於Raspberry Pi 的Linux 操作系統已經可用 http://www.linuxidc.com/Linux/2012-03/56058.htm

Raspberry Pi(樹莓派)試用小記 http://www.linuxidc.com/Linux/2013-10/91008.htm

Raspberry Pi(樹莓派)的安裝、配置IP及軟件源等入門 http://www.linuxidc.com/Linux/2013-10/91009.htm

原文鏈接: Mike Haldas 翻譯: 極客范 - 陳舸

譯文鏈接: http://www.geekfan.net/13505/

Copyright © Linux教程網 All Rights Reserved