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

來源:互聯網
上載者:User

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

本項目說明了如何從樹莓派發送推播通知給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”) 

Raspberry Pi 樹莓派上安裝Weston

用於Raspberry Pi 的Linux 作業系統已經可用

Raspberry Pi(樹莓派)試用小記

Raspberry Pi(樹莓派)的安裝、配置IP及軟體源等入門

 

原文連結: Mike Haldas 翻譯: 極客範 - 陳舸

譯文連結: http://www.geekfan.net/13505/

本文永久更新連結地址:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.