#!/usr/bin/env python # -*- coding: utf-8 -*- import requests import json import time def main(): fileRecord = open("result.txt", "w") fileRecord.write("connect to yeelink\n"); fileRecord.close() while True: # 打开文件 file = open("/sys/class/thermal/thermal_zone0/temp") # 读取结果,并转换为浮点数 temp = float(file.read()) / 1000 # 关闭文件 file.close() # 设备URI apiurl = 'http://api.yeelink.net/v1.1/device/1949/sensor/2510/datapoints' # 用户密码, 指定上传编码为JSON格式 apiheaders = {'U-ApiKey': 'ffa3826972d6cc7ba5b17e104ec59fa3', 'content-type': 'application/json'} # 字典类型数据,在post过程中被json.dumps转换为JSON格式字符串 {"value": 48.123} payload = {'value': temp} #发送请求 r = requests.post(apiurl, headers=apiheaders, data=json.dumps(payload)) # 向控制台打印结果 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(5*60) if __name__ == '__main__': main()
#!/bin/sh -e # # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "exit 0" on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # By default this script does nothing. # Print the IP address _IP=$(hostname -I) || true if [ "$_IP" ]; then printf "My IP address is %s\n" "$_IP" fi # 向yeelink上传树莓派CPU温度 /home/pi/python-works/yeelink-temp/auto-start.sh start exit 0
树莓派学习笔记——定时向yeelink上传树莓派CPU温度,布布扣,bubuko.com
原文地址:http://blog.csdn.net/xukai871105/article/details/38349519