树莓派(Raspberry Pi 3)
来源:互联网 发布:湖南棋牌app源码 编辑:程序博客网 时间:2024/06/02 19:40
Instapush 是一个免费推送消息的app,树莓派只需安装相关的驱动即可发送通知给 IOS 或者 Android 设备。这里主要是利用开关控制 Led 的打开/关闭来触发推送信息的事件。当开关打开,Led 灯点亮时,树莓派就发送消息到移动设备。
配置Instapush服务
- 1、进入 Instapush 官网完成注册登录(https://instapush.im/)
- 2、手机上下载 Instapush,并登陆账号
- 3、在 app 登陆之后,你会发现控制面板中 DEVICES 已经显示你的设备已连接到 Instapush 的账户上了
- 4、点击控制面板中 APPS,选择添加应用,为应用选择一个名字后(这里用的LedPush),即可进入应用的事件界面,点击添加事件
- 5、在 Event Title 中选择一个标题(代码中需要用到事件标题);Trakers 至少添加一个tracker,这基本上就是一个用在推送通知中的变量,在 Trakers 输入后需要按 Tab 键才能生效(生效后会出现在Trakers后,这里用的 message);最后输入你想要推送的消息内容,Python 代码将变量 {message} 传给 Instapush 服务,因此我建议你只把 {message} 添加到 Message 字段即可
- 6、点击 Basic Info 标签,记下 Application ID 和 Application Secret fields 这两个字段的内容。在编写Python代码时需要用到这些。
建立Led与树莓派的基本电路
- 1、将LED灯插入到实验电路板,长脚(正极)在一行中而短脚(负极)在另一行;在LED灯短脚的同一行中插入电阻的一根脚,并将电阻的另一根脚(脚2)接到一个空行;
- 2、使用跳线连接树莓派GPIO端口的Pin11,使其和电阻(脚2)在同一行
- 3、使用跳线连接到开关的一脚,另一脚和电阻(脚2)在同一行,将开关的公共脚接到一个空行;用跳线连接到树莓派GPIO端口的Pin 6,使其和开关公共脚在同一行
- 4、将跳线连接树莓派的Pin 1脚,使其和LED长腿在同一行
树莓派安装 pycurl 库
- 1、Python程序需要使用一个称为 pycurl 的库来发送 API 请求给 InstaPush 服务。在树莓派上运行下面的命令来安装这个 Python 库:sudo apt-get install python-pycurl
- 2、编写Python代码
#!/usr/bin/env pythonimport pycurl, json, timeimport RPi.GPIO as GPIOfrom StringIO import StringIOGPIO.setmode(GPIO.BOARD)GPIO.setup(11,GPIO.IN)#填写Application ID 和 Application Secret fields 这两个字段的内容appID = "5965c41ca4c48a8"appSecret = "2603724c9706"#填写Event Title中的内容pushEvent = "Ledcontral"#向手机推送的消息内容pushMessage = "Led Opened"c = pycurl.Curl()c.setopt(c.URL, 'https://api.instapush.im/v1/post')c.setopt(c.HTTPHEADER, ['x-instapush-appid:' + appID, 'x-instapush-appsecret:' + appSecret, 'Content-Type: application/json'])json_fields = {}json_fields['event'] = pushEventjson_fields['trackers'] = {}json_fields['trackers']['message'] = pushMessagepostfields = json.dumps(json_fields)c.setopt(c.POSTFIELDS, postfields)ifbj = 0elbj = 0while True: input_value = GPIO.input(11) if input_value == False: time.sleep(0.5) ifbj += 1 if ifbj >= 4: c.perform() print("The button has been pressed.") ifbj = 0 else: time.sleep(0.5) elbj += 1 if elbj >= 4: print("The button has not been pressed.") elbj = 0
- 3、运行Python代码进行测试,打开开关,当界面打印“The button has been pressed.”时,树莓派会向手机推送消息,这时会在手机收到“Led Opened”消息
阅读全文
0 0
- 树莓派(Raspberry Pi 3)
- 树莓派(Raspberry Pi 3)
- 树莓派(Raspberry Pi 3)
- 树莓派(Raspberry Pi 3)
- Raspberry Pi | 树莓派 (RASPBERRY PI 3 MODEL B) 上手
- 【raspberry pi】树莓派3测评
- 树莓派3初始化安装(Raspberry Pi III)
- 安装树莓派 Raspberry PI
- Raspberry Pi 树莓派入门
- Raspberry Pi 树莓派入门
- 树莓派 Raspberry Pi
- Raspberry Pi 树莓派
- Raspberry Pi 树莓派
- Raspberry Pi 树莓派入门
- 【raspberry pi】树莓派小车
- 树莓派 GrovePi Raspberry Pi 3B 添加 Grove Pi 扩展
- Raspberry Pi 3
- Raspberry Pi 3
- css垂直居中
- ie9 ajax 跨域请求不了 提交为空 jquery-ajaxtransport-xdomainrequest
- VMware 安装ubuntu 编译android系统源码
- 初识 Servlet
- 如何使用异步
- 树莓派(Raspberry Pi 3)
- Android APP 启动优化(冷启动)
- [Leetcode] 287. Find the Duplicate Number 解题报告
- 指针变量p与“零值”进行比较的if 语句怎么写?
- Spring+JUnit4编写测试案例
- linux常用头文件详解
- 【OAuth2.0】认识和使用OAuth2.0附OAuth实例
- 数据库死锁如何解锁
- android studio logcat一闪而过