You can get the CPU temperature of Raspberry Pi with vcgencmd measure_temp
.
I measured 60 times per second for monitoring, and finally displayed the average.
The language used is Python 3.7.
If you type Python on Raspberry Pi, Python2 will come out by default, but it is not compatible with 3.7, so please be careful not to stare at the error.
e? I'm the only one who does that. Yes.
If you give execute permission with sudo chmod a + x temp.py
and set it to ./temp.py
, it will work.
temp.py
#!/usr/bin/python3.7
import subprocess
import time
import sys
import re
num = 60
ave = 0
print(subprocess.run('lsb_release -a', shell=True, encoding='utf-8', stdout=subprocess.PIPE).stdout)
try:
while num > 0:
temp = subprocess.run('vcgencmd measure_temp', shell=True, encoding='utf-8', stdout=subprocess.PIPE).stdout.split('=')
freq = subprocess.run('vcgencmd measure_clock arm', shell=True, encoding='utf-8', stdout=subprocess.PIPE).stdout.split('=')
volt = subprocess.run('vcgencmd measure_volts', shell=True, encoding='utf-8', stdout=subprocess.PIPE).stdout.split('=')
temp = temp[1].replace('\n', '')
freq = int(freq[1].replace('\n', '')) / 1000000000
volt = volt[1].replace('\n', '')
print('Temp: ' + temp + ', Freq: ' + f'{freq:.2f}' + 'GHz, Volt: ' + str(volt) + ' (' + str(num) + ')')
ave += int(re.sub('\\D', '', temp))
num -= 1
time.sleep(1)
print('Average: ' + f'{ave / 600:.1f}' + "'C (60s)")
except KeyboardInterrupt:
sec = 60 - num
print(' Aborted.\nAverage: ' + f'{ave / sec / 10:.1f}' + "'C (" + str(sec) + 's)')
sys.exit()
Please forgive me for the headline that it is a free study during the summer vacation. I researched and wrote various things as a beginner.
~~ I don't really need it, but ~~ First, the Linux version information is displayed.
You can display the execution result of your favorite command by rewriting lsb_release -a
in print
before the loop.
I am completely self-satisfied.
Subprocess
I found that subprocess
is good for executing commands from python.
I'm not sure why, but I used subprocess
because I wanted to wrap it around a long object.
Since temperature alone is boring (what), I also displayed the clock frequency and operating voltage.
It's quite interesting to see this as an extra.
In my environment, I'm not using up to 1.5GHz.
KeyboardInterrupt
I can get out of the loop with Ctrl + C
, but I get an error.
Since it is not good for mental health, I enclose the loop processing with try
and catch Ctrl + C
with ʻexcept Keyboard Interrupt`.
By doing this, no error will be spit out, and you can write the processing when exiting.
--You can change the number of seconds to measure by changing the 60
of the variable num
.
--Put this file in / {hogehoge}
and type ln -s / {hogehoge} /temp.py /usr/bin/temp
and just type temp
in any directory to run this program. You can do it.
――I think there are many unsightly points because it was the first post of a hobby glamor, but please forgive me.
--If you are a "professional" glamor, please point out any comments.
Recommended Posts