Sparks135
Posts: 104
Joined: Tue Feb 28, 2017 7:44 pm

GPU & CPU Temp in Fahrenheit

Sat Apr 15, 2017 12:05 pm

I am using this code to show GPU Temp in Fahrenheit and CPU Temperature in Fahrenheit. How do I convert this to show both?

Code: Select all

#!/bin/bash
# Script: my-pi-temp.sh
# Purpose: Display the ARM CPU and GPU  temperature of Raspberry Pi 2/3 
# Author: Vivek Gite <www.cyberciti.biz> under GPL v2.x+
# -------------------------------------------------------
cpu=$(</sys/class/thermal/thermal_zone0/temp)
echo "$(date) @ $(hostname)"
echo "-------------------------------------------"
echo "GPU => $(/opt/vc/bin/vcgencmd measure_temp)"
echo "CPU => $((cpu/1000))'C"CENTIGRADE=$(vcgencmd measure_temp | awk -F "[=\']" '{print $2}')
FARENHEIT=$(echo "(1.8 * $CENTIGRADE) + 32" |bc)
echo temp=$FARENHEIT\'F
As you can see below the temp must be for the GPU I need it to show each.

This my output now:
GPU => temp=54.8'C
CPU => 54'C
temp=132.4'F

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 1771
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: GPU & CPU Temp in Fahrenheit

Tue Apr 18, 2017 2:00 pm

This is NOT a Device Tree question, so you're in the wrong forum - beginners might have been better - but here you go:

Code: Select all

#!/bin/bash
gpu=$(/opt/vc/bin/vcgencmd measure_temp | awk -F "[=\']" '{print $2}')
cpu=$(</sys/class/thermal/thermal_zone0/temp)
cpu=$(echo "$cpu / 100 * 0.1" | bc)
cpuf=$(echo "(1.8 * $cpu) + 32" |bc)
gpuf=$(echo "(1.8 * $gpu) + 32" |bc)
echo "$(date) @ $(hostname)"
echo "-------------------------------------------"
echo "GPU => $gpu'C ($gpuf'F)"
echo "CPU => $cpu'C ($cpuf'F)"
I've broken into down into a few more steps for clarity.
Running it now I get:

Code: Select all

Tue 18 Apr 13:59:01 UTC 2017 @ raspberrypi
-------------------------------------------
GPU => 47.8'C (118.0'F)
CPU => 48.3'C (118.9'F)
But Fahrenheit is an antique and should die quickly. 8-)

Sparks135
Posts: 104
Joined: Tue Feb 28, 2017 7:44 pm

Re: GPU & CPU Temp in Fahrenheit

Tue Apr 18, 2017 5:40 pm

PhilE wrote:This is NOT a Device Tree question, so you're in the wrong forum - beginners might have been better - but here you go:

Code: Select all

#!/bin/bash
gpu=$(/opt/vc/bin/vcgencmd measure_temp | awk -F "[=\']" '{print $2}')
cpu=$(</sys/class/thermal/thermal_zone0/temp)
cpu=$(echo "$cpu / 100 * 0.1" | bc)
cpuf=$(echo "(1.8 * $cpu) + 32" |bc)
gpuf=$(echo "(1.8 * $gpu) + 32" |bc)
echo "$(date) @ $(hostname)"
echo "-------------------------------------------"
echo "GPU => $gpu'C ($gpuf'F)"
echo "CPU => $cpu'C ($cpuf'F)"
I've broken into down into a few more steps for clarity.
Running it now I get:

Code: Select all

Tue 18 Apr 13:59:01 UTC 2017 @ raspberrypi
-------------------------------------------
GPU => 47.8'C (118.0'F)
CPU => 48.3'C (118.9'F)
But Fahrenheit is an antique and should die quickly. 8-)
Thank you that did the trick. Your method is so much cleaner. Thanks again.

OutsourcedGuru
Posts: 40
Joined: Mon Jun 19, 2017 11:15 pm

Re: GPU & CPU Temp in Fahrenheit

Fri Jun 30, 2017 10:54 pm

As someone in the states and since we still use Fahrenheit, it would be nice to have this support via raspi-config (display celsius or fahrenheit) versus a single setting.

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

Re: GPU & CPU Temp in Fahrenheit

Sat Jul 01, 2017 6:29 am

OutsourcedGuru wrote:As someone in the states and since we still use Fahrenheit, it would be nice to have this support via raspi-config (display celsius or fahrenheit) versus a single setting.
A better option would be for you to shout at your Government until they drop the use of Imperial and American units and start using ISO units for everything. I'm sure Mr. Trump would like the chaos that would cause.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

OutsourcedGuru
Posts: 40
Joined: Mon Jun 19, 2017 11:15 pm

Re: GPU & CPU Temp in Fahrenheit

Mon Jul 03, 2017 9:44 pm

DougieLawson wrote:
OutsourcedGuru wrote:As someone in the states and since we still use Fahrenheit, it would be nice to have this support via raspi-config (display celsius or fahrenheit) versus a single setting.
A better option would be for you to shout at your Government until they drop the use of Imperial and American units and start using ISO units for everything. I'm sure Mr. Trump would like the chaos that would cause.
"Is there anyone who ever remembers
Changing their mind from the paint on a sign?
Is there anyone who really recalls
Ever breaking record off
For something someone yelled real loud one time
?
Oh, everyone believes
In how they think it ought to be
Oh, everyone believes
And they're not going easily..."
~ John Mayer - Belief lyrics

Jeffrey.j
Posts: 8
Joined: Sun Mar 12, 2017 8:54 pm

Re: GPU & CPU Temp in Fahrenheit

Sun May 27, 2018 12:05 am

PhilE wrote:
Tue Apr 18, 2017 2:00 pm
This is NOT a Device Tree question, so you're in the wrong forum - beginners might have been better - but here you go:

Code: Select all

#!/bin/bash
gpu=$(/opt/vc/bin/vcgencmd measure_temp | awk -F "[=\']" '{print $2}')
cpu=$(</sys/class/thermal/thermal_zone0/temp)
cpu=$(echo "$cpu / 100 * 0.1" | bc)
cpuf=$(echo "(1.8 * $cpu) + 32" |bc)
gpuf=$(echo "(1.8 * $gpu) + 32" |bc)
echo "$(date) @ $(hostname)"
echo "-------------------------------------------"
echo "GPU => $gpu'C ($gpuf'F)"
echo "CPU => $cpu'C ($cpuf'F)"
I've broken into down into a few more steps for clarity.
Running it now I get:

Code: Select all

Tue 18 Apr 13:59:01 UTC 2017 @ raspberrypi
-------------------------------------------
GPU => 47.8'C (118.0'F)
CPU => 48.3'C (118.9'F)
But Fahrenheit is an antique and should die quickly. 8-)
Hi. I know this is an older post but I was hoping that you could briefly explain these parts:

Code: Select all

 awk -F "[=\']" '{print $2}' 
,

Code: Select all

cpu=$(echo "$cpu / 100 * 0.1" | bc)
(Just why this one is nessessary), and why is there a “<“ here:

Code: Select all

cpu=$(</sys/class/thermal/thermal_zone0/temp)
. Also, if I were to make this an alias, how would I do that? Thank you.

User avatar
Paeryn
Posts: 2055
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: GPU & CPU Temp in Fahrenheit

Sun May 27, 2018 1:54 am

Jeffrey.j wrote:
Sun May 27, 2018 12:05 am
PhilE wrote:
Tue Apr 18, 2017 2:00 pm

Code: Select all

#!/bin/bash
gpu=$(/opt/vc/bin/vcgencmd measure_temp | awk -F "[=\']" '{print $2}')
cpu=$(</sys/class/thermal/thermal_zone0/temp)
cpu=$(echo "$cpu / 100 * 0.1" | bc)
cpuf=$(echo "(1.8 * $cpu) + 32" |bc)
gpuf=$(echo "(1.8 * $gpu) + 32" |bc)
echo "$(date) @ $(hostname)"
echo "-------------------------------------------"
echo "GPU => $gpu'C ($gpuf'F)"
echo "CPU => $cpu'C ($cpuf'F)"
Hi. I know this is an older post but I was hoping that you could briefly explain these parts:

Code: Select all

 awk -F "[=\']" '{print $2}' 
That is a very simple awk program that splits each line from the input file into fields where fields are delimited by either an equals sign or a single quote and outputs the second field.
-F "[=\']" sets the field separator, if you give a string of more than one character then the string is a regular expression.
'{print $2}' is the awk program to run. No pattern is given so it will be applied to every line, $n means the item that is the nth field.
Jeffrey.j wrote:
Sun May 27, 2018 12:05 am

Code: Select all

cpu=$(echo "$cpu / 100 * 0.1" | bc)
(Just why this one is nessessary), and why is there a “<“ here:

Code: Select all

cpu=$(</sys/class/thermal/thermal_zone0/temp)
. Also, if I were to make this an alias, how would I do that? Thank you.
$(<filename) says return the contents of the file filename, here the content of that file is a string (e.g. 49388) so the variable cpu will have that string. Think of it as $(cat filename), just that bash will do it itself rather than calling cat to do it.
The temperature from that file is the temperature*1000. $(echo "$cpu / 100 * 0.1" | bc) passes the equation (using the value above) "49388 / 100 * 0.1" to the calculator program bc. It isn't just divided by 1000 because in bc the divide operator returns the quotient so 49388 / 100 will give the answer 493 (the 0.88 is discarded), this is then multiplied by 0.1 to effectively divide by a further 10 to get the answer but this time keeping the fractional part to give 49.3
She who travels light — forgot something.

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 1771
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: GPU & CPU Temp in Fahrenheit

Sun May 27, 2018 1:51 pm

That's a very detailed and accurate answer. All I would add is the reason for the awk command.

The output from "vcgencmd measure_temp" is of the form "temp=48.3'C", so a lazy (laziness can be a virtue) way of extracting the number is to split the input at the "=" and the "'", and keep the second element.

Jeffrey.j
Posts: 8
Joined: Sun Mar 12, 2017 8:54 pm

Re: GPU & CPU Temp in Fahrenheit

Sun May 27, 2018 7:14 pm

Paeryn wrote:
Sun May 27, 2018 1:54 am
Jeffrey.j wrote:
Sun May 27, 2018 12:05 am
PhilE wrote:
Tue Apr 18, 2017 2:00 pm

Code: Select all

#!/bin/bash
gpu=$(/opt/vc/bin/vcgencmd measure_temp | awk -F "[=\']" '{print $2}')
cpu=$(</sys/class/thermal/thermal_zone0/temp)
cpu=$(echo "$cpu / 100 * 0.1" | bc)
cpuf=$(echo "(1.8 * $cpu) + 32" |bc)
gpuf=$(echo "(1.8 * $gpu) + 32" |bc)
echo "$(date) @ $(hostname)"
echo "-------------------------------------------"
echo "GPU => $gpu'C ($gpuf'F)"
echo "CPU => $cpu'C ($cpuf'F)"
Hi. I know this is an older post but I was hoping that you could briefly explain these parts:

Code: Select all

 awk -F "[=\']" '{print $2}' 
That is a very simple awk program that splits each line from the input file into fields where fields are delimited by either an equals sign or a single quote and outputs the second field.
-F "[=\']" sets the field separator, if you give a string of more than one character then the string is a regular expression.
'{print $2}' is the awk program to run. No pattern is given so it will be applied to every line, $n means the item that is the nth field.
Jeffrey.j wrote:
Sun May 27, 2018 12:05 am

Code: Select all

cpu=$(echo "$cpu / 100 * 0.1" | bc)
(Just why this one is nessessary), and why is there a “<“ here:

Code: Select all

cpu=$(</sys/class/thermal/thermal_zone0/temp)
. Also, if I were to make this an alias, how would I do that? Thank you.
$(<filename) says return the contents of the file filename, here the content of that file is a string (e.g. 49388) so the variable cpu will have that string. Think of it as $(cat filename), just that bash will do it itself rather than calling cat to do it.
The temperature from that file is the temperature*1000. $(echo "$cpu / 100 * 0.1" | bc) passes the equation (using the value above) "49388 / 100 * 0.1" to the calculator program bc. It isn't just divided by 1000 because in bc the divide operator returns the quotient so 49388 / 100 will give the answer 493 (the 0.88 is discarded), this is then multiplied by 0.1 to effectively divide by a further 10 to get the answer but this time keeping the fractional part to give 49.3
Thank you so much this really helps! Also is there a way I could make this into an alias?

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 1771
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: GPU & CPU Temp in Fahrenheit

Sun May 27, 2018 7:17 pm

Yes, in one form or another, but what advantage do you think that would have?

Jeffrey.j
Posts: 8
Joined: Sun Mar 12, 2017 8:54 pm

Re: GPU & CPU Temp in Fahrenheit

Sun May 27, 2018 10:28 pm

PhilE wrote:
Sun May 27, 2018 7:17 pm
Yes, in one form or another, but what advantage do you think that would have?
Well I plan on checking the temperature every once in a while, so instead of typing this all in/copy and pasting it, I could just type in a simple command that is an alias to simplify it.

Update: I played around with it and I figured out how to do it :D! Thank you for the help though.

Return to “Device Tree”

Who is online

Users browsing this forum: No registered users and 4 guests