Method of Obtaining CPU Temperature under Embedded Linux

1 Introduction

  In embedded devices, for devices that work for a long time, especially industrial control products, a fan is generally added to actively dissipate heat for the cpu. The fan generally chooses an adjustable speed, so that the program can adjust the fan speed in real time according to the cpu temperature to achieve the purpose of reducing the cpu temperature in time at high temperatures and saving power consumption at low temperatures.


2 Get cpu temperature

  The linux kernel provided by the original cpu factory usually has a cpu temperature detection driver, and maps the temperature information to the user file system, and the user only needs to read the temperature information from the virtual file. The cpu temperature virtual file is located in “/sys/devices/virtual/thermal”or “/sys/class/thermal”under, named “thermal_zoneX”, X represents the cpu core temperature detection node, if it is a multi-core cpu, there will usually be multiple temperature detection nodes; use one of them, because the temperature usually has little difference.


rk3399 ubuntu16 cup core temperature node

root@firefly:/home# ls /sys/devices/virtual/thermal/
cooling_device0  cooling_device1  cooling_device2  thermal_zone0  thermal_zone1
root@firefly:/home# ls /sys/class/thermal/
cooling_device0  cooling_device1  cooling_device2  thermal_zone0  thermal_zone1

2.1 Obtain via shell

root@firefly:/home# cat /sys/class/thermal/thermal_zone0/temp
42222
root@firefly:/home# cat /sys/class/thermal/thermal_zone1/temp
41666

The temperature unit is 0.001℃, which means that the “thermal_zone0”current temperature of temperature node 0 is 42.222℃.


2.2 Real-time refresh through watch

  Through the watch command, the cpu temperature is read every 2 seconds and output to the terminal.

root@firefly:/home# watch -n 2 -d echo cpu temperature:[$(cat /sys/class/thermal/thermal_zone0/temp)]
Every 2.0s: echo cpu temperature:[42777]                                                                       Thu Jul 30 16:02:05 2020
cpu temperature:[42777]

2.3 Read through C language

#include <stdio.h>   
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#define CPU_TEMP_FILE0	"/sys/devices/virtual/thermal/thermal_zone0/temp"
#define CPU_TEMP_FILE1	"/sys/devices/virtual/thermal/thermal_zone0/temp"

int main(int arc, char *argv[])
{
    
    
	FILE *fp = NULL;
	int temp = 0;
	
	fp = fopen (CPU_TEMP_FILE0, "r");
	if (fp < 0)
	{
    
    
		printf("open file failed,%s\n", strerror(errno));
	}
	for (;;)
	{
    
    
		fscanf(fp, "%d", &temp);
		printf("cpu temperature: [%d.%d C]\n", temp/1000, temp%1000/100);
		sleep(2);
	}
	fclose(fp);
	return 0;
}

Compile and execute

root@firefly:/home# gcc cpu_temp.c -o cpu_temp
root@firefly:/home# ./cpu_temp
cpu temperature: [42.7 C]
cpu temperature: [42.7 C]
cpu temperature: [42.7 C]

Guess you like

Origin blog.csdn.net/qq_20553613/article/details/107703442