四种方法查看树莓派CPU温度
笔记在树莓派部署LAMP,在长时间运行后,树莓派24小时就宕机了,浏览器刚开始显示说网络不稳定,后来基本访问不了。最开始我还以为是LAMP安装有问题,到后来怀疑树莓派温度问题,随加了扇热小风扇,但笔记要确认一下是不是温度问题,现总结查看CPU温度的方法。
1、C语言读取文件内的数值
温度路径TEMP_PATH是 "/sys/class/thermal/thermal_zone0/temp" ,C语言读取内容 ,转换为浮点数打印温度值。具体代码如下:
#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 20
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: %.3f\n", temp);
// 关闭文件
close(fd);
}
gcc -o cpu_temp cpu_temp.c 编译C语言得到执行文件cpu_temp,./cpu_temp执行得到温度值。
2.vcgencmd查看硬件状态
命令行输入 vcgencmd measure_temp可以查看温度数值。
拓展一下,用python包装,还有查看其它参数。
import os
# Return CPU temperature as a character string
def getCPUtemperature():
res = os.popen('vcgencmd measure_temp').readline()
return(res.replace("temp=","").replace("'C\n",""))
# Return RAM information (unit=kb) in a list
# Index 0: total RAM
# Index 1: used RAM
# Index 2: free RAM
def getRAMinfo():
p = os.popen('free')
i = 0
while 1:
i = i + 1
line = p.readline()
if i==2:
return(line.split()[1:4])
# Return % of CPU used by user as a character string
def getCPUuse():
return(str(os.popen("top -n1 | awk '/Cpu\(s\):/ {print $2}'").readline().strip()))
# Return information about disk space as a list (unit included)
# Index 0: total disk space
# Index 1: used disk space
# Index 2: remaining disk space
# Index 3: percentage of disk used
def getDiskSpace():
p = os.popen("df -h /")
i = 0
while 1:
i = i +1
line = p.readline()
if i==2:
return(line.split()[1:5])
# CPU informatiom
CPU_temp = getCPUtemperature()
CPU_usage = getCPUuse()
# RAM information
# Output is in kb, here I convert it in Mb for readability
RAM_stats = getRAMinfo()
RAM_total = round(int(RAM_stats[0]) / 1000,1)
RAM_used = round(int(RAM_stats[1]) / 1000,1)
RAM_free = round(int(RAM_stats[2]) / 1000,1)
# Disk information
DISK_stats = getDiskSpace()
DISK_total = DISK_stats[0]
DISK_used = DISK_stats[1]
DISK_perc = DISK_stats[3]
if __name__ == '__main__':
print('')
print('CPU Temperature = '+CPU_temp)
print('CPU Use = '+CPU_usage)
print('')
print('RAM Total = '+str(RAM_total)+' MB')
print('RAM Used = '+str(RAM_used)+' MB')
print('RAM Free = '+str(RAM_free)+' MB')
print('')
print('DISK Total Space = '+str(DISK_total)+'B')
print('DISK Used Space = '+str(DISK_used)+'B')
print('DISK Used Percentage = '+str(DISK_perc))
命令行输入python get.py 可查看各个参数。
3、php读取文件内的数值
此方法和C语言类似,只是语言不一样,但是既然有LAMP,那么crontab定时任务就可以写入数据库了。
<?php
$TEMP_PATH ="/sys/class/thermal/thermal_zone0/temp";
$handle=fopen($TEMP_PATH,'r');
$contents = fread($handle, filesize ($TEMP_PATH));
$cpu_temp=((float)$contents)/1000;
echo $cpu_temp;
echo "\n";
fclose($handle);
?>
执行后的结果如下。
4、shell脚本读取温度值
方法也是读取文件里面的温度值,但shell读到了,那用途就自便了。
#!/bin/bash
cat /sys/class/thermal/thermal_zone0/temp|while read line
do
echo `expr $line / 1000`
done