[PYTHON] Display CPU temperature every 5 seconds on Raspberry Pi 4

Introduction

When using Raspberry Pi 4, the CPU gets quite hot. I wanted to find out how hot the CPU was, so I made a program using Python3 to display the CPU temperature every 5 seconds.

environment

OS : Ubuntu server20.04 + Xubuntu Desktop(gdm3)

program

cpu_temperature.py


import time
import threading
import subprocess

def schedule(interval, func, wait = True):
    base_time = time.time()
    next_time = 0
    while True:
        t = threading.Thread(target = func)
        t.start()
        if wait:
            t.join()
        next_time = ((base_time - time.time()) % interval) or interval
        time.sleep(next_time)

def get_cpu_temperature():
    output = subprocess.check_output("cat /sys/class/thermal/thermal_zone0/temp", shell = True, encording = "UTF-8")
    return int(output) / 1000

def print_cpu_temperature():
    print(get_cpu_temperature())

if __name__ == "__main__":
    schedule(5, print_cpu_temperature)

Commentary

schedule function

I referred to the following article. Execute processing in Python at regular intervals It is a plagiarism of the schedule function introduced in the article. You can specify how many seconds you want to execute with interval and which function you want to execute with func. For details, please refer to the article at the URL.

get_cpu_temperature function

I referred to the following article. I tried to get the CPU temperature of Raspberry Pi (Raspbian) without using cat Execute command from Python It seems that the CPU temperature can be obtained by using vcgencmd, but this time I decided to use cat instead of that method. If you use cat, it will be output at 1000 times the value, so it is difficult to understand if you display it as it is.

There are various ways to call Linux commands from Python3, but this time I wanted to process the string obtained using cat in Python, so I decided to use the check_output function.

Use cat in the check_output function to get the CPU temperature. Since the value is multiplied by 1000 as it is, I changed the character string to an int type and divided it by 1000 to make it return. If you use vcgencmd, you can return it as it is.

print_cpu_temperature function

It is a function that only prints the value obtained by the get_cput_temperature function. Originally I tried to draw a graph using the schedule function and get_cpu_temperature function, but before that, I had a problem if the above two functions did not work properly, so I made this function.

if name == in "main"

It means to execute the print_cpu_temperature function every 5 seconds. If you change the 5 part to 10, the interval will be 10 seconds.

Recommended Posts

Display CPU temperature every 5 seconds on Raspberry Pi 4
Accelerate Deep Learning on Raspberry Pi 4 CPU
Detect temperature using python on Raspberry Pi 3!
pigpio on Raspberry pi
Cython on Raspberry Pi
Measure CPU temperature of Raspberry Pi with Python
Introduced pyenv on Raspberry Pi
Use NeoPixel on Raspberry Pi
Install OpenCV4 on Raspberry Pi 3
Install TensorFlow 1.15.0 on Raspberry Pi
Detect "temperature (using A / D converter)" using python on Raspberry Pi 3!
Testing uart communication on Raspberry Pi
MQTT on Raspberry Pi and Mac
raspberry pi 4 centos7 install on docker
Install ghoto2 on Raspberry Pi (memo)
Try using ArUco on Raspberry Pi
OpenCV installation procedure on Raspberry Pi
Power on / off Raspberry pi on Arduino
Detect switch status on Raspberry Pi 3
Install OpenMedia Vault 5 on Raspberry Pi 4
L Chika on Raspberry Pi C #
Build wxPython on Ubuntu 20.04 on raspberry pi 4
Weighing instrument using raspberry pi and hx711 (GUI display on Tkinter)
Raspberry Pi --1 --First time (Connect a temperature sensor to display the temperature)
Use BME280 temperature / humidity / barometric pressure sensor from Python on Raspberry Pi 2
"Honwaka Notification Lamp" on Raspberry Pi Part 2
Detect "brightness" using python on Raspberry Pi 3!
"Honwaka Notification Lamp" on Raspberry Pi Part 1
Enable UART + serial communication on Raspberry Pi
Set swap space on Ubuntu on Raspberry Pi
Programming normally with Node-RED programming on Raspberry Pi 3
Use the Grove sensor on the Raspberry Pi
Install 64-bit OS (bate) on Raspberry Pi
Install docker-compose on 64-bit Raspberry Pi OS
Run servomotor on Raspberry Pi 3 using python
Working with sensors on Mathematica on Raspberry Pi
Build OpenCV-Python environment on Raspberry Pi B +
Mount Windows shared folder on Raspberry Pi
Matrix multiplication on Raspberry Pi GPU (Part 2)
How to install NumPy on Raspberry Pi
I installed OpenCV-Python on my Raspberry Pi
Working with GPS on Raspberry Pi 3 Python
Using the 1-Wire Digital Temperature Sensor DS18B20 from Python on a Raspberry Pi
Control Akizuki's I2C-connected OLED display from Raspberry Pi
Why detectMultiScale () is slow on Raspberry Pi B +
Detect slide switches using python on Raspberry Pi 3!
Build a Django environment on Raspberry Pi (MySQL)
Try using a QR code on a Raspberry Pi
Detect magnet switches using python on Raspberry Pi 3!
Enjoy electronic work with GPIO on Raspberry Pi
Power on / off your PC with raspberry pi
Use Grove-Temperature & Humidity Sensor (DHT11) on Raspberry Pi
Get CPU information of Raspberry Pi with Python
Make DHT11 available on Raspberry Pi + python (memo)
Beginning cross-compilation for Raspberry Pi Zero on Ubuntu
Sound the buzzer using python on Raspberry Pi 3!
Play with your Ubuntu desktop on your Raspberry Pi 4
Introduced Ceph on Kubernetes on Raspberry Pi 4B (ARM64)
Get temperature and humidity with DHT11 and Raspberry Pi
Connect to MySQL with Python on Raspberry Pi
Build a Python development environment on Raspberry Pi