python上传和查询中移Onenet云平台数据

import requests
import json
import time

class Device():
    def __init__(self,DEVICEID,APIKEY):
        self.DEVICEID = DEVICEID
        self.APIKEY = APIKEY
        self.url = 'http://api.heclouds.com/devices/%s/datapoints'%(self.DEVICEID)
        self.headers = { "api-key":self.APIKEY,"Connection":"close"}
    def upload_point(self,DataStreamName,VALUE):
        dict = {"datastreams":[{"id":"id","datapoints":[{"value":0}]}]}
        dict['datastreams'][0]['id'] = DataStreamName
        dict['datastreams'][0]['datapoints'][0]['value'] = VALUE
        if "succ" in requests.post(self.url,headers=self.headers,data = json.dumps(dict)).text:
            print("Value:",VALUE," has been uploaded to ",DataStreamName," at ",time.ctime())
    def get_point(self,DataStreamName):
        data = json.loads(requests.get(self.url,headers=self.headers,).text)
        for i in data['data']['datastreams']:
            if i["id"] == DataStreamName:
                return int(i['datapoints'][0]['value'])
        else:
            return "Not found DataStreamName - %s "%DataStreamName
if __name__ == "__main__":
    DeviceID = '295***31'                   #设备ID
    ApiKey = 'kNWaJ=bwGfV***XnkKapzCLsrNE=' #APIKey管理中的默认APIKEY
    DataStreamName = 'temperature'          #数据流名称,没有则新建数据流
    device = Device(DeviceID,ApiKey)
    device.upload_point(DataStreamName,66)  #向数据流中添加新数据
    print(device.get_point(DataStreamName)) #查询数据流中的最新数据

 

你可能感兴趣的:(python,树莓派,Micropython)