Reading CPU temps using python for Raspberry Pi

earlier, I found this, and thought it was cool so I modified it a bit to work how I needed it to.

http://www.raspberrypi.org/phpBB3/viewtopic.php?f=32&t=22180

and here is my modification:

#!/usr/bin/env python
import os

# Return CPU temperature as a character string
def getCPUtemperature():
res = os.popen(‘vcgencmd measure_temp’).readline()
return(res.replace(“temp=”,””).replace(“‘C\n”,””))

temp1=int(float(getCPUtemperature()))
temp2= 9.0/5.0*temp1+32
print temp1,”C”, “\n”,  temp2,”F”

Now I can have an IF statement watch the temperature and if it goes above a certain temp, I can turn on a GPIO pin using RPI.GPIO package, or anything I wanted to do with it. I figured I’d share this little trick with everyone. 😛

ps. if the guy that wrote the original function part ever reads this, thank you so much for that 😛

Advertisements

2 Responses to Reading CPU temps using python for Raspberry Pi

  1. benvk85 says:

    Hey,
    just wondering if you could clarify this line:
    return(res.replace(“temp=”,””).replace(“‘C\n”,””))

    I note there is a single quotation mark befor the C\n, is this needed?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: