DIY个人智能家庭网关——python篇之推送门磁报警信息到手机
2017-02-26 23:54
405 查看
见《通过openwrt推送门磁报警信息到android手机上》
python代码如下
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import serial
from time import sleep
import json
import subprocess
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=0.5)
print ser.port
print ser.baudrate
my_addr = "0x5555"
def push_msg(msg):
content = {"platform":"all","audience":"all", "notification":{"alert":msg}}
print content
json_str = json.dumps(content)
print json_str
cmd = "curl -X POST --cacert /etc/ssl/certs/ca-certificates.crt -v https://api.jpush.cn/v3/push/ -H \"Content-Type: application/json\" -u \"xxx:xxx\""
curl_cmdline = '%s -d \'%s\''%(cmd,json_str)
print curl_cmdline
rc = subprocess.call(curl_cmdline, shell=True);
def recv(serial):
while True:
data =serial.read(64)
if data == '':
continue
else:
break
sleep(0.02)
return data
while True:
data =recv(ser)
if data != '':
print data
s = json.loads(data)
print s["addr"]
if s["addr"] == my_addr:
push_msg("Alarm!!!")
门磁触发后输出结果
root@OpenWrt:/tmp# ./pushalarm.py &
root@OpenWrt:/tmp# /dev/ttyUSB0
9600
{"type":"trigger", "addr":"0x5555","data":"0xc0"}
0x5555
{'platform': 'all', 'audience': 'all', 'notification': {'alert': 'Alarm!!!'}}
{"platform": "all", "audience": "all", "notification": {"alert": "Alarm!!!"}}
curl -X POST --cacert /etc/ssl/certs/ca-certificates.crt -v https://api.jpush.cn/v3/push/ -H "Content-Type: application/json" -u "xxx:xxx" -d '{"platform": "all", "audience": "all", "notification": {"alert": "Alarm!!!"}}'
> POST /v3/push/ HTTP/1.1
> Authorization: Basic ZTUwNjhmNTE4Y2NiMDRjYzkyYWM2MDFmOjlkZDdhOGQzN2JiZmEyODE0NjQ4YTZjNQ==
> User-Agent: curl/7.40.0
> Host: api.jpush.cn
> Accept: */*
> Content-Type: application/json
> Content-Length: 77
>
< HTTP/1.1 200 OK
< Server: nginx
< Date: Sun, 26 Feb 2017 15:49:53 GMT
< Content-Type: application/json
< Transfer-Encoding: chunked
< Connection: keep-alive
< X-Rate-Limit-Limit: 600
< X-Rate-Limit-Remaining: 599
< X-Rate-Limit-Reset: 60
< X-JPush-MsgId: 1562770329
<
{"sendno":"0","msg_id":"1562770329"}
python代码如下
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import serial
from time import sleep
import json
import subprocess
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=0.5)
print ser.port
print ser.baudrate
my_addr = "0x5555"
def push_msg(msg):
content = {"platform":"all","audience":"all", "notification":{"alert":msg}}
print content
json_str = json.dumps(content)
print json_str
cmd = "curl -X POST --cacert /etc/ssl/certs/ca-certificates.crt -v https://api.jpush.cn/v3/push/ -H \"Content-Type: application/json\" -u \"xxx:xxx\""
curl_cmdline = '%s -d \'%s\''%(cmd,json_str)
print curl_cmdline
rc = subprocess.call(curl_cmdline, shell=True);
def recv(serial):
while True:
data =serial.read(64)
if data == '':
continue
else:
break
sleep(0.02)
return data
while True:
data =recv(ser)
if data != '':
print data
s = json.loads(data)
print s["addr"]
if s["addr"] == my_addr:
push_msg("Alarm!!!")
门磁触发后输出结果
root@OpenWrt:/tmp# ./pushalarm.py &
root@OpenWrt:/tmp# /dev/ttyUSB0
9600
{"type":"trigger", "addr":"0x5555","data":"0xc0"}
0x5555
{'platform': 'all', 'audience': 'all', 'notification': {'alert': 'Alarm!!!'}}
{"platform": "all", "audience": "all", "notification": {"alert": "Alarm!!!"}}
curl -X POST --cacert /etc/ssl/certs/ca-certificates.crt -v https://api.jpush.cn/v3/push/ -H "Content-Type: application/json" -u "xxx:xxx" -d '{"platform": "all", "audience": "all", "notification": {"alert": "Alarm!!!"}}'
> POST /v3/push/ HTTP/1.1
> Authorization: Basic ZTUwNjhmNTE4Y2NiMDRjYzkyYWM2MDFmOjlkZDdhOGQzN2JiZmEyODE0NjQ4YTZjNQ==
> User-Agent: curl/7.40.0
> Host: api.jpush.cn
> Accept: */*
> Content-Type: application/json
> Content-Length: 77
>
< HTTP/1.1 200 OK
< Server: nginx
< Date: Sun, 26 Feb 2017 15:49:53 GMT
< Content-Type: application/json
< Transfer-Encoding: chunked
< Connection: keep-alive
< X-Rate-Limit-Limit: 600
< X-Rate-Limit-Remaining: 599
< X-Rate-Limit-Reset: 60
< X-JPush-MsgId: 1562770329
<
{"sendno":"0","msg_id":"1562770329"}
相关文章推荐
- DIY个人智能家庭网关——python篇之读取USB串口,获取门磁报警信息
- DIY个人智能家庭网关——python篇之推送消息到手机
- DIY个人智能家庭网关——python篇之推送手机上下线事件
- DIY个人智能家庭网关—— python篇之获取手机WIFI连接(上线/下线)状态
- DIY个人智能家庭网关——python篇之门磁触发后自动点亮WIFI灯带
- DIY个人智能家庭网关—— 手机篇之Jpush推送平台
- DIY个人智能家庭网关—— python篇之获取路由器外网地址
- DIY个人智能家庭网关——手机篇之客户端设计
- DIY个人智能家庭网关—— 路由器篇之安装python
- DIY个人智能家庭网关—— 前言
- DIY个人智能家庭网关—— 路由器篇之刷机
- DIY个人智能家庭网关——硬件篇之WIFI灯带DIY
- DIY个人智能家庭网关—— 路由器篇之申请公网IP
- DIY个人智能家庭网关——硬件篇之433M无线接收解码器(二)
- 灯光控制及家庭智能信息终端方案(温湿度測量、烟雾报警、门磁控制、窗帘控制等)
- DIY个人智能家庭网关—— 路由器篇之配置DDNS
- DIY个人智能家庭网关——硬件篇之433M无线接收解码器(一)
- 通过openwrt推送门磁报警信息到android手机上
- 灯光控制及家庭智能信息终端方案(温湿度测量、烟雾报警、门磁控制、窗帘控制等)
- 重庆微企获5000万元风投-平板电脑和手机菜单系统-家庭智能信息终端