Page 1 of 3

[Tutorial] Show RPI's Temperature with a command.

Posted: Sun Feb 24, 2013 8:48 pm
by eXsoR
So i decided to make a quick Tutorial on how to show the Raspberry PI's Temperature.
Note: this is using the on-board sensor.

The command to do this is:
/opt/vc/bin/vcgencmd measure_temp
You can also simplify the command by making an alias and adding it to your ~/.bashrc or as instructed in the bashrc file. Make a file in your home directory called .bash_aliases and put your aliases in that file rather then in the .bashrc file.

Making the .bash_aliases file by running the command:
sudo nano .bash_aliases
Make sure you are in your home directory before making the file. You can check by making sure your terminal says [email protected] ~ $ and (~) meaning you are in your home directory. if you are not just run the command
cd ~
.

In the file wright the aliases. example:
alias temp='/opt/vc/bin/vcgencmd measure_temp'
note: if you don't put the apostrophe ( ' ) before and after the command it wont work. Also note that I put the nickname that you are going to give this command, followed up a equals sign (=).

I hope you guys find this useful is I did. Since i live in a very hot environment that I like to check the temp here and there. :D

Re: [Tutorial] Show RPI's Temperature with a command.

Posted: Sun Feb 24, 2013 10:43 pm
by rpdom
Nice tip!

I'd just like to point out that you don't need "sudo" to run nano on files in your home directory. You already have all the permissions you need. Keep root/superuser privileges for when they are really needed - it's safer prcatice.

Re: [Tutorial] Show RPI's Temperature with a command.

Posted: Mon Feb 25, 2013 1:09 pm
by BerryPicker
With the 2013-02-09 raspbian distribution the command and response is

Code: Select all

/opt/vc/bin/vcgencmd measure_temp
/opt/vc/bin/vcgencmd: error while loading shared libraries: libvcos.so: cannot open shared object file: No such file or directory
For information on this see:
Bug 15006 - Libraries in ld.so.cache ignored by ld-linux-armhf.so.3 on armv6l
http://sourceware.org/bugzilla/show_bug.cgi?id=15006
The offered workaround is

Code: Select all

[email protected] ~ $ cd /opt/vc/bin
[email protected] /opt/vc/bin $ LD_LIBRARY_PATH=/opt/vc/lib ./vcgencmd measure_temp
temp=49.2'C
So, now for another exercise in building an alias ...

Re: [Tutorial] Show RPI's Temperature with a command.

Posted: Fri Mar 15, 2013 5:25 pm
by SuPeRMiNoR2
BerryPicker wrote:With the 2013-02-09 raspbian distribution the command and response is

Code: Select all

/opt/vc/bin/vcgencmd measure_temp
/opt/vc/bin/vcgencmd: error while loading shared libraries: libvcos.so: cannot open shared object file: No such file or directory
For information on this see:
Bug 15006 - Libraries in ld.so.cache ignored by ld-linux-armhf.so.3 on armv6l
http://sourceware.org/bugzilla/show_bug.cgi?id=15006
The offered workaround is

Code: Select all

[email protected] ~ $ cd /opt/vc/bin
[email protected] /opt/vc/bin $ LD_LIBRARY_PATH=/opt/vc/lib ./vcgencmd measure_temp
temp=49.2'C
So, now for another exercise in building an alias ...
As far as I can tell, that bug has been fixed, but if anyone has a older version that still has that bug here is the the alias to use.

Code: Select all

alias temp="LD_LIBRARY_PATH=/opt/vc/lib && /opt/vc/bin/vcgencmd measure_temp"

Re: [Tutorial] Show RPI's Temperature with a command.

Posted: Wed Mar 20, 2013 10:02 pm
by m0rgo
Is it possible to import the system temperature into a python program?

Re: [Tutorial] Show RPI's Temperature with a command.

Posted: Thu Mar 21, 2013 12:23 am
by pluggy
An alternative is to read /sys/class/thermal/thermal_zone0/temp on a recent Raspdian. Divide it by 1000 for degrees C.

Code: Select all

cat /sys/class/thermal/thermal_zone0/temp 
will do it at the command line.

Re: [Tutorial] Show RPI's Temperature with a command.

Posted: Thu Mar 21, 2013 5:12 pm
by Fraoch
Does Pi Raspbian support lm-sensors? That's the package I use on my Ubuntu PC. However I think lm-sensors needs access to a hardware monitoring chip which I doubt the Pi has - unless there's something hidden in the SoC which wouldn't be terribly surprising, there's lots of stuff in there. ;)

It'd be nice to display the temperature using conky, I have to learn how to do this - looks like it can be done.

Re: [Tutorial] Show RPI's Temperature with a command.

Posted: Mon Apr 01, 2013 10:38 am
by badfur

Code: Select all

#!/bin/bash
cpuTemp0=$(cat /sys/class/thermal/thermal_zone0/temp)
cpuTemp1=$(($cpuTemp0/1000))
cpuTemp2=$(($cpuTemp0/100))
cpuTempM=$(($cpuTemp2 % $cpuTemp1))

echo CPU temp"="$cpuTemp1"."$cpuTempM"'C"
echo GPU $(/opt/vc/bin/vcgencmd measure_temp)

Re: [Tutorial] Show RPI's Temperature with a command.

Posted: Wed Apr 03, 2013 9:15 pm
by m0rgo
I asked earlier if the system temperature could be imported into a python script.

Here is my solution;

First, write a shell script to get system time and save it into a file in current directory.

e.g.

#!/bin/bash
#get system temperarure and save as 'sysTemp'

/opt/vc/bin/vcgencmd measure_temp > sysTemp

# this creates a file called sysTemp in current directory


Secondly, write a python script / function to import value in sysTemp and convert it to a number that can be used;

e.g.

#!/bin/python
#script to read a file and extract a number

fo = open("sysTemp") # Open a file
str = fo.read(); #read characters in sysTemp and hold them as 'str'
fo.close() #close opened file
print(str) # print string to check
temp1=str[5:9] #chop out required characters
temp2=eval(temp1) #convert string into number
print("system temp = ", temp2) # print value to check
temp=temp2*2 # multiply by 2 to check for mathematical useability
print("twice system temp = ",temp) # print multiplied number



When you get to the point that 'temp' is useable, it can be used for whatever you want.

Note. I've commented the above to death to explain what is going on. In use, you would remove unnecessary comments and print statements.

Re: [Tutorial] Show RPI's Temperature with a command.

Posted: Wed Apr 03, 2013 9:55 pm
by m0rgo
There is another command that you can use to get the system temperature;

cat /sys/class/thermal/thermal_zone0/temp.

This gives the system temperature in milli-degrees

To use the alternate temperature command and import it into Python;

First, write shell script to get system time and save it into a file in current directory. I used temp.sh for the shell script.

i.e.

#!/bin/bash
#get system temperature and save as 'sysTemp'

cat /sys/class/thermal/thermal_zone0/temp > sysTemp


Secondly, write a python script / function to import value in sysTemp and convert it to a number that can be used;
Note. This version gives a five figure number that is milli-degrees, so it will need converting for use.

i.e.

#!/bin/python
#script to read a file and extract a number

fo = open("sysTemp") # Open a file
str = fo.read(); # read characters in sysTemp
fo.close() # close opened file
print('string = ',str) # print string to check
t=eval(str) # convert string into number
print ('extracted number = ',t) # print evaluated number
t2=t*2 # multiply by 2, evaluated number
print('twice temp in milli-degrees = ',t2) # print result of multiplication
t3=(t/1000.00) # convert five figure temp (milli-degrees) to degrees to two decimal places
print ('temperature in degrees = ',t3) # print result


Again, remove extraneous print commands and comments to reduce size and clutter in script.

Re: [Tutorial] Show RPI's Temperature with a command.

Posted: Wed Apr 03, 2013 10:07 pm
by Joe Schmoe
Interestingly, I get different results from the program vs accessing the file in /sys. (And, yes, I ran this enough times to be confident that the results are not just instaneously changing...)

Also, these numbers seems high to me. The last time I played around with this temperature stuff, I was consistently getting readings around 47 degrees. Of course, now my Pi is in a box - instead of in the open air. I suppose that would account for the change.

Code: Select all

pts/2 [email protected] /var/log $ /opt/vc/bin/vcgencmd measure_temp
temp=53.0'C
pts/2 [email protected] /var/log $ gawk '{print $1/1000,"degrees C"}' /sys/class/thermal/thermal_zone0/temp
54.072 degrees C
pts/2 [email protected] /var/log $ 

Re: [Tutorial] Show RPI's Temperature with a command.

Posted: Mon Apr 29, 2013 8:45 am
by yuusou
badfur wrote:

Code: Select all

#!/bin/bash
cpuTemp0=$(cat /sys/class/thermal/thermal_zone0/temp)
cpuTemp1=$(($cpuTemp0/1000))
cpuTemp2=$(($cpuTemp0/100))
cpuTempM=$(($cpuTemp2 % $cpuTemp1))

echo CPU temp"="$cpuTemp1"."$cpuTempM"'C"
echo GPU $(/opt/vc/bin/vcgencmd measure_temp)
I quite liked your script, so I decided to clean it up a little:

Code: Select all

#!/bin/bash
cpuTemp0=$(cat /sys/class/thermal/thermal_zone0/temp)
cpuTemp1=$(($cpuTemp0/1000))
cpuTemp2=$(($cpuTemp0/100))
cpuTempM=$(($cpuTemp2 % $cpuTemp1))

gpuTemp0=$(/opt/vc/bin/vcgencmd measure_temp)
gpuTemp0=${gpuTemp0//\'/º}
gpuTemp0=${gpuTemp0//temp=/}

echo CPU Temp: $cpuTemp1"."$cpuTempM"ºC"
echo GPU Temp: $gpuTemp0
The output comes out as following:

Code: Select all

[email protected] ~ $ ./temp      
CPU Temp: 56.2ºC
GPU Temp: 56.2ºC
[email protected] ~ $ 

Re: [Tutorial] Show RPI's Temperature with a command.

Posted: Sat May 18, 2013 12:39 am
by solar3000

Code: Select all

 perl -e 'm/(\d+)/; $x=$1; s/\d+//; printf ("%.1f", ( $x / 1000))' -p /sys/class/thermal/thermal_zone0/temp

Re: [Tutorial] Show RPI's Temperature with a command.

Posted: Sat May 18, 2013 2:33 am
by sprinkmeier
Nice one!

in python:

Code: Select all

tempC = int(open('/sys/class/thermal/thermal_zone0/temp').read()) / 1e3

Re: [Tutorial] Show RPI's Temperature with a command.

Posted: Sat May 18, 2013 11:20 pm
by solar3000
Or both:

Code: Select all

perl -e 'm/(\d+)/; $c=$1/1000; s/\d+//; $f = $c * 9 / 5 + 32; for ($f, $cputemp){ $_ = sprintf ( "%.1f" ,  $_  ) }; print "$c C, $f F";' -p /sys/class/thermal/thermal_zone0/temp

Re: [Tutorial] Show RPI's Temperature with a command.

Posted: Sat May 18, 2013 11:22 pm
by solar3000
can you do one liners in python?

What I did above, you can just copy and paste in your bash prompt and hit enter (gently).

Re: [Tutorial] Show RPI's Temperature with a command.

Posted: Sat May 18, 2013 11:51 pm
by sprinkmeier
solar3000 wrote:can you do one liners in python?

Code: Select all

$ python -c "import base64;print(base64.decodestring('WUVTIQ==\n'))"

Re: [Tutorial] Show RPI's Temperature with a command.

Posted: Sun May 19, 2013 12:58 am
by solar3000

Code: Select all

python -c "import base64;print(base64.decodestring('QmlnIEZhdCBTaG93b2Zm\n'))"

Re: [Tutorial] Show RPI's Temperature with a command.

Posted: Sun May 19, 2013 3:29 am
by sprinkmeier

Code: Select all

$ python -c "import base64;import gzip;print(gzip.zlib.decompress(base64.decodestring('eJxTUFBQ5gJiIEQCyiDABWIooAiDCS5UZSjiaOqhxqCbjkUlAH+dENQ=\n')))"

Re: [Tutorial] Show RPI's Temperature with a command.

Posted: Sun May 19, 2013 5:54 am
by solar3000
since you're going there....
how about some ascii art?
ASCII art one liner?

http://www.codeproject.com/Articles/109 ... -Generator

Re: [Tutorial] Show RPI's Temperature with a command.

Posted: Sun May 19, 2013 6:30 am
by rpdom
Just because I was bored... ;-)

Code: Select all

sed "s/\(...\)$/.\1°C/" < /sys/class/thermal/thermal_zone0/temp

Re: [Tutorial] Show RPI's Temperature with a command.

Posted: Thu Nov 07, 2013 4:06 pm
by JacopoBassan
I know this post is old, but oh well…

The command "bc" is a REALLY useful utility to perform simple & complex math operation.
Performing Math calculation in Bash

Code: Select all

sudo apt-get install bc
After that if you just want to get something formatted like "32.8" type

Code: Select all

echo "scale=1; $(cat /sys/class/thermal/thermal_zone0/temp)/1000" | bc
Note: you can choose between "scale=1/2/3..." to get a different decimal precision.

Re: [Tutorial] Show RPI's Temperature with a command.

Posted: Mon Dec 02, 2013 9:40 pm
by slackpi
JacopoBassan wrote:I know this post is old, but oh well…

The command "bc" is a REALLY useful utility to perform simple & complex math operation.
Performing Math calculation in Bash

Code: Select all

sudo apt-get install bc
After that if you just want to get something formatted like "32.8" type

Code: Select all

echo "scale=1; $(cat /sys/class/thermal/thermal_zone0/temp)/1000" | bc
Note: you can choose between "scale=1/2/3..." to get a different decimal precision.
Excellent solution!

Re: [Tutorial] Show RPI's Temperature with a command.

Posted: Fri Jan 17, 2014 12:22 am
by phoenixDownunder
When I run /opt/vc/bin/vcgencmd measure_temp I get
temp=64.3'C
Is that a bit high? Ambient is 44C here

Re: [Tutorial] Show RPI's Temperature with a command.

Posted: Fri Jan 17, 2014 12:51 am
by AndyD
phoenixDownunder wrote:temp=64.3'C Is that a bit high? Ambient is 44C here
I think that is reasonable. Is your Raspberry Pi working hard? It is around 30°C in my house at the moment and my Raspberry Pis are idle and running at 55.1°C and 55.6°C. It is going to 44°C today (again) looking forward to a cool change tonight after 5 very hot days.