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 中会有所有温度数据的记录,也可直接从这里查看。