用树莓派给智能手机发送推送通知

您所在的位置:网站首页 树莓派收短信 用树莓派给智能手机发送推送通知

用树莓派给智能手机发送推送通知

2024-07-12 08:34:42| 来源: 网络整理| 查看: 265

用树莓派给智能手机发送推送通知

2014年12月14日 树莓派实验室 未分类 9

20141214221731256-0

本项目说明了如何从树莓派发送推送通知给iOS和Android设备,只需要用到一个免费的推送app即可。这里的主要思想就是利用一个电磁感应门来触发推送信息的事件。当电磁门打开时,树莓派就发送消息。在这个项目中,电磁感应门可以很容易替换成其他类型的告警设备,比如PIR运动传感器,红外引信等。

作者声明:我不是个Python专家,也不是树莓派的专家。虽然我有过很多软件开发的经验,而且也曾是个全职的开发者,但这是我的第一个树莓派项目和Python应用。因此,我写的Python代码很可能不是最简洁的,而且也可能会有其他更好的方式来配置树莓派。我个人很乐意接受建设性的批评和建议。如果有任何改进的建议,请在评论栏中告诉我。

配置树莓派发送推送消息

下面各项就是我们需要完成的:

在Instapush上建立推送服务,并安装移动app 将电磁感应门连接到树莓派上 安装pycurl库 加载python代码 运行python应用 测试,获取推送通知 在Instapush上建立推送服务,并安装移动app

要处理推送通知,我使用了一个名为Instapush的免费推送服务。Instapush在iOS和Android上有免费的app,而且这个平台上也有一个易于使用的REST API供软件开发者使用。

首先,在https://instapush.im/注册并登陆。 下载移动app(iOS版,Android版) 登陆到app上,使用你在网站上注册的账户即可 在app上登陆后,你会发现控制面板中已经显示你的设备已连接到Instapush的账户上了。去这里查看https://instapush.im/dashboard. 然后点击设备标签。我有两台设备都连接到了Instapush的账户上,见下图。 20141214221731930 接下来,点击app标签。然后选择添加应用。 为你的应用选择一个名称,然后点击Add。我把应用命名为“Door Push” 添加了你的应用之后,你会进入事件界面。点击添加事件 为你的时间选择一个标题。我建议在事件名中不要加入任何空格。我用的是“DoorAlert” 你需要添加至少一个tracker。这基本上就是一个用在推送通知中的变量。我给它命名为“message” 最后,输入你想要推送的消息内容。我的Python代码将变量{message}传给Instapush服务,因此我建议你只把{message}添加到Message字段即可。 20141214221731728 点击添加事件 点击Basic Info标签,记下Application ID和Application Secret fields这两个字段的内容。在编写Python代码时需要用到这些。可以参考下图中的示例。当然,我把我的ID做了些处理。20141214221731561 将电磁感应门连接到树莓派上

我使用了一个面包板套件来让这个过程变得简单些。我使用GPIO的第23号管脚以及接地管脚来连接电磁感应门。哪条线接GPIO,哪条线接地无关紧要。下面是示意图:

20141214221731860-0

安装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上也工作的一样好。

20141214221731133

这是一篇发布于 10年 前的文章,其中的信息可能已经有所发展或是发生改变,请了解。 6,172 文章标题:用树莓派给智能手机发送推送通知 - 树莓派实验室 固定链接:https://shumeipai.nxez.com/2014/12/14/send-push-notifications-from-raspberry-pi.html androidgpiohardwarelevel4linuxnetworkpythonraspberrypi树莓派


【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭