Raspberry - 监控CPU温度[使用yeelink作温度统计]

1. 获取CPU温度,将该温度上传至yeelink的python脚本(我的保存位置:~/program/python/upload_temperature_cron.py)

#!/usr/bin/python
#coding=utf-8

"""获取树莓派的温度,并上传到yeelink"""

import commands
import requests
import json
from time import strftime, sleep

headers = {"U-ApiKey": "XXX"}
DEVICE_ID = "XXX"
CPU_SENSOR_ID = "XXX"

def upload_temp(sensor_type, temp):
    if sensor_type == "cpu":
        sensor_id = CPU_SENSOR_ID
        print "CPU Temp:", temp
    else:
        sensor_id = GPU_SENSOR_ID
        print "GPU Temp:", temp
    url = r"http://api.yeelink.net/v1.0/device/%s/sensor/%s/datapoints" % (DEVICE_ID, sensor_id)
    #print url
    data = {"timestamp": strftime("%Y-%m-%dT%H:%M:%S"), "value": temp}
    #print data
    r = requests.post(url, data=json.dumps(data), headers=headers)

def get_cpu_temp():
    tempFile = open( "/sys/class/thermal/thermal_zone0/temp" )
    cpu_temp = tempFile.read()
    tempFile.close()
    return float(cpu_temp)/1000

upload_temp("cpu", get_cpu_temp())
(脚本基于: https://github.com/virusdefender/MyRasPi/blob/master/temperature.py 修改)

2. 获取 U-ApiKey & DEVICE_ID & CPU_SENSOR_ID。

获取方法:http://www.yeelink.net/ 注册完账号后进入用户中心,在账户->我的账户设置中找到API KEY。

在我的设备->增加新设备中增加一个设备,然后切换到管理设备,增加一个传感器。增加成功后,会看到一个类似这样的URL:http://api.yeelink.net/v1.0/device/12020/sensor/19995/datapoints,这个URL为API地址,该url中的12020为脚本中的DEVICE_ID,19995为CPU_SENSOR_ID。


3. 将该脚本加入crontab,每分钟运行一次。

crontab -e

添加 * * * * * /usr/bin/python ~/program/python/upload_temperature_cron.py >> ~/program/python/upload_temperature.output 到最后一行,保存退出

sudo /etc/init.d/cron restart


查看温度数据可以去yeelink用户中心,我的设备->管理设备中刚才添加传感器的地方查看。也可以直接访问:http://www.yeelink.net/devices/12020(将12020换成你的DEVICE_ID)。另,在~/program/python/upload_temperature.output 中会有所有温度数据的记录,也可直接从这里查看。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值