溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何讀取樹莓派4B處理器(CPU)的實(shí)時溫度

發(fā)布時間:2021-11-19 18:31:14 來源:億速云 閱讀:600 作者:柒染 欄目:互聯(lián)網(wǎng)科技

這篇文章將為大家詳細(xì)講解有關(guān)如何讀取樹莓派4B處理器(CPU)的實(shí)時溫度,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

讀取樹莓派4B處理器(CPU)的實(shí)時溫度

樹莓派發(fā)布4B后,性能提升了不少,但是溫度也是高的不行,所以最好配置一個小風(fēng)扇和散熱片還是比較好的,效果明顯。

1.Shell命令讀取

打開終端

cd /sys/class/thermal/thermal_zone0

查看溫度

cat temp

樹莓派的返回值 

35050

返回值除以1000為當(dāng)前CPU溫度值。即當(dāng)前溫度為53攝氏度。如下圖所示

如何讀取樹莓派4B處理器(CPU)的實(shí)時溫度

 2.編寫一段c語言程序讀取

程序源代碼

溫度是在   /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);
    }
}

編譯運(yùn)行結(jié)果

gcc -o read_cpu_temp read_cpu_temp.c

編譯程序出現(xiàn)三個警告,可以不用管它,生成可以執(zhí)行文件read_cpu_temp

 輸入./read_cpu_temp運(yùn)行程序

./read_cpu_temp

如何讀取樹莓派4B處理器(CPU)的實(shí)時溫度

(我安裝了風(fēng)扇和散熱片以及外殼,大概平均在36攝氏度左右)  

3. 編寫Erlang程序讀取

編寫代碼

創(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()
.

編譯運(yùn)行結(jié)果

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().

如何讀取樹莓派4B處理器(CPU)的實(shí)時溫度

獲取硬件相關(guān)信息

查看cpu信息

lscpu

如何讀取樹莓派4B處理器(CPU)的實(shí)時溫度

查看內(nèi)存信息

free -h

如何讀取樹莓派4B處理器(CPU)的實(shí)時溫度

關(guān)于如何讀取樹莓派4B處理器(CPU)的實(shí)時溫度就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(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)容。

AI