在 Linux 系统中,/sys/class/thermal/thermal_zone0/temp
文件中的温度值通常以 毫摄氏度(millidegree Celsius) 为单位存储。这意味着你需要将读取的值除以 1000 来转换为常见的摄氏度(°C)。
换算方法:
-
读取文件内容:
cat /sys/class/thermal/thermal_zone0/temp
例如,输出可能是
53000
。 -
转换为摄氏度:
温度(°C)= 读取的值 / 1000
例如:
53000 / 1000 = 53.0°C
注意事项:
-
单位确认:绝大多数 Linux 系统使用毫摄氏度(如
thermal_zone
),但极少数情况下可能是其他单位(如某些嵌入式设备)。如果不确定,可以查阅相关硬件文档。 -
多 thermal_zone:如果有多个传感器(如 CPU、GPU 等),可能会存在
thermal_zone1
、thermal_zone2
等,需分别读取。 -
动态更新:该文件的值会实时更新,可以通过
watch
命令动态监控:watch -n 1 'cat /sys/class/thermal/thermal_zone*/temp'
代码范例:
void* monitor_thermalSensor_thread(void *data)
{
FILE *fp = NULL;
char buffer[10];
int temp = 0;
ALOGI("[%s]\n",__func__);
while(1)
{
fp = fopen("/sys/class/thermal/thermal_zone0/temp", "r");
if(fp == NULL)
{
sleep(1);
continue;
}
memset(buffer, 0, sizeof(buffer));
fgets(buffer, sizeof(buffer), fp);
temp = atoi(buffer);
printf("temperature: %d \n", temp);
fclose(fp);
if(temp > 120000) //120度
{
ALOGI("CPU temperature is too high. Reboot!!");
sleep(6);
am_property_set("ctl.start","reboot");
}
sleep(30);
}
ALOGI("====== monitor_thermalSensor_thread exit!!! ======\n");
pthread_detach(pthread_self());
pthread_exit(NULL);
}
void monitor_thermalSensor_temperature(void)
{
pthread_t monitor_thermalSensorTemperature_thread;
pthread_create(&monitor_thermalSensorTemperature_thread, NULL, monitor_thermalSensor_thread, NULL);
return;
}