首先安装库文件
pip install requests
功能
发送数据名称为SENSOR_ID(这里用Temperature测试),数值为VALUE(这里用31做测试)的数据,发送到自己的OneNet对应设备
"""
-------------------------------------------------
File Name: Data To OneNet_Post
Description :
Author : 禾灮\HeGuang
date: 2018\08\17
-------------------------------------------------
"""
# -*- coding: utf-8 -*-
import sys
import requests
import json
defaultencoding = 'utf-8'
if sys.getdefaultencoding() != defaultencoding:
reload(sys)
sys.setdefaultencoding(defaultencoding)
#设备ID
DEVICE_ID = '39245655'
#APIKEY
APIKEY = 'UAXd=EZif9opr36q3E6L7Q1J2mk='
#数据流名称
SENSOR_ID = 'Temperature'
#数值
VALUE = 31
url = 'http://api.heclouds.com/devices/%s/datapoints'%(DEVICE_ID)
print(url)
dict = {"datastreams":[{"id":"TEMP","datapoints":[{"value":20}]}]}
dict['datastreams'][0]['id'] = SENSOR_ID
dict['datastreams'][0]['datapoints'][0]['value'] = VALUE
print(dict)
s = json.dumps(dict)
headers = {
"api-key":APIKEY,
"Connection":"close"
}
r = requests.post(url,headers = headers,data = s)
print(r.headers)
print(25 * '*')
print(r.text)
print(25 * '*')
功能
从OneNet平台对应设备获取相应数据
"""
-------------------------------------------------
File Name: Data From OneNet_Get
Description :
Author : 禾灮\HeGuang
date: 2018\08\17
-------------------------------------------------
"""
# -*- coding: utf-8 -*-
import sys
import requests
import json
defaultencoding = 'utf-8'
if sys.getdefaultencoding() != defaultencoding:
reload(sys)
sys.setdefaultencoding(defaultencoding)
#设备ID
DEVICE_ID = '39245655'
#APIKEY
APIKEY = 'UAXd=EZif9opr36q3E6L7Q1J2mk='
#数据流名称
SENSOR_ID = 'Temperature'
#数值
VALUE = 31
url = 'http://api.heclouds.com/devices/%s/datastreams/%s'%(DEVICE_ID,SENSOR_ID)
print(url)
headers = {
"api-key":APIKEY,
"Connection":"close"
}
R_GET = requests.get(url,headers = headers)
print(R_GET.headers)
print('1',20 * '*')
print(R_GET.content)
print('2',20 * '*')
print(R_GET.text)
print('3',20 * '*')
print ('访问禾灮工作室中移OneNet平台ID为%s的数据流,获得其最新数据为:%d'
% (json.loads(R_GET.content)['data']['id'],
json.loads(R_GET.content)['data']['current_value']))
print(25 * '*')
感谢一直关注着禾灮成长进步的朋友们。你们的信任、支持和鼓励,鞭策着我们一路走到了今天。
感谢所有的合作伙伴,我们相互促进,共同见证了彼此的成长。
感谢所有曾经在禾灮彼此倚靠、相互鼓励、携手同心、砥砺同行的兄弟姐妹。这里承载了我们的青春与热血。
禾灮,感谢有你。
未来,我们将一如既往,砥砺前行。
禾灮·小楊
2018.08.17