读取 raspberrypi 的cpu和gpu温度
#!/usr/bin/env python # -*- coding: utf-8 -*- import requests import json import time import commands def main(): # fileRecord = open("result.txt", "w") # fileRecord.write("connect to yeelink\n"); # fileRecord.close() while True: # 打开文件 apiheaders = {'U-ApiKey': 'a96bbccdd8f5e6e24fd3b2358d6cbc45', 'content-type': 'application/json'} gpu = commands.getoutput( '/opt/vc/bin/vcgencmd measure_temp' ).replace( 'temp=', '' ).replace( '\'C', '' ) gpu = float(gpu) #print('gpu value:%.2f' % gpu) # GPU设备URI apiurl_gpu = 'http://api.yeelink.net/v1.0/device/13926/sensor/23125/datapoints' #YEELINK 用户密码, 指定上传编码为JSON格式i #apiheaders = {'U-ApiKey': 'a96bbccdd8f5e6e24fd3b2358d6cbc45', 'content-type': 'application/json'} payload_gpu = {'value': gpu} r = requests.post(apiurl_gpu, headers=apiheaders, data=json.dumps(payload_gpu)) file = open("/sys/class/thermal/thermal_zone0/temp") # 读取结果,并转换为浮点数 cpu = float(file.read()) / 1000 # 关闭文件 file.close() #print('cpu value:%.2f' % cpu) # CPU设备URI apiurl_cpu = 'http://api.yeelink.net/v1.0/device/13926/sensor/23121/datapoints' #YEELINK 用户密码, 指定上传编码为JSON格式 #apiheaders = {'U-ApiKey': 'a96bbccdd8f5e6e24fd3b2358d6cbc45', 'content-type': 'application/json'} # 字典类型数据,在post过程中被json.dumps转换为JSON格式字符串 {"value": 48.123} payload_cpu = {'value': cpu} #发送请求 r = requests.post(apiurl_cpu, headers=apiheaders, data=json.dumps(payload_cpu)) # 向控制台打印结果 # fileRecord = open("result.txt", "a") # strTime = time.strftime('%Y-%m-%d:%H-%M-%S',time.localtime(time.time())) #fileRecord.writelines(strTime + "\n") #strTemp = "temp : %.1f" %temp + "\n" #fileRecord.writelines(strTemp) #fileRecord.writelines(str(r.status_code) + "\n") #fileRecord.close() time.sleep(1*60) if __name__ == '__main__': main()
源代码摘自:http://www.geekfan.net/6317/
laser杨万荣