本項目說明了如何從樹莓派發送推送通知給iOS和Android設備,只需要用到一個免費的推送app即可。這裡的主要思想就是利用一個電磁感應門來觸發推送信息的事件。當電磁門打開時,樹莓派就發送消息。在這個項目中,電磁感應門可以很容易替換成其他類型的告警設備,比如PIR運動傳感器,紅外引信等。
作者聲明:我不是個Python專家,也不是樹莓派的專家。雖然我有過很多軟件開發的經驗,而且也曾是個全職的開發者,但這是我的第一個樹莓派項目和Python應用。因此,我寫的Python代碼很可能不是最簡潔的,而且也可能會有其他更好的方式來配置樹莓派。我個人很樂意接受建設性的批評和建議。如果有任何改進的建議,請在評論欄中告訴我。
下面各項就是我們需要完成的:
要處理推送通知,我使用了一個名為Instapush的免費推送服務。Instapush在iOS和Android上有免費的app,而且這個平台上也有一個易於使用的REST API供軟件開發者使用。
我使用了一個面包板套件來讓這個過程變得簡單些。我使用GPIO的第23號管腳以及接地管腳來連接電磁感應門。哪條線接GPIO,哪條線接地無關緊要。下面是示意圖:
我們的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腳本保存到你的樹莓派上。
要測試是否能從樹莓派上發送推送通知,先運行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/