Page 1 of 1

RPi CPU temp with Python

Posted: Sat Jun 03, 2017 5:44 pm
by Ageir
Hi!

I'm trying to read the RPi CPU temperature with Python. I tried psutil but it won't work.
Must be another easy way to get the temperature. And are there any other temp you can read on the RPi except CPU? Wifi chip?

Re: RPi CPU temp with Python

Posted: Sat Jun 03, 2017 6:01 pm
by DirkS
I use 'vcgencmd measure_temp'

Code: Select all

from subprocess import PIPE, Popen

def get_cpu_temperature():
    """get cpu temperature using vcgencmd"""
    process = Popen(['vcgencmd', 'measure_temp'], stdout=PIPE)
    output, _error = process.communicate()
    return float(output[output.index('=') + 1:output.rindex("'")])
I think I 'borrowed' this snippet from someone else...

Re: RPi CPU temp with Python

Posted: Sat Jun 03, 2017 10:52 pm
by kusti8
Ageir wrote:Hi!

I'm trying to read the RPi CPU temperature with Python. I tried psutil but it won't work.
Must be another easy way to get the temperature. And are there any other temp you can read on the RPi except CPU? Wifi chip?
There isn't any other sensors and there's no need to know those temps since they don't affect anything.

Re: RPi CPU temp with Python

Posted: Sun Jun 04, 2017 6:10 am
by DougieLawson
Ageir wrote:Hi!

I'm trying to read the RPi CPU temperature with Python. I tried psutil but it won't work.
Must be another easy way to get the temperature. And are there any other temp you can read on the RPi except CPU? Wifi chip?
What do you think it will tell you? Running any program will make the CPU run hotter. What do you think you can do if it runs hot?

Code: Select all

#!/usr/bin/python
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)

while True:
  try:
    tFile = open('/sys/class/thermal/thermal_zone0/temp')
    temp = float(tFile.read())
    tempC = temp/1000
    if tempC > 43.5:
      GPIO.output(17, 1)
      print "HOT"
    else:
      GPIO.output(17, 0)
      print "COLD"

  except:
    tFile.close()
    GPIO.cleanup()
    exit

Re: RPi CPU temp with Python

Posted: Sun Jun 04, 2017 9:04 am
by Ageir
Thanks for the code! :)
What do you think it will tell you? Running any program will make the CPU run hotter. What do you think you can do if it runs hot?
I intend to place it in a greenhouse as a controller and would like to measure the temperature so it won't get too hot. I'm placing an external sensor as well. But knowing the CPU temp will help as a failsafe if the cooling system fails.

Re: RPi CPU temp with Python

Posted: Sun Jun 04, 2017 9:26 am
by DougieLawson
Measuring CPU temperature isn't useful for measuring the ambient temperature. You'll need to use a BMP085, BMP180, BME280, DHT11 or DS18B20 sensor or a MAX31855 SPI thermocouple amplifier (with a k-type thermocouple).

Re: RPi CPU temp with Python

Posted: Sun Jun 04, 2017 10:07 am
by Ageir
Measuring CPU temperature isn't useful for measuring the ambient temperature. You'll need to use a BMP085, BMP180, BME280, DHT11 or DS18B20 sensor or a MAX31855 SPI thermocouple amplifier (with a k-type thermocouple).
No that's why I mentioned I have an external sensor. DHT22 actually.

Re: RPi CPU temp with Python

Posted: Sun Jun 04, 2017 10:32 am
by DougieLawson
Then forget CPU temp ever existed and you're good to go.

Re: RPi CPU temp with Python

Posted: Sun Mar 25, 2018 4:48 pm
by krisztianlukacs

Code: Select all

import io 

f = open("/sys/class/thermal/thermal_zone0/temp", "r")
t = f.readline ()

cputemp = "CPU temp: "+t

print (cputemp)

Re: RPi CPU temp with Python

Posted: Tue Mar 27, 2018 5:48 pm
by Paul Hutch
Ageir wrote:
Sun Jun 04, 2017 9:04 am
I intend to place it in a greenhouse as a controller and would like to measure the temperature so it won't get too hot. I'm placing an external sensor as well. But knowing the CPU temp will help as a failsafe if the cooling system fails.
IIRC, the Pi has a CPU temperature fail safe already built-in, it will slow the processor and eventually stop it if the temperature gets too high.

Re: RPi CPU temp with Python

Posted: Tue Mar 27, 2018 6:34 pm
by jahboater
Paul Hutch wrote:
Tue Mar 27, 2018 5:48 pm
IIRC, the Pi has a CPU temperature fail safe already built-in, it will slow the processor and eventually stop it if the temperature gets too high.
Yes, the Pi is very robust. It wont actually stop, but in the extreme it will throttle down to 600Mhz.

I suggest getting the new Pi3B+ which has some seriously clever thermal management.
It will also have more CPU power available than you can possibly need, so it will be idle most of time and run cool.
(powerful CPU's use more electrical power of course, but complete their tasks in less time).

Re: RPi CPU temp with Python

Posted: Wed Mar 28, 2018 3:20 pm
by Paul Hutch
jahboater wrote:
Tue Mar 27, 2018 6:34 pm
Paul Hutch wrote:
Tue Mar 27, 2018 5:48 pm
IIRC, the Pi has a CPU temperature fail safe already built-in, it will slow the processor and eventually stop it if the temperature gets too high.
Yes, the Pi is very robust. It wont actually stop, but in the extreme it will throttle down to 600Mhz.
If it won't stop then DIY software CPU temperature monitoring and shutdown is essential to add for applications where no air conditioning is available. When ambient temperatures exceed 45°C even 600Mhz could still be too much load leading to processor burn out.

Anybody have more information on if there is a thermal shutdown (preferably from official RPF/T or Broadcom documentation)?

Re: RPi CPU temp with Python

Posted: Thu May 16, 2019 9:38 pm
by skypi
DougieLawson wrote:
Sun Jun 04, 2017 6:10 am
What do you think it will tell you? Running any program will make the CPU run hotter. What do you think you can do if it runs hot?
well, if you have control of the program (i.e. you wrote it or can amend it) that is driving the pi hard, then checking the cpu temperature you can slow that program down!

I came across this thread because I have a picamera skypi "skycam" node looking direct into the sun as it passes between 7-11 ish and that gets hot! if you try doing an scp/rsync from it at any time during the day it blows up!

so yeah, basically you do not do an scp during the day (and maybe I will fit a heat sink though ambient is 40c not sure how much that wil improve things...)

but the node was just locking up taking a picture every 7 seconds so now having observed the sysfs endpoint listed here (thanks) I can read the temperature and slow the rate of taking pictures when the cpu temperature is high! (maybe actually need to shut down picamera as well during the pause)

Re: RPi CPU temp with Python

Posted: Thu May 16, 2019 10:21 pm
by jahboater
I would try the new Pi3B+ model.
Really good thermal management is one of its key features.

Re: RPi CPU temp with Python

Posted: Tue May 21, 2019 9:14 pm
by skypi
yeah, but this device wants the compact nature and price of the zero really!

rsync is probably the problem LOL, that really drives the pi! (copying 1gb+)

time=now, ambient at pi = 22.5c, pi zero was running about 35c do an rsync and temperature up to 54c

and 10mins after rsync finished, cpu temp still not dropped back to 35c, currently at 40c, though not much airflow around where it is situated... and it is rolling a periodic python-picamera raw capture and pil annotate...

so ambient >=35c := rsync = no no

(though a periodic rsync maybe OK LOL just that single massive burst of several hundred mb+ of encryption+sd access over wifi whilst also running a picamera permanent instance also accessing sd)

though was a big kernel update just recently... not tried a midday rsync for couple of days....

skypi.org

Re: RPi CPU temp with Python

Posted: Thu May 23, 2019 10:46 pm
by ben_nuttall

Code: Select all

from gpiozero import CPUTemperature

cpu = CPUTemperature()

print(cpu.temperature)
https://gpiozero.readthedocs.io/en/stab ... emperature