#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define TEMP_PATH "/sys/class/thermal/thermal_zone0/temp"
#define MAX_SIZE 32
int main(void)
{
int fd;
double temp = 0;
char buf[MAX_SIZE];
// 打开/sys/class/thermal/thermal_zone0/temp
fd = open(TEMP_PATH, O_RDONLY);
if (fd < 0) {
fprintf(stderr, "failed to open thermal_zone0/temp\n");
return -1;
}
// 读取内容
if (read(fd, buf, MAX_SIZE) < 0) {
fprintf(stderr, "failed to read temp\n");
return -1;
}
// 转换为浮点数打印
temp = atoi(buf) / 1000.0;
printf("temp: %.2f\n", temp);
// 关闭文件
close(fd);
}!#/usr/bin/python
# -*- coding: utf-8 -*-
# 打开文件
file = open("/sys/class/thermal/thermal_zone0/temp")
# 读取结果,并转换为浮点数
temp = float(file.read()) / 1000
# 关闭文件
file.close()
# 向控制台打印
print "temp : %.1f" %temp
在yeelink-temp.py目录下输入以下指令执行脚本!#/usr/bin/python
# -*- coding: utf-8 -*-
import requests
import json
# 打开文件
file = open("/sys/class/thermal/thermal_zone0/temp")
# 读取结果,并转换为浮点数
temp = float(file.read()) / 1000
# 关闭文件
file.close()
# 向控制台打印结果
print "temp : %.1f" %temp
# 设备URI
apiurl = 'http://api.yeelink.net/v1.1/device/1949/sensor/2510/datapoints'
# 用户密码, 指定上传编码为JSON格式
apiheaders = {'U-ApiKey': 'ffa3826972d6cc7ba5b17e104ec5xxxx', 'content-type': 'application/json'}
# 字典类型数据,在post过程中被json.dumps转换为JSON格式字符串 {"value": 48.123}
payload = {'value': temp}
#发送请求
r = requests.post(apiurl, headers=apiheaders, data=json.dumps(payload))
# 打印返回码
print "response status: %d" %r.status_code APIKEY="U-ApiKey:ffa3826972d6cc7ba5b17e104ec5xxxx"
APIURL="http://api.yeelink.net/v1.0/device/1949/sensor/2510/datapoints"
curl --request GET --header $APIKEY $APIURL树莓派学习笔记——获取树莓派CPU温度,布布扣,bubuko.com
原文地址:http://blog.csdn.net/xukai871105/article/details/38349209