樹梅派應用47:用樹莓派給智能手機發送推送通知

20141214221731256-0

本項目說明了如何從樹莓派發送推送通知給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的賬戶上,見下圖。
    20141214221731930
  6. 接下來,點擊app標籤。然後選擇添加應用。
  7. 爲你的應用選擇一個名稱,然後點擊Add。我把應用命名爲“Door Push”
  8. 添加了你的應用之後,你會進入事件界面。點擊添加事件
  9. 爲你的時間選擇一個標題。我建議在事件名中不要加入任何空格。我用的是“DoorAlert”
  10. 你需要添加至少一個tracker。這基本上就是一個用在推送通知中的變量。我給它命名爲“message”
  11. 最後,輸入你想要推送的消息內容。我的Python代碼將變量{message}傳給Instapush服務,因此我建議你只把{message}添加到Message字段即可。
    20141214221731728
    點擊添加事件
  12. 點擊Basic Info標籤,記下Application ID和Application Secret fields這兩個字段的內容。在編寫Python代碼時需要用到這些。可以參考下圖中的示例。當然,我把我的ID做了些處理。20141214221731561

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

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

20141214221731860-0

安裝pycurl庫

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

1
sudo apt-get install python-pycurl

 Python代碼

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# ------------- 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服務接收到的響應。

1
2
3
4
5
6
7
pi@raspberrypi ~ $ sudo python doorSensor.py
 
Door Opened!
 
{“msg”:”Notification Sent Successfully”,”error”:false,”status”:200}
 
Door Closed!

 獲取推送通知

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

20141214221731133

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章