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


41 posts   Page 1 of 2   1, 2
by eXsoR » Sun Feb 24, 2013 8:48 pm
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 pi@raspberrypi ~ $ 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
User avatar
Posts: 1
Joined: Thu May 17, 2012 9:16 pm
by rpdom » Sun Feb 24, 2013 10:43 pm
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.
User avatar
Posts: 3756
Joined: Sun May 06, 2012 5:17 am
Location: Essex, UK
by BerryPicker » Mon Feb 25, 2013 1:09 pm
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
pi@raspberrypi ~ $ cd /opt/vc/bin
pi@raspberrypi /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 ...
Posts: 128
Joined: Tue Oct 16, 2012 3:03 pm
Location: The East of England
by SuPeRMiNoR2 » Fri Mar 15, 2013 5:25 pm
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
pi@raspberrypi ~ $ cd /opt/vc/bin
pi@raspberrypi /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"
Posts: 2
Joined: Wed Dec 19, 2012 1:13 am
by m0rgo » Wed Mar 20, 2013 10:02 pm
Is it possible to import the system temperature into a python program?
Posts: 6
Joined: Wed Mar 20, 2013 10:00 pm
by pluggy » Thu Mar 21, 2013 12:23 am
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.
Don't judge Linux by the Pi.......
User avatar
Posts: 2639
Joined: Thu May 31, 2012 3:52 pm
Location: Barnoldswick, Lancashire,UK
by Fraoch » Thu Mar 21, 2013 5:12 pm
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.
Pencoed-made Raspberry Pi Model B, Samsung memory
Posts: 26
Joined: Thu Mar 07, 2013 11:53 pm
Location: Cambridge, Ontario, Canada
by badfur » Mon Apr 01, 2013 10:38 am
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)
Posts: 1
Joined: Mon Apr 01, 2013 10:34 am
by m0rgo » Wed Apr 03, 2013 9:15 pm
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.
Posts: 6
Joined: Wed Mar 20, 2013 10:00 pm
by m0rgo » Wed Apr 03, 2013 9:55 pm
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.
Posts: 6
Joined: Wed Mar 20, 2013 10:00 pm
by Joe Schmoe » Wed Apr 03, 2013 10:07 pm
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 pi@raspberrypi /var/log $ /opt/vc/bin/vcgencmd measure_temp
temp=53.0'C
pts/2 pi@raspberrypi /var/log $ gawk '{print $1/1000,"degrees C"}' /sys/class/thermal/thermal_zone0/temp
54.072 degrees C
pts/2 pi@raspberrypi /var/log $
Never answer the question you are asked. Rather, answer the question you wish you had been asked.

- Robert S. McNamara - quoted in "Fog of War" -
Posts: 2799
Joined: Sun Jan 15, 2012 1:11 pm
by yuusou » Mon Apr 29, 2013 8:45 am
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
pi@YuuberryPi ~ $ ./temp     
CPU Temp: 56.2ºC
GPU Temp: 56.2ºC
pi@YuuberryPi ~ $
Posts: 2
Joined: Fri Nov 23, 2012 11:13 pm
by solar3000 » Sat May 18, 2013 12:39 am
Code: Select all
 perl -e 'm/(\d+)/; $x=$1; s/\d+//; printf ("%.1f", ( $x / 1000))' -p /sys/class/thermal/thermal_zone0/temp
Last edited by solar3000 on Sat May 18, 2013 10:33 pm, edited 1 time in total.
Posts: 299
Joined: Sat May 18, 2013 12:14 am
by sprinkmeier » Sat May 18, 2013 2:33 am
Nice one!

in python:
Code: Select all
tempC = int(open('/sys/class/thermal/thermal_zone0/temp').read()) / 1e3
Posts: 290
Joined: Mon Feb 04, 2013 10:48 am
by solar3000 » Sat May 18, 2013 11:20 pm
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
Posts: 299
Joined: Sat May 18, 2013 12:14 am
by solar3000 » Sat May 18, 2013 11:22 pm
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).
Posts: 299
Joined: Sat May 18, 2013 12:14 am
by sprinkmeier » Sat May 18, 2013 11:51 pm
solar3000 wrote:can you do one liners in python?


Code: Select all
$ python -c "import base64;print(base64.decodestring('WUVTIQ==\n'))"
Posts: 290
Joined: Mon Feb 04, 2013 10:48 am
by solar3000 » Sun May 19, 2013 12:58 am
Code: Select all
python -c "import base64;print(base64.decodestring('QmlnIEZhdCBTaG93b2Zm\n'))"
Posts: 299
Joined: Sat May 18, 2013 12:14 am
by sprinkmeier » Sun May 19, 2013 3:29 am
Code: Select all
$ python -c "import base64;import gzip;print(gzip.zlib.decompress(base64.decodestring('eJxTUFBQ5gJiIEQCyiDABWIooAiDCS5UZSjiaOqhxqCbjkUlAH+dENQ=\n')))"
Posts: 290
Joined: Mon Feb 04, 2013 10:48 am
by solar3000 » Sun May 19, 2013 5:54 am
since you're going there....
how about some ascii art?
ASCII art one liner?

http://www.codeproject.com/Articles/109 ... -Generator
Posts: 299
Joined: Sat May 18, 2013 12:14 am
by rpdom » Sun May 19, 2013 6:30 am
Just because I was bored... ;-)
Code: Select all
sed "s/\(...\)$/.\1°C/" < /sys/class/thermal/thermal_zone0/temp
User avatar
Posts: 3756
Joined: Sun May 06, 2012 5:17 am
Location: Essex, UK
by JacopoBassan » Thu Nov 07, 2013 4:06 pm
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.
Posts: 2
Joined: Thu Apr 11, 2013 8:08 am
by slackpi » Mon Dec 02, 2013 9:40 pm
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!
-------------------------------------------------------
Running Slackware ARM 14.1 on the Pi
Posts: 1
Joined: Mon Dec 02, 2013 9:37 pm
by phoenixDownunder » Fri Jan 17, 2014 12:22 am
When I run /opt/vc/bin/vcgencmd measure_temp I get
temp=64.3'C
Is that a bit high? Ambient is 44C here
Posts: 5
Joined: Thu Feb 14, 2013 1:27 pm
by AndyD » Fri Jan 17, 2014 12:51 am
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.
Posts: 973
Joined: Sat Jan 21, 2012 8:13 am
Location: Melbourne, Australia