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
I use 'vcgencmd measure_temp'
I think I 'borrowed' this snippet from someone else...
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("'")])
Re: RPi CPU temp with Python
There isn't any other sensors and there's no need to know those temps since they don't affect anything.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 are 10 types of people: those who understand binary and those who don't.
- DougieLawson
- Posts: 42312
- Joined: Sun Jun 16, 2013 11:19 pm
- Location: A small cave in deepest darkest Basingstoke, UK
Re: RPi CPU temp with Python
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?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?
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
Languages using left-hand whitespace for syntax are ridiculous
DMs sent on https://twitter.com/DougieLawson or LinkedIn will be answered next month.
Fake doctors - are all on my foes list.
The use of crystal balls and mind reading is prohibited.
DMs sent on https://twitter.com/DougieLawson or LinkedIn will be answered next month.
Fake doctors - are all on my foes list.
The use of crystal balls and mind reading is prohibited.
Re: RPi CPU temp with Python
Thanks for the code! 

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.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?
- DougieLawson
- Posts: 42312
- Joined: Sun Jun 16, 2013 11:19 pm
- Location: A small cave in deepest darkest Basingstoke, UK
Re: RPi CPU temp with Python
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).
Languages using left-hand whitespace for syntax are ridiculous
DMs sent on https://twitter.com/DougieLawson or LinkedIn will be answered next month.
Fake doctors - are all on my foes list.
The use of crystal balls and mind reading is prohibited.
DMs sent on https://twitter.com/DougieLawson or LinkedIn will be answered next month.
Fake doctors - are all on my foes list.
The use of crystal balls and mind reading is prohibited.
Re: RPi CPU temp with Python
No that's why I mentioned I have an external sensor. DHT22 actually.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).
- DougieLawson
- Posts: 42312
- Joined: Sun Jun 16, 2013 11:19 pm
- Location: A small cave in deepest darkest Basingstoke, UK
Re: RPi CPU temp with Python
Then forget CPU temp ever existed and you're good to go.
Languages using left-hand whitespace for syntax are ridiculous
DMs sent on https://twitter.com/DougieLawson or LinkedIn will be answered next month.
Fake doctors - are all on my foes list.
The use of crystal balls and mind reading is prohibited.
DMs sent on https://twitter.com/DougieLawson or LinkedIn will be answered next month.
Fake doctors - are all on my foes list.
The use of crystal balls and mind reading is prohibited.
- krisztianlukacs
- Posts: 1
- Joined: Sun Mar 25, 2018 4:44 pm
- Location: Budapest
Re: RPi CPU temp with Python
Code: Select all
import io
f = open("/sys/class/thermal/thermal_zone0/temp", "r")
t = f.readline ()
cputemp = "CPU temp: "+t
print (cputemp)
-
- Posts: 761
- Joined: Fri Aug 25, 2017 2:58 pm
- Location: Blackstone River Valley, MA, USA
Re: RPi CPU temp with Python
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
Yes, the Pi is very robust. It wont actually stop, but in the extreme it will throttle down to 600Mhz.Paul Hutch wrote: ↑Tue Mar 27, 2018 5:48 pmIIRC, 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.
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).
-
- Posts: 761
- Joined: Fri Aug 25, 2017 2:58 pm
- Location: Blackstone River Valley, MA, USA
Re: RPi CPU temp with Python
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.jahboater wrote: ↑Tue Mar 27, 2018 6:34 pmYes, the Pi is very robust. It wont actually stop, but in the extreme it will throttle down to 600Mhz.Paul Hutch wrote: ↑Tue Mar 27, 2018 5:48 pmIIRC, 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.
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
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!DougieLawson wrote: ↑Sun Jun 04, 2017 6:10 amWhat 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 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
I would try the new Pi3B+ model.
Really good thermal management is one of its key features.
Really good thermal management is one of its key features.
Re: RPi CPU temp with Python
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
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
- ben_nuttall
- Posts: 249
- Joined: Sun Aug 19, 2012 11:19 am
- Location: Cambridgeshire, UK
Re: RPi CPU temp with Python
Code: Select all
from gpiozero import CPUTemperature
cpu = CPUTemperature()
print(cpu.temperature)
Former RPF staff. Author of gpiozero and creator of piwheels.