树莓派上传数据到onenet云平台
生活随笔
收集整理的這篇文章主要介紹了
树莓派上传数据到onenet云平台
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
背景:通過樹莓派上傳數(shù)據(jù)到onenet云平臺
操作:看代碼
# -*- coding:utf-8 -*- # File: cputemp.py #向平臺已經(jīng)創(chuàng)建的數(shù)據(jù)流發(fā)送數(shù)據(jù)點 import urllib2 import json import time import datetimeAPIKEY = 'McYMwUV1DOmAC3Medopje1S0=' #改成你的APIKEYdef get_temp():# 打開文件 file = open("/sys/class/thermal/thermal_zone0/temp") # 讀取結(jié)果,并轉(zhuǎn)換為浮點數(shù) temp = float(file.read()) / 1000 # 關(guān)閉文件 file.close() # 向控制臺打印結(jié)果 print("CPU tempurature: %.3f" %temp )# 返回溫度值return tempdef http_put():temperature = get_temp() #獲取CPU溫度并上傳CurTime = datetime.datetime.now()# 這url只需把數(shù)字部分換成你在onenet中創(chuàng)建的設(shè)備號就行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) # 對數(shù)據(jù)進(jìn)行JSON格式化編碼#打印json內(nèi)容print jdatarequest = urllib2.Request(url, jdata)request.add_header('api-key', APIKEY)request.get_method = lambda:'POST' # 設(shè)置HTTP的訪問方式request = urllib2.urlopen(request)return request.read()while True:time.sleep(5)resp = http_put()print ("OneNET_result: %s" %resp)#time.sleep(5)查看:設(shè)備id號和apikey?
?
?根據(jù)第二個博客又進(jìn)行了觸發(fā)器設(shè)置,很簡單
效果圖:
參考:
【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
?
?
總結(jié)
以上是生活随笔為你收集整理的树莓派上传数据到onenet云平台的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: git push origin mast
- 下一篇: win10远程桌面连接ubuntu18.