使用Python2.7 POST 数据到 onenet 平台

功能

发送数据名称为SENSORID(这里用TEST测试),数值为VALUE(这里用49值做测试)的数据,发送到自己的onenet对应设备

效果发送成功

代码

# -*- coding: utf-8 -*-
"""
-------------------------------------------------
   File Name:     onenetPostTest
   Description :
   Author :       神秘藏宝室
   date:          2018-02-02
-------------------------------------------------
   Change Activity:
                   2018-02-02:
-------------------------------------------------
"""
import requests
import json
import sys
defaultencoding = 'utf-8'
if sys.getdefaultencoding() != defaultencoding:
    reload(sys)
    sys.setdefaultencoding(defaultencoding)

#设备ID
DEVICEID = '3225187'
#数据流名称
SENSORID = 'TEST'
#数值
VALUE = 49
#APIKEY
APIKEY = 'R9xO5NZm6oVI4YBHvCPKEqtwYtMA'

url = 'http://api.heclouds.com/devices/%s/datapoints'%(DEVICEID)
print url
dict = {"datastreams":[{"id":"TEMP","datapoints":[{"value":50}]}]}
dict['datastreams'][0]['id'] = SENSORID
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 '1',20 * '*'
print r.text
print '2',20 * '*'

posted on 2018-02-02 22:10  神秘藏宝室  阅读(1657)  评论(0编辑  收藏  举报

 >>>转载请注明出处<<<