这篇文章将为大家详细讲解有关如何读取树莓派4B处理器(CPU)的实时温度,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
树莓派发布4B后,性能提升了不少,但是温度也是高的不行,所以最好配置一个小风扇和散热片还是比较好的,效果明显。
cd /sys/class/thermal/thermal_zone0
cat temp
35050
返回值除以1000为当前CPU温度值。即当前温度为53摄氏度。如下图所示
温度是在 /sys/class/thermal/thermal_zone0/temp 文件下看的
创建程序文件 read_cpu_temp.c 并打开编写代码
//读取树莓派4B处理器(CPU)的实时温度
// 编译
// gcc -o read_cpu_temp read_cpu_temp.c
// 运行
// ./read_cpu_temp
#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 buffer[MAX_SIZE];
int i;
while(i < 100)
{
i+=1;
//延时1s
sleep(1);
//打开文件
fd = open(TEMP_PATH,O_RDONLY);
if(fd < 0)
{
fprintf(stderr,"Failed to open thermal_zone0/temp\n");
return - 1;
}
//读取文件
if(read(fd,buffer,MAX_SIZE) < 0)
{
fprintf(stderr,"Failed to read temp\n");
return -1;
}
//计算温度值
temp = atoi(buffer) / 1000.0;
//打印输出温度
printf("Temp:%.4f\n",temp);
//关闭文件
close(fd);
}
}
gcc -o read_cpu_temp read_cpu_temp.c
编译程序出现三个警告,可以不用管它,生成可以执行文件read_cpu_temp
输入./read_cpu_temp运行程序
./read_cpu_temp
(我安装了风扇和散热片以及外壳,大概平均在36摄氏度左右)
创建程序文件 read_cpu_temp.erl 并打开编写代码
%%%-------------------------------------------------------------------
%%% @author SummerGao
%%% @copyright (C) 2020, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 15. 7月 2020 13:59
%%%-------------------------------------------------------------------
-module(read_cpu_temp).
-author("SummerGao").
%% API
-export([loop/0]).
loop() ->
{ok, Original} = file:read_file("/sys/class/thermal/thermal_zone0/temp"),
[A, _] = binary:split(Original, [<<"\n">>]),
T = binary_to_integer(A) / 1000,
io:format("Temp ℃ : ~p~n", [T]),
timer:sleep(1000),
loop()
.
Erlang/OTP 22 [erts-10.4] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1]
Eshell V10.4 (abort with ^G)
1> c(read_cpu_temp).
{ok,read_cpu_temp}
2> read_cpu_temp:loop().
lscpu
free -h
关于如何读取树莓派4B处理器(CPU)的实时温度就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/zhenggao/blog/4336898