树莓派上传数据到onenet云平台
生活随笔
收集整理的這篇文章主要介紹了
树莓派上传数据到onenet云平台
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
背景:通過樹莓派上傳數據到onenet云平臺
操作:看代碼
# -*- coding:utf-8 -*- # File: cputemp.py #向平臺已經創建的數據流發送數據點 import urllib2 import json import time import datetimeAPIKEY = 'McYMwUV1DOmAC3Medopje1S0=' #改成你的APIKEYdef get_temp():# 打開文件 file = open("/sys/class/thermal/thermal_zone0/temp") # 讀取結果,并轉換為浮點數 temp = float(file.read()) / 1000 # 關閉文件 file.close() # 向控制臺打印結果 print("CPU tempurature: %.3f" %temp )# 返回溫度值return tempdef 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 jdatarequest = 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
?
?
總結
以上是生活随笔為你收集整理的树莓派上传数据到onenet云平台的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: git push origin mast
- 下一篇: win10远程桌面连接ubuntu18.