您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)如何讀取樹莓派4B處理器(CPU)的實(shí)時溫度,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
樹莓派發(fā)布4B后,性能提升了不少,但是溫度也是高的不行,所以最好配置一個小風(fēng)扇和散熱片還是比較好的,效果明顯。
cd /sys/class/thermal/thermal_zone0
cat temp
35050
返回值除以1000為當(dāng)前CPU溫度值。即當(dāng)前溫度為53攝氏度。如下圖所示
溫度是在 /sys/class/thermal/thermal_zone0/temp 文件下看的
創(chuàng)建程序文件 read_cpu_temp.c 并打開編寫代碼
//讀取樹莓派4B處理器(CPU)的實(shí)時溫度 // 編譯 // gcc -o read_cpu_temp read_cpu_temp.c // 運(yùn)行 // ./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; } //計(jì)算溫度值 temp = atoi(buffer) / 1000.0; //打印輸出溫度 printf("Temp:%.4f\n",temp); //關(guān)閉文件 close(fd); } }
gcc -o read_cpu_temp read_cpu_temp.c
編譯程序出現(xiàn)三個警告,可以不用管它,生成可以執(zhí)行文件read_cpu_temp
輸入./read_cpu_temp運(yùn)行程序
./read_cpu_temp
(我安裝了風(fēng)扇和散熱片以及外殼,大概平均在36攝氏度左右)
創(chuàng)建程序文件 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
關(guān)于如何讀取樹莓派4B處理器(CPU)的實(shí)時溫度就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。