Ageir
Posts: 17
Joined: Wed May 03, 2017 5:48 pm

RPi CPU temp with Python

Sat Jun 03, 2017 5:44 pm

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?

DirkS
Posts: 9842
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: RPi CPU temp with Python

Sat Jun 03, 2017 6:01 pm

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...

User avatar
kusti8
Posts: 3439
Joined: Sat Dec 21, 2013 5:29 pm
Location: USA

Re: RPi CPU temp with Python

Sat Jun 03, 2017 10:52 pm

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.
There are 10 types of people: those who understand binary and those who don't.

User avatar
DougieLawson
Posts: 35369
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: RPi CPU temp with Python

Sun Jun 04, 2017 6:10 am

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
Note: Having anything remotely humorous in your signature is completely banned on this forum.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

Ageir
Posts: 17
Joined: Wed May 03, 2017 5:48 pm

Re: RPi CPU temp with Python

Sun Jun 04, 2017 9:04 am

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.

User avatar
DougieLawson
Posts: 35369
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: RPi CPU temp with Python

Sun Jun 04, 2017 9:26 am

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).
Note: Having anything remotely humorous in your signature is completely banned on this forum.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

Ageir
Posts: 17
Joined: Wed May 03, 2017 5:48 pm

Re: RPi CPU temp with Python

Sun Jun 04, 2017 10:07 am

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.

User avatar
DougieLawson
Posts: 35369
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: RPi CPU temp with Python

Sun Jun 04, 2017 10:32 am

Then forget CPU temp ever existed and you're good to go.
Note: Having anything remotely humorous in your signature is completely banned on this forum.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
krisztianlukacs
Posts: 1
Joined: Sun Mar 25, 2018 4:44 pm
Location: Budapest
Contact: Facebook Skype Twitter

Re: RPi CPU temp with Python

Sun Mar 25, 2018 4:48 pm

Code: Select all

import io 

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

cputemp = "CPU temp: "+t

print (cputemp)

Paul Hutch
Posts: 354
Joined: Fri Aug 25, 2017 2:58 pm
Location: Blackstone River Valley, MA, USA
Contact: Website

Re: RPi CPU temp with Python

Tue Mar 27, 2018 5:48 pm

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.

jahboater
Posts: 4448
Joined: Wed Feb 04, 2015 6:38 pm

Re: RPi CPU temp with Python

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.

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).

Paul Hutch
Posts: 354
Joined: Fri Aug 25, 2017 2:58 pm
Location: Blackstone River Valley, MA, USA
Contact: Website

Re: RPi CPU temp with Python

Wed Mar 28, 2018 3:20 pm

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)?

skypi
Posts: 107
Joined: Sat Aug 09, 2014 11:48 pm

Re: RPi CPU temp with Python

Thu May 16, 2019 9:38 pm

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)

jahboater
Posts: 4448
Joined: Wed Feb 04, 2015 6:38 pm

Re: RPi CPU temp with Python

Thu May 16, 2019 10:21 pm

I would try the new Pi3B+ model.
Really good thermal management is one of its key features.

skypi
Posts: 107
Joined: Sat Aug 09, 2014 11:48 pm

Re: RPi CPU temp with Python

Tue May 21, 2019 9:14 pm

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

User avatar
ben_nuttall
Raspberry Pi Foundation Employee & Forum Moderator
Raspberry Pi Foundation Employee & Forum Moderator
Posts: 228
Joined: Sun Aug 19, 2012 11:19 am
Location: Cambridge, UK
Contact: Website

Re: RPi CPU temp with Python

Thu May 23, 2019 10:46 pm

Code: Select all

from gpiozero import CPUTemperature

cpu = CPUTemperature()

print(cpu.temperature)
https://gpiozero.readthedocs.io/en/stab ... emperature
Community Manager - Raspberry Pi Foundation
Author of GPIO Zero and creator of piwheels

Return to “Python”