How do I get the CPU temperature?
Asked Answered
F

5

14

I know it's somehow possible to get the CPU's temperature because I downloaded an app that does it in an unrooted device. How is it done?

edit: The application is called A1 CPU Tool. I spent several days searching for an answer.

edit2: Here's the code I tried

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
    Sensor TempSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE);
    mSensorManager.registerListener(temperatureSensor, TempSensor, SensorManager.SENSOR_DELAY_FASTEST);
}

private SensorEventListener temperatureSensor = new SensorEventListener(){

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        // TODO Auto-generated method stub
        float temp = event.values[0];
        Log.i("sensor", "sensor temp = " + temp);
    }
};
Furunculosis answered 25/12, 2013 at 9:10 Comment(1)
When I do this I get that the TempSensor is always null. According to the docs that means the sensor does not exist so of course I get no callback. Have you checked for a null value of TempSensor?Confiscatory
P
12

Read the file from following paths (Since it is different for different devices) to get cpu temperature details from different devices, One of the path will return the needed file.

    "/sys/devices/system/cpu/cpu0/cpufreq/cpu_temp"             
    "/sys/devices/system/cpu/cpu0/cpufreq/FakeShmoo_cpu_temp"    
    "/sys/class/thermal/thermal_zone1/temp"                      
    "/sys/class/i2c-adapter/i2c-4/4-004c/temperature"            
    "/sys/devices/platform/tegra-i2c.3/i2c-4/4-004c/temperature" 
    "/sys/devices/platform/omap/omap_temp_sensor.0/temperature"  
    "/sys/devices/platform/tegra_tmon/temp1_input"               
    "/sys/kernel/debug/tegra_thermal/temp_tj"                   
    "/sys/devices/platform/s5p-tmu/temperature"                  
    "/sys/class/thermal/thermal_zone0/temp"                      
    "/sys/devices/virtual/thermal/thermal_zone0/temp"            
    "/sys/class/hwmon/hwmon0/device/temp1_input"                 
    "/sys/devices/virtual/thermal/thermal_zone1/temp"            
    "/sys/devices/platform/s5p-tmu/curr_temp"    

To read the file, I used:

RandomAccessFile reader = new RandomAccessFile("/sys/devices/system/cpu/cpu0/cpufreq/cpu_temp", "r");
String line = reader.readLine();

Here is the another way: Read system files

Peptic answered 6/2, 2018 at 10:55 Comment(4)
Would be great if you gave a reference to an explanation of how to read the paths so it can be used programatically.Priebe
Here's a description of This is a post that describes how to use a file reader for that purpose: https://mcmap.net/q/829355/-read-android-system-filePriebe
@CorneliusRoemer I just used RandomAccessFile.Peptic
Nice! That's much more concise than any alternatives I found. Thanks for the edit. If you like, you can also add these extra file paths I found: /sys/htc/cpu_temp /sys/devices/platform/tegra-i2c.3/i2c-4/4-004c/ext_temperature /sys/devices/platform/tegra-tsensor/tsensor_temperaturePriebe
D
6

If a device doesn't expose CPU temperature via either temperature sensor (that is, asking for a sensor of type TEMPERATURE or AMBIENT_TEMPERATURE gives you nothing), you won't be able to read it in a standard manner. Of course, that says nothing about doing it in a non-standard way which might be suitable for your application if it's not going for wide distribution.

You can read off device files in /sys/devices/platform if you know where to look. If you run adb shell and cd into that folder, you've got fairly good chances of find | grep temp giving you files representing temperature sensors.

These files also aren't just going to be for the CPU only - as an example the battery temperature sensor for my Google Glass can be accessed by reading /sys/devices/platform/omap_i2c.1/i2c-1/1-0055/power_supply/bq27520-0/temp.

Looking at that path it's clear why you shouldn't do this and expect it to work on all devices - the paths and also the formats of the files will vary from device to device. However in a pinch it's a quick and dirty way to get the job done.

Domenic answered 18/6, 2014 at 8:31 Comment(1)
I am having most success with sys/devices/virtual/thermal/thermal_zone0/temp. See #24798369Hudgens
W
3

http://repo.xposed.info/module/org.narcotek.cputemp

Read this one carefully, I think it might help you.

I am using an app called cool tool; it's amazing it has lots of features... Then I enabled custom labels 1 and followed what the link said. I changed the path to /sys/class/thermal/thermal_zone0/temp and there was this option which I didn't change refer:(/d+)/d{3} And also replacement pattern:1$ Hope this helps use cool tool if you have to!

Weirdie answered 25/8, 2016 at 21:6 Comment(0)
P
3

Here is a current set of paths that are used in production in Dec 2019 to find CPU temperature. There are so many different paths because different OEMs put the temperature in different places.

These three locations are also used in addition to the one listed above:

/sys/htc/cpu_temp
/sys/devices/platform/tegra-i2c.3/i2c-4/4-004c/ext_temperature
/sys/devices/platform/tegra-tsensor/tsensor_temperature

The ones already listed as answer previously still hold true:

/sys/devices/system/cpu/cpu0/cpufreq/cpu_temp
/sys/devices/system/cpu/cpu0/cpufreq/FakeShmoo_cpu_temp
/sys/class/thermal/thermal_zone1/temp
/sys/class/i2c-adapter/i2c-4/4-004c/temperature
/sys/devices/platform/tegra-i2c.3/i2c-4/4-004c/temperature
/sys/devices/platform/omap/omap_temp_sensor.0/temperature
/sys/devices/platform/tegra_tmon/temp1_input
/sys/kernel/debug/tegra_thermal/temp_tj
/sys/devices/platform/s5p-tmu/temperature
/sys/class/thermal/thermal_zone0/temp
/sys/devices/virtual/thermal/thermal_zone0/temp
/sys/class/hwmon/hwmon0/device/temp1_input
/sys/devices/virtual/thermal/thermal_zone1/temp
/sys/devices/platform/s5p-tmu/curr_temp

Here's a code snippet for how to read those system files. This is classic Unix philosophy 'everything is a file', even sensor readings in this case are accessed by reading a 'file'.

RandomAccessFile reader = new RandomAccessFile("/sys/devices/system/cpu/cpu0/cpufreq/cpu_temp", "r");
String line = reader.readLine();

The result is most likely in milli Celsius, such as 33000 for 33 degree C, so you need to divide by 1000 to get temperature in C.

Priebe answered 12/12, 2019 at 14:17 Comment(2)
Tested this on OnePlus 7 Pro Android 10, and the following work: /sys/class/thermal/thermal_zone1/temp, /sys/class/thermal/thermal_zone0/temp,/sys/devices/virtual/thermal/thermal_zone0/temp. However the value written to these files is like a large number i.e. 33000. Given the fact that I used a Cpu Monitor app in paralel which showed 33 C degrees, I assume they're reading the same file but dividing the number to a readable formatPseudonym
@Peter, yes, the reading is in mC, so divide by 1000.Priebe
S
-1

The temperature sensor even if exists if for the battery temperature and CPU temperature.

Most phones only include CPU temperature measurement sensor Sensor.TYPE_TEMPERATURE which is deprecated

You should use Sensor.TYPE_AMBIENT_TEMPERATURE which I don't think many phones have.

For More information please go through this

http://developer.android.com/reference/android/hardware/Sensor.html#TYPE_AMBIENT_TEMPERATURE

Schiller answered 25/12, 2013 at 9:22 Comment(4)
I already tried both of these on my Nexus 4 (on which the tool worked). Neither worked.Furunculosis
@AsafSchreiber are you sure you tried it correctly? Someone needs to post some code ;)Tymes
I should mention that I tried this exact code with both TYPE_AMBIENT_TEMPERATURE and TYPE_TEMPERATURE and in both cases I simply got no messages in the logcat (meaning the onSensorChanged function never ran)Furunculosis
@Furunculosis how u resolved issue i m also facing same none of the temperature sensors are available in my deviceSharell

© 2022 - 2024 — McMap. All rights reserved.