因为想着要见识下不同的云平台,所以这次选择了onenet。整个操作过程也不复杂,不需要用到什么额外的硬件,只需要一块树莓派即可。
首先,你需要在onenet平台注册,成为一名开发者。接着在平台上面创建产品,添加设备。这一系列操作,可参考官网文档。
创建产品:https://open.iot.10086.cn/doc/book/easy-manual/product&device/product-create.html
创建设备:https://open.iot.10086.cn/doc/book/easy-manual/product&device/device-create/single-device.html
做完以上操作之后,注意力就转到树莓派上。
整个代码是用python完成的。
# -*- coding:utf-8 -*-
# File: cputemp.py
#向平台已经创建的数据流发送数据点
import urllib2
import json
import time
import datetime
APIKEY = ' #改成你的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":"rayne","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)
"succ"即为上传成功
在数据流这里查看
就是这些啦 希望对大家有些帮助!