目录

树莓派上传数据到onenet云平台

目录

树莓派上传数据到onenet云平台

背景:通过树莓派上传数据到onenet云平台

操作:看代码

# -*- coding:utf-8 -*-
# File: cputemp.py
#向平台已经创建的数据流发送数据点
import urllib2
import json
import time
import datetime

APIKEY = 'McYMwUV1DOmAC3Medopje1S0='  #改成你的APIKEY

def get_temp():
        # 打开文件 
        file = open("/sys/class/thermal/thermal_zone0/temp") 
        # 读取结果,并转换为浮点数 
        temp = float(file.read()) / 1000 
        # 关闭文件 
        file.close() 
        # 向控制台打印结果 
        print("CPU tempurature: %.3f" %temp )
        # 返回温度值
        return temp
        
        
def http_put():
    temperature = get_temp() #获取CPU温度并上传
    CurTime = datetime.datetime.now()
    # 这url只需把数字部分换成你在onenet中创建的设备号就行
    url='http://api.heclouds.com/devices/30948032/datapoints'

    values={'datastreams':[{"id":"danger_index","datapoints":[{"time":CurTime.isoformat(),"value":temperature}]}]}

    print ("Cur_time:%s" %CurTime.isoformat())
    print ("tempure:%.3f" %temperature)

    jdata = json.dumps(values)                  # 对数据进行JSON格式化编码
    #打印json内容
    print jdata
    request = urllib2.Request(url, jdata)
    request.add_header('api-key', APIKEY)
    request.get_method = lambda:'POST'          # 设置HTTP的访问方式
    request = urllib2.urlopen(request)
    return request.read()

while True:
        time.sleep(5)
        resp = http_put()
        print ("OneNET_result: %s" %resp)
        #time.sleep(5)

查看:设备id号和apikey

https://i-blog.csdnimg.cn/blog_migrate/42d4c31b2bb75b997bf550cc79e58170.png

根据第二个博客又进行了触发器设置,很简单

效果图:

https://i-blog.csdnimg.cn/blog_migrate/9f0a958f2f12a62f9fb54936e15f29d4.png

参考:

【1】

【2】