树莓派上传数据到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 

 

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

效果图:

参考:

【1】https://open.iot.10086.cn/bbs/forum.php?mod=viewthread&tid=35112&highlight=%E6%A0%91%E8%8E%93%E6%B4%BE

【2】https://www.jianshu.com/p/5de7e3fd2fef

 

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值