# 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 的Linux 操作系统已经可用
Raspberry Pi(树莓派)的安装、配置IP及软件源等入门
原文链接: Mike Haldas 翻译: 极客范 - 陈舸
译文链接: